In the
Activity declare a GLSurfaceView variable; for example:
private
GLSurfaceView glSurfaceView;
In the onCreate
event of the Activity set the GLSurfaceView variable, set OpenGL its version,
and then set its renderer:
glSurfaceView = new
GLSurfaceView(this);
glSurfaceView.setEGLContextClientVersion(2);
glSurfaceView.setRenderer(new
CubeRenderer(this));
In the
Renderer, declare a Context variable:
private final
Context context;
In the
constructor of the renderer, set the context:
this.context =
context;
Create a ByteBuffer
object, and put the data provided for all the vertices in it:
private final FloatBuffer
vertexData;
vertexData = ByteBuffer.allocateDirect(data.length * BYTES_PER_FLOAT)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
vertexData.put(data);
In the onSurfaceCreated
event of the Renderer, set:
glClearColor(0.0f,
0.0f, 0.0f, 0.0f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Read the
source of shaders:
String
vertexShaderSource = TextResourceReader
.readTextFileFromResource(context, R.raw.simple_vertex_shader);
String
fragmentShaderSource = TextResourceReader
.readTextFileFromResource(context, R.raw.simple_fragment_shader);
Compile the
shader sources:
int vertexShader =
ShaderHelper.compileVertexShader(vertexShaderSource);
int fragmentShader =
ShaderHelper.compileFragmentShader(fragmentShaderSource);
Link the compiled
code to create a Program:
private int program;
program =
ShaderHelper.linkProgram(vertexShader, fragmentShader);
Ask OpenGL
to use the program:
glUseProgram(program);
Get the
Uniform, Attribute, and Color positions from OpenGL:
uMatrixLocation = glGetUniformLocation(program, U_MATRIX);
aPositionLocation = glGetAttribLocation(program, A_POSITION);
aColorLocation = glGetAttribLocation(program, A_COLOR);
Set the
Model matrix as unit, and then, if it is
required rotate it:
setIdentityM(modelMatrix,
0);
rotateM(modelMatrix,
0, -45f, 1f, 0f, 0f);
Set the
Projection matrix:
MatrixHelper.perspectiveM(projectionMatrix,
45, (float) width / (float)
height, 1f, 10f);
Set the View
matrix:
setLookAtM(viewMatrix,
0, 0f, 1.2f, 2.2f, 0f, 0f, 0f, 0f, 1f, 0f);
Set the View
Projection matrix:
multiplyMM(viewProjectionMatrix,
0, projectionMatrix, 0, viewMatrix, 0);
Set the
Model View Projection matrix:
multiplyMM(modelViewProjectionMatrix,
0, viewProjectionMatrix, 0, modelMatrix, 0);
Set the
Uniform Matrix for Location using the Model View Projection matrix:
glUniformMatrix4fv(uMatrixLocation, 1,
false, modelViewProjectionMatrix, 0);
Bind data to
the Position location, and enable it:
vertexData.position(0);
glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT,GL_FLOAT, false, STRIDE,
vertexData);
glEnableVertexAttribArray(aPositionLocation);
Bind data to
the Color location, and enable it:
vertexData.position(POSITION_COMPONENT_COUNT);
glVertexAttribPointer(aColorLocation, COLOR_COMPONENT_COUNT, GL_FLOAT, false, STRIDE,
vertexData);
glEnableVertexAttribArray(aColorLocation);
In the onDrawFrame
event of the Renderer, clear the rendering surface:
glClear(GL_COLOR_BUFFER_BIT);
In the onDrawFrame
event of the Renderer, draw all the arrays:
glDrawArrays(GL_TRIANGLES, 0,
18);