OpenGL 不渲染正方形

Posted

技术标签:

【中文标题】OpenGL 不渲染正方形【英文标题】:OpenGL doesn´t render a square 【发布时间】:2020-07-24 16:45:05 【问题描述】:

我目前正在运行 opengl 4.5。

我从互联网上复制粘贴了一个代码来绘制一个三角形,它工作得很好。我玩过着色器,一切都很棒。

然后我有了一个好主意,为什么不画一个正方形,所以我尝试了一下,它只是渲染了背景。

代码如下:

   glfwGetFramebufferSize(window, &width, &height);
   glViewport(0, 0, width, height);


   // Build and compile our shader program
   // Vertex shader
   vertexShaderSource = read_file("res/shaders/vxShader.vert").c_str();
   fragmentShaderSource = read_file("res/shaders/fgShader.frag").c_str();


   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
   GLuint 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);


   // Set up vertex data (and buffer(s)) and attribute pointers
   GLfloat vertices[] = 
        0.5f,  0.5f, 0.0f, // Left  
        0.0f,  0.5f, 0.0f, // Right 
        0.0f,  0.0f, 0.0f,  // Top
        0.5f,  0.0f, 0.0f  // Top  
   ;
   GLuint VBO, VAO;
   glGenVertexArrays(1, &VAO);
   glGenBuffers(1, &VBO);
   // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
   glBindVertexArray(VAO);

   glBindBuffer(GL_ARRAY_BUFFER, VBO);
   glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
   glEnableVertexAttribArray(0);

   glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind

   glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)

   // Game loop
   while (!glfwWindowShouldClose(window))
   
       // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
       glfwPollEvents();

       // Render
       // Clear the colorbuffer
       glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
       glClear(GL_COLOR_BUFFER_BIT);

       // Draw our first triangle
       glUseProgram(shaderProgram);
       glBindVertexArray(VAO);
       glDrawArrays(GL_POLYGON, 0, 4);
       glBindVertexArray(0);

       // Swap the screen buffers
       glfwSwapBuffers(window);
   
   // Properly de-allocate all resources once they've outlived their purpose
   glDeleteVertexArrays(1, &VAO);
   glDeleteBuffers(1, &VBO);
   // Terminate GLFW, clearing any resources allocated by GLFW.
   glfwTerminate();
   return 0; 

以防万一,我只向缓冲区添加了一个顶点,并将glDrawArrays 参数从GL_TRIANGLE 更改为GL_POLYGON。我想我忘了做点什么。

【问题讨论】:

【参考方案1】:

原始类型 GL_POLYGON 已弃用,仅受兼容性配置文件 OpenGL context 支持。如果您使用核心配置文件 OpenGL 上下文,则必须改用 OpenGL 原始类型 GL_TRIANGLE_FAN

glDrawArrays(GL_POLYGON, 0, 4);

glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

注意,OpenGL 只能渲染点、线和三角形。如果你想渲染一个多边形,那么你必须用三角形来组装它。一个四边形可以由 2 个三角形组成。见Primitive。

【讨论】:

以上是关于OpenGL 不渲染正方形的主要内容,如果未能解决你的问题,请参考以下文章

使用 OpenGL 时,我尝试渲染的正方形没有显示 [重复]

如何在 OpenGL 中将文本渲染到正方形(4 个顶点)?

OpenGL仅渲染黑色正方形

OpenGL:GL_QUADS 不绘制正方形

我的 opengl 代码应该显示多个立方体,但只显示一个正方形

Modern OpenGL - 渲染矩体/矩形体/立方体/正方体/长方体