package classwork;

import java.awt.event.KeyEvent;
import javax.media.opengl.*;
import jocode.*;
import javax.media.opengl.*;
import jomodel.JOVector;


public class HW3 extends JOApp{
	
	float rotation=0.6f;
	float speed=10f;
 	
 // hold the viewpoint position in a vector object
	JOVector cameraPos = new JOVector(100f, 0f, 100f);

	// toggle value to control perspective/ortho rendering
	boolean perspectiveOn = true;
	
	
	public static void main(String args[]){
		HW3 app= new HW3();
		
		windowTitle   = "Perspective";
    	displayWidth  = 700;
    	displayHeight = 500;

		app.run();
	}
	
    @Override
    public void setup() {
    	// set a background color
        gl.glClearColor(0f, 0f, 0f, 1f);
    }

	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(
                cameraPos.x, cameraPos.y, cameraPos.z,    // eye is up the Y axis
    			0f, 0f, 0f,     // target to look at
    			0f, 1f, 0f);    // which way is up
    	
    	/*
    	for (int z = -3; z < 3; z++) {

    		for (int i = -3; i < 3; i++) { 
    			for (int c = -3; c < 3; c++) { 
    			gl.glPushMatrix();
				{
					gl.glColor3f(0.1f,i*.1f,z*.5f);
					// shift coordinate system to building position
					gl.glTranslatef(c*20 ,i*20 ,z*20 );
					// scale coordinate system to building dimensions
					gl.glScalef(3,3,3);
					// now the 1x1x1 cube will be stretched to the building size
					renderSphere(5);
				}
				gl.glPopMatrix();
    			}
    		}
    	}
    	*/
    	
    	//for (int z = -3; z < 3; z++) {

    		for (int i = -3; i < 3; i++) { 
    			for (int c = -3; c < 3; c++) { 
    			gl.glPushMatrix();
				{
					gl.glColor3f(0.1f,i*.1f, c*.7f);
					// shift coordinate system to building position
					gl.glTranslatef(c*20 ,i*20 ,i*20 );
					// scale coordinate system to building dimensions
					gl.glScalef(3,3,3);
					// now the 1x1x1 cube will be stretched to the building size
					renderSphere(5);
				}
				gl.glPopMatrix();
				
				gl.glPushMatrix();
				{
					gl.glColor3f(0.1f,i*.1f,c*.5f);
					// shift coordinate system to building position
					gl.glTranslatef(-c*20 ,-i*10 ,i*20 );
					// scale coordinate system to building dimensions
					gl.glScalef(3,3,3);
					// now the 1x1x1 cube will be stretched to the building size
					renderSphere(5);
				}
				gl.glPopMatrix();
				
    			}
    		}
    	//}
    	
    	
    	
    	
		//rotation=rotation+speed;
    	gl.glColor3f(1f,1f,1f);
    	print(60, displayHeight-50, "I'm always on top no matter what's going on");	
	}

    public void keyDown(int keycode) {
    	float increment = .2f;
    	if (keycode == KeyEvent.VK_LEFT) {
    		cameraPos.x -= increment;
    	}
    	else if (keycode == KeyEvent.VK_RIGHT) {
    		cameraPos.x += increment;
    	}
    	else if (keycode == KeyEvent.VK_UP) {
    		cameraPos.z -= increment;
    	}
    	else if (keycode == KeyEvent.VK_DOWN) {
    		cameraPos.z += increment;
    	}
    }

    public void keyUp(int keycode) {
    	if (keycode == KeyEvent.VK_SPACE) {
    		perspectiveOn = !perspectiveOn;
        	if (perspectiveOn) {
        		setPerspective();
        	}
        	else {
        		setOrtho();
        	}
    	}
    }

    public void setPerspective() {
        // select projection matrix (controls perspective) and reset it
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();

        glu.gluPerspective(
        		55,      // how wide is the field of view, in degrees
        		(float)viewportW / (float)viewportH,    // what is the aspect ratio of the view (match it to viewport)
        		1,      // how close can geometry be to the eye before it's clipped
        		500);   // how far away can geometry be from eye before it's clipped

        // return to modelview matrix
        gl.glMatrixMode(GL.GL_MODELVIEW);
    }

    public void setOrtho() {
        // select projection matrix and reset it
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();

  
        gl.glOrtho(             // these are all world coordinates
        		-13.3, 13.3,    // left and right edge of scene  (26 units wide)
        		-10, 10,        // bottom and top edge of scene  (20 units high, makes a 3x4 aspect ratio same as window)
        		 1f, 500);      // near plane, far plane

        // return to modelview matrix
        gl.glMatrixMode(GL.GL_MODELVIEW);
    }//end setOrtho
	
}//end class