-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
192 lines (163 loc) · 4.31 KB
/
Copy pathmain.c
File metadata and controls
192 lines (163 loc) · 4.31 KB
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
#include <GL/freeglut_std.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include "helper.h"
#include <stdio.h>
// variables to hold temporary values of the angles and given inputs from the
// keyboard
float aX = 30, aY = 1, aZ = 0, tY = 0;
int frame = 0, stop = 0;
// All faces and vertices of the cube, each row represents the four vertices of
// each face
struct Point3D _vertices[6][4] = {
{{0.5, 0.5, 0.5}, {0.5, -0.5, 0.5}, {-0.5, -0.5, 0.5}, {-0.5, 0.5, 0.5}},
{{-0.5, 0.5, 0.5},
{-0.5, -0.5, 0.5},
{-0.5, -0.5, -0.5},
{-0.5, 0.5, -0.5}},
{{-0.5, 0.5, -0.5},
{-0.5, -0.5, -0.5},
{0.5, -0.5, -0.5},
{0.5, 0.5, -0.5}},
{{0.5, 0.5, -0.5}, {0.5, -0.5, -0.5}, {0.5, -0.5, 0.5}, {0.5, 0.5, 0.5}},
{{-0.5, 0.5, -0.5}, {0.5, 0.5, -0.5}, {0.5, 0.5, 0.5}, {-0.5, 0.5, 0.5}},
{{-0.5, -0.5, -0.5},
{0.5, -0.5, -0.5},
{0.5, -0.5, 0.5},
{-0.5, -0.5, 0.5}}};
// All the colors for each face of the cube
struct ColorRGB _colors[6] = {{0.10, 0.07, 0.93}, {0.39, 0.07, 0.33},
{0.33, 0.36, 0.07}, {0.64, 0.33, 0.07},
{0.76, 0.40, 0.45}, {0.98, 0.23, 0.07}};
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("cubeSim");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutIdleFunc(display);
glutKeyboardFunc(handleKeyboard);
display_init();
glutMainLoop();
return 0;
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Push a new matrix in the stack copy the values from the current one
glPushMatrix();
// Do all the rotations for current values of angles with x,y and z axes
glRotatef(aX, 1, 0, 0);
glRotatef(aZ, 0, 0, 1);
glRotatef(tY, 0, 1, 0);
// Now draw cube over the rotated space
drawCube(30);
// Copy the values of the new matrix to current matrix and pop off the new one
glPopMatrix();
glColor4f(0.56, .22, 1, 1); // COLOR of TITLE
displayString(-16.0f, 45.0, 0.0, TITLE, 1); // DRAW TITLE
glColor4f(.470, .56, .61, 1);
displayString(-47, -47, 0.0, INSTRUCTION, 0);
glFlush();
glutSwapBuffers();
if (!stop) {
tY += aY;
if (tY >= 360) {
tY = 0;
}
}
}
void display_init() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50, 50, -50, 50, -50, 50);
glMatrixMode(GL_MODELVIEW);
glClearColor(0, 0, 0, 0);
}
void drawCube(GLint size) {
int i;
for (size_t i = 0; i < 6; i++) {
drawQuad(size, _vertices[i], _colors[i]);
}
}
void drawQuad(GLint size, struct Point3D points[], struct ColorRGB color) {
GLint i;
glPolygonMode(GL_FRONT_AND_BACK, frame? GL_LINE: GL_FILL);
glColor4f(color.R, color.G, color.B, 1);
glBegin(GL_QUADS);
for (short i = 0; i < 4; i++) {
glVertex3i(size * points[i].x, size * points[i].y, size * points[i].z);
}
glEnd();
}
void displayString(float x, float y, float z, char *string, int is_big) {
glRasterPos3f(x, y, z);
for (size_t i = 0; string[i] != 0; i++) {
glutBitmapCharacter(
is_big ? GLUT_BITMAP_HELVETICA_18 : GLUT_BITMAP_HELVETICA_12, string[i]);
}
}
void handleKeyboard(unsigned char key, int x, int y) {
switch (key) {
// map 'W' to increment x-axis rotation angle
case 'W':
aX += ROTATION_ANGLE;
if (aX >= 359) {
aX = 0;
}
break;
// map 'S' to decrement x-axis rotation angle
case 's':
case 'S':
aX -= ROTATION_ANGLE;
if (aX <= 0) {
aX = 359;
}
break;
// map 'A' to increment z-axis rotation angle
case 'a':
case 'A':
aZ += ROTATION_ANGLE;
if (aZ >= 359) {
aZ = 0;
}
break;
// map 'D' to decrement z-axis rotation angle
case 'd':
case 'D':
aZ -= ROTATION_ANGLE;
if (aZ <= 0) {
aZ = 359;
}
break;
// map '+' to increment y-axis rotation speed
case '+':
aY += 1;
if (aY >= 360) {
aY = 0;
}
break;
// map '-' to decrement y-axis roation speed
case '-':
aY -= 1;
if (aY <= 0) {
aY = 360;
}
break;
// map 'k' to toggle frame draw switch
case 'k':
case 'K':
frame ^= 1;
break;
// map 'SPACE' to toggle stop y-axis rotation switch
case 0x20:
stop ^= 1;
break;
// map 'Q' to exit
case 0x1B:
case 'q':
case 'Q':
exit(0);
break;
}
}