由于右眼和左眼观看显示器的角度不同,利用这一角度差遮住光线就可将图像分配给右眼或者左眼,经过大脑将这两幅由差别的图像合成为一副具有空间深度和维度信息的图像,从而可以看到3D图像。
1 #include "GL/glut.h"
2 #include "stdlib.h"
3 #include "stdio.h"
4 #include "math.h"
5
6 static int big = 0;
7 static bool isLeftEye = false;
8
9 #define PI 3.1415926
10 const GLfloat R = 8.0;
11
12 static GLfloat leftMatrix[16] = {1.0, 0.0, 0.0, 0.0,
13 0.0, 1.0, 0.0, 0.0,
14 0.0, 0.0, 1.0, 0.0,
15 0.0, 0.0, 0.0, 1.0};
16
17 static GLfloat rightMatrix[16] = {1.0, 0.0, 0.0, 0.0,
18 0.0, 1.0, 0.0, 0.0,
19 0.0, 0.0, 1.0, 0.0,
20 0.0, 0.0, 0.0, 1.0};
21
22 static GLfloat leftPersMatrix[16] = {1.0, 0.0, 0.0, 0.0,
23 0.0, 1.0, 0.0, 0.0,
24 0.0, 0.0, 1.0, 0.0,
25 0.0, 0.0, 0.0, 1.0};
26
27 static GLfloat rightPersMatrix[16] = {1.0, 0.0, 0.0, 0.0,
28 0.0, 1.0, 0.0, 0.0,
29 0.0, 0.0, 1.0, 0.0,
30 0.0, 0.0, 0.0, 1.0};
31 void init(void)
32 {
33 const GLfloat SD = 0.06;
34 GLfloat n = SD*R/2.0;
35 //要是转秩
36 //n=0;
37 leftMatrix[12] = n;
38 rightMatrix[12] = -n;
39
40 //这里假设眼到屏幕为一米,以米为单位
41 GLfloat p = SD/(2*1*tan(PI/6)*1);
42 //p = 0.0;
43 leftPersMatrix[12] = -p;
44 rightPersMatrix[12] = p;
45
46 GLfloat mat_specular[] = {0.8, 0.8, 0.0, 1.0};
47 GLfloat mat_shininess[] = {50.0};
48 GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
49 GLfloat white_light[] = {1.0, 1.0, 1.0, 1.0};
50 GLfloat yellow_light[] = {1.0, 1.0, 0.0, 1.0};
51 GLfloat lmodel_ambient[] = {0.0, 0.7, 0.5, 1.0};
52 glClearColor(1.0, 1.0, 1.0, 0.0);
53
54 glShadeModel(GL_SMOOTH);
55 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
56 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
57 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
58 glLightfv(GL_LIGHT0, GL_DIFFUSE, yellow_light);//主体的颜色
59 glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);//高光的颜色
60 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
61
62 glEnable(GL_LIGHTING);
63 glEnable(GL_LIGHT0);
64 glEnable(GL_DEPTH_TEST);
65 }
66
67 void display(void)
68 {
69 glColorMask(1.0, 1.0,1.0,1.0);
70 glClearColor(0.0,0.0,0.0,1.0);
71 glClearDepth(1.0);
72
73 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
74 glColor3f(1.0, 1.0, 1.0);
75
76
77 // 画左眼
78 glMatrixMode(GL_PROJECTION);
79 glPushMatrix();
80 float mat[16];
81 glGetFloatv(GL_PROJECTION_MATRIX,mat);
82 glLoadIdentity();
83
84 glMultMatrixf(leftPersMatrix);
85 glMultMatrixf(mat);
86
87 glMatrixMode(GL_MODELVIEW);
88 glPushMatrix();
89 glGetFloatv(GL_MODELVIEW_MATRIX,mat);
90 glLoadIdentity();
91
92 glMultMatrixf(leftMatrix);
93 glMultMatrixf(mat);
94
95 glColorMask(1.0, 0.0,0.0,1.0);
96
97 glRotatef((GLfloat) big, 0.0, 1.0, 0.0);
98 glutSolidTeapot(2.0);
99 glPopMatrix();
100
101 glMatrixMode(GL_PROJECTION);
102 glPopMatrix();
103
104 glFlush();
105
106 //画右眼
107 glClearDepth(1.0);
108 glClear(GL_DEPTH_BUFFER_BIT);
109
110 glMatrixMode(GL_PROJECTION);
111 glPushMatrix();
112 glGetFloatv(GL_PROJECTION_MATRIX,mat);
113 glLoadIdentity();
114
115 glMultMatrixf(rightPersMatrix);
116 glMultMatrixf(mat);
117
118 glMatrixMode(GL_MODELVIEW);
119 glPushMatrix();
120 glGetFloatv(GL_MODELVIEW_MATRIX,mat);
121 glLoadIdentity();
122
123 glMultMatrixf(rightMatrix);
124 glMultMatrixf(mat);
125 glColorMask(0.0, 1.0,1.0,1.0);
126 glRotatef((GLfloat) big, 0.0, 1.0, 0.0);
127 glutSolidTeapot(2.0);
128 glPopMatrix();
129
130 glMatrixMode(GL_PROJECTION);
131 glPopMatrix();
132 glFlush();
133 //glPopMatrix();
134 //if(isLeftEye)
135 //{
136 // glMatrixMode(GL_PROJECTION);
137 // glMultMatrixf(leftPersMatrix);
138
139 // glMatrixMode(GL_MODELVIEW);
140 // glMultMatrixf(leftMatrix);
141 // glColorMask(1.0, 0.0,0.0,1.0);
142 //
143 //
144 //
145 // isLeftEye = false;
146 //}else
147 //{
148 //
149 // glMatrixMode(GL_PROJECTION);
150 // glMultMatrixf(rightPersMatrix);
151
152 // glMatrixMode(GL_MODELVIEW);
153 // glMultMatrixf(rightMatrix);
154 // glColorMask(0.0, 1.0,1.0,1.0);
155 // isLeftEye = true;
156 //}
157
158 //glRotatef((GLfloat) big, 0.0, 1.0, 0.0);
159 //glutSolidTeapot(1.0);
160
161 //glRotatef((GLfloat) big, 0.0, 1.0, 0.0);
162 //glTranslatef(3.0, 0.0, 0.0);
163 //glutSolidTeapot(0.5);
164
165 glutSwapBuffers();
166
167
168 }
169 void reshape(int w, int h)
170 {
171 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
172
173 glMatrixMode(GL_PROJECTION);
174 glLoadIdentity();
175 gluPerspective(60, (GLfloat)w/(GLfloat)h, 0.01, 20.0);
176
177 glMatrixMode(GL_MODELVIEW);
178 glLoadIdentity();
179 gluLookAt(0.0, 0.0, R, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
180 }
181 void keyboard(unsigned char key, int x, int y)
182 {
183 switch (key)
184 {
185 case 'b':
186 big = (big + 1) % 360;
187 glutPostRedisplay();
188 break;
189 case 'B':
190 big = (big - 1) % 360;
191 glutPostRedisplay();
192 break;
193 case 27: // 按ESC键时退出程序
194 exit (0);
195 break;
196 default:
197 break;
198 }
199 }
200 void spinDisplay(void)
201 {
202 big = (big + 1) % 360;
203 glutPostRedisplay();
204 }
205 int main (int argc, char** argv)
206 {
207 glutInit(&argc, argv);
208 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
209 glutInitWindowSize(500, 500);
210 glutInitWindowPosition(100, 100);
211 glutCreateWindow(argv[0]);
212 init();
213 glutDisplayFunc(display);
214 glutReshapeFunc(reshape);
215 glutKeyboardFunc(keyboard);
216 glutIdleFunc(spinDisplay);
217 glutMainLoop();
218
219 return 0;
220 }
最终效果图如下:

浙公网安备 33010602011771号