OPENGL 游戏编程 第四章 投影 glut
2014-10-25 20:53
260 查看
#include "stdafx.h" #include <Windows.h> #include <GL/glew.h> #include <GL/glut.h> void init() { glClearColor(0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); } void changeSize(int w, int h) { float ratio = 1.0 * w / h; glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0, 0, w, h); gluPerspective(45, ratio, 1, 1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0, 0, 5, 0, 0, -1, 0, 1, 0); } void DrawCube(float xpos = 0.0f, float ypos = 0.0f, float zpos = 0.0f) { glClearColor(0.0, 0.0, 0.0, 0.0); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glPushMatrix(); glBegin(GL_QUADS); glVertex3f(0.0f, 0.0f, 0.0f); // top face glVertex3f(0.0f, 0.0f, -1.0f); glVertex3f(-1.0f, 0.0f, -1.0f); glVertex3f(-1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); // right face glVertex3f(0.0f, -1.0f, 0.0f); glVertex3f(0.0f, -1.0f, -1.0f); glVertex3f(0.0f, 0.0f, -1.0f); glVertex3f(-1.0f, 0.0f, 0.0f); // left face glVertex3f(-1.0f, 0.0f, -1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glVertex3f(0.0f, -1.0f, 0.0f); // bottom face glVertex3f(0.0f, -1.0f, -1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glVertex3f(0.0f, 0.0f, -1.0f); // back face glVertex3f(-1.0f, 0.0f, -1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glVertex3f(0.0f, -1.0f, -1.0f); glEnd(); glutSwapBuffers(); } void render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // load the identity matrix (clear to default position and orientation) glLoadIdentity(); // position and size the cube glTranslatef(0, 0.0, -5); /*glRotatef(15.0, 1.0, 0.0, 0.0); glRotatef(30.0f, 0.0, 1.0, 0.0); glScalef(0.75, 0.75, 0.75);*/ DrawCube(0.0, 0.0, 0.0); } void UpdateProjection(bool toggle) { static bool usePerspective = toggle; // select the projection matrix and clear it out glMatrixMode(GL_PROJECTION); glLoadIdentity(); // choose the appropriate projection based on the currently toggled mode if (usePerspective) { // set the perspective with the appropriate aspect ratio glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 1000.0); } else { // set up an orthographic projection with the same near clip plane glOrtho(-1.0, 1.0, -1.0, 1.0, 1.0, 1000.0); } // select modelview matrix and clear it out glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void ResizeScene(int width, int height) { // avoid divide by zero if (height == 0) { height = 1; } // reset the viewport to the new dimensions glViewport(0, 0, width, height); // set up the projection, without toggling the projection mode UpdateProjection(false); } int _tmain(int argc, char* argv[]) { glutInit(&argc, argv); init(); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize(1000, 600); glutInitWindowPosition(0, 0); glutCreateWindow("hello"); glutDisplayFunc(render); glutIdleFunc(render); glutReshapeFunc(ResizeScene); glutMainLoop(); return 0; }
相关文章推荐
- Windows 游戏编程大师技巧第四章第6个例子---定时器的使用
- 【OpenGL】游戏编程常用TGA图像格式详解以及加载纹理编程实现
- Android游戏开发之OpenGL之视图-投影矩阵 杂谈
- Windows 游戏编程大师技巧第四章第一个例子
- OpenGL glut 编程环境设…
- OpenGL编程指南3:GLUT:OpenGL使用工具库
- openGL编程GLUT配置和“helloworld”
- 深入浅出游戏算法(1)-配置opengl、glut在codeblocks和vs2012(1)
- 问题解决——OpenGL超级宝典 第四章 4.5.2 关于freeglut.lib问题的解决过程
- Windows 游戏编程大师技巧第四章第9个例子
- opengl es 2.0游戏与图形编程——2.设置图形投影
- Windows 游戏编程大师技巧第四章第9个例子
- 问题解决——OpenGL超级宝典 第四章 4.5.2 关于freeglut.lib问题的解决过程
- 深入浅出游戏算法(1)-配置opengl、glut在codeblocks和vs2012(3)
- 【图形学与游戏编程】开发笔记-入门篇3:d3d,opengl以及GPU
- Qt OpenGL:学习现代3D图形编程之四,透视投影
- OpenGL Glut剖析(2)--平行投影及其显示范围
- OpenGL 编程指南笔记一之GLUT
- Windows 游戏编程大师技巧第四章第4个例子
- 【OpenGL编程指南】之投影和视口变换