OpenGL 入门教程(五) -- 模型视图投影矩阵
2017-12-07 11:38
387 查看
// ModelviewProjection.cpp // OpenGL SuperBible // Demonstrates OpenGL the ModelviewProjection matrix // Program by Richard S. Wright Jr. #include <GLTools.h> // OpenGL toolkit #include <GLMatrixStack.h> #include <GLFrame.h> #include <GLFrustum.h> #include <GLGeometryTransform.h> #include <GLBatch.h> #include <StopWatch.h> #include <math.h> #ifdef __APPLE__ #include <glut/glut.h> #else #define FREEGLUT_STATIC #include <GL/glut.h> #endif // Global view frustum class 平截头体 GLFrustum viewFrustum; // The shader manager GLShaderManager shaderManager; // The torus GLTriangleBatch torusBatch; // Set up the viewport and the projection matrix void ChangeSize(int w, int h) { // Prevent a divide by zero if (h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); //设置透视投影 视角 方向 近 远 viewFrustum.SetPerspective(35.0f, float(w) / float(h), 1.0f, 10.0f); } // Called to draw scene void RenderScene(void) { // Set up time based animation static CStopWatch rotTimer; float yRot = rotTimer.GetElapsedSeconds() * 60.0f; // Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Matrix Variables M3DMatrix44f mTranslate, mRotate, mModelview, mModelViewProjection; // Create a translation matrix to move the torus back and into sight m3dTranslationMatrix44(mTranslate, 0.0f, 0.0f, -2.5f); // Create a rotation matrix based on the current value of yRot m3dRotationMatrix44(mRotate, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f); // Add the rotation to the translation, store the result in mModelView m3dMatrixMultiply44(mModelview, mTranslate, mRotate); //将坐标系缩减到正方体中(3D,模型视图是2D的,通过深度测试绘制出类似3D效果) //通过将投影矩阵mModelviewviewFrustu得到m.GetProjectionMatrix()矩阵模型视图投影矩阵mModelViewProjection // Add the modelview matrix to the projection matrix, // the final matrix is the ModelViewProjection matrix. //mModelview --copy to--> viewFrustum.GetProjectionMatrix() -- copy to -> mModelViewProjection //mModelview -> viewFrustum投影矩阵 -> mModelViewProjection最终矩阵<模型视图投影矩阵>(透视投影的最终矩阵) m3dMatrixMultiply44(mModelViewProjection, viewFrustum.GetProjectionMatrix(), mModelview); // Pass this completed matrix to the shader, and render the torus GLfloat vBlack[] = { 0.0f, 0.0f, 0.0f, 1.0f }; shaderManager.UseStockShader(GLT_SHADER_FLAT, mModelViewProjection, vBlack); torusBatch.Draw(); // Swap buffers, and immediately refresh glutSwapBuffers(); glutPostRedisplay(); } // This function does any needed initialization on the rendering // context. void SetupRC() { // Black background glClearColor(0.8f, 0.8f, 0.8f, 1.0f); glEnable(GL_DEPTH_TEST); shaderManager.InitializeStockShaders(); // This makes a torus gltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); } /////////////////////////////////////////////////////////////////////////////// // Main entry point for GLUT based programs int main(int argc, char* argv[]) { gltSetWorkingDirectory(argv[0]); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL); glutInitWindowSize(800, 600); glutCreateWindow("ModelViewProjection Example"); glutReshapeFunc(ChangeSize); glutDisplayFunc(RenderScene); GLenum err = glewInit(); if (GLEW_OK != err) { fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); return 1; } SetupRC(); glutMainLoop(); return 0; }
相关文章推荐
- OpenGL 入门教程 模型视图投影矩阵 变换光线
- opengl入门系列2- 模型视图矩阵变换
- OpenGL 模型视图投影矩阵 仿射矩阵
- openGL中根据读取的数据,确定投影矩阵和模型视图矩阵
- 小白学opengl之 获取模型视图矩阵和投影矩阵
- 【LWJGL2 WIKI】【现代OpenGL篇】用投影、视图、模型矩阵画方形
- OpenGL.ES在Android上的简单实践:8-曲棍球(构建冰球木槌 下 & 模型视图投影矩阵)
- 【转】模型视图矩阵和投影矩阵(webgl笔记)
- OpenGL: 模型视图矩阵变换
- 关于OpenGL模型视图矩阵的研究
- WEBGL学习【八】模型视图投影矩阵
- OpenGL矩阵推导——模型视图变化
- OpenGL中查询模型视图(Model View)矩阵中的数据
- OpenGL图形管线和坐标变换[转](模型视图矩阵推导)
- OpenGL 入门基础教程 —— 矩阵的变换
- OpenGL 入门基础教程 —— 模型的变换
- 一文带你认识 模型 视图 投影 矩阵
- 模型矩阵、视图矩阵、投影矩阵
- 一、OpenGL灯光位置的控制 -- 模型视图矩阵的本质
- OpenGL(五) 三维变换之模型视图矩阵