import org.lwjgl.opengl.*; import org.lwjgl.opengl.glu.GLU; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.glu.Sphere; public class Bug { private boolean done = false; private final String windowTitle = "Basic OpenGL App"; private DisplayMode displayMode; float rotation = 0f; // camera position and rotation (see handleNavigationKeys()) float mag=2; float t_h=0; float t_v=0; Organism yuck; Vector3D away, origin; //Main function just creates and runs the application. public static void main(String args[]) { Bug app = new Bug(); app.run(); } //Initialize the app, then sit in a render loop until done==true. public void run() { try { init(); while (!done) { mainloop(); render(); Display.update(); } cleanup(); } catch (Exception e) { e.printStackTrace(); System.exit(0); } } //Initialize the environment private void init() throws Exception { initDisplay(); initGL(); origin= new Vector3D(0,0,0); away= new Vector3D(2,2,2); yuck = new Organism(); yuck.initialize(origin, .05f, .0002f); } //Create an OpenGL display, in this case a fullscreen window. private void initDisplay() throws Exception { // set to full screen, no chrome Display.setFullscreen(false); // get all possible display resolutions DisplayMode d[] = Display.getAvailableDisplayModes(); // find a resolution we like for (int i = 0; i < d.length; i++) { if (d[i].getWidth() == 800 && d[i].getHeight() == 600 && d[i].getBitsPerPixel() == 32) { displayMode = d[i]; break; } } // set the display to the resolution we picked Display.setDisplayMode(displayMode); Display.setTitle(windowTitle); // create the window Display.create(); } //Initialize OpenGL private void initGL() { // Select the Projection Matrix (controls perspective) GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); // Reset The Projection Matrix // Define perspective GLU.gluPerspective( 45.0f, // Field Of View (float)displayMode.getWidth() / (float)displayMode.getHeight(), // aspect ratio 0.1f, // near Z clipping plane 100.0f); // far Z clipping plane // Where is the 'eye' GLU.gluLookAt( -.5f, 2f, 7f, // eye position //-.5,1,7 //0f,0f,15f, 0f, 0f, 0f, // target to look at 0f, 1f, 0f); // which way is up // Select The Modelview Matrix (controls model orientation) GL11.glMatrixMode(GL11.GL_MODELVIEW); // depth GL11.glEnable(GL11.GL_DEPTH_TEST); } //Handle keyboard input. Just check for escape key or user private void mainloop() { if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { // Escape is pressed done = true; } if(Display.isCloseRequested()) { // Window is closed done = true; } handleNavigationKeys(); } ///////////////////////////////////////////////////////////////////////// //Handle the keyboard public void handleNavigationKeys() { // Zoom in if (Keyboard.isKeyDown(Keyboard.KEY_ADD)) { mag=mag-.01f; } // Zoom out if (Keyboard.isKeyDown(Keyboard.KEY_SUBTRACT)) { mag=mag+.01f; } // rotate up if (Keyboard.isKeyDown(Keyboard.KEY_UP)) { t_v=t_v+.1f; } // rotate down if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { t_v=t_v-.1f; } // rotate left if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { t_h=t_h-.1f; } // rotate right if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { t_h=t_h+.1f; } } float sin(float a) { return (float) Math.sin(Math.toRadians((double)a)); } float cos(float a) { return (float) Math.cos(Math.toRadians((double)a)); } //Render the scene. private void render() { rotation += .01f; // Clear screen and depth buffer GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Reset the Modelview matrix // this resets the coordinate system to center of screen GL11.glLoadIdentity(); // rotate coord system around Y ///////GL11.glRotatef(rotation, 0,1,0); /* x=mag*cos(t_h); z=mag*sin(t_h); y=mag*sin(t_v); */ // Place the 'camera' GLU.gluLookAt( // camera position mag*cos(t_h), mag*sin(t_v), mag*sin(t_h), // look at a origin 0, 0, 0, // which way is up 0f, 1f, 0f); // draw crosshairs drawOrigin(); yuck.render(); yuck.seek(away); away.add(random(.7f),random(.7f), random(.7f)); if(away.magnitude()>25) { away.normalize(); } } //returns -magnitude to +magnitude float random(float magnitude) { return ((float)Math.random()-.5f)*2*magnitude; } //Create Class public void drawOrigin() { // draw origin GL11.glColor3f(.5f, .5f, .5f); GL11.glLineWidth(1); GL11.glBegin(GL11.GL_LINE_STRIP); GL11.glVertex3f( -1f, 0f, 0f); // left GL11.glVertex3f( 1f, 0f, 0f); // right GL11.glEnd(); GL11.glBegin(GL11.GL_LINE_STRIP); GL11.glVertex3f( 0f, -1f, 0f); // bottom GL11.glVertex3f( 0f, 1f, 0f); // top GL11.glEnd(); GL11.glBegin(GL11.GL_LINE_STRIP); GL11.glVertex3f( 0f, 0f, -1f); // far GL11.glVertex3f( 0f, 0f, 1f); // near GL11.glEnd(); GL11.glBegin(GL11.GL_LINE_STRIP); GL11.glVertex3f( -1f, 0f, 1f); // left near GL11.glVertex3f( 1f, 0f, 1f); // rite near GL11.glVertex3f( 1f, 0f, -1f); // rite far GL11.glVertex3f( -1f, 0f, -1f); // left far GL11.glVertex3f( -1f, 0f, 1f); // left near (close) GL11.glEnd(); } //Cleanup all the resources. private void cleanup() { Display.destroy(); } }