I’m currently learning computer graphics using OpenGL. I’ve designed a basic lamp. The plan is to animate it to jump around.
Animated gif

This lamp was made by implementing the following hierarchy structure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
public void render(GL2 gl) { double baseWidth = 1.0; double baseHeight = baseWidth*0.1; double baseLength = baseWidth; double lowerArmLength = 1.5; double lowerArmRadius = 0.1; double lowerArmRotation = 10; double upperArmLength = 1.0; double upperArmRadius = lowerArmRadius; double upperArmRotation = 45; double jointRadius = baseWidth*0.2; double coneSize = 0.7*baseWidth; double coneRotation = 270; double armSpacing = 0.1; double jumpX = 0; double jumpY = 0; double jumpZ = 0; int stacks = 100; int slices = 100; gl.glClear(GL2.GL_COLOR_BUFFER_BIT|GL2.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); camera.view(glu); // Orientate the camera //doLight(gl); // Place the lights //doLight1(g1); if (axes.getSwitchedOn()) axes.display(gl, glut); if (objectsOn) { // Render the objects //float[] matDiffuse = {0.9f, 0.9f, 0.9f, 1.0f}; // use glMaterialfv. There is no glMaterialdv //gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_DIFFUSE, matDiffuse, 0); gl.glPushMatrix(); gl.glTranslated(jumpX,jumpY,jumpZ); //Draw base gl.glPushMatrix(); gl.glScaled(baseWidth,baseHeight,baseLength); gl.glRotated(90,-1,0,0); cylinder.renderImmediateMode(gl); gl.glPopMatrix(); // Draw bottom joint gl.glPushMatrix(); gl.glScaled(baseWidth*0.4,jointRadius,baseWidth*0.4); gl.glRotated(90,-1,0,0); cylinder.renderImmediateMode(gl); gl.glPopMatrix(); gl.glRotated(lowerArmRotation,1,0,0); //Draw lower strength arm gl.glPushMatrix(); gl.glTranslated(armSpacing*baseWidth,lowerArmLength*0.5+baseHeight,0); gl.glRotated(90,0,0,1); gl.glScaled(lowerArmRadius,armSpacing*baseWidth*2,lowerArmRadius); gl.glRotated(90,-1,0,0); cylinder.renderImmediateMode(gl); gl.glPopMatrix(); //Draw lower arms gl.glPushMatrix(); gl.glTranslated(baseWidth*-armSpacing,baseHeight,0); gl.glScaled(lowerArmRadius,lowerArmLength,lowerArmRadius); gl.glRotated(90,-1,0,0); cylinder.renderImmediateMode(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslated(baseWidth*armSpacing,baseHeight,0); gl.glScaled(lowerArmRadius,lowerArmLength,lowerArmRadius); gl.glRotated(90,-1,0,0); cylinder.renderImmediateMode(gl); gl.glPopMatrix(); gl.glTranslated(0,lowerArmLength,0); gl.glRotated(upperArmRotation,1,0,0); //Draw middle joint gl.glPushMatrix(); glut.glutSolidSphere(jointRadius,slices,stacks); gl.glPopMatrix(); //Draw upper arms gl.glPushMatrix(); gl.glTranslated(baseWidth*armSpacing,0,0); gl.glScaled(upperArmRadius,upperArmLength,upperArmRadius); gl.glRotated(90,-1,0,0); cylinder.renderImmediateMode(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslated(baseWidth*-armSpacing,0,0); gl.glScaled(upperArmRadius,upperArmLength,upperArmRadius); gl.glRotated(90,-1,0,0); cylinder.renderImmediateMode(gl); gl.glPopMatrix(); gl.glTranslated(0,upperArmLength,0); //Draw cone structure gl.glPushMatrix(); gl.glRotated(coneRotation,1,0,0); gl.glTranslated(0,-coneSize,0); gl.glPushMatrix(); gl.glPushMatrix(); gl.glTranslated(0,coneSize-0.9*0.8*coneSize,0); gl.glScaled(coneSize*0.6,coneSize*0.8,coneSize*0.6); gl.glRotated(90,-1,0,0); cylinder.renderImmediateMode(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslated(0,coneSize,0); gl.glRotated(30,0,0,1); gl.glTranslated(-0.05*coneSize,0,0); gl.glScaled(0.4*coneSize,0.2*coneSize,0.1*coneSize); cube.renderImmediateMode(gl); gl.glPopMatrix(); gl.glPopMatrix(); gl.glRotated(90,-1,0,0);//Cone upright gl.glDisable(GL2.GL_CULL_FACE); glut.glutSolidCone(coneSize,coneSize,slices,stacks); gl.glEnable(GL2.GL_CULL_FACE); doLight1(gl); gl.glPopMatrix(); gl.glPopMatrix(); } } |