OpenGL:不能不渲染三角形
Posted
技术标签:
【中文标题】OpenGL:不能不渲染三角形【英文标题】:OpenGL: Cannot not render a triangle 【发布时间】:2018-04-01 20:23:10 【问题描述】:编写代码并运行后,没有任何效果。我设法填充了背景,但没有别的。
这是我的代码:
#include <iostream>
#include <glad\glad.h>
#include <GLFW\glfw3.h>
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
const char *vertexShaderSource = "#version 450 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"\0";
const char *fragmentShaderSource = "#version 450 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"\n"
"FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"\0";
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
int main()
// Initializes GLFW and passes window hints
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Creates GLFWwindow called window
GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "LearningOpenGL", NULL, NULL);
if (window == NULL)
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
glfwMakeContextCurrent(window);
// Check if GLAD is initialized
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
float vertices[] =
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
;
int success;
char infoLog[512];
// Creating Vertex Shader Object
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
// Creating Fragment Shader Object
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
// Shader program
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
// Attatching both shaders to the program and then linking them
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
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);
// Interpreting vertex data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// Generating a Vertex Array Object
unsigned int VAO;
unsigned int VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Size of rendering window
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// While window is open
while (!glfwWindowShouldClose(window))
// Finally
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO);
glUseProgram(shaderProgram);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
// If loop is exited, terminate all allocated resources and close the program
glfwTerminate();
return 0;
// Frame buffer function that resizes the viewport when the window gets resized
void framebuffer_size_callback(GLFWwindow * window, int width, int height)
glViewport(0, 0, width, height);
【问题讨论】:
你高兴的生成字符串/URL 是什么?--profile="core" --api="gl=4.5" --generator="c" --spec="gl" --no-loader --extensions=""
/glad.dav1d.de/…?
【参考方案1】:
在绑定 VAO 之前,不要尝试启用或设置顶点属性指针。在 Core 上下文中不会做任何有用的事情。
绑定一个VAO然后启用/设置属性:
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Generating a Vertex Array Object
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray( VAO );
// Interpreting vertex data
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
【讨论】:
【参考方案2】:我注意到您的代码存在一些错误或问题:第一个明显的错误是在您调用以下代码后:
glfwMakeContextCurrent(window);
你应该打电话给:
glfwSetFramebufferSizeCallback( window, framebuffer_size_callback );
这应该在使窗口成为当前上下文之后并且在所有 OpenGL
内容之前完成。
我看到的第二个问题是在您的 VAO
和 VBO
的设置中,在从您的着色器中读取信息后,您的某些东西的顺序不正确,您甚至错过了一两个电话从您的代码的这一部分:
// Interpreting vertex data glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); // Generating a Vertex Array Object unsigned int VAO; unsigned int VBO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // Size of rendering window glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
首先,您应该在调用glVertexAttribPointer()
和glEnableVertexAtrribArray()
之前创建和初始化VBO
和VAO
。从 API - 库的角度来考虑这一点。如何启用指向内存中尚不存在的东西的指针?
其次,您错过了对glBindVertexArray(VAO);
的调用,这应该是在您生成VAO
和VBO
之后。
第三,最后两行代码我已经解释了函数glfwSetFramebufferSizeCallback()
和它应该放在哪里;但对于这一部分,我将解释原因。这是设置视口的函数,因为它是回调类型的函数。将窗口设置为当前上下文后;这是你想告诉 OpenGL 你的视口或窗口的大小,以便它知道Screen Size
在将所有顶点数据转换为屏幕空间坐标时使用。通过修复此问题,您将不必单独调用glViewport()
;因为这就是回调函数为您所做的。
这部分代码应该如下所示:
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(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(float), (void*)0);
glEnableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0);
我看到的最后一件事是在你的 while 循环中进行渲染,并且在你的循环之后:
// While window is open while (!glfwWindowShouldClose(window)) // Finally glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glBindVertexArray(VAO); glUseProgram(shaderProgram); glDrawArrays(GL_TRIANGLES, 0, 3); glfwSwapBuffers(window); glfwPollEvents(); // If loop is exited, terminate all allocated resources and close the program glfwTerminate(); return 0;
我在这里看到两个问题:
首先,使用程序和绑定顶点数组的顺序是倒序的,应该是:
glUseProgram(shaderProgram); glBindVertexArray(VAO);
在你的while循环之后和
glfwTerimate()
调用之前,你应该删除你的顶点数组和缓冲区glDeleteVertexArrays( 1, &VAO ); glDeleteBuffers( 1, &VBO );
如果这些简单的更正不能解决您的问题,您可以尝试另一件事。您可以尝试将OpenGL
的版本从4.5
更改为3.3
,因为他们的GLSL Shader
语言的版本略有不同。
如果这不起作用,那么它可能与您设置正在使用的外部库的方式有关。
总结
在尝试设置任何顶点数据之前,您首先必须设置视口。接下来,您必须先设置您的VAO
和VBO
信息,然后才能调用glVertexAtrribPointer()
和glEnableVertexAttribArray()
,并且不要忘记在调用后调用glBindVertexArray(VAO)
以生成它们。在所有初始化都按正确顺序之后,在渲染循环中使用它时,您应该在glBindVertexArray
之前调用glUseProgram
。最后,在您的渲染循环之后,您应该进行适当的调用以删除您的顶点数组和缓冲区。如果这些都失败了,那么您可以尝试更改OpenGl - GLSL
的版本并检查所有正在使用的外部库的设置。
【讨论】:
这是一篇很棒的文章,你提出了很多好的观点。很抱歉您没有得到复选标记。以上是关于OpenGL:不能不渲染三角形的主要内容,如果未能解决你的问题,请参考以下文章