我目前正在做一个小型的OpenGlug游戏。我试着做一个呈现器,它呈现一个简单的矩形,但是我什么都没有呈现。我很确定着色器工作,所以我认为问题出在渲染器的代码上。
我认为问题出在这段代码上:
#include "Renderer.h"
#include <game/Game.h>
#include <glad/glad.h>
Renderer::Renderer() {
initQuadRenderData();
}
void Renderer::initQuadRenderData() {
float vertices[]{
0.0f, 1.0f,
1.0f, 0.0f,
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f
};
unsigned int vbo;
glGenVertexArrays(1, &quadVAO);
glGenBuffers(1, &vbo);
glBindVertexArray(quadVAO);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Renderer::renderRect(Shader& shader, glm::vec2 pos, glm::vec2 size, glm::vec3 color, float opacity) {
shader.use();
glBindVertexArray(quadVAO);
glDrawArrays(GL_VERTEX_ARRAY, 0, 6);
glBindVertexArray(0);
}
我的着色器类只有两个功能:编译和使用:
#include "Shader.h"
#include <game/Game.h>
#include <glad/glad.h>
void Shader::compile(const char* vertexSource, const char* fragmentSource) {
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, nullptr);
glCompileShader(vertexShader);
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, nullptr);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
id = glCreateProgram();
glAttachShader(id, vertexShader);
glAttachShader(id, fragmentShader);
glLinkProgram(id);
glGetProgramiv(id, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(id, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
void Shader::use() {
glUseProgram(id);
}
错误就在这一行:
glDrawArrays(GL_VERTEX_ARRAY, 0, 6);
glDrawArrays(GL_TRIANGLES, 0, 6);