我正在尝试使用 SFML(而不是 GLFW)在 OpenGL 中渲染一些立方体。这两个对象都有不同的片段和顶点着色器。问题是,当我使用 W、A、S、D 和鼠标控制相机时,照明立方体似乎在移动,但草立方体固定在相机上,挡住了视线。
截图: (红色圈出的光立方)
我尝试修改着色器,但结果是一样的。
草块的顶点着色器:
#version 430 core
layout(location=0) in vec3 position;
layout(location=1) in vec2 textureCords;
layout(location=2) in vec3 normals;
uniform mat4 perspective;
uniform mat4 model;
uniform mat4 view;
out vec2 texCordinates;
out vec3 aNormals;
void main() {
gl_Position = perspective * view * model * vec4(position, 1.0);
texCordinates = textureCords;
aNormals = normals;
}
光源块的Vertex Shader:
#version 430 core
layout(location=0) in vec3 position;
uniform mat4 perspective;
uniform mat4 model;
uniform mat4 view;
void main() {
gl_Position = perspective * view * model * vec4(position, 1.0);
}
草块的片段着色器:
#version 430 core
in vec2 texCordinates;
in vec3 aNormals;
out vec4 fragColor;
uniform sampler2D tex;
void main()
{
fragColor = texture(tex, texCordinates);
}
光源块的片段着色器:
#version 430 core
out vec4 fragColor;
void main() {
fragColor = vec4(1.0);
}
我使用不同的 VAO 和程序,但对立方体使用相同的 VBO 和 EBO。我正在设置相同的透视和视图矩阵但不同的模型矩阵。
我的渲染代码:
//Use the grass block program
program.use();
vao.bind(); //Binds the vao
ebo.bind(); //Binds the ebo
vbo.bind(); //Binds the vbo
//Render the grass blocks
CubeGenerator(0, program, GRASS, vec3(0.0, 0.0, 0.0), 16, 1, 16, 2.0f).render();
//Use the light program
light_program.use();
light_vao.bind(); //Bind the light vao
ebo.bind(); //Bind the light ebo
vbo.bind(); //Bind the light vbo
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); //render the light cube
VAO、VBO、EBO 和 Program 类是我制作的,但效果很好。当我尝试使用不同的着色器集绘制照明立方体时,问题就出现了;
我启用了这些设置:
//Anti aliasing
glEnable(GL_MULTISAMPLE);
glSampleCoverage(0.8f, GL_FALSE);
//Depth test
glEnable(GL_DEPTH_TEST);
//Face culling
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glFrontFace(GL_CW);
我有一个工作正常的相机系统。