使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
更新時(shí)間:2013年05月24日 10:51:05 作者:
本篇文章是對使用OpenGL實(shí)現(xiàn)3D立體顯示的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
由于左眼和右眼觀看顯示器的角度不同,利用這一角度差遮住光線就可將圖像分配給右眼或者左眼,經(jīng)過大腦將這兩幅由差別的圖像合成為一副具有空間深度和維度信息的圖像,從而可以看到3D圖像。
完整的實(shí)現(xiàn)代碼如下所示:
#include "stdafx.h"
#include "GL/glut.h"
#include "stdlib.h"
#include "stdio.h"
#include "math.h"
static int big = 0;
static bool isLeftEye = false;
#define PI 3.1415926
const GLfloat R = 8.0;
static GLfloat leftMatrix[16] = {1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0};
static GLfloat rightMatrix[16] = {1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0};
static GLfloat leftPersMatrix[16] = {1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0};
static GLfloat rightPersMatrix[16] = {1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0};
void init(void)
{
const GLfloat SD = 0.06;
GLfloat n = SD*R/2.0;
//要是轉(zhuǎn)秩
//n=0;
leftMatrix[12] = n;
rightMatrix[12] = -n;
//這里假設(shè)眼到屏幕為一米,以米為單位
GLfloat p = SD/(2*1*tan(PI/6)*1);
//p = 0.0;
leftPersMatrix[12] = -p;
rightPersMatrix[12] = p;
GLfloat mat_specular[] = {0.8, 0.8, 0.0, 1.0};
GLfloat mat_shininess[] = {50.0};
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
GLfloat white_light[] = {1.0, 1.0, 1.0, 1.0};
GLfloat yellow_light[] = {1.0, 1.0, 0.0, 1.0};
GLfloat lmodel_ambient[] = {0.0, 0.7, 0.5, 1.0};
glClearColor(1.0, 1.0, 1.0, 0.0);
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, yellow_light);//主體的顏色
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);//高光的顏色
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
glColorMask(1.0, 1.0,1.0,1.0);
glClearColor(0.0,0.0,0.0,1.0);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
// 畫左眼
glMatrixMode(GL_PROJECTION);
glPushMatrix();
float mat[16];
glGetFloatv(GL_PROJECTION_MATRIX,mat);
glLoadIdentity();
glMultMatrixf(leftPersMatrix);
glMultMatrixf(mat);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glGetFloatv(GL_MODELVIEW_MATRIX,mat);
glLoadIdentity();
glMultMatrixf(leftMatrix);
glMultMatrixf(mat);
glColorMask(1.0, 0.0,0.0,1.0);
glRotatef((GLfloat) big, 0.0, 1.0, 0.0);
glutSolidTeapot(2.0);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glFlush();
//畫右眼
glClearDepth(1.0);
glClear(GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glGetFloatv(GL_PROJECTION_MATRIX,mat);
glLoadIdentity();
glMultMatrixf(rightPersMatrix);
glMultMatrixf(mat);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glGetFloatv(GL_MODELVIEW_MATRIX,mat);
glLoadIdentity();
glMultMatrixf(rightMatrix);
glMultMatrixf(mat);
glColorMask(0.0, 1.0,1.0,1.0);
glRotatef((GLfloat) big, 0.0, 1.0, 0.0);
glutSolidTeapot(2.0);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glFlush();
//glPopMatrix();
//if(isLeftEye)
//{
// glMatrixMode(GL_PROJECTION);
// glMultMatrixf(leftPersMatrix);
// glMatrixMode(GL_MODELVIEW);
// glMultMatrixf(leftMatrix);
// glColorMask(1.0, 0.0,0.0,1.0);
//
//
//
// isLeftEye = false;
//}else
//{
//
// glMatrixMode(GL_PROJECTION);
// glMultMatrixf(rightPersMatrix);
// glMatrixMode(GL_MODELVIEW);
// glMultMatrixf(rightMatrix);
// glColorMask(0.0, 1.0,1.0,1.0);
// isLeftEye = true;
//}
//glRotatef((GLfloat) big, 0.0, 1.0, 0.0);
//glutSolidTeapot(1.0);
//glRotatef((GLfloat) big, 0.0, 1.0, 0.0);
//glTranslatef(3.0, 0.0, 0.0);
//glutSolidTeapot(0.5);
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)w/(GLfloat)h, 0.01, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, R, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'b':
big = (big + 1) % 360;
glutPostRedisplay();
break;
case 'B':
big = (big - 1) % 360;
glutPostRedisplay();
break;
case 27: // 按ESC鍵時(shí)退出程序
exit (0);
break;
default:
break;
}
}
void spinDisplay(void)
{
big = (big + 1) % 360;
glutPostRedisplay();
}
int main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(spinDisplay);
glutMainLoop();
return 0;
}
最終效果圖如下所示:
完整的實(shí)現(xiàn)代碼如下所示:
復(fù)制代碼 代碼如下:
#include "stdafx.h"
#include "GL/glut.h"
#include "stdlib.h"
#include "stdio.h"
#include "math.h"
static int big = 0;
static bool isLeftEye = false;
#define PI 3.1415926
const GLfloat R = 8.0;
static GLfloat leftMatrix[16] = {1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0};
static GLfloat rightMatrix[16] = {1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0};
static GLfloat leftPersMatrix[16] = {1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0};
static GLfloat rightPersMatrix[16] = {1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0};
void init(void)
{
const GLfloat SD = 0.06;
GLfloat n = SD*R/2.0;
//要是轉(zhuǎn)秩
//n=0;
leftMatrix[12] = n;
rightMatrix[12] = -n;
//這里假設(shè)眼到屏幕為一米,以米為單位
GLfloat p = SD/(2*1*tan(PI/6)*1);
//p = 0.0;
leftPersMatrix[12] = -p;
rightPersMatrix[12] = p;
GLfloat mat_specular[] = {0.8, 0.8, 0.0, 1.0};
GLfloat mat_shininess[] = {50.0};
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
GLfloat white_light[] = {1.0, 1.0, 1.0, 1.0};
GLfloat yellow_light[] = {1.0, 1.0, 0.0, 1.0};
GLfloat lmodel_ambient[] = {0.0, 0.7, 0.5, 1.0};
glClearColor(1.0, 1.0, 1.0, 0.0);
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, yellow_light);//主體的顏色
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);//高光的顏色
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
glColorMask(1.0, 1.0,1.0,1.0);
glClearColor(0.0,0.0,0.0,1.0);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
// 畫左眼
glMatrixMode(GL_PROJECTION);
glPushMatrix();
float mat[16];
glGetFloatv(GL_PROJECTION_MATRIX,mat);
glLoadIdentity();
glMultMatrixf(leftPersMatrix);
glMultMatrixf(mat);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glGetFloatv(GL_MODELVIEW_MATRIX,mat);
glLoadIdentity();
glMultMatrixf(leftMatrix);
glMultMatrixf(mat);
glColorMask(1.0, 0.0,0.0,1.0);
glRotatef((GLfloat) big, 0.0, 1.0, 0.0);
glutSolidTeapot(2.0);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glFlush();
//畫右眼
glClearDepth(1.0);
glClear(GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glGetFloatv(GL_PROJECTION_MATRIX,mat);
glLoadIdentity();
glMultMatrixf(rightPersMatrix);
glMultMatrixf(mat);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glGetFloatv(GL_MODELVIEW_MATRIX,mat);
glLoadIdentity();
glMultMatrixf(rightMatrix);
glMultMatrixf(mat);
glColorMask(0.0, 1.0,1.0,1.0);
glRotatef((GLfloat) big, 0.0, 1.0, 0.0);
glutSolidTeapot(2.0);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glFlush();
//glPopMatrix();
//if(isLeftEye)
//{
// glMatrixMode(GL_PROJECTION);
// glMultMatrixf(leftPersMatrix);
// glMatrixMode(GL_MODELVIEW);
// glMultMatrixf(leftMatrix);
// glColorMask(1.0, 0.0,0.0,1.0);
//
//
//
// isLeftEye = false;
//}else
//{
//
// glMatrixMode(GL_PROJECTION);
// glMultMatrixf(rightPersMatrix);
// glMatrixMode(GL_MODELVIEW);
// glMultMatrixf(rightMatrix);
// glColorMask(0.0, 1.0,1.0,1.0);
// isLeftEye = true;
//}
//glRotatef((GLfloat) big, 0.0, 1.0, 0.0);
//glutSolidTeapot(1.0);
//glRotatef((GLfloat) big, 0.0, 1.0, 0.0);
//glTranslatef(3.0, 0.0, 0.0);
//glutSolidTeapot(0.5);
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)w/(GLfloat)h, 0.01, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, R, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'b':
big = (big + 1) % 360;
glutPostRedisplay();
break;
case 'B':
big = (big - 1) % 360;
glutPostRedisplay();
break;
case 27: // 按ESC鍵時(shí)退出程序
exit (0);
break;
default:
break;
}
}
void spinDisplay(void)
{
big = (big + 1) % 360;
glutPostRedisplay();
}
int main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(spinDisplay);
glutMainLoop();
return 0;
}
最終效果圖如下所示:

相關(guān)文章
C++常用函數(shù)之XML JSON格式轉(zhuǎn)換問題
XML在Json出現(xiàn)前應(yīng)用很廣泛,靈活性好,應(yīng)用語言也沒有限制,發(fā)展了這么長時(shí)間后xml標(biāo)準(zhǔn)已經(jīng)很臃腫。這篇文章主要介紹了C++常用函數(shù)之XML JSON格式轉(zhuǎn)換問題,需要的朋友可以參考下2020-02-02C++實(shí)現(xiàn)LeetCode(117.每個(gè)節(jié)點(diǎn)的右向指針之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(117.每個(gè)節(jié)點(diǎn)的右向指針之二),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07淺析C/C++中的可變參數(shù)與默認(rèn)參數(shù)
C支持可變參數(shù)的函數(shù),這里的意思是C支持函數(shù)帶有可變數(shù)量的參數(shù),最常見的例子就是我們十分熟悉的printf()系列函數(shù)。我們還知道在函數(shù)調(diào)用時(shí)參數(shù)是自右向左壓棧的2013-09-09C++實(shí)現(xiàn)結(jié)束應(yīng)用進(jìn)程小工具
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)結(jié)束應(yīng)用進(jìn)程小工具,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05VS2019安裝配置MFC(安裝vs2019時(shí)沒有安裝mfc)
這篇文章主要介紹了VS2019安裝配置MFC(安裝vs2019時(shí)沒有安裝mfc),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03