package classwork;

import javax.media.opengl.*;

import jocode.*;


/**
 *  GLART_4_light_example.java
 *
 *  Creates a light and material using JOApp functions that hide some of
 *  the complexity of the OpenGL functions.  See setup().
 *  
 *  uses JOApp.setMaterial() and JOApp.setLight()
 */
public class HW4 extends JOApp {
    float rotation = 0;
    int texture = 0;
    int buildingTexture = 0;

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

    /**
     * Initialize OpenGL
     *
     */
    public void setup() {
        // setup a basic perspective view
        setPerspective();

        // turn lighting on (does not create a light)
        gl.glEnable(GL.GL_LIGHTING);

        // set the background color
        gl.glClearColor(.0f, .1f, .2f, 1);

    	// load and activate a texture for the buildings 
        texture = makeTexture("images/earthmap1k.jpg");
        gl.glBindTexture(GL.GL_TEXTURE_2D, texture);
        
        buildingTexture = makeTexture("images/mahog_texture.jpg");
        gl.glBindTexture(GL.GL_TEXTURE_2D, buildingTexture);
        
		/* * * * * * * * * * * * * * * * * * * * * * * * *
		 *
		 * Setup light and material using JOApp functions
		 *
		 * * * * * * * * * * * * * * * * * * * * * * * * */

        // set overall scene lighting
        setAmbientLight( new float[] {.2f, .2f, .2f, 1f} );

        // create a light
        setLight( GL.GL_LIGHT1,
        		new float[] {.5f, .5f, .8f, 1f},      // diffuse color (direct light)
        		new float[] {.8f, .8f, .7f, 1f},    // ambient color (light scattered in environment)
        		new float[] {1f, 1f, .8f, 1f},      // specular color (reflected highlight, same as direct)
        		new float[] {-6f, 6f, 4, 1f}        // position 
        		);

        // create and activate material
        setMaterial(
        		new float[] {.8f, .8f, .9f, 1f},    // bluish material
        		.6f);              // medium shiny (0=matte 1=glossy)
	}

    /**
     * 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)
        // and reset the coordinate system to center of screen
        gl.glMatrixMode(GL.GL_MODELVIEW);
        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

        gl.glBindTexture(GL.GL_TEXTURE_2D, texture);
        
        // rotate
        gl.glPushMatrix();
		{
			gl.glRotatef(rotation, 0,1,0);
			 renderSphere(48);
		}
    
		gl.glPopMatrix();
        
        // draw sphere with the current material settings
       
		 gl.glBindTexture(GL.GL_TEXTURE_2D, buildingTexture);
		 
		 gl.glRotatef(rotation*-.5f, 0,1,0);
		 
		 for (int r=0; r < 3; r++) {
				for (int c=0; c < 3; c++) {
        //now do the cube
		
        gl.glPushMatrix(); 
        {
			// shift coordinate system to building position
        	
			gl.glTranslatef(r*1.5f-1.5f, 0, c*1.5f-1.5f);
			
			// scale coordinate system to building dimensions
			gl.glScalef(.1f, 2.5f, .1f);
			// now the 1x1x1 cube will be stretched to the building size
			
			if((r !=1) || (c !=1)){
			   renderCube();
			}//end if
		
        }
        gl.glPopMatrix();
        
		}//end for
	 }//end for
		 
		 //now make the top and bottom
		 
		 gl.glPushMatrix(); 
	        {
	     gl.glTranslatef(0, 1.3f, 0);
		 gl.glScalef(4f, .2f, 4f);
		 renderCube();
	        }
	        gl.glPopMatrix();
	        gl.glPushMatrix(); 
	        {
		 gl.glTranslatef(0, -1.3f, 0);
		 gl.glScalef(4f, .2f, 4f);
		 renderCube();
	        }
	        gl.glPopMatrix();
        
    }//end function
    
    /**
     * Reshape() is called when window is resized.  Reset the viewport to the new window
     * dimensions and call setPerspective() to reset the perspective view.  This will use
     * the new aspect ratio of the window so the scene will not be squashed or stretched.
     */
    public void reshape(int newDisplayWidth, int newDisplayHeight) {
    	setViewport(0,0,newDisplayWidth,newDisplayHeight);
    	setPerspective();
    }
}
