import glmodel.GL_Mesh; import glmodel.GL_Triangle; import java.nio.*; // Native IO buffers: LWJGL uses these to efficiently exchange data with system memory import org.lwjgl.opengl.*; import org.lwjgl.opengl.glu.GLU; import org.lwjgl.opengl.glu.Sphere; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.glu.*; /* * */ public class Sawako_Surface02 { private boolean done = false; private boolean culf = false; private boolean rot = true; private boolean rot2 = true; private final String windowTitle = "::ribbon flower::"; private DisplayMode displayMode; float rotation = 0; float rotation2 = 0; // camera position and rotation (see handleNavigationKeys()) float[] cameraPos = new float[] {-1,0,4}; float cameraRotation = 0f; final float piover180 = 0.0174532925f;// A constant used in navigation private GL_Mesh obj; private GL_Mesh obj2; private GL_Mesh obj3; /////////////////basic settings/////////////////// public static void main(String args[]) { Sawako_Surface02 app = new Sawako_Surface02(); app.run(); } 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(); obj = new GL_Mesh(); sphereMesh(); obj.rebuild(); obj.regenerateNormals(); obj2 = new GL_Mesh(); sphereMesh02(); obj2.rebuild(); obj2.regenerateNormals(); obj3 = new GL_Mesh(); sphereMesh03(); obj3.rebuild(); obj3.regenerateNormals(); } 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(); } private void initGL() { // Select the Projection Matrix (controls perspective) GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); // Reset The Projection Matrix // Define perspective GLU.gluPerspective( 65.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( -1f, 0f, 4f, // eye position 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); // make sure OpenGL correctly layers objects GL11.glEnable(GL11.GL_DEPTH_TEST); // OpenGL won't draw backward facing triangles ("back faces") GL11.glDisable(GL11.GL_CULL_FACE); // set the background color GL11.glClearColor(.8f, .8f, .83f, 0.8f); // blending GL11.glEnable(GL11.GL_BLEND); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); // Force normals to length 1 GL11.glEnable(GL11.GL_NORMALIZE); } /////////////////////////key issue///////////////////////////// private void mainloop() { if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { // Escape is pressed done = true; } if(Display.isCloseRequested()) { // Window is closed done = true; } if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){ culf = !culf; } if(Keyboard.isKeyDown(Keyboard.KEY_R)){ rot = !rot; } if(Keyboard.isKeyDown(Keyboard.KEY_T)){ rot2 = !rot2; } // Handle key press events (down/up) handleKeyPressEvents(); // Handle arrow keys (press and hold) handleNavigationKeys(); } // ---------------------------------------------------------------- // Keyboard event handlers //---------------------------------------------------------------- public void handleKeyPressEvents() { while ( Keyboard.next() ) { if (Keyboard.getEventKeyState()) { keyDown(Keyboard.getEventKey()); } else { keyUp(Keyboard.getEventKey()); } } } public void handleNavigationKeys() { // Turn left if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { cameraRotation += 0.3f; } // Turn right if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { cameraRotation -= 0.3f; } // move forward in current direction if (Keyboard.isKeyDown(Keyboard.KEY_UP)) { cameraPos[0] -= (float) Math.sin(cameraRotation * piover180) * 0.3f; cameraPos[2] -= (float) Math.cos(cameraRotation * piover180) * 0.3f; } // move backward in current direction if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { cameraPos[0] += (float) Math.sin(cameraRotation * piover180) * 0.3f; cameraPos[2] += (float) Math.cos(cameraRotation * piover180) * 0.3f; } // move camera down if (Keyboard.isKeyDown(Keyboard.KEY_PRIOR)) { cameraPos[1] += .3f; } // move camera up if (Keyboard.isKeyDown(Keyboard.KEY_NEXT)) { cameraPos[1] -= .3f; } } ///////////////////////Render the scene/////////////////////////// private void render() { if(rot = true){ rotation += .02f; }else{ rotation = 0f; } if(rot2 = true){ rotation2 += .1f; } GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); GL11.glLoadIdentity(); GL11.glRotatef(rotation2, 0,1,0); // make depth buffer read-only before drawing translucent objects GL11.glDepthMask(false); GL11.glPushMatrix(); { // Place the 'camera' GLU.gluLookAt( // camera position cameraPos[0], cameraPos[1], cameraPos[2], // look at a point directly in front of camera cameraPos[0]- (float) Math.sin(cameraRotation* piover180), cameraPos[1], cameraPos[2]- (float) Math.cos(cameraRotation* piover180), // which way is up 0f, 1f, 0f); if(culf == true){ GL11.glDisable(GL11.GL_CULL_FACE); }else{ GL11.glEnable(GL11.GL_CULL_FACE); } float sc1 = (1f + (float)(Math.sin(rotation)))/2f+0.2f; GL11.glScalef(.82f*sc1, .82f*sc1, .82f*sc1); renderMesh(obj, 0); //renderMesh(obj3, 0); float sc2 = (1f + (float)(Math.sin(rotation-0.01f)))*3f; GL11.glScalef(15.82f+sc2, 15.82f+sc2, 15.82f+sc2); renderMesh(obj3, 0); float sc3 = ((float)(Math.sin(rotation-0.015f)))*5f; GL11.glScalef(30.82f+sc2, 30.82f+sc2, 30.82f+sc2); renderMesh(obj2, 0); } GL11.glPopMatrix(); GL11.glDepthMask(true); } /////////Cleanup all the resources. private void cleanup() { Display.destroy(); } ///////////////////sphere algorithm////////////////////// public void sphereMesh() { int countTri = 0; int reso = 15; for(int u=0; u