import org.lwjgl.opengl.*; import org.lwjgl.opengl.glu.GLU; import org.lwjgl.input.Keyboard; //City View with stationary window public class City { private boolean done = false; private final String windowTitle = "Basic OpenGL App"; private DisplayMode displayMode; float rotation = 0f; //Main function just creates and runs the application. public static void main(String args[]) { City app = new City(); 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: @throws Exception private void init() throws Exception { initDisplay(); initGL(); } //Create an OpenGL display, in this case a fullscreen window. @throws Exception 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, 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 clicking to close the window. private void mainloop() { if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { // Escape is pressed done = true; } if(Display.isCloseRequested()) { // Window is closed done = true; } } //Render the Scene private void render() { float cameraX=0; float cameraZ=0; // 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(); rotation += .05f; cameraX = (float) Math.sin(Math.toRadians(rotation)); cameraZ = (float) Math.cos(Math.toRadians(rotation)); GLU.gluLookAt( 0, 0f, 5, // 0f, 0f, 0f, // target to look at (Origin) 0f, 1f, 0f); // which way is up //Put in the window GL11.glPushMatrix(); { GL11.glScalef(2,2,5); GL11.glBegin(GL11.GL_LINE_STRIP); GL11.glVertex3f(-1.0f, -1.0f, 1f); // Bottom Right GL11.glVertex3f(-1.0f, 1.0f, 1f); // Top Right GL11.glVertex3f( 1.0f, 1.0f, 1f); // Top Left GL11.glVertex3f( 1.0f, -1.0f, 1f); GL11.glVertex3f(-1.0f, -1.0f, 1f); GL11.glEnd(); } GL11.glPopMatrix(); GLU.gluLookAt( cameraX, .1f, cameraZ, // 0f, 0f, 0f, // target to look at (Origin) 0f, 1f, 0f); // which way is up //Draw Center Cube GL11.glColor3f(.6f,.6f,.6f); renderCube(.6f,1f, .2f); GL11.glPushMatrix(); { GL11.glColor3f(.5f,.5f,.5f); GL11.glTranslatef(2, .3f, 0); renderCube(.6f, 1.3f, .2f); } GL11.glPopMatrix(); GL11.glPushMatrix(); { GL11.glColor3f(.6f,.6f,.6f); GL11.glTranslatef(2, 0, 2); renderCube(1f,1f, .4f); } GL11.glPopMatrix(); GL11.glPushMatrix(); { GL11.glColor3f(.3f,.3f,.3f); GL11.glTranslatef(0, 0, 3); renderCube(1f,1f, .4f); } GL11.glPopMatrix(); } //Renders a Cube whose 3 dimensions are scalable public void renderCube(float scalex, float scaley, float scalez) { GL11.glPushMatrix(); { GL11.glScalef(scalex,scaley,scalez); GL11.glBegin(GL11.GL_QUADS); // Front Face GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left GL11.glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left // Back Face GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right GL11.glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left GL11.glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left // Top Face GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right GL11.glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right // Bottom Face GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right GL11.glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left GL11.glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right // Right face GL11.glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right GL11.glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right GL11.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left GL11.glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left // Left Face GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left GL11.glEnd(); } GL11.glPopMatrix(); } //Cleanup Resources private void cleanup() { Display.destroy(); } }