package classwork; import javax.media.opengl.*; import jocode.*; import jomodel.*; /** * GLART_4_normal.java * * Very basic normal demo. Just draws a triangle, calculates and * draws its normal. * * Uses the JOVector class to hold vertex positions and calculate normal. */ public class GLART_4_normal extends JOApp { float rotation = 0; /** * Main function just creates and runs the application. */ public static void main(String args[]) { GLART_4_normal app = new GLART_4_normal(); 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); // OpenGL won't draw backward facing triangles ("back faces") gl.glEnable(GL.GL_CULL_FACE); } /** * Render the scene. */ public void draw() { // hold triangle vertex positions in Vector objects JOVector v1 = new JOVector( 0.0f, 1.0f, 0.0f); JOVector v2 = new JOVector(-1.0f,-1.0f, 0.0f); JOVector v3 = new JOVector( 1.0f,-1.0f, 0.0f); // calculate normal of triangle JOVector normal = JOVector.getNormal(v1, v2, v3); // calculate center of triangle JOVector center = JOVector.avg(v1,v2,v3); // to turn the object rotation += .5f; // lines will be 2 pixels wide gl.glLineWidth(2); // 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 // Draw the triangle and normal, rotating gl.glPushMatrix(); { // rotate on X Y and X axes gl.glRotatef(rotation, .5f, 1, .3f); // draw a white triangle gl.glColor4f(1, 1, 1, 1); gl.glBegin(GL.GL_TRIANGLES); { gl.glVertex3f( 0.0f, .75f, 0.0f); // Top gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left gl.glVertex3f( 1.0f, -1.0f, 0.0f); // Bottom Right } gl.glEnd(); // draw the normal in red gl.glColor4f(1, 0, 0, 1); gl.glBegin(GL.GL_LINES); { gl.glVertex3f(center.x, center.y, center.z); gl.glVertex3f(center.x+normal.x, center.y+normal.y, center.z+normal.z); } gl.glEnd(); } gl.glPopMatrix(); } }