NeHe Lesson 4 - it moves!

back to index

New bits in red. We need data fields to remember the rotation angles..

public class Lesson4 implements GLEventListener {
float        rtri=0.0f;
float        rquad=0.0f;

..

public void display(GLAutoDrawable drawable) {
// like int DrawGLScene(GLvoid)
GL gl = drawable.getGL();

gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);    // Clear The Screen And The Depth Buffer
gl.glLoadIdentity();                    // Reset The Current Modelview Matrix

gl.glTranslatef(-1.5f,0.0f,-6.0f);                // Left 1.5 Then Into Screen Six Units
 gl.glRotatef(rtri,0.0f,1.0f,0.0f); 
gl.glBegin(GL.GL_TRIANGLES);
gl.glColor3f(1.0f,0.0f,0.0f);            // Set The Color To Red
gl.glVertex3f( 0.0f, 1.0f, 0.0f);
gl.glColor3f(0.0f,1.0f,0.0f);            // Set The Color To Green
gl.glVertex3f(-1.0f,-1.0f, 0.0f);
gl.glColor3f(0.0f,0.0f,1.0f);            // Set The Color To Blue
gl.glVertex3f( 1.0f,-1.0f, 0.0f);            // Right And Down One Unit (Bottom Right)
gl.glEnd();                        // Done Drawing A Triangl.gle
gl.glLoadIdentity();
gl.glTranslatef(1.5f,0.0f,-6.0f);
gl.glRotatef(rquad,1.0f,0.0f,0.0f);
gl.glColor3f(0.5f,0.5f,1.0f);                // Set The Color To Blue One Time Only
gl.glBegin(GL.GL_QUADS);                    // Start Drawing Quads
gl.glVertex3f(-1.0f, 1.0f, 0.0f);            // Left And Up 1 Unit (Top Left)
gl.glVertex3f( 1.0f, 1.0f, 0.0f);            // Right And Up 1 Unit (Top Right)
gl.glVertex3f( 1.0f,-1.0f, 0.0f);            // Right And Down One Unit (Bottom Right)
gl.glVertex3f(-1.0f,-1.0f, 0.0f);            // Left And Down One Unit (Bottom Left)
gl.glEnd();
rtri+=0.2f;                        // Increase The Rotation Variable For The Triangle ( NEW )
rquad-=0.15f;
}