首页 > 学院 > 开发设计 > 正文

OpenGL学习笔记4_2(透视相关)

2019-11-10 19:30:54
字体:
来源:转载
供稿:网友

蓝宝书 第四章

正交投影

在ChangeSize函数中使用glOrtho函数调整

glOrtho参考:点击打开链接

正交投影ChangeSize代码参考

void ChangeSize(GLsizei w, GLsizei h){	GLfloat nRange = 100.0f;	// PRevent a divide by zero  	if (h == 0)		h = 1;	// Set Viewport to window dimensions  	glViewport(0, 0, w, h);	// Reset projection matrix stack  	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	// Establish clipping volume (left, right, bottom, top, near, far)  	if (w <= h)		glOrtho(-nRange, nRange, -nRange*h / w, nRange*h / w, -nRange, nRange);	else		glOrtho(-nRange*w / h, nRange*w / h, -nRange, nRange, -nRange, nRange);	// Reset Model view matrix stack  	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();}

透视投影

在ChangeSize函数中使用gluPerspective函数调整

gluPerspective参考:点击打开链接

透视投影ChangeSize代码参考

void ChangeSize(GLsizei w, GLsizei h){	GLfloat faspect;	// Prevent a divide by zero	if (h == 0)		h = 1;	// Set viewport to window dimensions	glViewport(0, 0, w, h);	fAspect = (GLfloat)w / (GLfloat)h;	// Reset coordinate system	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	// Produce the perspective projection	gluPerspective(60.0f, fAspect, 1.0, 400.0);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();}glOrtho函数和gluPerspective的作用是在整个世界中勾出一个视线可以达到的范围

例4.3 太阳地球月亮模型

光源参考:点击打开链接

#include <windows.h>  #include <math.h>  #include <GL/GL.h>  #include <GL/GLU.h>  #include <GL/glut.h>   void RenderScene(void){	static float fMoonRot = 0.0f;	static float fEarthRot = 0.0f;		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glMatrixMode(GL_MODELVIEW);	glPushMatrix();	glTranslatef(0.0f, 0.0f, -300.0f);	//sun	glColor3ub(255, 255, 0);	glDisable(GL_LIGHTING);	glutSolidSphere(15.0f, 15, 15);	glEnable(GL_LIGHTING);	GLfloat lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f};	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);	glRotatef(fEarthRot, 0.0f, 1.0f, 0.0f);	glColor3ub(0, 0, 255);	glTranslatef(105.0f, 0.0f, 0.0f);	glutSolidSphere(15.0f, 15, 15);	glColor3ub(200, 200, 200);	glRotatef(fMoonRot, 0.0f, 1.0f, 0.0f);	glTranslatef(30.0f, 0.0f, 0.0f);	fMoonRot += 15.0f;	if (fMoonRot > 360.0f)		fMoonRot = 0.0f;	glutSolidSphere(6.0f, 15, 15);	glPopMatrix(); // Modelview matrix	fEarthRot += 5.0f;	if (fEarthRot > 360.0f)		fEarthRot = 0.0f;	// Show the image	glutSwapBuffers();}void SetupRC(){	glEnable(GL_DEPTH_TEST);	glFrontFace(GL_CCW);	glEnable(GL_CULL_FACE);	glEnable(GL_LIGHTING);	glEnable(GL_LIGHT0);	glEnable(GL_COLOR_MATERIAL);	// Black background	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);}void timerProc(int id){	RenderScene();	glutTimerFunc(100, timerProc, 1);}void ChangeSize(GLsizei w, GLsizei h){	GLfloat fAspect;	// Prevent a divide by zero	if (h == 0)		h = 1;	// Set viewport to window dimensions	glViewport(0, 0, w, h);	fAspect = (GLfloat)w / (GLfloat)h;	// Reset coordinate system	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	// Produce the perspective projection	gluPerspective(45.0f, fAspect, 1.0, 425.0);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();}int main(int argc, char* argv[]){	glutInit(&argc, argv);	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);	glutInitWindowSize(800, 600);	glutCreateWindow("Earth & Moon & Sun System");	glutDisplayFunc(RenderScene);//显示回调函数  	glutReshapeFunc(ChangeSize);//窗口大小变形回调函数  	glutTimerFunc(100, timerProc, 1);	SetupRC();	glutMainLoop();	return 0;}


上一篇:126. Word Ladder II

下一篇:注解 Annotation

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表