OpenGL立体显示代码(测试环境:3D显示+Nv 3D显卡)




#include "stdafx.h"

#include <gl/glew.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#include <gl/glut.h>
#include <math.h>

int width, height;

void draw()
{
	glutWireSphere(1.5, 20, 30);
}

void display()
{
	double Near = 1, Far = 1000, fov = 45;
	double ratio = width / height;
	double radians, DTOR = 0.0174532925;
	double ViewWidth2, ViewHeight2;
	double left, right, top, bottom;
	double eyesep = 2;

	radians = DTOR * fov;                                                                            //角度和弧度換算
	ViewHeight2 = Near * tan(radians);                                                        //計算視野的高度  
	ViewWidth2 = ViewHeight2 * ratio;                                                       //計算視野的寬度

	glDrawBuffer(GL_BACK_RIGHT);                                                        //清除左右後緩衝區
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glDrawBuffer(GL_BACK_LEFT);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	//以下為控制右眼的部分
	///
	glMatrixMode(GL_PROJECTION);                                                        //先將投影矩陣還原為單位矩陣
	glLoadIdentity();


	left = -ViewWidth2 - 0.5 * eyesep * 0.3;                                                //計算右眼的視野範圍
	right = ViewWidth2 - 0.5 * eyesep * 0.3;
	top = ViewHeight2;
	bottom = -ViewHeight2;

	glFrustum(left, right, bottom, top, Near, Far);                                         //依照右眼的視野範圍,控制投影矩陣變成上圖的藍色投影平面


	gluLookAt(0 + eyesep / 2, 0, 5, 0 + eyesep / 2, 0, -5, 0, 1, 0);                //決定右眼的位置


	glDrawBuffer(GL_BACK_RIGHT);                                                        //告訴OpenGL使用右,後緩衝區
	draw();                                                                                                    //繪出場景
	///

	//以下為控制左眼的部分
	///
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();


	left = -ViewWidth2 + 0.5 * eyesep * 0.3;
	right = ViewWidth2 + 0.5 * eyesep * 0.3;
	top = ViewHeight2;
	bottom = -ViewHeight2;

	glFrustum(left, right, bottom, top, Near, Far);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glDrawBuffer(GL_BACK_LEFT);
	gluLookAt(0 - eyesep / 2, 0, 5, 0 - eyesep / 2, 0, -5, 0, 1, 0);
	draw();
	///
	glutSwapBuffers();                                                                                       //將左右緩衝區交換顯示
}

void reshape(int w, int h)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
	width = w;
	height = h;
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, w / h, 1.0, 1000.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

int main(int argc, char* argv[])
{
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STEREO);    //GLUT_STEREO參數指的是允許使用OpenGL的立體模式
	glutInitWindowSize(600, 600);
	glutInitWindowPosition(100, 100);
	glutCreateWindow(argv[0]);
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMainLoop();
	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值