STEP 1
In the
Activity declare a GLSurfaceView variable; for example:
private
GLSurfaceView glSurfaceView;
STEP 2
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));
glSurfaceView.setEGLContextClientVersion(2);
glSurfaceView.setRenderer(new CubeRenderer(this));
STEP 3
In the
Renderer, declare a Context variable:
private final
Context context;
STEP 4
In the
constructor of the renderer, set the context:
this.context =
context;
STEP 5
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);
STEP 6
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);
STEP 7
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);
STEP 8
Compile the
shader sources:
int vertexShader =
ShaderHelper.compileVertexShader(vertexShaderSource);
int fragmentShader =
ShaderHelper.compileFragmentShader(fragmentShaderSource);
STEP 9
Link the compiled
code to create a Program:
private int program;
program =
ShaderHelper.linkProgram(vertexShader, fragmentShader);
STEP 10
Ask OpenGL
to use the program:
glUseProgram(program);
STEP 11
Get the
Uniform, Attribute, and Color positions from OpenGL:
uMatrixLocation = glGetUniformLocation(program, U_MATRIX);
aPositionLocation = glGetAttribLocation(program, A_POSITION);
aColorLocation = glGetAttribLocation(program, A_COLOR);
STEP 12
Set the
Model matrix as unit, and then, if it is
required rotate it:
setIdentityM(modelMatrix,
0);
rotateM(modelMatrix,
0, -45f, 1f, 0f, 0f);
STEP 13
Set the
Projection matrix:
MatrixHelper.perspectiveM(projectionMatrix,
45, (float) width / (float)
height, 1f, 10f);
STEP 14
Set the View
matrix:
setLookAtM(viewMatrix,
0, 0f, 1.2f, 2.2f, 0f, 0f, 0f, 0f, 1f, 0f);
STEP 15
Set the View
Projection matrix:
multiplyMM(viewProjectionMatrix,
0, projectionMatrix, 0, viewMatrix, 0);
STEP 16
Set the
Model View Projection matrix:
multiplyMM(modelViewProjectionMatrix,
0, viewProjectionMatrix, 0, modelMatrix, 0);
STEP 17
Set the
Uniform Matrix for Location using the Model View Projection matrix:
glUniformMatrix4fv(uMatrixLocation, 1,
false, modelViewProjectionMatrix, 0);
STEP 18
Bind data to
the Position location, and enable it:
vertexData.position(0);
glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT,GL_FLOAT, false, STRIDE,
vertexData);
vertexData);
glEnableVertexAttribArray(aPositionLocation);
STEP 19
Bind data to
the Color location, and enable it:
vertexData.position(POSITION_COMPONENT_COUNT);
glVertexAttribPointer(aColorLocation, COLOR_COMPONENT_COUNT, GL_FLOAT, false, STRIDE,
vertexData);
vertexData);
glEnableVertexAttribArray(aColorLocation);
STEP 20
In the onDrawFrame
event of the Renderer, clear the rendering surface:
glClear(GL_COLOR_BUFFER_BIT);
STEP 21
In the onDrawFrame
event of the Renderer, draw all the arrays:
glDrawArrays(GL_TRIANGLES, 0,
18);
No comments:
Post a Comment