package com.jibberia.gl.asn2; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.glu.GLU; public class City { private boolean done = false; private final String windowTitle = "Mi Ciudad"; private DisplayMode displayMode; boolean clear = true; float camx = -.5f, camy=6, camz=18, keyDelta = 0.05f; private float cameraRotation = 30; private float cameraDistance = 20; int numBuildings = 5; private boolean drawSolid = true; public static void main(String args[]) { City app = new City(); app.run(); } public void run() { System.out.println("use WASD, RF to navigate"); try { init(); while (!done) { updateWindow(); // render(); Display.update(); } cleanup(); } catch (Exception e) { e.printStackTrace(); System.exit(0); } } private void init() throws Exception { initDisplay(); initGL(); rotateCamera(); // updatePhysics(); // render(); } private void initDisplay() throws Exception { Display.setFullscreen(false); DisplayMode d[] = Display.getAvailableDisplayModes(); 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; } } Display.setDisplayMode(displayMode); Display.setTitle(windowTitle); Display.create(); } private void updateWindow() { if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { // Escape is pressed System.out.println("escape == quit."); done = true; } boolean r = false; if (Keyboard.isKeyDown(Keyboard.KEY_R)) { cameraDistance -= keyDelta; r = true; } if (Keyboard.isKeyDown(Keyboard.KEY_F)) { cameraDistance += keyDelta; r = true; } if (Keyboard.isKeyDown(Keyboard.KEY_A)) { cameraRotation -= 2*keyDelta; r = true; } if (Keyboard.isKeyDown(Keyboard.KEY_D)) { cameraRotation += 2*keyDelta; r = true; } if (Keyboard.isKeyDown(Keyboard.KEY_W)) { camy += keyDelta; r = true; } if (Keyboard.isKeyDown(Keyboard.KEY_S)) { camy -= keyDelta; r = true; } if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) { drawSolid = !drawSolid; r = true; } if(r) rotateCamera(); if (Display.isCloseRequested()) { // Window is closed done = true; } // clear = !Keyboard.isKeyDown(Keyboard.KEY_SPACE); } private void rotateCamera() { camx = cameraDistance * (float) Math.sin(Math.toRadians(cameraRotation)); camz = cameraDistance * (float) Math.cos(Math.toRadians(cameraRotation)); // System.out.printf("%5.2f, %5.2f, %5.2f\nr: %f d:%f\n:", // camx, camy, camz, cameraRotation, cameraDistance); render(); } private void initGL() { GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); 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 GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glEnable(GL11.GL_DEPTH_TEST); } public void render() { if (clear) GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); GL11.glEnable(GL11.GL_DEPTH_TEST); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); 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 GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glLoadIdentity(); GLU.gluLookAt( camx, camy, camz, // eye position 0f, 0f, 0f, // target to look at 0f, 1f, 0f); // which way is up // GL11.glPushMatrix(); // GL11.glScalef(3, 3, 3); // drawAxes(); // GL11.glPopMatrix(); GL11.glTranslatef(-4,0,0); drawBuilding(1,1,3); GL11.glTranslatef(4, 0, 0); drawBuilding(2,2,2); GL11.glTranslatef(4,0,0); drawBuilding(1,1,2); GL11.glTranslatef(-3, 0, -4); drawBuilding(3, 2, 1); GL11.glDisable(GL11.GL_DEPTH_TEST); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glOrtho(-40f, 40f, -30f, 30f, -10f, 10f); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glLoadIdentity(); GL11.glScalef(38f, 28f, 30f); GL11.glPushMatrix(); { GL11.glColor3f(1,1,0); GL11.glBegin(GL11.GL_LINE_LOOP); { GL11.glVertex3f(-1.0f, -1.0f, 0f); GL11.glVertex3f(1.0f, -1.0f, 0f); GL11.glVertex3f(1.0f, 1.0f, 0f); GL11.glVertex3f(-1.0f, 1.0f, 0f); } GL11.glEnd(); } GL11.glPopMatrix(); // GL11.glTranslatef(camx,camy,camz); } private void drawBuilding(float width, float depth, float height) { GL11.glPushMatrix(); { GL11.glTranslatef(0, height, 0); GL11.glColor3d(0.2, 0.2, 0.2); GL11.glScalef(width, height, depth); drawCube(); } GL11.glPopMatrix(); } private void drawAxes() { GL11.glLineWidth(1); GL11.glBegin(GL11.GL_LINE_STRIP); GL11.glVertex3f( -1f, 0f, 0f); // left GL11.glVertex3f( 1f, 0f, 0f); // right GL11.glEnd(); GL11.glColor3f(1, 0, 0); GL11.glBegin(GL11.GL_LINE_STRIP); GL11.glVertex3f( 0f, -1f, 0f); // bottom GL11.glVertex3f( 0f, 1f, 0f); // top GL11.glEnd(); GL11.glColor3f(0,1,0); GL11.glBegin(GL11.GL_LINE_STRIP); GL11.glVertex3f( 0f, 0f, -1f); // far GL11.glVertex3f( 0f, 0f, 1f); // near GL11.glEnd(); GL11.glColor3f(1,1,1); 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(); } /** * Draw a 2x2x2 cube centered at the origin */ private void drawCube() { if(drawSolid) GL11.glBegin(GL11.GL_QUADS); else GL11.glBegin(GL11.GL_LINE_STRIP); // 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(); } private void cleanup() { Display.destroy(); } }