OpenGL 入门教程 模型视图投影矩阵 变换光线
2017-12-07 14:31
429 查看
// SphereWorld.cpp // OpenGL SuperBible // New and improved (performance) sphere world // Program by Richard S. Wright Jr. #include <GLTools.h> #include <GLShaderManager.h> #include <GLFrustum.h> #include <GLBatch.h> #include <GLMatrixStack.h> #include <GLGeometryTransform.h> #include <StopWatch.h> #include <math.h> #include <stdio.h> #ifdef __APPLE__ #include <glut/glut.h> #else #define FREEGLUT_STATIC #include <GL/glut.h> #endif GLShaderManager shaderManager; // Shader Manager GLMatrixStack modelViewMatrix; // Modelview Matrix GLMatrixStack projectionMatrix; // Projection Matrix GLFrustum viewFrustum; // View Frustum 用来构造投影矩阵 GLGeometryTransform transformPipeline; // Geometry Transform Pipeline GLTriangleBatch torusBatch; GLBatch floorBatch; ////////////////////////////////////////////////////////////////// // This function does any needed initialization on the rendering // context. void SetupRC() { // Initialze Shader Manager shaderManager.InitializeStockShaders(); glEnable(GL_DEPTH_TEST); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // This makes a torus gltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30); floorBatch.Begin(GL_LINES, 324); for (GLfloat x = -20.0; x <= 20.0f; x += 0.5) { floorBatch.Vertex3f(x, -0.55f, 20.0f);//画模型视图投影矩阵的|线 floorBatch.Vertex3f(x, -0.55f, -20.0f); floorBatch.Vertex3f(20.0f, -0.55f, x);//画模型视图投影矩阵的-线 floorBatch.Vertex3f(-20.0f, -0.55f, x); } floorBatch.End(); } /////////////////////////////////////////////////// // Screen changes size or is initialized void ChangeSize(int nWidth, int nHeight) { glViewport(0, 0, nWidth, nHeight); //创建模型视图投影矩阵 // Create the projection matrix, and load it on the projection matrix stack viewFrustum.SetPerspective(35.0f, float(nWidth) / float(nHeight), 1.0f, 100.0f); projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix()); //变换管线 // Set the transformation pipeline to use the two matrix stacks transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix); } // Called to draw scene void RenderScene(void) { // Color values static GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f }; static GLfloat vTorusColor[] = { 1.0f, 0.0f, 0.0f, 1.0f }; // Time Based animation static CStopWatch rotTimer; float yRot = rotTimer.GetElapsedSeconds() * 60.0f; // Clear the color and depth buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Save the current modelview matrix (the identity matrix) modelViewMatrix.PushMatrix(); // Draw the ground shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vFloorColor); floorBatch.Draw(); // Draw the spinning Torus modelViewMatrix.Translate(0.0f, 0.0f, -2.5f); modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vTorusColor); torusBatch.Draw(); // Restore the previous modleview matrix (the idenity matrix) modelViewMatrix.PopMatrix(); // Do the buffer Swap glutSwapBuffers(); // Tell GLUT to do it again glutPostRedisplay(); } int main(int argc, char* argv[]) { gltSetWorkingDirectory(argv[0]); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH 4000 ); glutInitWindowSize(800, 600); glutCreateWindow("OpenGL SphereWorld"); 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之 获取模型视图矩阵和投影矩阵
- OpenGL 入门基础教程 —— 模型的变换
- OpenGL.ES在Android上的简单实践:8-曲棍球(构建冰球木槌 下 & 模型视图投影矩阵)
- OpenGL(五) 三维变换之模型视图矩阵
- 【LWJGL2 WIKI】【现代OpenGL篇】用投影、视图、模型矩阵画方形
- 关于OpenGL中模型视图投影视口变换的理解
- OpenGL: 模型视图矩阵变换
- openGL中根据读取的数据,确定投影矩阵和模型视图矩阵
- OpenGL 入门基础教程 —— 矩阵的变换
- OpenGL 模型视图投影矩阵 仿射矩阵
- OPENGL 变换视图模型变换与部分透视变换
- 【循序渐进学图形学之】OpenGL视图和模型变换中各变换发生顺序的思考
- OpenGL模型视图变换
- OpenGL中查询模型视图(Model View)矩阵中的数据
- OpenGL 模型视图变换、投影变换、视口变换
- [OpenGL ES 03]3D变换:模型,视图,投影与Viewport