OpenGL学习笔记4_2(透视相关)
来源:互联网 发布:平衡流量计计算软件 编辑:程序博客网 时间:2024/04/29 22:32
蓝宝书 第四章
正交投影
在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);elseglOrtho(-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 zeroif (h == 0)h = 1;// Set viewport to window dimensionsglViewport(0, 0, w, h);fAspect = (GLfloat)w / (GLfloat)h;// Reset coordinate systemglMatrixMode(GL_PROJECTION);glLoadIdentity();// Produce the perspective projectiongluPerspective(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);//sunglColor3ub(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 matrixfEarthRot += 5.0f;if (fEarthRot > 360.0f)fEarthRot = 0.0f;// Show the imageglutSwapBuffers();}void SetupRC(){glEnable(GL_DEPTH_TEST);glFrontFace(GL_CCW);glEnable(GL_CULL_FACE);glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glEnable(GL_COLOR_MATERIAL);// Black backgroundglClearColor(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 zeroif (h == 0)h = 1;// Set viewport to window dimensionsglViewport(0, 0, w, h);fAspect = (GLfloat)w / (GLfloat)h;// Reset coordinate systemglMatrixMode(GL_PROJECTION);glLoadIdentity();// Produce the perspective projectiongluPerspective(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;}
0 0
- OpenGL学习笔记4_2(透视相关)
- OpenGL 学习笔记3_2(绘制线相关)
- OpenGL学习笔记4_1(矩阵相关)
- OpenGL学习笔记---黑屏解决之透视投影
- Opengl学习笔记1 -- 做一个透视立方体
- Java学习笔记(多线程_2)
- Java学习笔记(集合_2)
- Swift学习笔记_2
- autolayout学习笔记_2
- EasyUI学习笔记_2
- OPENGL 变换_视图模型变换与部分透视变换_学习笔记
- OpenGL学习十:视口,正投影,透视投影
- opengl学习笔记(4)
- openGL学习笔记4
- ZigBee学习笔记_InitBoard()_2
- ZigBee学习笔记_HalDriverInit()_2
- 学习要点笔记---多线程_2
- 数据结构与算法学习笔记02_2(线性表)
- django DateTimeField和DateField和TimeField
- 使用Https进行网络访问
- leecode 解题总结:33. Search in Rotated Sorted Array
- android白色背景状态栏字体颜色
- Jquery 常见案例
- OpenGL学习笔记4_2(透视相关)
- 线性表的顺序存储结构
- HDU5619 Jam's store(最小费用最大流 MCMF)
- Node.js官方文档:关于Node.js
- html之marquee详解--->滚动条
- http://java.sun.com/jsp/jstl/core cannot be resolved
- MySQL之索引与视图
- Python os模块介绍
- 不使用第三方变量,交换两个变量的值 ,并且适度考虑范围溢出