-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path04.CubeWithCamera.cpp
More file actions
123 lines (112 loc) · 2.82 KB
/
04.CubeWithCamera.cpp
File metadata and controls
123 lines (112 loc) · 2.82 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
/*
4. Draw a color cube and allow the user to move the camera suitably to experiment
with perspective viewing.
*/
#include<stdio.h>
#include<math.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 } };
float theta[3] = { 0,0,0 };
int flag = 2;
void display();
void myinit();
void keyboardFunc(unsigned char key, int x, int y);
void reshape(int w, int h);
void mouseFunc(int button, int status, int x, int y);
void drawPolygon(int a, int b, int c, int d);
void colorCube();
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2, 2, -2, 2, 2, 20);
glMatrixMode(GL_MODELVIEW);
}
int viewer[3] = { 0,0,2 };
void keyboardFunc(unsigned char key, int x, int y)
{
if (key == 'x') viewer[0]--;
if (key == 'X') viewer[0]++;
if (key == 'y') viewer[1]--;
if (key == 'Y') viewer[1]++;
if (key == 'z') viewer[2]--;
if (key == 'Z') viewer[2]++;
glutPostRedisplay();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glFrustum(-2, 2, -2 * (GLfloat)h / (GLfloat)w, 2 * (GLfloat)h / (GLfloat)w, 2, 20);
else
glFrustum(-2 * (GLfloat)h / (GLfloat)w, 2 * (GLfloat)h / (GLfloat)w, -2, 2, 2, 20);
glMatrixMode(GL_MODELVIEW);
}
void mouseFunc(int button, int status, int x, int y)
{
if (status == GLUT_DOWN)
{
if (button == GLUT_LEFT_BUTTON)
flag = 2;
if (button == GLUT_MIDDLE_BUTTON)
flag = 1;
if (button == GLUT_RIGHT_BUTTON)
flag = 0;
}
theta[flag]++;
if (theta[flag] >= 360)theta[flag] = 0;
glutPostRedisplay();
}
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);
glColor3f(1, 0, 0);
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
gluLookAt(viewer[0], viewer[1], viewer[2], 0, 0, 0, 0, 1, 0);
glRotatef(theta[0], 1, 0, 0);//x
glRotatef(theta[1], 0, 1, 0);//y
glRotatef(theta[2], 0, 0, 1);//z
colorCube();
glFlush();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("Cube with camera movement");
myinit();
glutDisplayFunc(display);
glutMouseFunc(mouseFunc);
glutKeyboardFunc(keyboardFunc);
glutReshapeFunc(reshape);
glutMainLoop();
}