//jleblanc: 2.22.06 //rudimentry exploration of branching //got to sync up rotate and translate import org.lwjgl.opengl.*; import org.lwjgl.opengl.glu.GLU; import org.lwjgl.input.Keyboard; public class tree { private boolean done = false; private final String windowTitle = "Basic OpenGL App"; private DisplayMode displayMode; float rotation = 0f; //Main function just creates and runs the application. public static void main(String args[]) { tree app = new tree(); 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(); } cleanup(); } catch (Exception e) { e.printStackTrace(); System.exit(0); } } //Initialize the environment private void init() throws Exception { initDisplay(); initGL(); } //Create an OpenGL display, in this case a fullscreen window. 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); // create the window Display.create(); } //Initialize OpenGL private void initGL() { // Select the Projection Matrix (controls perspective) GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); // Reset The Projection Matrix // Define perspective 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 // Where is the 'eye' GLU.gluLookAt( -.5f, 2f, 7f, // eye position //-.5,1,7 //0f,0f,15f, 0f, 0f, 0f, // target to look at 0f, 1f, 0f); // which way is up // Select The Modelview Matrix (controls model orientation) GL11.glMatrixMode(GL11.GL_MODELVIEW); // depth GL11.glEnable(GL11.GL_DEPTH_TEST); } //Handle keyboard input. Just check for escape key or user private void mainloop() { if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { // Escape is pressed done = true; } if(Display.isCloseRequested()) { // Window is closed done = true; } } //Render the scene. private void render() { rotation += .08f; // 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 crosshairs drawOrigin(); // rotate coord system around Y GL11.glRotatef(rotation, 0,1,0); //Draw Center Cube rendertree(rotation, 2, 1); } public void rendertree(float turn, float scale, float level) { if(level<7) { GL11.glPushMatrix(); { GL11.glColor3f(.5f+level/10,1f-level/10,level/6);//Yellow GL11.glRotatef(turn, 1,0,0); GL11.glRotatef(turn/4, 0,0,1); //Interesting but sickening //GL11.glTranslatef(0, (1+.2f*(float)Math.sin(turn/10))*scale, 0); //The translate function needs to be re-synched. GL11.glTranslatef(0, 1*scale, 0); renderCube(.01f, scale, .01f); rendertree(turn/3, scale*.5f, level+1); rendertree(-1*turn/2, scale*.5f, level+1); } GL11.glPopMatrix(); } } public void drawOrigin() { // draw origin GL11.glColor3f(.5f, .5f, .5f); GL11.glLineWidth(1); GL11.glBegin(GL11.GL_LINE_STRIP); GL11.glVertex3f( -1f, 0f, 0f); // left GL11.glVertex3f( 1f, 0f, 0f); // right GL11.glEnd(); GL11.glBegin(GL11.GL_LINE_STRIP); GL11.glVertex3f( 0f, -1f, 0f); // bottom GL11.glVertex3f( 0f, 1f, 0f); // top GL11.glEnd(); GL11.glBegin(GL11.GL_LINE_STRIP); GL11.glVertex3f( 0f, 0f, -1f); // far GL11.glVertex3f( 0f, 0f, 1f); // near GL11.glEnd(); 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 public void renderCube(float scalex, float scaley, float scalez) { GL11.glPushMatrix(); { GL11.glScalef(scalex,scaley,scalez); GL11.glBegin(GL11.GL_QUADS); // 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(); } GL11.glPopMatrix(); //NOTE: In some ways easier to do the push/pop for this part //RESCALE //GL11.glScalef(1/scale,1/scale,1/scale); } //Cleanup all the resources. private void cleanup() { Display.destroy(); } }