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; public class Flowers { private boolean done = false; private DisplayMode displayMode; boolean clear = true; // int numPetals; float rota = 0f; double[][] c = { {0.7, 0.4, 0.5}, {1, .97, .17}, {.17, .43, 1}, {.92, .17, 1}, {1, .4, .17} }; public static void main(String args[]) { Flowers app = new Flowers(); app.run(); } public void run() { System.out.println("press return to generate more flowers"); try { init(); render(); while (!done) { updateWindow(); Display.update(); } cleanup(); } catch (Exception e) { e.printStackTrace(); System.exit(0); } } private void init() throws Exception { initDisplay(); initGL(); } 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("Kevin's Flowers"); Display.create(); } private void updateWindow() { if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { System.out.println("escape == quit."); done = true; } else if (Keyboard.isKeyDown(Keyboard.KEY_RETURN)) { render(); } if (Display.isCloseRequested()) { done = true; } clear = !Keyboard.isKeyDown(Keyboard.KEY_SPACE); } public void updatePhysics() { // numPetals = (numPetals+1) % 30; // rota += .08f; } private void initGL() { GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glOrtho(-400f, 400f, -300f, 300f, -100f, 100f); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glDisable(GL11.GL_DEPTH_TEST); } public void render() { if (clear) GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); GL11.glLoadIdentity(); // unncessary with ortho? drawField(); // draw flowers for(int i=0; i<(int)r(2, 20); i++) { GL11.glPushMatrix(); { GL11.glTranslatef(r(-200,200), r(-200,200) , 0f); GL11.glRotatef(r(0,360), 0,0,1); float r = r(1,100); GL11.glScalef(r,r,r); // GL11.glColor3d(c[i][0], c[i][1], c[i][2]); GL11.glColor3f(r(),r(),r()); int numPetals = (int) r(4,10); for (int j = 0; j < numPetals; j++) { GL11.glRotatef(360f/(float)numPetals, 0, 0, 1); drawPetal(.3f+1f/(float)numPetals); } } GL11.glPopMatrix(); } } private void drawField() { GL11.glColor3d(0.58, 1, 0.39); GL11.glBegin(GL11.GL_POLYGON); { GL11.glVertex3f(1000, 1000, 1); GL11.glVertex3f(1000, -1000, 1); GL11.glVertex3f(-1000, -1000, 1); GL11.glVertex3f(-1000, 1000, 1); } GL11.glEnd(); } public float r() { return r(0,1); } public float r(float lo, float hi) { return (float) Math.random()*(hi-lo) + lo; } public void drawPetal(float w) { GL11.glBegin(GL11.GL_POLYGON); { GL11.glVertex3f(0f, 0f, 0f); GL11.glVertex3f(w, 1.2f, 0f); GL11.glVertex3f(.7f*w, 1.5f, 0f); GL11.glVertex3f(.4f*w, 1.8f, 0f); GL11.glVertex3f(0f, 1.9f, 0f); GL11.glVertex3f(-.4f*w, 1.8f, 0f); GL11.glVertex3f(-.7f*w, 1.5f, 0f); GL11.glVertex3f(-w, 1.2f, 0f); } GL11.glEnd(); } private void cleanup() { Display.destroy(); } }