OpenGL实现3D立体显示

来源:互联网 发布:淘宝网中老年春秋装 编辑:程序博客网 时间:2024/04/28 21:03

由于左眼和右眼观看显示器的角度不同,利用这一角度差遮住光线就可将图像分配给右眼或者左眼,经过大脑将这两幅由差别的图像合成为一副具有空间深度和维度信息的图像,从而可以看到3D图像。

        完整的实现代码如下所示:

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