Hello World ~ OpenGL Style
- Create a class that extends JFrame. Let's call it MyClass to keep it simple
- Import the following packages to MyClass
- javax.media.opengl.*
javax.media.opengl.glu.*
com.sun.opengl.*
com.sun.opengl.util.* - Have MyClass implement GLEventListenter and define the following classes
- public void init(GLAutoDrawable drawable);
public void display(GLAutoDrawable drawable);
public void dispalyChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged);
reshape(GLAutoDrawable drawable, int x, int y, int width, int height); - Create a GLCanvas class level variable in MyClass
- In the constructor
- Instantiate the GLCanvas
- Call the addGLEventListener method and pass MyClass
- addGLEventListener(this);
- Set the size of the GLCanavas
- setSize( x, y)
- Add the GLCanvas to the center of the JFrame.
- Note If you add the GLCanvas to East, South, West, or North of the BorderLayout, it won't work. I don't know why.
- The next place is the init method. In this method you can set anything that you only need to set once for the program.
- Set clear and set the perspective
- gl.glMatrixMode(GL.GL_PROJECTION)
- gl.glLoadIdentity()
- glu.gluPerspective(fovy, aspect, zNear, zFar)
- Enable the depth test
- glEnable(GL.GL_DEPTH_TEST)
- Every time the canvas redraws itself if calls the display method. This is where the magic happens.
- Clear the color and depth buffer
- glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT )
- Clear the model view matrix
- gl.glMatrixMode(GL.GL_MODELVIEW)
- gl.glLoadIdentity()
- Setup your camera
- glu.gluLookAt(x,y,z, x,y,z, x,y,z) //camera origin, what is points at, what direction is up
- Push the matrix
- glPushMatrix()
- Set the color
- glColor3d( red, green, blue)
- Draw the teapot
- glutTeapot( scale)
- Pop the matrix
- glPopMatrix()
- That all you need to do.
