/** * A little toy program for showing students of COMP3421 * the basics of 3D rendering. * */ import java.awt.*; import java.awt.event.*; import javax.media.opengl.*; import javax.media.opengl.glu.*; import com.sun.opengl.util.*; public class Toy2 extends Frame implements GLEventListener, KeyListener { GLCanvas glc; int aWidth = 400; int aHeight = 400; public static void main(String[] args){ Toy2 t = new Toy2(); } public Toy2(){ super("Toy2"); glc = new GLCanvas(); glc.addGLEventListener(this); glc.addKeyListener(this); setLayout(new BorderLayout()); add("Center", glc); setSize(aWidth,aHeight); setVisible(true); } /* The functions below are required because we are a GLEventListener. We could also have put them in another class and put that class in the addGLEventListener method above. */ /** * Executed exactly once to initialize the * associated GLDrawable */ public void init(GLAutoDrawable drawable) { GL gl = drawable.getGL(); /** * Set the background colour when the GLDrawable * is cleared */ gl.glClearColor(0.2f, 0.3f, 0.2f, 1.0f); } /** * Executed if the associated GLDrawable is resized */ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL gl = drawable.getGL(); GLU glu = new GLU(); gl.glViewport(x, y, width, height); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); // gl.glOrtho(-2, 2, -2, 2, -5, 10); glu.gluPerspective(60, 1, 0.5, 100); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); glu.gluLookAt(5, 5, 5, 0, 0, 0, 1, 0, 0); gl.glEnable(GL.GL_DEPTH_TEST); } /** This method handles the painting of the GLDrawable */ public void display(GLAutoDrawable drawable) { float[] xwingMat = {0.6f, 0.6f, 0.6f, 1.0f}; float[] lightpos = {5f, 5f, 5f, 1.0f}; GL gl = drawable.getGL(); GLU glu = new GLU(); gl.glShadeModel(GL.GL_SMOOTH); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightpos, 0); gl.glEnable(GL.GL_LIGHTING); gl.glEnable(GL.GL_LIGHT0); gl.glEnable(GL.GL_NORMALIZE); //scale normals to have length 1 gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE, xwingMat, 0); gl.glPushMatrix(); StarWars.xwing(gl,10); gl.glPopMatrix(); } /** This method handles things if display depth changes */ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged){ } public void keyTyped(KeyEvent evt){ } public void keyReleased(KeyEvent evt){ } public void keyPressed(KeyEvent evt){ if(evt.getKeyChar() == 'q') dispose(); } }