21 package com.arm.malideveloper.openglessdk.spinningcubejava;
23 import java.io.InputStream;
24 import java.nio.FloatBuffer;
25 import java.nio.ShortBuffer;
26 import android.graphics.Bitmap;
27 import android.graphics.BitmapFactory;
28 import android.opengl.GLES20;
29 import android.opengl.GLUtils;
30 import android.opengl.Matrix;
31 import android.content.Context;
77 "uniform mat4 uModelViewProjection;\n" +
78 "attribute vec4 aPosition;\n" +
79 "attribute vec2 aTextureCoordinate;\n" +
80 "varying vec2 vTextureCoordinate;\n" +
82 "gl_Position = uModelViewProjection * aPosition;\n" +
83 "vTextureCoordinate = aTextureCoordinate;\n" +
87 "precision mediump float;\n" +
88 "varying vec2 vTextureCoordinate;\n" +
89 "uniform sampler2D sTextureSampler;\n" +
91 "gl_FragColor = texture2D(sTextureSampler, vTextureCoordinate);\n" +
110 this.m_Context = a_Context;
118 GLES20.glEnable(GLES20.GL_CULL_FACE);
121 GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
124 GLES20.glEnable(GLES20.GL_DEPTH_TEST);
140 Matrix.setLookAtM(this.m_ModelViewMatrix, 0, 0, 0, -2, 0
f, 0
f, 0
f, 0
f, 1.0f, 0.0f);
150 this.m_CubeDiffuseTextureId = textureIds[0];
153 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, this.m_CubeDiffuseTextureId);
154 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
155 GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
156 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
157 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
160 InputStream inputStream = this.m_Context.getResources().openRawResource(R.raw.bubbles);
161 Bitmap bitmapTexture = BitmapFactory.decodeStream(inputStream);
164 GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmapTexture, 0);
167 bitmapTexture.recycle();
173 int vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
176 GLES20.glShaderSource(vertexShader, this.m_VertexShaderSource);
177 GLES20.glCompileShader(vertexShader);
180 int fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
183 GLES20.glShaderSource(fragmentShader, this.m_FragmentShaderSource);
184 GLES20.glCompileShader(fragmentShader);
187 this.m_ShaderProgramID = GLES20.glCreateProgram();
189 GLES20.glAttachShader(this.m_ShaderProgramID, vertexShader);
190 GLES20.glAttachShader(this.m_ShaderProgramID, fragmentShader);
193 GLES20.glLinkProgram(this.m_ShaderProgramID);
196 this.m_VerticesPositionAttributeId = GLES20.glGetAttribLocation(this.m_ShaderProgramID,
"aPosition");
198 throw new RuntimeException(
"aPosition attribute location invalid");
202 this.m_VerticesTextureAttributeId = GLES20.glGetAttribLocation(this.m_ShaderProgramID,
"aTextureCoordinate");
204 throw new RuntimeException(
"aTextureCoordinate attribute location invalid");
208 this.m_ModelViewProjectionId = GLES20.glGetUniformLocation(this.m_ShaderProgramID,
"uModelViewProjection");
210 throw new RuntimeException(
"uModelViewProjection location invalid");
216 int floatSize = Float.SIZE >> 3;
218 GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
221 GLES20.glUseProgram(this.m_ShaderProgramID);
224 GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
225 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, this.m_CubeDiffuseTextureId);
228 GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.m_VertexAttributeVBO[0]);
229 GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, this.m_VertexAttributeIBO[0]);
233 GLES20.glVertexAttribPointer(this.m_VerticesPositionAttributeId, 3, GLES20.GL_FLOAT,
false, 0, 0);
234 GLES20.glEnableVertexAttribArray(this.m_VerticesPositionAttributeId);
239 GLES20.glVertexAttribPointer(this.m_VerticesTextureAttributeId, 2, GLES20.GL_FLOAT,
false, 0 , this.m_VertexCount * 3 * floatSize);
240 GLES20.glEnableVertexAttribArray(this.m_VerticesTextureAttributeId);
250 this.m_RotateY = 360;
253 float[] m_ModelingMatrix1 =
new float[16];
254 float[] m_ModelingMatrix2 =
new float[16];
255 float[] m_ModelingMatrix3 =
new float[16];
258 Matrix.setRotateM(m_ModelingMatrix1, 0, this.m_RotateX, 1.0f, 0.0f, 0.0f);
259 Matrix.setRotateM(m_ModelingMatrix2, 0, this.m_RotateY, 0.0f, 1.0f, 0.0f);
262 Matrix.multiplyMM(m_ModelingMatrix3, 0, m_ModelingMatrix1, 0, m_ModelingMatrix2, 0);
263 Matrix.multiplyMM(this.m_ModelingMatrix, 0,
m_ModelViewMatrix, 0, m_ModelingMatrix3, 0);
266 Matrix.multiplyMM(this.m_ModelViewProjectionMatrix, 0, this.m_ProjectionMatrix, 0, this.m_ModelingMatrix, 0);
269 GLES20.glUniformMatrix4fv(this.m_ModelViewProjectionId, 1,
false, this.m_ModelViewProjectionMatrix, 0);
272 GLES20.glDrawElements(GLES20.GL_TRIANGLES, 36, GLES20.GL_UNSIGNED_SHORT, 0);
277 int floatSize = Float.SIZE >> 3;
278 int shortSize = Short.SIZE >> 3;
280 float cubeVerticesAttributes[] =
new float[] {
334 short cubeTriangleIndices[] =
new short[] {
368 FloatBuffer vertexDataBuffer;
369 ShortBuffer indexDataBuffer;
372 vertexDataBuffer = FloatBuffer.wrap(cubeVerticesAttributes);
373 indexDataBuffer = ShortBuffer.wrap(cubeTriangleIndices);
376 GLES20.glGenBuffers(1, this.m_VertexAttributeVBO, 0);
377 GLES20.glGenBuffers(1, this.m_VertexAttributeIBO, 0);
380 GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, this.m_VertexAttributeVBO[0]);
381 GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, (this.m_VertexCount * 3 * floatSize) + (this.
m_VertexCount * 2 * floatSize) , vertexDataBuffer, GLES20.GL_STATIC_DRAW);
384 GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, this.m_VertexAttributeIBO[0]);
385 GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, this.m_IndexCount * shortSize , indexDataBuffer, GLES20.GL_STATIC_DRAW);
391 GLES20.glViewport(0, 0, a_Width, a_Height);
394 float aspectRatio = (
float) a_Width / (
float) a_Height;
395 Matrix.frustumM(this.m_ProjectionMatrix, 0, -aspectRatio, aspectRatio, -1, 1, 1, 10);
int[] m_VertexAttributeVBO
int m_VerticesTextureAttributeId
int m_VerticesPositionAttributeId
int m_CubeDiffuseTextureId
int[] m_VertexAttributeIBO
final String m_VertexShaderSource
SpinningCube3D(Context a_Context)
final String m_FragmentShaderSource
float[] m_ModelViewProjectionMatrix
float[] m_ModelViewMatrix
float[] m_ProjectionMatrix
void setProjection(int a_Width, int a_Height)
void createShaderProgram()
void buildVertexAndIndexBufferObjects()
void createSpinningCube()
int m_ModelViewProjectionId