-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcube_rotation.cpp
114 lines (107 loc) · 2.85 KB
/
cube_rotation.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
#include <cstdlib>
#include <GL/glut.h>
#include <GL/glut.h>
#include <unistd.h>
void display()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
//Set fill color (R,G,B):
glColor3f(1.0,1.0,1.0);
glOrtho(-2.0,2.0,-2.0,2.0,-2.0,1.0);
//(angle of rotation, (x, y, z) of a vector)
glRotatef(45,1,0.5,1);
//Just draw the triangles edges:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
GLfloat vertices[8][3] = //Vector of vectors with cube vertices positions
{
{0,0,0}, //0-front-bottom-left
{0,1,0}, //1-front-upper-left
{1,1,0}, //2-front-upper-right
{1,0,0}, //3-front-bottom-right
{0,0,1}, //4-rear-bottom-left
{0,1,1}, //5-rear-upper-left
{1,1,1}, //6-rear-upper-right
{1,0,1} //7-rear-bottom-right
};
glBegin(GL_TRIANGLES); //front face - triangle 1
glVertex3fv(vertices[0]);
glVertex3fv(vertices[1]);
glVertex3fv(vertices[2]);
glEnd();
glBegin(GL_TRIANGLES); //front face - triangle 2
glVertex3fv(vertices[2]);
glVertex3fv(vertices[3]);
glVertex3fv(vertices[0]);
glEnd();
glBegin(GL_TRIANGLES); //top face - triangle 3
glVertex3fv(vertices[1]);
glVertex3fv(vertices[5]);
glVertex3fv(vertices[6]);
glEnd();
glBegin(GL_TRIANGLES); //top face - triangle 4
glVertex3fv(vertices[6]);
glVertex3fv(vertices[2]);
glVertex3fv(vertices[1]);
glEnd();
glBegin(GL_TRIANGLES); //left face - triangle 5
glVertex3fv(vertices[0]);
glVertex3fv(vertices[4]);
glVertex3fv(vertices[5]);
glEnd();
glBegin(GL_TRIANGLES); //left face - triangle 6
glVertex3fv(vertices[5]);
glVertex3fv(vertices[1]);
glVertex3fv(vertices[0]);
glEnd();
glBegin(GL_TRIANGLES); //right face - triangle 7
glVertex3fv(vertices[3]);
glVertex3fv(vertices[7]);
glVertex3fv(vertices[6]);
glEnd();
glBegin(GL_TRIANGLES); //right face - triangle 8
glVertex3fv(vertices[6]);
glVertex3fv(vertices[2]);
glVertex3fv(vertices[3]);
glEnd();
glBegin(GL_TRIANGLES); //rear face - triangle 9
glVertex3fv(vertices[7]);
glVertex3fv(vertices[4]);
glVertex3fv(vertices[5]);
glEnd();
glBegin(GL_TRIANGLES); //rear face - triangle 10
glVertex3fv(vertices[5]);
glVertex3fv(vertices[6]);
glVertex3fv(vertices[7]);
glEnd();
glBegin(GL_TRIANGLES); //bottom face - triangle 11
glVertex3fv(vertices[0]);
glVertex3fv(vertices[4]);
glVertex3fv(vertices[7]);
glEnd();
glBegin(GL_TRIANGLES); //bottom face - triangle 12
glVertex3fv(vertices[7]);
glVertex3fv(vertices[3]);
glVertex3fv(vertices[0]);
glEnd();
glFlush();
}
void keyboard(unsigned char key, int x, int y)
{
if (key == 'q' || key == 'Q')
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
// Initialize GLUT
glutInit(&argc, argv);
// Create a window
glutCreateWindow("OpenGL Cube");
// Register display callback
glutDisplayFunc(display);
// Register keyboard callback
glutKeyboardFunc(keyboard);
// Enter main event loop
glutMainLoop();
return (EXIT_SUCCESS);
}