opengl程序无法呈现三角形

时间:2017-01-19 22:02:35

标签: c++ opengl

我基于两个教程修改了三角形程序: https://learnopengl.com/code_viewer.php?code=getting-started/hellotrianglehttp://ogldev.atspace.co.uk/www/tutorial04/tutorial04.html 但是我只能渲染一个没有三角形的窗口。我的代码如下:

          #include <iostream>
          #include <string.h>
          #include <GL/glew.h>
          #include <GL/freeglut.h>
          GLuint VBO;
          GLuint shaderProgram;
          const GLchar* vertexShaderSource = "#version 330 core\n"
              "layout (location = 0) in vec3 position;\n"
              "void main()\n"
              "{\n"
              "gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
              "}\0";
          const GLchar* fragmentShaderSource = "#version 330 core\n"
              "out vec4 color;\n"
              "void main()\n"
              "{\n"
              "color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
              "}\n\0";

   void RenderSceneCB()
   {   GLuint VAO;
       glClear(GL_COLOR_BUFFER_BIT);
       glUseProgram(shaderProgram);

       glBindVertexArray(VAO);
       glBindBuffer(GL_ARRAY_BUFFER, VBO);
       glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
       glEnableVertexAttribArray(0);
       glDrawArrays(GL_TRIANGLES, 0, 3);

       glDisableVertexAttribArray(0);

       glutSwapBuffers();
   }


   void InitializeGlutCallbacks()
   {
       glutDisplayFunc(RenderSceneCB);
   }

   void CreateVertexBuffer()
   {
       GLfloat Vertices[] = {
           -0.5f, -0.5f, 0.0f, // Left  
            0.5f, -0.5f, 0.0f, // Right 
            0.0f,  0.5f, 0.0f  // Top   
       };
       glGenBuffers(1, &VBO);
       glBindBuffer(GL_ARRAY_BUFFER, VBO);
       glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
   }

   void shader() {
       // Build and compile our shader program
       // Vertex shader
       GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
       glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
       glCompileShader(vertexShader);
       // Check for compile time errors
       GLint success;
       GLchar infoLog[512];
       glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
       if (!success)
       {
           glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
           std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" <<               infoLog << std::endl;
       }
       // Fragment shader
       GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
       glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
       glCompileShader(fragmentShader);
       // Check for compile time errors
       glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
       if (!success)
       {
           glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
           std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
       }
       // Link shaders
       shaderProgram = glCreateProgram();
       glAttachShader(shaderProgram, vertexShader);
       glAttachShader(shaderProgram, fragmentShader);
       glLinkProgram(shaderProgram);
       // Check for linking errors
       glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
       if (!success) {
           glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
           std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
       }
       glDeleteShader(vertexShader);
       glDeleteShader(fragmentShader);
   }

   int main(int argc, char** argv)
   {   glutInitContextVersion (3, 3);
       glutInit(&argc, argv);
       glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
       glutInitWindowSize(1024, 768);
       glutInitWindowPosition(100, 100);
       glutCreateWindow("Triangle"); 
       InitializeGlutCallbacks();
       // Must be done after glut is initialized!
       glewExperimental = GL_TRUE;
       GLenum res = glewInit();
       if (res != GLEW_OK) {
         std::cout << "Error: '%s'\n" << glewGetErrorString(res) << std::endl;
         return 1;
       }
       glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

       CreateVertexBuffer();

       shader();

       glutMainLoop();

       return 0;
   }

有人可以帮忙吗?如何按预期渲染三角形?谢谢

0 个答案:

没有答案