我正在尝试学习OpenGL中使用的不同绘图命令。运行此代码后,我得到的是一个黑屏。编译着色器代码时没有任何错误,调用glGetError()时没有任何错误
#include <iostream>
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>
#include <glm\gtx\transform.hpp>
#include <common\shader.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 GLFW\n";
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 window\n";
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
cerr << "Failed to initialize GLEW\n";
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;
}
答案 0 :(得分:1)
您的绘图代码与着色器之间存在不匹配。在绘图代码中,您可以设置投影矩阵:
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范围内。