-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorCube.c
86 lines (82 loc) · 1.7 KB
/
ColorCube.c
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
"Develop a OpenGL program to draw a color cube and allow the user to
move the camera suitably to experiment with perspective viewing."
#include <GL/glu.h>
#include <GL/glut.h>
GLfloat Cx=0,Cy=0,Cz=3;
void init()
{
glClearColor(0,0,0,1);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1,1,-1,1,2,10);
glMatrixMode(GL_MODELVIEW);
}
void Face(GLfloat A[],GLfloat B[],GLfloat C[],GLfloat D[])
{
glBegin(GL_POLYGON);
glVertex3fv(A);
glVertex3fv(B);
glVertex3fv(C);
glVertex3fv(D);
glEnd();
}
void Cube(GLfloat V0[],GLfloat V1[],GLfloat V2[],GLfloat V3[],GLfloat
V4[],GLfloat V5[],GLfloat V6[],GLfloat V7[])
{
glColor3f(1,0,0);
Face(V0,V1,V2,V3);
glColor3f(0,1,0);
Face(V4,V5,V6,V7);
glColor3f(0,0,1);
Face(V0,V4,V7,V3);
glColor3f(0,1,1);
Face(V1,V5,V6,V2);
glColor3f(1,1,0);
Face(V3,V2,V6,V7);
glColor3f(1,0,1);
Face(V0,V1,V5,V4);
}
void Draw()
{
GLfloat V[8][3]={
{-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},
};
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
Cube(V[0],V[1],V[2],V[3],V[4],V[5],V[6],V[7]);
glLoadIdentity();
gluLookAt(Cx,Cy,Cz,0,0,0,0,1,0);
glutSwapBuffers();
}
void keys(unsigned char ch,int x,int y)
{
switch(ch)
{
case 'x': Cx=Cx-0.5;break;
case 'X': Cx=Cx+0.5;break;
case 'y': Cy=Cy-0.5;break;
case 'Y': Cy=Cy+0.5;break;
case 'z': Cz=Cz-0.5;break;
case 'Z': Cz=Cz+0.5;break;
}
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100,150);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Colorcube with Camera");
init();
glutDisplayFunc(Draw);
glutKeyboardFunc(keys);
glutMainLoop();
return 0;