OpenGL: 实现立体显示
https://blog.csdn.net/augusdi/article/details/19922295
立体显示原理:设没有立体显示的模型视图矩阵ModelView为Mv,投影矩阵为Mp,则、物体空间的任何一点为P,则变换到屏幕坐标P*=Mp×Mv×P;注意前面已经说过opengl里面坐标列优先,所以矩阵都是右乘。
左眼和右眼的变换都是由中间的变换矩阵变换而来,则立体显示中左眼的变换矩阵公式为:
P(L)*=Ms(L) × Mp(L) × Mt(L) × Mv(L) × P;
右眼的矩阵变换公式为:
P(R)*=Ms(R) × Mp(R) × Mt(R) × Mv(R) × P;
其中Ms,Mt是立体显示需要而增加的变换。
程序里面有几个参数,现实世界眼睛到屏幕的距离Fd,两眼之间的距离Sd,比例尺R,如图:

如上图:没有立体显示,视点位于就是中间的蓝色位置,立体显示就是将左眼(红色),右眼(绿色)的视图分开绘制。
程序中左眼用红色去画,右眼同时用绿色和蓝色绘制。
代码:
- #include <windows.h>
- #include <GL/glut.h>
- #include <math.h>
- #pragma comment(lib,"glut32.lib")
- #pragma comment(lib,"glu32.lib")
- #pragma comment(lib,"opengl32.lib")
- void init(void)
- {
- GLfloat mat_diffuse[] = { 1.0, 1.0, 0.0 };
- GLfloat mat_specular[] = {0.8, 0.8, 0.0, 1.0};
- GLfloat mat_shininess[] = { 300. };
- GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
- GLfloat light_diffuse[] = { 1.0, 1.0, 0.0 };
- GLfloat light_ambient[] = {0.7, 0.2, 0.2, 1.0};
- glClearColor (0.0, 0.0, 0.0, 0.0);
- glShadeModel (GL_SMOOTH);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_DEPTH_TEST);
- }
- /**//*----------------------------------------------------------------------------
- * 初始化参数
- */
- GLfloat PI=3.1415926;
- GLfloat Fd=5.0; //fusion distance
- GLfloat RealScreenToEyeDistance=1.0;
- GLfloat R = Fd / RealScreenToEyeDistance; //比例尺 R = Fd / RealScreenToEyeDistance
- GLfloat Sd = 0.05; //两眼之间的距离
- GLfloat aspect = 1.0; //gluLookAt函数里面的参数
- GLfloat fovy = 60.0; //张角
- GLfloat f = 1 / tan( (fovy * PI) / (2 * 180) ); //f=ctg(fovy/2);
- //列优先的矩阵模型视图矩阵,投影矩阵
- GLfloat LeftModelViewMatrix[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,
- Sd * R / 2.0, 0.0, 0.0, 1.0
- };
- GLfloat LeftProjectMatrix[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,
- -(Sd * f) / (2.0 * Fd * aspect), 0.0, 0.0, 1.0
- };
- GLfloat RightModelViewMatrix[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,
- -Sd * R / 2.0, 0.0, 0.0, 1.0
- };
- GLfloat RightProjectMatrix[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,
- (Sd * f) / (2.0 * Fd * aspect), 0.0, 0.0, 1.0
- };
- //for the use of rotating
- static GLfloat spin = 0.0;
- void display(void)
- {
- GLfloat matrix[16]={0.};
- 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);
- //---------------------------------------------------------------------------------------------
- //Left View port
- glMatrixMode(GL_PROJECTION);
- glPushMatrix();
- {
- glGetFloatv(GL_PROJECTION_MATRIX, matrix);
- glLoadIdentity();
- glMultMatrixf(LeftProjectMatrix);
- glMultMatrixf(matrix);
- {
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslated(0.0, 0.0, -Fd);
- glPushMatrix();
- {
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- glLoadIdentity();
- glMultMatrixf(LeftModelViewMatrix);
- glMultMatrixf(matrix);
- glColorMask(1.0, 0.0, 0.0, 1.0);
- /**//*
- * 物体的坐标Vp
- * 变换到屏幕坐标:Vp'= LeftProjectMatrix×Mp × LeftModelViewMatrix×Mv × Mr×Vp
- */
- glPushMatrix();
- {
- glRotatef(spin, 0.0, 1.0, 0.0);
- glutSolidTeapot(1.0);
- }
- glPopMatrix();
- }
- }
- glPopMatrix();
- glMatrixMode(GL_PROJECTION);
- }
- glPopMatrix();
- glFlush();
- //---------------------------------------------------------------------------------------------
- //Right View port
- glMatrixMode(GL_PROJECTION);
- glPushMatrix();
- {
- glGetFloatv(GL_PROJECTION_MATRIX, matrix);
- glLoadIdentity();
- glMultMatrixf(RightProjectMatrix);
- glMultMatrixf(matrix);
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- {
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- glLoadIdentity();
- glMultMatrixf(RightModelViewMatrix);
- glMultMatrixf(matrix);
- glColorMask(0.0, 1.0, 1.0, 1.0);
- glClearDepth(1.0);
- glClear(GL_DEPTH_BUFFER_BIT);
- /**//*
- * 物体的坐标Vp
- * 变换到屏幕坐标:Vp'= RightProjectMatrix×Mp× RightModelViewMatrix×Mv × Mr×Vp
- */
- glPushMatrix();
- {
- glRotatef(spin, 0.0, 1.0, 0.0);
- glutSolidTeapot(1.0);
- //glutSolidSphere(1.0, 20, 5);
- }
- }
- glPopMatrix();
- glMatrixMode(GL_PROJECTION);
- }
- glPopMatrix();
- glFlush ();
- glutSwapBuffers();
- }
- void reshape (int w, int h)
- {
- if (h == 0)
- {
- h == 1;
- }
- glViewport (0, 0, (GLsizei) w, (GLsizei) h);
- glMatrixMode (GL_PROJECTION);
- glLoadIdentity();
- //投影矩阵:Mp
- gluPerspective(fovy, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
- }
- void spinDisplay(void)
- {
- spin = spin + 1.0;
- if (spin > 360.0)
- {
- spin = spin - 360.0;
- }
- 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);
- glutIdleFunc(spinDisplay);
- glutMainLoop();
- return 0;
- }
相关立体显示链接:http://local.wasp.uwa.edu.au/~pbourke/projection/stereorender/
http://blog.csdn.net/ryfdizuo/article/details/2327478
OpenGL: 实现立体显示的更多相关文章
- 3D立体显示大屏幕拼接视频墙系统解决方案【转】
http://shop.souvr.com/thread-123416-1-1.html 随着3D立体视像.全息影像等技术不断取得突破性进展,国内外越来越多的公司投身3D显示领域,产品层出不穷.3D技 ...
- CreatarGlobe实现多机立体显示方案(初稿)
CreatarGlobe实现多机立体显示方案(初稿) 关键字 : 集群渲染 立体显示 大屏幕 边缘融合 多机同步 多机同步显示 关键字: 大屏幕投影融合系统解决方案 集群渲染 多机3D同步显示又称“集 ...
- 基于3D Vision眼镜的OSG立体显示 【转】
http://blog.csdn.net/qq_20038925/article/details/50510565 OSG 立体显示 3D Vision眼镜:所实现的是被动立体. 1.本人最近在做os ...
- 【AR实验室】OpenGL ES绘制相机(OpenGL ES 1.0版本)
0x00 - 前言 之前做一些移动端的AR应用以及目前看到的一些AR应用,基本上都是这样一个套路:手机背景显示现实场景,然后在该背景上进行图形学绘制.至于图形学绘制时,相机外参的解算使用的是V-SLA ...
- [OpenGL超级宝典]专栏前言
我小时候的梦想呢,是做宇航员或者科学家或者是做一款属于自己的游戏,后来前面两个梦想都没有实现,于是我就来实现我的第三个梦想了,,,我呢,也算是零基础,因为我的专业是物联网工程,这个专业覆盖面之广,简直 ...
- OpenGL超级宝典笔记----渲染管线
在OpenGL中任何事物都在3D空间中,但是屏幕和窗口是一个2D像素阵列,所以OpenGL的大部分工作都是关于如何把3D坐标转变为适应你屏幕的2D像素.3D坐标转为2D坐标的处理过程是由OpenGL的 ...
- OpenGL超级宝典笔记----框架搭建
自从工作后,总是或多或少的会接触到客户端3d图形渲染,正好自己对于3d图形的渲染也很感兴趣,所以最近打算从学习OpenGL的图形API出发,进而了解3d图形的渲染技术.到网上查了一些资料,OpenGL ...
- 现代3D图形编程学习-基础简介(3)-什么是opengl (译)
本书系列 现代3D图形编程学习 OpenGL是什么 在我们编写openGL程序之前,我们首先需要知道什么是OpenGL. 将OpenGL作为一个API OpenGL 通常被认为是应用程序接口(API) ...
- OpenGL shader 中关于顶点坐标值的思考
今天工作中需要做一个事情: 在shader内部做一些空间距离上的计算,而且需要对所有的点进行计算,符合条件的显示,不符合条件的点不显示. 思路很简单,在vertex shader内知道顶点坐标,进行计 ...
随机推荐
- 将c语言的结构体定义变成对应的golang语言的结构体定义,并将golang语言结构体变量的指针传递给c语言,cast C struct to Go struct
https://groups.google.com/forum/#!topic/golang-nuts/JkvR4dQy9t4 https://golang.org/misc/cgo/gmp/gmp. ...
- git flow 使用步骤
Mac安装git-flow:brew install git-flow 克隆新代码:git clone git@gitlab.xxx.cn:abc/test.git 切换到远程的develop分支(很 ...
- CentOS 查看系统 CPU 个数、核心数、线程数
1.查看 CPU 物理个数 grep 'physical id' /proc/cpuinfo | sort -u | wc -l 2.查看 CPU 核心数量 grep 'core id' /proc/ ...
- 项目发布脚本-nodejs
#!/bin/bash export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin clear printf &q ...
- PHP处理大文件下载
<?php /** * Created by PhpStorm. * User: Kung * Date: 15-10-21 * Time: 下午8:00 */ set_time_limit(0 ...
- 配置opensips经验总结
主要参考https://www.cnblogs.com/Forever-Kenlen-Ja/p/7741776.html (ubuntu),还有https://blog.csdn.net/sunyun ...
- iOS 新浪微博-1.1框架升级
在iOS 新浪微博-1.0框架搭建 中,使用的是xcode5.1.1开发.现在把重整了一下框架 改为xcode7.0开发 使用cocoaPad管理第三方库 程序将托管到github上 在改为xcode ...
- idc函数大全
A80_addcA80_addcixA80_addciyA80_addixA80_addiyA80_cmpdA80_cmpiA80_im0A80_im1A80_im2A80_jrcA80_jrncA8 ...
- vue中动态绑定class
我用的element-ui primary默认是这个颜色,ui设计的是这个颜色所以我们先要重写 .el-button--primary的样式 因为默认是没有勾选的所有事灰色所以下面重写样式为灰色 .e ...
- cocos2d JS 本地缓存存储登陆记住账号密码->相当于C++中的UserDefault
在cocos-js 3.0以上的版本中,当我们用到本地存储的时候,发现以前用到的UserDefault在JS中并没有导出,而是换成了LocalStorage. 在LocalStorage.h文件中我们 ...