openGPMP
Open Source Mathematics Package
openGL_astronomy_animated.c
Go to the documentation of this file.
1 /*
2  * Testing openGL with drawing an animate torus
3  */
4 #include <GL/glut.h>
5 #include <math.h>
6 #include <stdio.h>
7 
8 #define pi 3.142857
9 
10 static bool spinning = true;
11 static const int FPS = 20;
12 
13 // keep track of current orentation
14 static GLfloat current_rot = 0.0;
15 
16 // In the GLUT library someone put the polar regions on the z-axis -
17 // yech!! We fixed it so that they are on the y-axis. We do this by
18 // rotating -90 degrees about the x-axis which brings (0,0,1) to
19 // (0,1,0).
20 void myWireSphere(GLfloat radius, int slices, int stacks) {
21  glPushMatrix();
22  glRotatef(-90.0, 1.0, 0.0, 0.0);
23  glutWireSphere(radius, slices, stacks);
24  glPopMatrix();
25 }
26 
27 // In our solar system simulation we keep track of the current "year"
28 // and current "day" in global variables. Actually we just make the
29 // planet go around the sun in increments of 5 degrees (the "year")
30 // and rotate the planet on its own axis in increments of 10 degrees
31 // (the "day").
32 static int year = 0, day = 0;
33 
34 // As usual, the routine to display the current state of the system is
35 // bracketed with a clearing of the window and a glFlush call.
36 // Immediately within those calls the drawing code itself is bracketed
37 // by pushing and popping the current transformation. And also as
38 // usual, we are assuming the current matrix mode is GL_MODELVIEW. We
39 // finish with a SwapBuffers call because we'll animate.
40 void display() {
41  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
42  glPushMatrix();
43 
44  // Draw sun: a yellow sphere of radius 1 centered at the origin.
45  glColor3f(1.0, 1.0, 0.0);
46  myWireSphere(1.0, 15, 15);
47 
48  // Draw planet: a blue sphere of radius 0.2, 2 units away from
49  // sun, with a white "pole" for its axis.
50  glRotatef((GLfloat)year, 0.0, 1.0, 0.0);
51  glTranslatef(2.0, 0.0, 0.0);
52  glRotatef((GLfloat)day, 0.0, 1.0, 0.0);
53  glColor3f(0.0, 0.0, 1.0);
54  myWireSphere(0.2, 15, 15);
55  glColor3f(1, 1, 1);
56  glBegin(GL_LINES);
57  glVertex3f(0, -0.3, 0);
58  glVertex3f(0, 0.3, 0);
59  glEnd();
60 
61  glPopMatrix();
62  glFlush();
63  glutSwapBuffers();
64 }
65 
66 // Viewing routines. Basically the camera is sitting on a comet
67 // orbiting the sun with a "year" 8 times that of the planet. To
68 // animate we have the function nextAnimationFrame() registered as the
69 // idle function. It increments the value of u (to "move" the
70 // camera), ticks off another portion of a day and portion of a year,
71 // then reorients the camera and refreshes the display.
72 static GLfloat u = 0.0; // curve parameter for comet pos
73 static GLfloat du = 0.1; // amt to increment u each frame
74 
75 void timer(int v) {
76  u += du;
77  day = (day + 1) % 360;
78  year = (year + 2) % 360;
79  glLoadIdentity();
80  gluLookAt(20 * cos(u / 8.0) + 12,
81  5 * sin(u / 8.0) + 1,
82  10 * cos(u / 8.0) + 2,
83  0,
84  0,
85  0,
86  0,
87  1,
88  0);
89  glutPostRedisplay();
90  glutTimerFunc(1000 / 60, timer, v);
91 }
92 
93 // As usual we reset the projection transformation whenever the window
94 // is reshaped. This is done (of course) by setting the current
95 // matrix mode to GL_PROJECTION and then setting the matrix. It is
96 // easiest to use the perspective-projection-making matrix from the GL
97 // utiltiy library. Here we set a perspective camera with a 60-degree
98 // vertical field of view, an aspect ratio to perfectly map into the
99 // system window, a near clipping plane distance of 1.0 and a far
100 // clipping distance of 40.0. The last thing done is to reset the
101 // current matrix mode to GL_MODELVIEW, as that is expected in all the
102 // calls to display().
103 void reshape(GLint w, GLint h) {
104  glViewport(0, 0, w, h);
105  glMatrixMode(GL_PROJECTION);
106  glLoadIdentity();
107  gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 40.0);
108  glMatrixMode(GL_MODELVIEW);
109 }
110 
111 // The usual main() for a GLUT application.
112 int main(int argc, char **argv) {
113  glutInit(&argc, argv);
114  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
115  glutInitWindowSize(800, 600);
116  glutCreateWindow("Comet, Planet Simulation");
117  glutDisplayFunc(display);
118  glutReshapeFunc(reshape);
119  glutTimerFunc(100, timer, 0);
120  glEnable(GL_DEPTH_TEST);
121  glutMainLoop();
122 }
static GLfloat du
void display()
void reshape(GLint w, GLint h)
static GLfloat current_rot
int main(int argc, char **argv)
static int day
void timer(int v)
static int year
static GLfloat u
static const int FPS
void myWireSphere(GLfloat radius, int slices, int stacks)
static bool spinning