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 GLART_4_light_example extends JOApp { float rotation = 0; int texture = 0; /** * Main function just creates and runs the application. */ public static void main(String args[]) { GLART_4_light_example app = new GLART_4_light_example(); 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/marble.jpg"); gl.glBindTexture(GL.GL_TEXTURE_2D, texture); /* * * * * * * * * * * * * * * * * * * * * * * * * * * 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[] {1f, 1f, .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, 4f, // eye position 0f, 0f, 0f, // target to look at 0f, 1f, 0f); // which way is up // rotate gl.glRotatef(rotation, 0,1,0); // draw sphere with the current material settings renderSphere(48); } /** * 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(); } }