import javax.media.opengl.*; import com.sun.opengl.util.Animator; import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Complex implements GLEventListener { public static void main(String[ ] args) { Frame frame = new Frame("Basic"); // create the frame frame.setSize(500, 500); // give it a size GLCanvas canvas = new GLCanvas(); // create the canvas Complex basic = new Complex(); // create the listener canvas.addGLEventListener(basic); // add listener to canvas frame.add(canvas);//add canvas to window frame.setVisible(true); // show window } public void init(GLAutoDrawable drawable) { GL gl = drawable.getGL(); gl.glEnable(GL.GL_CULL_FACE); gl.glEnable(GL.GL_DEPTH_TEST); } public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL gl = drawable.getGL(); float ratio = (float)height / (float)width; // compute height/width ratio gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustum(-1.0f, 1.0f, -ratio, ratio, 5.0f, 6000.0f); gl.glMatrixMode(GL.GL_MODELVIEW); } public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL(); gl.glClearColor(0.3f, 0.3f, 0.99f, 1.0f); // a blue sky gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // set the view transformation gl.glLoadIdentity(); gl.glTranslatef(0.f, -4.f, 0.0f); // look from a bit above the floor // draw the floor drawFloor(gl); // draw some pyramids drawScenery(gl); } public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) { // TODO Auto-generated method stub } // draw the floor private void drawFloor(GL gl) { gl.glBegin(GL.GL_QUADS); gl.glColor3f(0.3f, 0.7f, 0.3f); gl.glVertex3f(-1000.0f, 0.0f, 1000.0f); gl.glVertex3f( 1000.0f, 0.0f, 1000.0f); gl.glVertex3f( 1000.0f, 0.0f, -1000.0f); gl.glVertex3f(-1000.0f, 0.0f, -1000.0f); gl.glEnd(); } // draw some pyramids private void drawScenery(GL gl) { for(int i = 0; i < 10; i++) { for(int j = 0; j < 10; j++) { gl.glPushMatrix(); gl.glTranslatef(i*200.f - 900.f, 5.f, j*200.f - 1000.f); gl.glScalef(10.f, 10.f, 10.f); gl.glColor3f(((float)i) / 10.0f, ((float)j) / 10.0f, ((float)i+j) / 20.0f); gl.glBegin(GL.GL_TRIANGLES); gl.glVertex3f(0.0f, 1.0f, 0.0f); //Top Of Triangle (Front) gl.glVertex3f(-1.0f, -1.0f, 1.0f); //Left Of Triangle (Front) gl.glVertex3f(1.0f, -1.0f, 1.0f); //Right Of Triangle (Front) gl.glVertex3f(0.0f, 1.0f, 0.0f); //Top Of Triangle (Right) gl.glVertex3f(1.0f, -1.0f, 1.0f); //Left Of Triangle (Right) gl.glVertex3f(1.0f, -1.0f, -1.0f); //Right Of Triangle (Right) gl.glVertex3f(0.0f, 1.0f, 0.0f); //Top Of Triangle (Back) gl.glVertex3f(1.0f, -1.0f, -1.0f); //Left Of Triangle (Back) gl.glVertex3f(-1.0f, -1.0f, -1.0f); //Right Of Triangle (Back) gl.glVertex3f(0.0f, 1.0f, 0.0f); //Top Of Triangle (Left) gl.glVertex3f(-1.0f, -1.0f, -1.0f); //Left Of Triangle (Left) gl.glVertex3f(-1.0f, -1.0f, 1.0f); //Right Of Triangle (Left) gl.glEnd(); gl.glPopMatrix(); } } } }