package classwork;

import javax.media.opengl.*;
import jocode.*;

/**
 * GLART_2_rotation.java
 *
 * Two quads orbit the origin and spin on their own axes.
 * Uses pushMatrix() and popMatrix() to preserve the previous
 * state of the coordinate system and to isolate separate
 * translations and rotations.
 */
public class GLART_2_rotation extends JOApp {
 	float rotation = 0f;

    /**
     * Main function just creates and runs the application.
     */
    public static void main(String args[]) {
    	GLART_2_rotation app = new GLART_2_rotation();
        app.run();
    }

    /**
     * Render the scene.
     */
    public void draw() {
     	rotation += .5f;
        // Clear screen and depth buffer
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        // Select The Modelview Matrix (controls model orientation)
        gl.glMatrixMode(GL.GL_MODELVIEW);

        // Reset the Modelview matrix
        // this resets the coordinate system to center of screen
        gl.glLoadIdentity();

        // Where is the 'eye'
        glu.gluLookAt(
            -.5f, 1f, 7f,    // eye position
            0f, 0f, 0f,    // target to look at
            0f, 1f, 0f);   // which way is up

        // draw crosshairs
        drawOrigin();

		// rotate coord system around Y
		gl.glRotatef(rotation, 0,1,0);

		// 1: shift right and draw yellowish quad
        gl.glPushMatrix();
		{
			// shift coordinate system right 2 units
			gl.glTranslatef(2, 0, 0);
			// rotate coord system
			gl.glRotatef(rotation * 3f, 0, 1, 0);
			// draw a quad
			gl.glColor3f(.8f,.7f,0);
			drawQuad();
		}
        gl.glPopMatrix();

        // 2: shift left and draw bluish quad
        gl.glPushMatrix();
		{
			// rotate 180 degrees around Y
			gl.glRotatef(180, 0,1,0);
			// shift coordinate system right 2 units
			gl.glTranslatef(2, 0, 0);
			// rotate coord system
			gl.glRotatef(rotation * 3f, 0, 1, 0);
			// draw a quad
			gl.glColor3f(0,.5f,.6f);
			drawQuad();
		}
        gl.glPopMatrix();
    }

    /**
     * Draw crosshairs at 0,0,0
     */
    public void drawOrigin() {
    	gl.glColor3f(.5f, .5f, .5f);
        gl.glLineWidth(1);
        gl.glBegin(GL.GL_LINES);
	    	gl.glVertex3f( -1f, 0f, 0f);        // left
	    	gl.glVertex3f( 1f, 0f, 0f);         // right

	    	gl.glVertex3f( 0f, -1f, 0f);        // bottom
	    	gl.glVertex3f( 0f, 1f, 0f);         // top

			gl.glVertex3f( 0f, 0f, -1f);        // far
			gl.glVertex3f( 0f, 0f, 1f);         // near
		gl.glEnd();
		gl.glBegin(GL.GL_LINE_STRIP);
			gl.glVertex3f( -1f, 0f, 1f);        // left near
			gl.glVertex3f( 1f, 0f, 1f);         // rite near
			gl.glVertex3f( 1f, 0f, -1f);        // rite far
			gl.glVertex3f( -1f, 0f, -1f);       // left far
			gl.glVertex3f( -1f, 0f, 1f);        // left near (close)
		gl.glEnd();
    }


    public void drawQuad() {
		gl.glBegin(GL.GL_QUADS);
			gl.glVertex3f( -1f, 0f, 0f);       // left bottom
			gl.glVertex3f(  1f, 0f, 0f);       // rite bottom
			gl.glVertex3f(  1f, 2f, 0f);       // rite top
			gl.glVertex3f( -1f, 2f, 0f);       // left top
		gl.glEnd();
    }
}
