package classwork;

import javax.media.opengl.*;
import java.util.ArrayList;
import java.awt.event.MouseEvent;
import jocode.*;
import jomodel.JOVector;    

/**
 * GLART_3_blackboard.java
 *
 * Create an ortho projection with world coordinates that exactly match 
 * the window pixel coordinates. So the viewing volume will have the origin 
 * in the lower left corner and will be 800 units wide and 600 units high.
 * 
 * This allows us to easily map the mouse screen position to points in the 
 * world space.  
 *  
 * Move the mouse around the screen to draw lines.
 */
public class GLART_3_blackboard extends JOApp {
	// create array to hold vertex positions  
	ArrayList points = new ArrayList();
	
    /**
     * Main function just creates and runs the application.
     */
    public static void main(String args[]) {
    	GLART_3_blackboard app = new GLART_3_blackboard();
    	displayWidth = 800;
    	displayHeight = 600;
        app.run();
    }

    /**
     * Initialize OpenGL
     */
    public void setup() {
    	// set the background color
    	gl.glClearColor(0, 0, 0, 1f);

    	// set ortho projection to the same size as the window
    	// this means that our world coordinates exactly match the screen coordinates
     	setOrtho(0, 0, displayWidth, displayHeight);
    }

    /**
     * Render the scene.
     */
    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
        gl.glLoadIdentity();

        // Where is the 'eye'
        glu.gluLookAt(
            0, 0, 10,    // eye 
            0, 0,  0,    // look at the origin
            0, 1,  0);   // camera top is up the Y

        // green
        gl.glColor3f(0,1,0);
        
        // draw all the points left from mouse motion
    	gl.glBegin(GL.GL_POINTS);
        for (int i=0; i < points.size(); i++) {
        	JOVector p = (JOVector)points.get(i);
        	gl.glVertex3f(p.x, p.y, 0);
		}
    	gl.glEnd();
    }

    /**
     * override the JOApp mouseMoved() function: record position of mouse motions.
     */
    public void mouseMoved(MouseEvent me) {
        super.mouseMoved(me);   // let the JOApp.mouseMoved function set cursorX, cursorY
        
        // hold the mouse position in a list
        points.add(new JOVector(cursorX, cursorY, 0));
    }
}

