Graphics Programs Reference
In-Depth Information
▪ The code inside the
onDrawFrame()
method is refactored and
is extracted to two separate methods:
▪
void updateModel(int up-
DownValue, float zAngle)
▪
void renderModel(GL10 gl)
▪ The
updateModel()
method contains the lines of code from
Listing 6-7
.
From this point onwards, the
updateModel()
method will contain the code that updates the matrices which in
turn update the position of objects.
▪ The
renderModel()
method, as the name suggests, contains
the code that renders graphics. This comprises the calls to meth-
ods, such as
glUseProgram
,
glUniform*
,
glVertexAt-
tribPointer
,
glEnableVertexAttribArray
,
glDraw*
.
▪ To ensure that rendering of every frame takes the same amount of
time, we need to calculate the time taken by the current frame and
then sleep it accordingly. Because of the way garbage collection
takes place in Java, the time taken for rendering of each frame is
not guaranteed to be the same. By calling
Thread.sleep()
method (as shown in
Listing 6-8
) in the Renderer thread, we can
adjust this time. Also note that before the rendering begins, it is
always good to call the garbage collector explicitly so that it can
free up the memory occupied by objects that are no longer in use.
Note
Although it is all right to block the Renderer thread, for in-
stance by calling the
Thread.sleep()
method, the UI thread
should never be blocked.
Listing 6-8.
TANK FENCE GAME 2/src/com/apress/android/tankfencegame2/
GLES20Renderer.java
public void onDrawFrame(GL10 gl) {
System.gc();
Search WWH ::
Custom Search