Action

Up to now the cube has just been sitting there without moving. That's pretty boring, so let's make it move. To do that, we need to make a couple of changes to our onSurfaceCreated() and onDrawFrame() methods in GLRenderer.

Download OpenGL/src/org/example/opengl/GLRenderer.java

private long startTime; private long fpsStartTime; private long numFrames;

public void onSurfaceCreated(GL10 gl, EGLConfig config) { // ...

startTime = System.currentTimeMillis(); fpsStartTime = startTime; numFrames = 0;

// Set rotation angle based on the time long elapsed = System.currentTimeMillis() - startTime; gl.glRotatef(elapsed * (30f / 1000f), 0, 1, 0); gl.glRotatef(elapsed * (15f / 1000f), 1, 0, 0);

This code rotates the cube a little bit every time through the main loop. Specifically, every second it rotates 30 degrees around the x-axis and 15 degrees around the y-axis. The result will be a nice, smooth, spinning cube (see Figure 10.5, on the following page).

0 0

Post a comment