package classwork;

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

/**
 * GLART_5_textured_quad.java
 *
 * This is a bare-bones texture example.  To apply a texture to geometry:
 *
 * 1) Use JOImage to load an array of image pixel data (see setup())
 * 2) Use glEnable(GL.GL_TEXTURE_2D) to enable textures (see setup())
 * 3) Use glGenTextures() to allocate a new texture resource (see JOApp.makeTexture(...))
 * 4) Use glTexImage2D() and glTexParameteri() to configure the texture (see JOApp.makeTexture())
 * 5) Use glTexCoord2f() to assign texture coordinates to vertices (see draw())
 *
 * NOTE:
 *   Texture pixels must be flipped vertically to fit the OpenGL coordinate system (0,0 in lower left)
 *         JOImage flips the pixels when it loads an image file.
 */
public class GLART_5_textured_quad extends JOApp {
    private float rotation = 0f;

    // texture handle (a number that refers to an allocated texture)
    int myTextureHandle = 0;

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

    /**
     * Initialize OpenGL
     *
     */
    public void setup() {
        // 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);

        // enable texturing
        gl.glEnable(GL.GL_TEXTURE_2D);

        // if CULL_FACE is enabled backward facing triangles will not be drawn
        gl.glDisable(GL.GL_CULL_FACE);

        // set the background color
        gl.glClearColor(.2f, .5f, .23f, 1);
        
        // Load the image as RGBA pixels
        JOImage textureImg = new JOImage("images/metal_texture_grid.png");

        // Allocate and configure a texture based on the image
        myTextureHandle = makeTexture(textureImg);

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

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

        // Reset the Modelview matrix
        // this resets the coordinate system to center of screen
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();

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

        // rotate scene
        gl.glRotatef(rotation, 0,1,0);

        // "select" our texture
    	gl.glBindTexture(GL.GL_TEXTURE_2D,myTextureHandle);

    	// optional: glColor() can tint the texture
    	gl.glColor4f(1f,1f,1f,1f);

    	// draw a quad with texture coordinates
        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();
    }
}
