package classwork; import javax.media.opengl.*; import jocode.*; /** * GLART_2_cube.java * * Rotate a cube at the origin. Demonstrates a simple animation, and * includes a function to draw a cube. */ public class GLART_2_cube extends JOApp { float rotation = 0f; /** * Start the application. run() calls setup(), handles mouse and keyboard input, preps the OpenGL context. */ public static void main(String args[]) { // create the app GLART_2_cube demo = new GLART_2_cube(); // set title, window size windowTitle = "Draw a Cube"; displayWidth = 640; displayHeight = 480; // start running: will call init(), setup(), draw(), mouse functions demo.run(); } /** * Render the scene. */ public void draw() { rotation += .2f; // 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 Modelview matrix // this resets the coordinate system to center of screen gl.glLoadIdentity(); // Where is the 'eye' glu.gluLookAt( 0f, 2f, 5f, // eye position 0f, 0f, 0f, // target to look at 0f, 1f, 0f); // which way is up // draw crosshairs drawOrigin(); // draw large cube at center, turning slowly on its axis gl.glColor3f(1f,1f,0f); gl.glPushMatrix(); { gl.glRotatef(rotation, 0, 1, 0); gl.glScalef(.6f, .6f, .6f); draw_a_cube(); } gl.glPopMatrix(); } /** * Draw crosshairs at 0,0,0 */ public void drawOrigin() { gl.glColor3f(.5f, .5f, .5f); gl.glLineWidth(1); gl.glBegin(GL.GL_LINES); gl.glVertex3f( -1f, 0f, 0f); // left gl.glVertex3f( 1f, 0f, 0f); // right gl.glVertex3f( 0f, -1f, 0f); // bottom gl.glVertex3f( 0f, 1f, 0f); // top gl.glVertex3f( 0f, 0f, -1f); // far gl.glVertex3f( 0f, 0f, 1f); // near gl.glEnd(); gl.glBegin(GL.GL_LINE_STRIP); gl.glVertex3f( -1f, 0f, 1f); // left near gl.glVertex3f( 1f, 0f, 1f); // rite near gl.glVertex3f( 1f, 0f, -1f); // rite far gl.glVertex3f( -1f, 0f, -1f); // left far gl.glVertex3f( -1f, 0f, 1f); // left near (close) gl.glEnd(); } /** * Draw a 2x2x2 cube centered at the origin */ public void draw_a_cube() { gl.glBegin(GL.GL_QUADS); // Front Face gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left gl.glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right gl.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left // Back Face gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right gl.glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left gl.glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left // Top Face gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left gl.glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right gl.glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right // Bottom Face gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right gl.glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left gl.glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right // Right face gl.glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right gl.glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right gl.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left gl.glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left // Left Face gl.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left gl.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right gl.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left gl.glEnd(); } }