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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| import general.OGLStarter; import java.nio.ByteBuffer; import java.nio.FloatBuffer; import org.lwjgl.opengl.Display; import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.util.glu.GLU.*; import org.lwjgl.util.glu.Sphere;
public class Solar extends OGLStarter { ByteBuffer bBuffer = ByteBuffer.allocateDirect(1024 + 128); FloatBuffer lightPos = bBuffer.asFloatBuffer(); FloatBuffer whiteLight = bBuffer.asFloatBuffer(); FloatBuffer sourceLight = bBuffer.asFloatBuffer(); Sphere glSphere = new Sphere(); public Solar() { float light[] = { 0.0f, 0.0f, 0.0f, 1.0f }; float whiteLight2[] = { 0.2f, 0.2f, 0.2f, 1.0f }; float sourceLight2[] = { 0.8f, 0.8f, 0.8f, 1.0f }; lightPos.put(light); lightPos.flip(); whiteLight.put(whiteLight2); whiteLight.flip(); sourceLight.put(sourceLight2); sourceLight.flip(); } public void renderScene() { float fMoonRot = 0.0f; float fEarthRot = 0.0f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); glPushMatrix();
glTranslatef(0.0f, 0.0f, -300.0f);
glDisable(GL_LIGHTING); glColor3ub((byte)255, (byte)255, (byte)0); glSphere.draw(15.0f, 30, 17); glEnable(GL_LIGHTING);
glLight(GL_LIGHT0,GL_POSITION,lightPos);
glRotatef(fEarthRot, 0.0f, 1.0f, 0.0f);
glColor3ub((byte)0,(byte)0,(byte)255); glTranslatef(105.0f,0.0f,0.0f); glSphere.draw(15.0f, 30, 17);
glColor3ub((byte)200,(byte)200,(byte)200); glRotatef(fMoonRot,0.0f, 1.0f, 0.0f); glTranslatef(30.0f, 0.0f, 0.0f); fMoonRot+= 15.0f; if(fMoonRot > 360.0f) fMoonRot = 0.0f;
glSphere.draw(6.0f, 30, 17);
glPopMatrix();
fEarthRot += 5.0f; if(fEarthRot > 360.0f) fEarthRot = 0.0f;
Display.update(); Display.sync(10); }
public void setupRC() { glEnable(GL_DEPTH_TEST); glFrontFace(GL_CCW); glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING);
glLightModel(GL_LIGHT_MODEL_AMBIENT,whiteLight); glLight(GL_LIGHT0,GL_DIFFUSE,sourceLight); glLight(GL_LIGHT0,GL_POSITION,lightPos); glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); }
public void checkSize(int w, int h) { float fAspect;
if(h == 0) h = 1;
glViewport(0, 0, w, h);
fAspect = (float)w/(float)h;
glMatrixMode(GL_PROJECTION); glLoadIdentity();
gluPerspective(45.0f, fAspect, 1.0f, 425.0f);
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } public static void main(String[] args) { new Solar().run(); } } |