import org.lwjgl.opengl.*; import org.lwjgl.opengl.glu.GLU; import org.lwjgl.input.Keyboard; //Flowers //Ortho mode with world coords mapped to screen coords. public class Flowers { private boolean done = false; private final String windowTitle = "Flowers"; private DisplayMode displayMode; float rotation = 0f; int[] randomX = new int[20]; int[] randomY = new int[20]; //Main function just creates and runs the application. public static void main(String args[]) { Flowers app = new Flowers(); 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(); // update video display } cleanup(); } catch (Exception e) { e.printStackTrace(); System.exit(0); } } //Initialize the environment: @throws Exception private void init() throws Exception { initDisplay(); initGL(); randomX = randomInts(randomX, displayMode.getWidth()); randomY = randomInts(randomY, displayMode.getHeight()); } //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); // Synchronize display updates with the video refresh rate. // Graphics cards usually refresh video screen about // 60 times per second. If opengl tries to update screen // faster than 60 x per sec, then you'll see "tearing" on // the screen (two animation frames overlapping) on screen. Display.setVSyncEnabled(true); // create the window Display.create(); } //Initialize OpenGL private void initGL() { // Select the Projection Matrix (controls perspective) GL11.glMatrixMode(GL11.GL_PROJECTION); // Reset The Projection Matrix GL11.glLoadIdentity(); // Create a 2D (orthographic) view GL11.glOrtho( 0, 800f, //X: left side, right side 0, 600f, //Y: bottom, top -1f, 1f //Z: near, far ); // Select The Modelview Matrix (controls model orientation) GL11.glMatrixMode(GL11.GL_MODELVIEW); // turn depth testing on 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 SCENE private void render() { int w = displayMode.getWidth(); int h = displayMode.getHeight(); rotation += .005f; // 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(); // draw a bunch of circles for (int i=0; i < randomX.length; i++) { // preserve the current coordinate system GL11.glPushMatrix(); { // shift to a random x,y position //GL11.glTranslatef((w/4f) + random(w/2f), (h/4f)+random(h/2f), 0); GL11.glTranslatef(randomX[i], randomY[i], 0); //GL11.glTranslatef((rotation%1f)*displayMode.getWidth(),i*30,0); //Draw Flower DrawFlower(50+randomX[i]/5, 2+(randomY[i]%2)*2,1); } GL11.glPopMatrix(); // glPopMatrix will return to previous coord system // in effect clearing the translate we did above. } } //============================================================ //Draws a flower centered at 0,0. Use translate() to place circle at desired coords. //r radius of flower petals, nodes*2 number of petals, stepSize definition public void DrawFlower(float r, int nodes, int stepSize) { int s = 0; int e = 360; float ts, tc, rs, rc; //Draw Center of Flower s = 0;e = 360; GL11.glColor3f(1f,1f,0f); GL11.glBegin(GL11.GL_QUAD_STRIP); { while ( (s = ( (s + stepSize) / stepSize) * stepSize) < e) { ts = (float) Math.sin(Math.toRadians(s)); tc = (float) Math.cos(Math.toRadians(s)); GL11.glVertex2f(0,0); GL11.glVertex2f(tc*r*.1f, ts*r*.1f); } } GL11.glEnd(); //Draw Petals s = 0; e = 360; GL11.glColor3f(1f,1f,1f); GL11.glBegin(GL11.GL_QUAD_STRIP); { while ( (s = ( (s + stepSize) / stepSize) * stepSize) < e) { ts = (float) Math.sin(Math.toRadians(s)); tc = (float) Math.cos(Math.toRadians(s)); rs = (float) Math.sin(Math.toRadians(s*nodes)); rc = (float) Math.cos(Math.toRadians(s*nodes)); GL11.glVertex2f(0,0); GL11.glVertex2f(tc*(rc*rs)*r, ts*(rc*rs)*r); } } GL11.glEnd(); }//END DrawFlower public float random(float upperbound) { return (float)Math.random()*((float)upperbound); } public int[] randomInts(int[] array, int upperBound) { for (int i=0; i