openGPMP
Open Source Mathematics Package
openGL_torus_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 /*
17  * handles window reshaping/resizing
18  */
19 void reshape(GLint w, GLint h) {
20  glViewport(0, 0, w, h);
21  GLfloat aspect = (GLfloat)w / (GLfloat)h;
22  glMatrixMode(GL_PROJECTION);
23  glLoadIdentity();
24 
25  if (w <= h) {
26  // width is smaller, go from -50 .. 50 in width
27  glOrtho(-50.0, 50.0, -50.0 / aspect, 50.0 / aspect, -1.0, 1.0);
28  }
29 
30  else {
31  // height is smaller, go from -50 .. 50 in height
32  // glOrtho(-50.0*aspect, 50.0*aspect, -50.0, 50.0, -1.0, 1.0);
33 
34  glOrtho(-10.0 * aspect, 10.0 * aspect, -10.0, 10.0, -1.0, 1.0);
35  }
36 }
37 
38 void display() {
39  // setting every pixel in frame buffer to a clear color
40  glClear(GL_COLOR_BUFFER_BIT);
41  glMatrixMode(GL_MODELVIEW);
42  glLoadIdentity();
43 
44  glColor3f(1.0, 1.0, 1.0);
45  glRotatef(current_rot, 0.0, 0.0, 1.0);
46  glutWireTorus(0.5, 3, 15, 30);
47 
48  // being drawing
49 
50  glBegin(GL_POLYGON);
51  glRotatef(current_rot, 1.0, 0.0, 1.0);
52  glColor3f(1, 0, 0);
53  glVertex3f(0, 0, 0);
54  glVertex3f(10, 0, 0);
55  glColor3f(0, 1, 0);
56  glVertex3f(0, 0, 0);
57  glVertex3f(0, 10, 0);
58  glColor3f(0, 0, 1);
59  glVertex3f(0, 0, 0);
60  glVertex3f(0, 0, 10);
61  glEnd();
62 
63  glRotatef(current_rot, 1.0, 0.0, 1.0);
64 
65  // flush the draw cmd to display image immediately
66  glFlush();
67  glutSwapBuffers();
68 }
69 
70 /*
71  * timer that increments the torus orientations for animation
72  */
73 void timer(int v) {
74  if (spinning) {
75  current_rot += 1.0;
76 
77  if (current_rot > 360.0) {
78  current_rot -= 360.0;
79  }
80  glutPostRedisplay();
81  }
82 
83  glutTimerFunc(1000 / FPS, timer, v);
84 }
85 
86 /*
87  * mouse events, left click generates new animations, right clock
88  * removes idle-time callback
89  */
90 void mouse(int button, int state, int x, int y) {
91  if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
92  spinning = true;
93  } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
94  spinning = false;
95  }
96 }
97 
98 void init() {
99  glClearColor(0.0, 0.0, 0.0, 1.0);
100  glColor3f(1.0, 1.0, 1.0);
101 
102  glMatrixMode(GL_PROJECTION);
103  glLoadIdentity();
104  gluPerspective(60.0, 4.0 / 3.0, 1, 40);
105 
106  glMatrixMode(GL_MODELVIEW);
107  glLoadIdentity();
108  gluLookAt(4, 6, 5, 0, 0, 0, 0, 1, 0);
109 }
110 
111 int main(int argc, char **argv) {
112  glutInit(&argc, argv);
113  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
114 
115  // Position window at (80,80)-(480,380) and give it a title.
116  glutInitWindowPosition(80, 80);
117  glutInitWindowSize(1000, 800);
118  glutCreateWindow("A Torus on a Plane");
119 
120  glutReshapeFunc(reshape);
121 
122  // Tell GLUT that whenever the main window needs to be repainted
123  // that it should call the function display().
124  glutDisplayFunc(display);
125  glutTimerFunc(100, timer, 0);
126  glutMouseFunc(mouse);
127 
128  init();
129 
130  glutMainLoop();
131 }
void init()
void display()
void reshape(GLint w, GLint h)
static GLfloat current_rot
int main(int argc, char **argv)
void timer(int v)
static const int FPS
void mouse(int button, int state, int x, int y)
static bool spinning