package com.jibberia.gl.play; import java.util.ArrayList; import java.util.HashMap; 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 Squares { private boolean done = false; private DisplayMode displayMode; private boolean clear = true; private ArrayList squares; private boolean waitForSpace, drawOrigin = false, waitForO; float camx=0, camy=0, camz=10f; float keyDelta = 0.01f, cameraDistance=10f, cameraRotation=0; public static void main(String args[]) { Squares app = new Squares(); app.run(); } public void run() { System.out.println("space: new square;\n up/down: speed;\n o: show origin;\n wasd/rf: navigate camera;"); try { init(); while (!done) { updateWindow(); render(); Display.update(); } cleanup(); } catch (Exception e) { e.printStackTrace(); System.exit(0); } } private void init() throws Exception { initDisplay(); initGL(); squares = new ArrayList(); squares.add(new Square()); rotateCamera(); } 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.setVSyncEnabled(true); Display.setTitle("Squares"); Display.create(); } private void updateWindow() { if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { System.out.println("escape == quit."); done = true; } if (Keyboard.isKeyDown(Keyboard.KEY_UP)) { Square.zDelta += 0.0001f; } if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { Square.zDelta -= 0.0001f; } 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 -= 4*keyDelta; r = true; } if (Keyboard.isKeyDown(Keyboard.KEY_D)) { cameraRotation += 4*keyDelta; r = true; } if (Keyboard.isKeyDown(Keyboard.KEY_W)) { camy += keyDelta; r = true; } if (Keyboard.isKeyDown(Keyboard.KEY_S)) { camy -= keyDelta; r = true; } if(r) rotateCamera(); if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) { waitForSpace = true; } else if(waitForSpace) { squares.add(new Square()); waitForSpace = false; } if(Keyboard.isKeyDown(Keyboard.KEY_O)) { waitForO = true; } else if(waitForO) { drawOrigin = !drawOrigin; waitForO = false; } clear = !Keyboard.isKeyDown(Keyboard.KEY_C); if (Display.isCloseRequested()) { done = true; } } 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); } private void initGL() { GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GLU.gluPerspective( 60.0f, // field of view (float) displayMode.getWidth() / (float) displayMode.getHeight(), // aspect ratio 0.1f, // near Z clipping plane 35.0f); // far Z clipping plane GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glEnable(GL11.GL_DEPTH_TEST); } private void render() { if (clear) GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); GL11.glLoadIdentity(); GLU.gluLookAt(camx, camy, camz, 0, 0, 0, 0, 1, 0); if(drawOrigin) drawAxes(); for(Square sq : squares) sq.render(); } 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(); } private void cleanup() { Display.destroy(); } } class Emitter { float cx, cy, cz; // center public ArrayList squares; Emitter() { } void render() { } } class Square { public static float zMin = -20.0f, zMax = 10.0f, zDelta = 0.01f; public static float scalarDelta; float scalar; float z = zMin; void render() { GL11.glPushMatrix(); GL11.glColor3f(1,1,1); GL11.glBegin(GL11.GL_LINE_LOOP); { GL11.glVertex3f(1f, 1f, z); GL11.glVertex3f(1f, -1f, z); GL11.glVertex3f(-1f, -1f, z); GL11.glVertex3f(-1f, 1f, z); } GL11.glEnd(); GL11.glPopMatrix(); z += zDelta; if(z > zMax) z = zMin; // System.out.println(z); } }