-
Notifications
You must be signed in to change notification settings - Fork 0
/
machThree.cpp
282 lines (170 loc) · 4.5 KB
/
machThree.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//g++ machThree.cpp -o OpenGLExample -lglut -lGL -lGLEW -lGLU
//./OpenGLExample
#include<GL/glut.h>
#include <stdlib.h>
#include <math.h>
#define RED 1
#define GREEN 2
#define BLUE 3
#define WHITE 4
GLfloat cameraZ=10;
GLfloat rotate=0.0;
GLfloat xRotation=0;
GLfloat yRotation=1.0;
float angle = 0.0, deltaAngle=2.0;
//float day1=0.0, deltaDay1=1.0.0;
float year1=0.0, deltaYear1=1.0;
float year2=0.0, deltaYear2=1.0;
float year3=0.0, deltaYear3=1.0;
float year4=0.0, deltaYear4=1.0;
float red=1.0, blue=1.0, green=1.0;
float width,height;
int specialKey;
void changeSize(int w, int h) {
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
// we're storing these values for latter use in the
// mouse motion functions
width = w;
height = h;
float ratio = 1.0* w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mySolidSphere(GLfloat radius, int slices, int stacks)
{
glPushMatrix();
glRotatef(90, 1.0, 0.0, 0.0);
glutWireSphere(radius, slices, stacks);
//glutSolidSphere(radius, slices, stacks);
glPopMatrix();
}
//angle describes the camera's rotation relative to the world.
//angle2 describes the rotation of planet 2 and so forth.
void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( 0.0, 0.0, cameraZ,
0.0, 0.0, 0.0,
0.0f, 1.0f, 0.0f);
glPushMatrix();
glRotatef(angle,xRotation,yRotation,0.0);
glColor3f(red,green,blue);
mySolidSphere(7.0, 20, 20);
glPushMatrix();
glRotatef(year1, 0.0, 1.0, 0.0);
glTranslatef(10.0, 0.0, 0.0);
glColor3f(red, green, 1.0);
mySolidSphere(1.0, 20, 20);
glPushMatrix();
glRotatef(year2, 1.0, 0.0, 0.0);
glTranslatef(0.0, 10.0, 0.0);
glColor3f(1.0, green, blue);
mySolidSphere(1.0, 20, 20);
glPushMatrix();
glRotatef(year3, 0.0, 0.0, 1.0);
glTranslatef(10.0, 10.0, 0.0);
glColor3f(red, 1.0, blue);
mySolidSphere(1.0, 20, 20);
/* glPushMatrix();
glRotatef(year4, -1.0, -1.0, 0.0);
glTranslatef(-5.0, 10.0, 0.0);
glColor3f(red, 1.0, 1.0);
*/
glPopMatrix();
year1+=deltaYear1;
year2+=deltaYear2;
year3+=deltaYear3;
year4+=deltaYear4;
// day1+=deltaDay1;
glutSwapBuffers();
}
void processMouse(int button, int state, int x, int y) {
if ((button==4)||(button==5))
{
if (state==GLUT_UP)
return;
cameraZ=cameraZ+1.0;
}
else
{
cameraZ=cameraZ-1.0;
}
glLoadIdentity();
glutPostRedisplay();
}
void processMouseActiveMotion(int x, int y) {
// the ALT key was used in the previous function
if (specialKey != GLUT_ACTIVE_ALT) {
// setting red to be relative to the mouse
// position inside the window
if (x < 0)
red = 0.0;
else if (x > width)
red = 1.0;
else
red = ((float) x)/height;
// setting green to be relative to the mouse
// position inside the window
if (y < 0)
green = 0.0;
else if (y > width)
green = 1.0;
else
green = ((float) y)/height;
// removing blue from the color.
blue = 0.0;
}
}
void processSpecialKeys(int key, int xx, int yy) {
float fraction = 0.1f;
switch (key) {
case GLUT_KEY_LEFT :
angle -= 1.0;
yRotation=1.0;
xRotation=0.0;
break;
case GLUT_KEY_RIGHT :
angle += 1.0;
yRotation=1.0;
xRotation=0.0;;
break;
case GLUT_KEY_UP :
angle -= 1.0f;
xRotation=1.0;
yRotation=0.0;
break;
case GLUT_KEY_DOWN :
angle += 1.0;
xRotation=1.0;
yRotation=0.0;
break;
}
// glLoadIdentity();
// glutPostRedisplay();
glutSwapBuffers();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(1000,1000);
glutCreateWindow("machThree");
glutSpecialFunc(processSpecialKeys);
glutMouseFunc(processMouse);
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);
glutMotionFunc(processMouseActiveMotion);
glutMainLoop();
}