21 package com.arm.malideveloper.openglessdk.rendertotexturejava;
23 import java.io.InputStream;
24 import java.nio.FloatBuffer;
25 import java.nio.ShortBuffer;
27 import android.graphics.Bitmap;
28 import android.graphics.BitmapFactory;
29 import android.opengl.GLES20;
30 import android.opengl.GLUtils;
31 import android.opengl.Matrix;
32 import android.content.Context;
85 "uniform mat4 uModelViewProjection;\n" +
86 "attribute vec4 aPosition;\n" +
87 "attribute vec2 aTextureCoordinate;\n" +
88 "varying vec2 vTextureCoordinate;\n" +
90 "gl_Position = uModelViewProjection * aPosition;\n" +
91 "vTextureCoordinate = aTextureCoordinate;\n" +
95 "precision mediump float;\n" +
96 "varying vec2 vTextureCoordinate;\n" +
97 "uniform sampler2D sTextureSampler;\n" +
99 "gl_FragColor = texture2D(sTextureSampler, vTextureCoordinate);\n" +
126 this.m_Context = a_Context;
134 GLES20.glEnable(GLES20.GL_CULL_FACE);
137 GLES20.glEnable(GLES20.GL_DEPTH_TEST);
153 Matrix.setLookAtM(this.m_ModelViewMatrix, 0, 0, 0, -2, 0
f, 0
f, 0
f, 0
f, 1.0f, 0.0f);
162 this.m_CubeDiffuseTextureId = textureIds[0];
165 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, this.m_CubeDiffuseTextureId);
166 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
167 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
168 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
169 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
172 InputStream inputStream = this.m_Context.getResources().openRawResource(R.raw.bubbles);
173 Bitmap bitmapTexture = BitmapFactory.decodeStream(inputStream);
176 GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmapTexture, 0);
179 bitmapTexture.recycle();
185 int vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
188 GLES20.glShaderSource(vertexShader, this.m_VertexShaderSource);
189 GLES20.glCompileShader(vertexShader);
192 int fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
195 GLES20.glShaderSource(fragmentShader, this.m_FragmentShaderSource);
196 GLES20.glCompileShader(fragmentShader);
199 this.m_ShaderProgramID = GLES20.glCreateProgram();
201 GLES20.glAttachShader(this.m_ShaderProgramID, vertexShader);
202 GLES20.glAttachShader(this.m_ShaderProgramID, fragmentShader);
205 GLES20.glLinkProgram(this.m_ShaderProgramID);
208 this.m_VerticesPositionAttributeId = GLES20.glGetAttribLocation(this.m_ShaderProgramID,
"aPosition");
210 throw new RuntimeException(
"aPosition attribute location invalid");
214 this.m_VerticesTextureAttributeId = GLES20.glGetAttribLocation(this.m_ShaderProgramID,
"aTextureCoordinate");
216 throw new RuntimeException(
"aTextureCoordinate attribute location invalid");
220 this.m_ModelViewProjectionId = GLES20.glGetUniformLocation(this.m_ShaderProgramID,
"uModelViewProjection");
222 throw new RuntimeException(
"uModelViewProjection location invalid");
230 int floatSize = Float.SIZE >> 3;
233 GLES20.glUseProgram(this.m_ShaderProgramID);
236 GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.m_VertexAttributeVBO[0]);
237 GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, this.m_VertexAttributeIBO[0]);
241 GLES20.glVertexAttribPointer(this.m_VerticesPositionAttributeId, 3, GLES20.GL_FLOAT,
false, 0, 0);
242 GLES20.glEnableVertexAttribArray(this.m_VerticesPositionAttributeId);
247 GLES20.glVertexAttribPointer(this.m_VerticesTextureAttributeId, 2, GLES20.GL_FLOAT,
false, 0 , this.m_VertexCount * 3 * floatSize);
248 GLES20.glEnableVertexAttribArray(this.m_VerticesTextureAttributeId);
258 this.m_RotateY = 360;
261 float[] m_ModelingMatrix1 =
new float[16];
262 float[] m_ModelingMatrix2 =
new float[16];
263 float[] m_ModelingMatrix3 =
new float[16];
266 Matrix.setRotateM(m_ModelingMatrix1, 0, this.m_RotateX, 1.0f, 0.0f, 0.0f);
267 Matrix.setRotateM(m_ModelingMatrix2, 0, this.m_RotateY, 0.0f, 1.0f, 0.0f);
270 Matrix.multiplyMM(m_ModelingMatrix3, 0, m_ModelingMatrix1, 0, m_ModelingMatrix2, 0);
271 Matrix.multiplyMM(this.m_ModelingMatrix, 0,
m_ModelViewMatrix, 0, m_ModelingMatrix3, 0);
274 this.setProjection(this.m_FBOWidth, this.m_FBOHeight);
277 Matrix.multiplyMM(this.m_ModelViewProjectionMatrix, 0, this.m_ProjectionMatrix, 0, this.m_ModelingMatrix, 0);
280 GLES20.glUniformMatrix4fv(this.m_ModelViewProjectionId, 1,
false, this.m_ModelViewProjectionMatrix, 0);
283 GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
286 GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, this.m_RenderToTextureFBO[0]);
289 GLES20.glClearColor(1.0f, 0.5f, 0.0f, 1.0f);
290 GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
293 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, this.m_CubeDiffuseTextureId);
296 GLES20.glDrawElements(GLES20.GL_TRIANGLES, 36, GLES20.GL_UNSIGNED_SHORT, 0);
299 GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
302 GLES20.glClearColor(0.0f, 0.2f, 0.3f, 1.0f);
303 GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
306 this.setProjection(this.m_Width, this.m_Height);
309 Matrix.multiplyMM(this.m_ModelViewProjectionMatrix, 0, this.m_ProjectionMatrix, 0, this.m_ModelingMatrix, 0);
312 GLES20.glUniformMatrix4fv(this.m_ModelViewProjectionId, 1,
false, this.m_ModelViewProjectionMatrix, 0);
315 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, this.m_CubeFBOTextureId);
318 GLES20.glDrawElements(GLES20.GL_TRIANGLES, 36, GLES20.GL_UNSIGNED_SHORT, 0);
323 int floatSize = Float.SIZE >> 3;
324 int shortSize = Short.SIZE >> 3;
326 float cubeVerticesAttributes[] =
new float[] {
380 short cubeTriangleIndices[] =
new short[] {
414 FloatBuffer vertexDataBuffer;
415 ShortBuffer indexDataBuffer;
418 vertexDataBuffer = FloatBuffer.wrap(cubeVerticesAttributes);
419 indexDataBuffer = ShortBuffer.wrap(cubeTriangleIndices);
422 GLES20.glGenBuffers(1, this.m_VertexAttributeVBO, 0);
423 GLES20.glGenBuffers(1, this.m_VertexAttributeIBO, 0);
426 GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.m_VertexAttributeVBO[0]);
427 GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, (this.m_VertexCount * 3 * floatSize) + (this.
m_VertexCount * 2 * floatSize) , vertexDataBuffer, GLES20.GL_STATIC_DRAW);
430 GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, this.m_VertexAttributeIBO[0]);
431 GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, this.m_IndexCount * shortSize , indexDataBuffer, GLES20.GL_STATIC_DRAW);
436 this.m_CubeFBOTextureId = textureIds[0];
439 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, this.m_CubeFBOTextureId);
440 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
441 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
442 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
443 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
444 GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, this.m_FBOWidth, this.m_FBOHeight, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
448 GLES20.glGenFramebuffers(1, this.m_RenderToTextureFBO, 0);
451 GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, this.m_RenderToTextureFBO[0]);
454 GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, this.m_CubeFBOTextureId, 0);
457 int iResult = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
458 if(iResult != GLES20.GL_FRAMEBUFFER_COMPLETE)
460 throw new RuntimeException(
"Error: Frame Buffer Status is not complete. Terminated.");
464 GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
468 this.m_Width = a_Width;
469 this.m_Height = a_Height;
475 GLES20.glViewport(0, 0, a_Width, a_Height);
478 float aspectRatio = (
float) a_Width / (
float) a_Height;
479 Matrix.frustumM(this.m_ProjectionMatrix, 0, -aspectRatio, aspectRatio, -1, 1, 1, 10);
void createShaderProgram()
void buildVertexAndIndexBufferObjects()
void createSpinningCube()
int m_ModelViewProjectionId
final String m_VertexShaderSource
final String m_FragmentShaderSource
int m_VerticesTextureAttributeId
int m_VerticesPositionAttributeId
int[] m_VertexAttributeIBO
float[] m_ModelViewMatrix
void setWidthHeight(int a_Width, int a_Height)
SpinningCube3D(Context a_Context)
float[] m_ModelViewProjectionMatrix
int[] m_RenderToTextureFBO
int m_CubeDiffuseTextureId
void setProjection(int a_Width, int a_Height)
int[] m_VertexAttributeVBO
float[] m_ProjectionMatrix