-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcubepp.cpp
More file actions
89 lines (80 loc) · 1.74 KB
/
cubepp.cpp
File metadata and controls
89 lines (80 loc) · 1.74 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
#include <GL/glu.h>
#include <GL/glut.h>
#include <gl/gl.h>
void display()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Cube coordinates
static GLfloat coords[]=
{
-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
};
// Cube colors
static GLfloat color[6][3]=
{
{ 1.0, 0.0, 0.0 },
{ 0.0, 1.0, 0.0 },
{ 0.0, 0.0, 1.0 },
{ 0.0, 1.0, 1.0 },
{ 1.0, 1.0, 0.0 },
{ 1.0, 0.0, 1.0 },
};
// Here's where the faces are stored, every vertex owns a number
static GLubyte num[6][4]=
{
{ 0, 1, 2, 3 },
{ 1, 5, 6, 2 },
{ 4, 5, 6, 7 },
{ 0, 4, 7, 3 },
{ 0, 1, 5, 4 },
{ 2, 6, 7, 3 }
};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, coords);
glRotatef(0.06, 0.0127, 0.025, 0.025); // Cube rotation
// Draw cube
for(int x=0; x!=6; x++)
{
glBegin(GL_QUADS);
glColor3f(color[x][0], color[x][1], color[x][2]); // Set color for one face
for(int i=0; i!=4; ++i)
glArrayElement(num[x][i]);
glEnd();
}
glutSwapBuffers();
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 27 : // Press Esc to quit
exit(0);
}
}
void glInit(void)
{
glEnable(GL_DEPTH_TEST); // Activate depth
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
int main(int argc, char **argv)
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInit(&argc, argv);
glutInitWindowSize(720, 720);
glutInitWindowPosition(100, 100);
glutCreateWindow("Cube++");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glInit();
glutMainLoop();
return 0;
}