所以我按照LearnOpenGL的教程来做相机。我试着渲染一个立方体(用顶点矢量定义),然后绕着立方体转一圈,确保它渲染的正确。我可以很好地渲染立方体的正面,但是一旦我切换着色器使用MVP,什么也渲染不了
void GraphicsProgram::run_program()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
state = RUNNING;
// TODO: Move these into their respective classes
// Hardcoded for some testing
// ----------------------------------------------
const char *vertexShaderPath = "vertex_shader.vs";
const char *fragmentShaderPath = "fragment_shader.fs";
Shader firstShader = Shader(vertexShaderPath, fragmentShaderPath);
std::vector<Vertex> verts;
Vertex v1, v2, v3, v4, v5, v6, v7, v8;
v1.position = { -0.5, -0.5, +0.5 };
v2.position = { +0.5, -0.5, +0.5 };
v3.position = { +0.5, +0.5, +0.5 };
v4.position = { -0.5, +0.5, +0.5 };
v5.position = { +0.5, -0.5, -0.5 };
v6.position = { +0.5, +0.5, -0.5 };
v7.position = { -0.5, +0.5, -0.5 };
v8.position = { -0.5, -0.5, -0.5 };
verts.push_back(v1);
verts.push_back(v2);
verts.push_back(v3);
verts.push_back(v4);
verts.push_back(v5);
verts.push_back(v6);
verts.push_back(v7);
verts.push_back(v8);
std::vector<unsigned int> indicies = {
0, 1, 2, 0, 2, 3, // front
1, 4, 5, 1, 5, 2, // right
4, 7, 6, 4, 6, 7, // back
7, 0, 3, 7, 3, 6, // left
3, 2, 5, 3, 5, 6, // top
7, 4, 1, 7, 1, 0 // bottom
};
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, verts.size() * sizeof(Vertex), &verts[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicies.size() * sizeof(unsigned int), &indicies[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glUseProgram(firstShader.shaderID);
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
firstShader.set_mat4("projection", projection);
while (state == RUNNING) {
currentFrame = (float)SDL_GetTicks() / 1000;
deltaTime = lastFrame - currentFrame;
lastFrame = currentFrame;
process_input();
glUseProgram(firstShader.shaderID);
glm::mat4 view;
float radius = 10.0f;
float camX = sin((float)SDL_GetTicks() / 1000) * radius;
float camZ = cos((float)SDL_GetTicks() / 1000) * radius;
view = glm::lookAt(glm::vec3(camX, 0.0f, camZ), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
firstShader.set_mat4("view", view);
glBindVertexArray(VAO);
glm::mat4 model;
model = glm::translate(model, glm::vec3(0.0, 0.0, 0.0));
float angle = 0.0;
model = glm::rotate(model, glm::radians(angle), glm::vec3(0,0,0));
firstShader.set_mat4("model", model);
glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_INT, 0);
SDL_GL_SwapWindow(window);
}
shutdown_program();
}
顶点结构:
struct Vertex {
glm::vec3 position;
glm::vec3 normal;
glm::vec2 texCoord;
};
顶点着色器代码:
# version 330 core
layout (location = 0) in vec3 aPos;
out vec4 vertexColor;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = projection * view * model * vec4(aPos, 1.0f);
vertexColor = vec4(abs(aPos.x), abs(aPos.y), abs(aPos.z), 1.0);
}
片段着色器:
# version 330 core
in vec4 vertexColor;
out vec4 fragColor;
void main() {
fragColor = vertexColor;
}
我最初使用的顶点着色器只是接受了顶点位置。
1条答案
按热度按时间6yoyoihd1#
您必须初始化矩阵变量
glm::mat4 model
和glm::mat4 view
。glm API documentation指的是The OpenGL Shading Language specification 4.20。
5.4.2向量和矩阵构造函数
如果向量构造函数只有一个标量参数,则用于将构造向量的所有分量初始化为该标量的值。如果矩阵构造函数只有一个标量参数,则用于初始化矩阵对角线上的所有分量,其余分量初始化为0.0。
这意味着,单位矩阵可以由单个参数1.0初始化:
您没有在
glm::rotate
中设置正确的旋转轴:您必须在每个帧(
glClear
)之前清除默认farmebuffer:您应该启用Depth Test并清除深度缓冲区: