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 Snake { private boolean done = false; private final String windowTitle = "snakeribbon"; private DisplayMode displayMode; boolean clear = true; float camx = 0f, camy=6, camz=18, keyDelta = 0.05f; private float cameraRotation = -50; private float cameraDistance = 20; float sx=0,sy=0,sz=0; // snake float id=0.1f, jd=0.015f; boolean idGoUp = true, jdGoUp = true; private boolean spiral = false; private boolean waitForSpace = false; public static void main(String args[]) { Snake app = new Snake(); app.run(); } public void run() { System.out.println("wasd, rf to rotate; space to switch line to circle"); 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 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); // GL11.glBlendFunc(GL11.GL_SRC_ALPHA_SATURATE, GL11.GL_ONE); } private void updateWindow() { if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { // Escape is pressed 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)) { waitForSpace = true; } else if(waitForSpace) { spiral = !spiral; waitForSpace = false; } if(r) rotateCamera(); if (Display.isCloseRequested()) { // Window is closed done = true; } clear = !Keyboard.isKeyDown(Keyboard.KEY_C); } 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(); } public void render() { if (clear) { // GL11.glClearColor(1,1,1,1); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); } 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 // drawAxes(); float steps = 50; if(spiral) { steps = 360; // GL11.glTranslatef(-25, 0, 0); } else { // float steps = 180; GL11.glTranslatef(-steps/2, 0, 0); } float r = 5; GL11.glBegin(GL11.GL_QUAD_STRIP); for(float i=0, j=0; i 0.3f) idGoUp = false; else if(id < 0.05f) idGoUp = true; if(idGoUp ) id += 0.0001f; else id -= 0.0001f; if(jd > 0.03f) jdGoUp = false; else if(jd < 0.001f) jdGoUp = true; if(jdGoUp ) jd += 0.0000075f; else jd -= 0.0000075f; // System.out.printf("%f %f\n", id, jd); /* sx=0;sy=0;sz=0; GL11.glBegin(GL11.GL_LINE_STRIP); for(int i=0; i<10; i++) { GL11.glVertex3f(sx,sy,sz); sx += Math.random(); sy += Math.random(); sz += Math.random(); for(int j=0; j<3; j++) { float jit = (float) ((float) (Math.random() * 0.4) - 0.2); GL11.glVertex3f(sx+jit,sy+jit,sz+jit); } } GL11.glEnd(); */ } 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(); } }