package demo;

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

/**
 * DemoQuadPopArt.java
 * <P>
 * Eyeball agitating pop-art (rotating quads)
 * <P>
 * napier at potatoland dot org
 */
public class DemoQuadPopArt extends JOApp {
    private float rotation = 0;

	/**
	 * Start the application, Run() initializes the OpenGL context, calls setup(), handles mouse and keyboard input,
	 * and calls draw() in a loop.
	 */
    public static void main(String args[]) {
    	// create the app
    	DemoQuadPopArt demo = new DemoQuadPopArt();

    	// set title, window size
    	windowTitle   = "Hello World";
    	displayWidth  = 640;
    	displayHeight = 480;

    	// start running: will call init(), setup(), draw(), mouse functions
    	demo.run();
    }

    /**
     * Initialize settings.  Will be called once when app starts.  Called by JOApp.init().
     */
    @Override
    public void setup() {
    	// set a background color
        gl.glClearColor(.2f, .2f, .5f, 1f);

        setPerspective(65);
        
        gl.glDisable(GL.GL_CULL_FACE);
    }

    /**
     * Render one frame.  Called by the JOApp.display() callback function.
     */
    @Override
    public void draw() {
        // bump up rotation a little each frame
        rotation += .05f;

        // 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();

        // Place the 'eye'
        glu.gluLookAt(
            0f, 0f, 10f,   // eye position
            0f, 0f, 0f,    // target to look at (the origin)
            0f, 1f, 0f);   // which way is up

        // draw grid of quads rotating around Z
        for (int x=0; x < 20; x++) {
                for (int y=0; y < 16; y++) {
                        gl.glPushMatrix();
                        {
                                gl.glTranslatef(x-10, y-8, 0);   // shift coordinate system to x,y
                                gl.glRotatef(-rotation, 0,0,1);  // rotate coordinate system around Z axis
                                gl.glBegin(GL.GL_QUADS);      // start a quad
                                {
                                    gl.glColor3f(.2f, .2f, 1);      // color: blue
                                    gl.glVertex3f( .5f,  .5f, 0f);    // top right
                                    
                                    gl.glColor3f(.2f, .2f, .8f);      // color: blue
                                    gl.glVertex3f( -.5f,  .5f, 0f);    // top left
                                    
                                    gl.glColor3f(.7f, .9f, .1f);      // color: red
                                    gl.glVertex3f( -.5f, -.5f, 0f);   // lower left
                                    
                                    gl.glColor3f(.8f, .8f, .1f);      // color: yellow
                                    gl.glVertex3f( .5f,  -.5f, 0f);   // lower right
                                }
                                gl.glEnd();
                        }
                        gl.glPopMatrix();
                }
        }

        // draw grid of quads rotating around Z and Y
        for (int x=0; x < 20; x++) {
                for (int y=0; y < 16; y++) {
                        gl.glPushMatrix();
                        {
                                gl.glTranslatef(x-10, y-8, 0);   // shift coordinate system to x,y
                                gl.glRotatef(rotation, 0,0,1);   // rotate coordinate system around Z axis
                                gl.glRotatef(rotation, 0,1,0);   // rotate coordinate system around Y axis
                                gl.glBegin(GL.GL_QUADS);       // start a quad
                                {
                                    gl.glColor3f(.2f, .2f, .8f);      // color: blue
                                    gl.glVertex3f( .5f,  .5f, 0f);    // top right
                                    
                                    gl.glColor3f(.2f, .2f, 1f);      // color: blue
                                    gl.glVertex3f( -.5f,  .5f, 0f);    // top left
                                    
                                    gl.glColor3f(.7f, .9f, .1f);      // color: red
                                    gl.glVertex3f( -.5f, -.5f, 0f);   // lower left
                                    
                                    gl.glColor3f(.8f, .8f, .1f);      // color: yellow
                                    gl.glVertex3f( .5f,  -.5f, 0f);   // lower right
                                }
                                gl.glEnd();
                        }
                        gl.glPopMatrix();
                }
        }
    }
}
