未知的OpenGL错误

Unknown OpenGL Error

本文关键字:错误 OpenGL 未知      更新时间:2023-10-16

我正在尝试学习OpenGL中使用的不同绘图命令。运行这段代码后,我得到的只是一个黑屏。我没有得到任何错误编译着色器代码,我没有得到任何错误时,调用glGetError()

#include <iostream>
#include <GLglew.h>
#include <GLFWglfw3.h>
#include <glmglm.hpp>
#include <glmgtcmatrix_transform.hpp>
#include <glmgtxtransform.hpp>
#include <commonshader.h>
using namespace std;
GLFWwindow* window;
float aspect = 4.0f / 3.0f;
enum VAO_IDs { Triangles, NumVAOs };
enum Buffer_IDs { ArrayBuffer, NumBuffers };
enum Attrib_IDs { vPosition, vColor };
enum EBO_IDs { ElementBuffer, NumElementBuffers };
GLuint program;
GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];
GLuint EBOs[NumElementBuffers];
GLint render_projection_matrix_loc;
GLint render_model_matrix_loc;
void init(void)
{
program = LoadShaders("triangles.vert", "triangles.frag");
glUseProgram(program);
GLfloat vertex_positions[] = 
{
    -1.0f, -1.0f, 0.0f, 1.0f,
    1.0f, -1.0f, 0.0f, 1.0f,
    -1.0f, 1.0f, 0.0f, 1.0f,
    -1.0f, -1.0f, 0.0f, 1.0f,
};
GLfloat vertex_colors[] = 
{
    1.0f, 1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 0.0f, 1.0f,
    1.0f, 0.0f, 1.0f, 1.0f,
    0.0f, 1.0f, 1.0f, 1.0f
};
GLushort vertex_indices[] = 
{
    0, 1, 2
};
glGenBuffers(NumElementBuffers, EBOs);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOs[ElementBuffer]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertex_indices), vertex_indices, GL_STATIC_DRAW);
glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);
glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_positions) + sizeof(vertex_colors), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertex_positions), vertex_positions);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertex_positions), sizeof(vertex_colors), vertex_colors);
glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)sizeof(vertex_positions));
glEnableVertexAttribArray(vPosition);
glEnableVertexAttribArray(vColor);
}
void display(void)
{
glm::mat4 model_matrix;
glEnable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
glm::mat4 projection_matrix = glm::frustum(-1.0f, 1.0f, -aspect, aspect, 1.0f, 500.0f);
glUniformMatrix4fv(render_projection_matrix_loc, 1, GL_FALSE, &projection_matrix[0][0]);
glBindVertexArray(VAOs[Triangles]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOs[ElementBuffer]);
model_matrix = glm::translate(glm::vec3(-3.0f, 0.0f, -5.0f));
glUniformMatrix4fv(render_model_matrix_loc, 4, GL_FALSE, &model_matrix[0][0]);
glDrawArrays(GL_TRIANGLES, 0, 3);
model_matrix = glm::translate(glm::vec3(1.0f, 0.0f, -5.0f));
glUniformMatrix4fv(render_model_matrix_loc, 4, GL_FALSE, &model_matrix[0][0]);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, NULL);
}
int main(int argc, char** argv)
 {
if (!glfwInit())
{
    cerr << "Failed to initialize GLFWn";
    exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(1024, 768, "C++ Graphics", NULL, NULL);
if (window == NULL){
    cerr << "Failed to open GLFW windown";
    glfwTerminate();
    exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
    cerr << "Failed to initialize GLEWn";
    exit(EXIT_FAILURE);
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
init();
do{
    display();
    glGetError();
    glGetError();
    glfwSwapBuffers(window);
    glfwPollEvents();
}
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
glfwTerminate();
return 0;
}
下面是我的GLSL代码:

triangles.vert

#version 430 core
layout(location = 0) in vec4 vPosition;
layout(location = 1) in vec4 vColor;
out vec4 fragmentColor;
uniform mat4 modelMatrix;
void main()
{
    gl_Position = modelMatrix * vPosition;
    fragmentColor = vColor;
}

triangles.frag

#version 430 core
in vec4 fragmentColor;
out vec4 color;
void main()
{
    color = fragmentColor;
}

您的绘图代码和着色器之间存在不匹配。在绘图代码中,设置一个投影矩阵:

glUniformMatrix4fv(render_projection_matrix_loc, 1, GL_FALSE, &projection_matrix[0][0]);

但是你的顶点着色器不应用投影矩阵。它甚至没有这个统一的定义。

如果您仅应用modelMatrix与设置代码中的值,而不使用投影,则整个几何图形将被转换到剪辑边界之外。

model_matrix = glm::translate(glm::vec3(-3.0f, 0.0f, -5.0f));
glUniformMatrix4fv(render_model_matrix_loc, 4, GL_FALSE, &model_matrix[0][0]);

这在z方向上平移了-5.0f。因为你的原始三角形坐标在z = 0.0f,它们最终在-5.0f。裁剪发生在顶点着色器写入gl_Position的坐标的-1.0f到1.0f的z范围内。