package classwork;

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

/**
 * GLART_8_text.java
 *
 * Demonstrates text by drawing characters textured onto quads. OpenGL
 * does not have any font or text capabilities, but we can create
 * text by texturing images of text onto quads.
 *
 * A texture image (font_tahoma.png) contains a full character set
 * arranged in order by ascii value.
 *
 * The JOApp.buildFont() function creates a set of quads, each textured
 * with a character from the character set image.  The textured quads
 * are stored as display lists for faster rendering.
 *
 * The JOApp.print() function draws text by matching each character in
 * a text string with the correct display list, then running callList()
 * to draw that character.  print() calls setOrthoOn() to set the
 * projection mode to ortho, and disables the depth test so text draws
 * on top of other geometry.  Some other settings that print() has to
 * change:
 *
 *      light   - disable lighting or the text may appear too dark
 *      texture - has to be enabled since text is actually a texture
 *      blend   - enable blending so background of text will be transparent
 *                   glEnable(GL_BLEND)
 *                   glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)
 */
public class GLART_8_text extends JOApp {
    float rotation = 0;
    JOModel obj;

    /**
     * create and run the application.
     */
    public static void main(String args[]) {
    	GLART_8_text app = new GLART_8_text();
    	windowTitle = "Text drawn over a scene";
    	displayWidth = 800;
    	displayHeight = 600;
        app.run();
    }

    /**
     * Initialize the environment
     */
    public void setup() {
		// color and position of light source
		float lightDiffuse[]  = { .7f, .7f, .6f, 1f }; // direct light
		float lightSpecular[] = { .7f, .7f, .6f, 1f }; // highlight
		float lightAmbient[]  = { .7f, .7f, .6f, 1f }; // scattered light
		float lightPosition[] = { -10f, 4f, 6, 1f };

		// 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
            1000.0f);      // far Z clipping plane

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

        gl.glEnable(GL.GL_DEPTH_TEST);

        gl.glEnable(GL.GL_LIGHTING);

        setLight( GL.GL_LIGHT1, lightDiffuse, lightAmbient, lightSpecular, lightPosition );

        gl.glClearColor(0f, 0f, 0f, 1);

        //===========================================================
        // For text rendering
        //===========================================================

        // Enable blending so that text background is transparent
        gl.glEnable(GL.GL_BLEND);
        gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

        // Turn texturing on (text is a texture so we need this on)
        gl.glEnable(GL.GL_TEXTURE_2D);
        
        obj = new JOModel("models/teapotT.obj");      //"shark.obj"  "castle.obj"  "venus.obj"
    }

    /**
     * Render the scene.
     */
    public void draw() {
    	rotation += .1f;

		// 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 coordinate system to center of screen
        gl.glLoadIdentity();

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

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

    	// draw the mesh
	    obj.renderTextured(0);

	    // write some text
	    print(20,20,"The model contains " + obj.mesh.numTriangles + " triangles");
    }
}
