/* * SimpleOpenGL.java * */ import com.sun.opengl.util.FPSAnimator; import com.sun.opengl.util.GLUT; import java.awt.BorderLayout; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.media.opengl.GL; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCanvas; import javax.media.opengl.GLEventListener; import javax.media.opengl.glu.GLU; import javax.swing.JFrame; /** * This class illustrates a simple Java OpenGL program. * The openGL commands are rendered into a GLCanvas which * is displayed in a JFrame. A glut teapot is displayed. */ public class SimpleOpenGL implements GLEventListener { private GLU glu = new GLU(); // OpenGL Utility Library - used to set camera view private GLUT glut = new GLUT(); // OpenGL Utility Toolkit - contains teapot /** * Create a SimpleOpenGL object. */ public SimpleOpenGL() { } /** * Initializes parameters. * @param d Object that provides support for OpenGL rendering. **/ public void init( GLAutoDrawable d ) { GL gl = d.getGL(); gl.glClearColor( .2f,.2f,.5f,0 ); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(20.0f, 1.0f, 1.0f, 200.0f); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glEnable(GL.GL_DEPTH_TEST); } /** * Called when the window is resized. * @param d Object that provides support for OpenGL rendering. * @param x horizontal location of upper left corner of window. * @param y vertical location of upper left corner of window. * @param width width of window. * @param height height of window. **/ public void reshape(GLAutoDrawable d, int x, int y, int width, int height ){ GL gl = d.getGL(); gl.glViewport(0,0, width, height); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(60.,(double)width/height, 1.0, 300.); gl.glMatrixMode(GL.GL_MODELVIEW); } /** * Draws what is on the canvas. * @param d Object that provides support for OpenGL rendering. **/ public void display( GLAutoDrawable d ) { GL gl = d.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); glu.gluLookAt(5.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); gl.glRotated(45,0,1,0); // rotate by 45 degrees about y (up) axis glut.glutWireTeapot(1); } /** * Starting point of execution. * @param args command line arguments. Not used here. **/ public static void main( String[] args ) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // Create a GLCanvas object and place into the JFrame GLCanvas canvas = new GLCanvas(); canvas.setSize(400,400); // Add an instance of SimpleOpenGL as the GLEventListener for // the GLCanvas. SimpleOpenGL listener = new SimpleOpenGL(); canvas.addGLEventListener( listener ); // add the GLCanvas to the JFrame frame.getContentPane().add( canvas, BorderLayout.CENTER ); frame.pack(); frame.setVisible( true ); } /** * Called by the drawable when the display mode or the display device associated * with the GLAutoDrawable has changed. Not used here but must be included * for interface. **/ public void displayChanged( GLAutoDrawable d, boolean modeChanged, boolean deviceChanged ) { } }