package classwork;

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

/**
 * GLART_8_textInSpace.java
 *
 * Draws text in 3D space by texturing quads with font images.
 * Does some basic keyboard input.
 *
 * see glText()
 */
public class GLART_8_textInSpace extends JOApp {
    float rotation = 0;
    String keyInfo = "";
    String userInput = "";
    JOModel obj;

    /**
     * Main function just create and run the application.
     */
    public static void main(String args[]) {
    	GLART_8_textInSpace app = new GLART_8_textInSpace();
    	windowTitle = "Text drawn in space";
    	displayWidth = 800;
    	displayHeight = 600;
        app.run();
    }

    /**
     * Initialize the app
     */
    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 (controls model orientation)
        gl.glMatrixMode(GL.GL_MODELVIEW);

        // depth test on
        gl.glEnable(GL.GL_DEPTH_TEST);

        // turn lighting on 
        gl.glEnable(GL.GL_LIGHTING);

        // Create a light
        setLight( GL.GL_LIGHT1, lightDiffuse, lightAmbient, lightSpecular, lightPosition );
        
        // set a white material
        setMaterial(new float[] {1,1,1,1}, .2f);

        // set the background color
        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/pig.obj");      //"shark.obj"  "castle.obj"  "venus.obj"
    }

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

		// 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, 6f,   // eye position
            0f, 2.5f, 0f,    // target to look at
            0f, 1f, 0f);   // which way is up

        // shift back and left
    	gl.glTranslatef(-5f,0,-5);

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

        obj.renderTextured(0);
        
        // draw text in 3D space, scaled down
        printZ(0,0,0, 0, .05f, (":" + userInput));

	    // write some text
	    print(20,20,"Type something: " + keyInfo);
    }
    
   	/**
	 * respond to keys typed
	 */
	public void keyUp(int keycode) {
		String keyText = KeyEvent.getKeyText(keycode);
		char keyChar = lastKeyEvent.getKeyChar();

		// add key to input string
    	keyInfo = "character pressed = " + keyChar;

    	// Handle backspace and enter keys
    	if (keycode == KeyEvent.VK_BACK_SPACE) {
    		msg("backspace lenght="+userInput.length());
    		if (userInput.length() > 0) {
    			userInput = userInput.substring(0,userInput.length()-1);
    		}
    	}
    	else if (keycode == KeyEvent.VK_ENTER) {
    		userInput = "";
    	}
    	else {
        	userInput += keyChar;
    	}
    }
}
