package demo;

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

/**
 * DemoBasicGeometry.java
 * <P>
 * Demonstrate six types of geometry using glBegin()...glEnd()
 * <P>
 * napier at potatoland dot org
 */
public class DemoBasicGeometry extends JOApp {

	/**
	 * 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
    	DemoBasicGeometry demo = new DemoBasicGeometry();

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

    /**
     * Render one frame.  Called by the JOApp.display() callback function.
     */
    @Override
    public void draw() {
    	// 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(
    			0f, 0f, 10f,    // eye position
    			0f, 0f, 0f,     // target to look at
    			0f, 1f, 0f);    // which way is up

    	// draw one point upper left
    	gl.glBegin(GL.GL_POINTS);
    	{
    		gl.glVertex3f( -4f, 1f, 0f);    // point
    	}
    	gl.glEnd();

    	// draw a line upper center
    	gl.glBegin(GL.GL_LINES);
    	{
    		gl.glVertex3f(-1f, 1f, 0f);   // start point
    		gl.glVertex3f( 0f, 2f, 0f);   // end point
    	}
    	gl.glEnd();

    	// draw a long line upper center
    	gl.glBegin(GL.GL_LINE_STRIP);
    	{
    		gl.glVertex3f( 2f, 1f, 0f);  // start point
    		gl.glVertex3f( 3f, 1f, 0f);  // next point
    		gl.glVertex3f( 4f, 2f, 0f);  // next point
    		gl.glVertex3f( 3f, 3f, 0f);  // next point ...
    	}
    	gl.glEnd();

    	// draw a blue triangle lower left
    	gl.glColor3f(.5f, .5f, .9f);  // color will affect all the following verts
    	gl.glBegin(GL.GL_TRIANGLES);
    	{
    		gl.glVertex3f( -4f,   -1f, 0f);   // top
    		gl.glVertex3f( -4.5f, -2f, 0f);   // lower left
    		gl.glVertex3f( -3.5f, -2f, 0f);   // lower right
    	}
    	gl.glEnd();

    	// draw a violet square lower center
    	gl.glColor3f(.8f, .5f, 1f);
    	gl.glBegin(GL.GL_QUADS);
    	{
    		gl.glVertex3f(-1f, -1f, 0f);    // top left
    		gl.glVertex3f(-1f, -2f, 0f);    // lower left
    		gl.glVertex3f( 0f, -2f, 0f);    // lower right
    		gl.glVertex3f( 0f, -1f, 0f);    // top right
    	}
    	gl.glEnd();

    	// draw a polygon lower right
    	gl.glBegin(GL.GL_POLYGON);
    	{
    		gl.glColor3f(0f, 0f, .5f);         // blue
    		gl.glVertex3f( 3.5f, -2.7f, 0f);
    		gl.glVertex3f( 4.3f, -2.2f, 0f);
    		gl.glVertex3f( 4.5f, -1.5f, 0f);

    		gl.glColor3f(.3f, 1f, .0f);        // green
    		gl.glVertex3f( 4f, -.5f, 0f);

    		gl.glColor3f(.8f, .1f, .1f);       // red
    		gl.glVertex3f( 1.5f, -1f, 0f);

    		gl.glColor3f(1f, 1f, 0f);          // yellow
    		gl.glVertex3f( 1.5f, -2.5f, 0f);

    	}
    	gl.glEnd();

        // reset vertex color to white
    	gl.glColor3f(1f, 1f, 1f);
    }
}
