本文共 2838 字,大约阅读时间需要 9 分钟。
// FlatShader.cpp// OpenGL SuperBible// Demonstrates a simple flat shader with transformations// Program by Richard S. Wright Jr.#pragma comment(lib, "gltools.lib")#include// OpenGL toolkit#include #include #include #include #include #include #ifdef __APPLE__#include #else#define FREEGLUT_STATIC#include #endifGLFrame viewFrame;GLFrustum viewFrustum;GLTriangleBatch torusBatch;GLMatrixStack modelViewMatrix;GLMatrixStack projectionMatrix;GLGeometryTransform transformPipeline;GLuint flatShader; // The Flat shaderGLint locMVP; // The location of the ModelViewProjection matrix uniformGLint locColor; // The location of the color value uniform// This function does any needed initialization on the rendering// context. void SetupRC(void){ // Background glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glEnable(GL_DEPTH_TEST); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); viewFrame.MoveForward(4.0f); // Make the torus gltMakeTorus(torusBatch, .80f, 0.25f, 52, 26); flatShader = gltLoadShaderPairWithAttributes("FlatShader.vp", "FlatShader.fp", 1, GLT_ATTRIBUTE_VERTEX, "vVertex"); locMVP = glGetUniformLocation(flatShader, "mvpMatrix"); locColor = glGetUniformLocation(flatShader, "vColorValue");}// Cleanupvoid ShutdownRC(void){}// Called to draw scenevoid RenderScene(void){ static CStopWatch rotTimer; // Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); modelViewMatrix.PushMatrix(viewFrame); modelViewMatrix.Rotate(rotTimer.GetElapsedSeconds() * 10.0f, 0.0f, 1.0f, 0.0f); GLfloat vColor[] = { 0.1f, 0.1f, 1.f, 1.0f }; glUseProgram(flatShader); glUniform4fv(locColor, 1, vColor); glUniformMatrix4fv(locMVP, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix()); torusBatch.Draw(); modelViewMatrix.PopMatrix(); glutSwapBuffers(); glutPostRedisplay();}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, 100.0f); projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix()); transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);}///// Main entry point for GLUT based programsint main(int argc, char* argv[]){ gltSetWorkingDirectory(argv[0]); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL); glutInitWindowSize(800, 600); glutCreateWindow("Simple Transformed Geometry"); glutReshapeFunc(ChangeSize); glutDisplayFunc(RenderScene); GLenum err = glewInit(); if (GLEW_OK != err) { fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); return 1; } SetupRC(); glutMainLoop(); ShutdownRC(); return 0;}
OpenGL案例代码发现的问题:
1、ShutdownRC函数没有glDeleteProgram(flatShader);
2、RenderScene函数中一直在创建vColor一维数组转载地址:http://czycz.baihongyu.com/