-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path03.SpinCube.cpp
More file actions
98 lines (88 loc) · 1.95 KB
/
03.SpinCube.cpp
File metadata and controls
98 lines (88 loc) · 1.95 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
/*
3. Draw a colour cube and spin it using OpenGL transformation matrices.
*/
#include <stdio.h>
#include <glut.h>
float points[][3] = { { -1,1,-1 },{ -1,-1,-1 },{ 1,-1,-1 },{ 1,1,-1 },{ -1,1,1 },{ -1,-1,1 },{ 1,-1,1 },{ 1,1,1 } };
float colors[][3] = { { 1,0,0 },{ 0,1,0 },{ 0,0,1 },{ 1,1,0 },{ 0,1,1 },{ 1,0,1 },{ 0.5,0.5,0.5 },{ 0.75,0.25,1 } };
int flag = 2;
float theta[] = { 0,0,0 };
void init();
void idleFunction();
void mouse(int key, int state, int x, int y);
void drawPolygon(int a, int b, int c, int d);
void colorCube();
void display();
void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2, 2, -2, 2, -2, 2);
glMatrixMode(GL_MODELVIEW);
}
void idleFunction()
{
++theta[flag];
if (theta[flag] >= 360)
{
theta[flag] = 0;
}
for (int i = 0; i < 1000000; i++);
glutPostRedisplay();
}
void mouse(int key, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
if (key == GLUT_LEFT_BUTTON)
flag = 2;
else if (key == GLUT_MIDDLE_BUTTON)
flag = 1;
else if (key == GLUT_RIGHT_BUTTON)
flag = 0;
}
}
void drawPolygon(int a, int b, int c, int d)
{
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glVertex3fv(points[a]);
glColor3fv(colors[b]);
glVertex3fv(points[b]);
glColor3fv(colors[c]);
glVertex3fv(points[c]);
glColor3fv(colors[d]);
glVertex3fv(points[d]);
glEnd();
}
void colorCube()
{
drawPolygon(0, 1, 2, 3);
drawPolygon(4, 5, 6, 7);
drawPolygon(5, 1, 2, 6);
drawPolygon(4, 0, 3, 7);
drawPolygon(6, 2, 3, 7);
drawPolygon(5, 1, 0, 4);
}
void display()
{
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glRotatef(theta[0], 1, 0, 0);
glRotatef(theta[1], 0, 1, 0);
glRotatef(theta[2], 0, 0, 1);
colorCube();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Rotate Cube");
init();
glutDisplayFunc(display);
glutIdleFunc(idleFunction);
glutMouseFunc(mouse);
glutMainLoop();
}