openGPMP
Open Source Mathematics Package
torus_animated.c
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file.
25  *
26  *
27  *
28  * This software is distributed on an AS IS basis, WITHOUT
29  * WARRANTY OF ANY KIND, either express or implied.
30  *
31  ************************************************************************/
32 
33 /*
34  * Testing openGL with drawing an animate torus
35  */
36 #include <GL/glut.h>
37 #include <math.h>
38 #include <stdio.h>
39 
40 #define pi 3.142857
41 
42 static bool spinning = true;
43 static const int FPS = 20;
44 
45 // keep track of current orentation
46 static GLfloat current_rot = 0.0;
47 
48 /*
49  * handles window reshaping/resizing
50  */
51 void reshape(GLint w, GLint h) {
52  glViewport(0, 0, w, h);
53  GLfloat aspect = (GLfloat)w / (GLfloat)h;
54  glMatrixMode(GL_PROJECTION);
55  glLoadIdentity();
56 
57  if (w <= h) {
58  // width is smaller, go from -50 .. 50 in width
59  glOrtho(-50.0, 50.0, -50.0 / aspect, 50.0 / aspect, -1.0, 1.0);
60  }
61 
62  else {
63  // height is smaller, go from -50 .. 50 in height
64  // glOrtho(-50.0*aspect, 50.0*aspect, -50.0, 50.0, -1.0, 1.0);
65 
66  glOrtho(-10.0 * aspect, 10.0 * aspect, -10.0, 10.0, -1.0, 1.0);
67  }
68 }
69 
70 void display() {
71  // setting every pixel in frame buffer to a clear color
72  glClear(GL_COLOR_BUFFER_BIT);
73  glMatrixMode(GL_MODELVIEW);
74  glLoadIdentity();
75 
76  glColor3f(1.0, 1.0, 1.0);
77  glRotatef(current_rot, 0.0, 0.0, 1.0);
78  glutWireTorus(0.5, 3, 15, 30);
79 
80  // being drawing
81 
82  glBegin(GL_POLYGON);
83  glRotatef(current_rot, 1.0, 0.0, 1.0);
84  glColor3f(1, 0, 0);
85  glVertex3f(0, 0, 0);
86  glVertex3f(10, 0, 0);
87  glColor3f(0, 1, 0);
88  glVertex3f(0, 0, 0);
89  glVertex3f(0, 10, 0);
90  glColor3f(0, 0, 1);
91  glVertex3f(0, 0, 0);
92  glVertex3f(0, 0, 10);
93  glEnd();
94 
95  glRotatef(current_rot, 1.0, 0.0, 1.0);
96 
97  // flush the draw cmd to display image immediately
98  glFlush();
99  glutSwapBuffers();
100 }
101 
102 /*
103  * timer that increments the torus orientations for animation
104  */
105 void timer(int v) {
106  if (spinning) {
107  current_rot += 1.0;
108 
109  if (current_rot > 360.0) {
110  current_rot -= 360.0;
111  }
112  glutPostRedisplay();
113  }
114 
115  glutTimerFunc(1000 / FPS, timer, v);
116 }
117 
118 /*
119  * mouse events, left click generates new animations, right clock
120  * removes idle-time callback
121  */
122 void mouse(int button, int state, int x, int y) {
123  if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
124  spinning = true;
125  } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
126  spinning = false;
127  }
128 }
129 
130 void init() {
131  glClearColor(0.0, 0.0, 0.0, 1.0);
132  glColor3f(1.0, 1.0, 1.0);
133 
134  glMatrixMode(GL_PROJECTION);
135  glLoadIdentity();
136  gluPerspective(60.0, 4.0 / 3.0, 1, 40);
137 
138  glMatrixMode(GL_MODELVIEW);
139  glLoadIdentity();
140  gluLookAt(4, 6, 5, 0, 0, 0, 0, 1, 0);
141 }
142 
143 int main(int argc, char **argv) {
144  glutInit(&argc, argv);
145  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
146 
147  // Position window at (80,80)-(480,380) and give it a title.
148  glutInitWindowPosition(80, 80);
149  glutInitWindowSize(1000, 800);
150  glutCreateWindow("A Torus on a Plane");
151 
152  glutReshapeFunc(reshape);
153 
154  // Tell GLUT that whenever the main window needs to be repainted
155  // that it should call the function display().
156  glutDisplayFunc(display);
157  glutTimerFunc(100, timer, 0);
158  glutMouseFunc(mouse);
159 
160  init();
161 
162  glutMainLoop();
163 }
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