未知的OpenGL错误
Unknown OpenGL Error
我正在尝试学习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范围内。
相关文章:
- C++atioglxx.pdb未加载错误glBufferData OpenGL
- OpenGL 4.3 错误地将第 4 个纹理坐标映射到与第 3 个纹理坐标相同的位置
- 现代OpenGL和GLEW Libraray的编译错误
- GLFW DDS 加载函数的 OpenGL 链路错误
- MSVC __debugbreak() 与 openGL 错误回调一起使用时不会产生调用堆栈
- C++ OpenGL 中,绘制形状返回错误
- OpenGL C++(制作简单窗口时抛出错误)
- CMake on FindOpenGL.cmake 中的错误,当使用导入的目标 OpenGL::GLU?
- C++/OpenGL 随机分段错误
- 当我在网格/面中实现索引时,为什么它会返回OpenGl错误
- 在 CLion (MacOS) 中使用 OpenGL 和 GLUT 库时出现链接错误
- OpenGL, GLFW, GLAD. glViewport(0, 0, 800, 600) 抛出内存错误
- OpenGL "Segmentation fault"错误
- 在运行时处理 OpenGL 错误
- OpenGL 顶点缓冲区类重定义和模板方法错误
- 假设传递给 OpenGL 的结构数组的内存布局存在潜在错误
- 如何找到 NVIDIA OpenGL 驱动程序的错误 ID 名称?
- C++生成文件错误 (openGl)
- cocos2dx错误:opengl版本太旧
- SDL错误.OpenGL激活,使用SDL_GL_SwapBuffers()