编写代码并运行后,没有任何效果。我设法填补了背景,但没有别的。
这是我的代码:
#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);
}
答案 0 :(得分:4)
在绑定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);
答案 1 :(得分:0)
我注意到您的代码出现了一些错误或问题:第一个明显的错误是您致电后:
glfwMakeContextCurrent(window);
你应该打电话:
glfwSetFramebufferSizeCallback( window, framebuffer_size_callback );
这应该在将窗口设置为当前上下文之后和所有OpenGL
之前完成。
我看到的第二个问题是在VAO
&amp;的设置范围内。 {1}}在阅读着色器的信息之后,您发现订单不正确,甚至错过了代码的这一部分中的一两个电话:
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);
&amp;在致电VBO
&amp;之前VAO
glVertexAttribPointer()
。从API - Library的角度考虑这一点。如何启用指向内存中尚不存在的内容的指针?
其次,您错过了对glEnableVertexAtrribArray()
的来电。这应该是在您生成glBindVertexArray(VAO);
&amp; VAO
。
第三,我已经解释过关于函数VBO
的最后两行代码以及它应该放在哪里;但对于这一部分,我将解释原因。这是设置视口的功能,因为它是一种回调类型的函数。将窗口设置为当前上下文后;这是您想要告诉OpenGL视口或窗口大小的位置,以便它知道glfwSetFramebufferSizeCallback()
在将所有顶点数据转换为屏幕空间坐标时使用。通过修复此问题,您无需独立调用Screen Size
;因为这就是回调功能对你的作用。
此部分代码应如下所示:
glViewport()
我看到的最后一件事就是在你的while循环中进行渲染以及循环之后:
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 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;
在while循环之后和
glUseProgram(shaderProgram); glBindVertexArray(VAO);
调用之前,你应该删除你的顶点数组和缓冲区glfwTerimate()
如果这些简单的更正无法解决您的问题,您还可以尝试另外一件事。您可以尝试将glDeleteVertexArrays( 1, &VAO );
glDeleteBuffers( 1, &VBO );
的版本从OpenGL
更改为4.5
,因为他们的3.3
语言版本略有不同。
如果这不起作用,则可能是您设置外部库的方式。
摘要
首先必须在尝试设置任何顶点数据之前设置视口。接下来,您必须设置GLSL Shader
&amp;在VAO
之前拨打VBO
之前的glVertexAtrribPointer()
信息glEnableVertexAttribArray()
并且在调用生成它们之后不要忘记调用glBindVertexArray(VAO)
。在所有初始化顺序正确后,在渲染循环中使用它时,您应该在glUseProgram
之前调用glBindVertexArray
。最后,在渲染循环之后,您应该进行适当的调用以删除顶点数组和缓冲区。如果这些失败,那么您可以尝试更改OpenGl - GLSL
的版本并检查正在使用的所有外部库的设置。