You are on page 1of 2

1

// In this program the camera orbits a lit moon to simulate the phases of the
2 // moon.
3
4 #ifdef __APPLE_CC__
5 #include <GLUT/glut.h>
6 #else
7 #include <GL/glut.h>
8 #endif
9 #include <cmath>
10
11 // A class for the moon. A moon is really just an OpenGL display list. The
12 // display list is created with create(), and the draw() method calls it.
13 // The reason we don't create the display list in the constructor is that
14 // clients may want to declare moon objects before the call to initializing
15 // OpenGL.
16 //
17 // The moon is a sphere of radius 1 centered at the origin, built from 25
18 // slices and 25 stacks, lit with GL_LIGHT0 as a directional light pointing
19 // along <1,1,1>.
20 class Moon {
21 int displayListId;
22 public:
23 void create() {
24 displayListId = glGenLists(1);
25 glNewList(displayListId, GL_COMPILE);
26 GLfloat direction[] = {-1.0, -1.0, -1.0, 0.0};
27 glLightfv(GL_LIGHT0, GL_POSITION, direction);
28 glutSolidSphere(1.0, 25, 25);
29 glEndList();
30 }
31 void draw() {
32 glCallList(displayListId);
33 }
34 };
35
36 // The one and only moon.
37 static Moon moon;
38
39 // An orbiter is an object that flies on a circle of a certain radius on the
40 // xz plane. You supply the radius at construction time.
41 class Orbiter {
42 double radius;
43 double u;
44 public:
45 Orbiter(double radius): radius(radius), u(0.0) {}
46 void advance(double delta) {u += delta;}
47 void getPosition(double& x, double& y, double& z) {
48 x = radius * cos(u);
49 y = 0;
50 z = radius * sin(u);
51 }
52 };
53
54 // The one and only orbiter.
55 static Orbiter orbiter(5.0);
56
57 // Clears the window (and the depth buffer) and draws the moon as viewed from
58 // the current position of the orbiter.
59 void display() {
60 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
61 glMatrixMode(GL_MODELVIEW);
62 glPushMatrix();
63 glLoadIdentity();
64 double x, y, z;
65 orbiter.getPosition(x, y, z);
66 gluLookAt(x, y, z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
67 moon.draw();
68 glPopMatrix();
69 glutSwapBuffers();
70 }
71
72 // Advances the orbiter and requests to draw the next frame.

73 void timer(int v) {
74 orbiter.advance(0.01);
75 glutPostRedisplay();
76 glutTimerFunc(1000/60, timer, v);
77 }
78
79 // reshape() fixes up the projection matrix so that we always see a sphere
80 // instead of an ellipsoid.
81 void reshape(GLint w, GLint h) {
82 glViewport(0, 0, w, h);
83 glMatrixMode(GL_PROJECTION);
84 glLoadIdentity();
85 gluPerspective(40.0, GLfloat(w) / GLfloat(h), 1.0, 10.0);
86 }
87
88 // Enables depth testing, enables lighting for a bright yellowish diffuse
89 // light, and creates a moon.
90 void init() {
91 glEnable(GL_DEPTH_TEST);
92 GLfloat yellow[] = {1.0, 1.0, 0.5, 1.0};
93 glLightfv(GL_LIGHT0, GL_DIFFUSE, yellow);
94 glEnable(GL_LIGHTING);
95 glEnable(GL_LIGHT0);
96 moon.create();
97 }
98
99 // The usual application code.
100 int main(int argc, char** argv) {
101 glutInit(&argc, argv);
102 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
103 glutInitWindowPosition(80, 80);
104 glutInitWindowSize(500, 500);
105 glutCreateWindow("The Moon");
106 glutDisplayFunc(display);
107 glutTimerFunc(100, timer, 0);
108 glutReshapeFunc(reshape);
109 init();
110 glutMainLoop();
111 }

You might also like