package classwork;

import javax.media.opengl.*;
import java.awt.event.*;
import jocode.*;

/**
 * GLART_12_screen_capture.java
 *
 * Save the framebuffer contents (aka "the screen") to a file.
 *
 * Hit F1 to save the screen to a file
 *
 * The functions:
 * 		screenShot()
 */
public class GLART_12_screen_capture extends JOApp {
    float rotation = 0f;
    float rotation2 = 0f;
    float rotationAmount = .08f;
    int marbleTextureHandle;


    /**
     * Create and run the application.
     */
    public static void main(String args[]) {
    	GLART_12_screen_capture app = new GLART_12_screen_capture();
	    windowTitle = "Save Frame to File";
	    displayWidth = 800;
	    displayHeight = 600;
	    fullScreen = true;
        app.run();
    }

    /**
     * Initialize the environment
     */
    public void setup() {
        // Allocate and configure a texture based on the image
        marbleTextureHandle = makeTexture("images/marble.jpg");

        // Random value for rotation increment: .05 - 1.0
        rotationAmount = (float) (.05 + (Math.random()*.05));

        // Select the Projection Matrix (controls perspective)
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();    // Reset The Projection Matrix

        // Define perspective
        glu.gluPerspective(
            45.0f,        // Field Of View
            (float)getWidth() / (float)getHeight(), // aspect ratio
            0.1f,         // near Z clipping plane
            100.0f);      // far Z clipping plane

        // Select The Modelview Matrix (controls model orientation)
        gl.glMatrixMode(GL.GL_MODELVIEW);

        // Depth test ON
        gl.glEnable(GL.GL_DEPTH_TEST);

        // set the background color
        gl.glClearColor(.3f, .1f, .62f, 1);
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        // To create transparencies (alpha blending)
        gl.glEnable(GL.GL_BLEND);
        gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

        // textures on
        gl.glEnable(GL.GL_TEXTURE_2D);
    }

    /**
     * Render the scene.
     */
    public void draw() {
    	if (!paused) {
    		rotation += .05f;
    		rotation2 += rotationAmount;
    	}

    	// clear the color and depth buffers
    	gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        // Reset the Modelview matrix
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();

        gl.glBindTexture(GL.GL_TEXTURE_2D,marbleTextureHandle);

        // be sure we're in modelview mode (frameDraw switches to Projection matrix)
        gl.glMatrixMode(GL.GL_MODELVIEW);

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

        // rotate and shift scene
      	gl.glRotatef(rotation*3.3f, 0,0,1);
    	gl.glTranslatef(.5f,0,0);

    	// draw a red quad
    	gl.glColor4f(1,0f,0f,.9f);
    	gl.glPushMatrix();
    	{
        	gl.glRotatef(rotation*.7f, 0,1,1);
    		gl.glTranslatef(0,-1.5f,0);
    		gl.glRotatef(rotation2, 0,0,1);
    		drawQuad();
    	}
    	gl.glPopMatrix();

    	// rotate more
    	gl.glRotatef(rotation2*4.7f, 0,0,1);
    	gl.glTranslatef(1,0,0);

    	// draw a green quad
    	gl.glColor4f(0f,1,0f,.5f);
    	gl.glPushMatrix();
    	{
        	gl.glRotatef(rotation2*1.5f, 0,0,1);
    		gl.glTranslatef(-.5f,-.5f,0);
    		gl.glRotatef(rotation2, 0,0,1);
    		drawQuad();
    	}
    	gl.glPopMatrix();

    	// draw a blue quad
    	gl.glColor4f(0f,0f,(rotation2%360f)/360f,.5f);
    	gl.glPushMatrix();
    	{
        	gl.glRotatef(rotation2*4.2f, 1,0,1);
    		gl.glTranslatef(.5f,-.5f,0);
    		gl.glRotatef(rotation2, 0,0,1);
    		drawQuad();
    	}
    	gl.glPopMatrix();
    }

    /**
     * draw a 1x1 square
     */
    public void drawQuad() {
        gl.glBegin(GL.GL_QUADS);
        {
            gl.glTexCoord2f(0, 0);
            gl.glVertex3f(-1.0f,-1.0f, 0.0f);         // Bottom Left

            gl.glTexCoord2f(1, 0);
            gl.glVertex3f( 1.0f,-1.0f, 0.0f);         // Bottom Right

            gl.glTexCoord2f(1, 1);
            gl.glVertex3f( 1.0f, 1.0f, 0.0f);         // Top Right

            gl.glTexCoord2f(0, 1);
            gl.glVertex3f(-1.0f, 1.0f, 0.0f);         // Top left
        }
        gl.glEnd();
    }

    /**
     */
    public void keyUp(int keycode) {
        // set flag to save screen (see render())
        if (keycode == KeyEvent.VK_F1) {
	    	// save framebuffer pixels to PNG image
    		screenShot();
        }
        else if (keycode == KeyEvent.VK_P) {
	    	// save framebuffer pixels to PNG image
    		paused = !paused;
        }
    }
    
    boolean paused = false;
}
