在 2d gui 上渲染 3d 模型时出现问题
Problems rendering a 3d model on a 2d gui
我不知道为什么我的模型在透视模式下渲染良好,但在正交模式下却有问题。该模型由 2 个立方体组成,一大一小在上面。在 Ortho 模式下,小立方体渲染在大立方体后面,尽管它不应该:
显然我做对了,因为大立方体呈现完全正确,即使在任何方向旋转时也是如此。也许与在正交模式下深度缓冲区是线性的事实有关?
我在正交模式中的 znear 和 zfar 值是 0.4 和 1000。深度测试已启用。模型(=两个立方体)存储在具有 3 个 vbos(用于顶点、uvs 和 rgbas)的 VAO 中
使用此着色器渲染模型:
顶点:
#version 330
#extension GL_ARB_explicit_attrib_location: enable
layout(location = 0) in vec3 vertexPositionIn;
layout(location = 1) in vec2 uvIn;
layout(location = 2) in vec4 colorIn;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
out vec2 uv;
out vec4 color;
void main(void)
{
uv = uvIn;
color = colorIn;
gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPositionIn, 1.0);
}
片段:
#version 330
in vec2 uv;
in vec4 color;
uniform sampler2D itemTex;
void main () {
gl_FragColor = texture(itemTex, uv) * color;
}
正交模式设置
public void OrthoMode(int width, int height)
{
GlMatrixModeProjection();
GlPushMatrix();
GlLoadIdentity();
GlOrtho(0, width, height, 0, 0.4f, 1001);
LoadCurrentProjectionMatrix();
GlMatrixModeModelView();
GlPushMatrix();
GlLoadIdentity();
GlTranslate(0, 0, -500); // Translate far in the back so we can also render 3d stuff
LoadCurrentModelViewMatrix();
}
绘图调用:
[....]
game.GlPushMatrix();
game.GlTranslate((float)posX, (float)posY, (float)posZ);
DrawItemStack(modelref, size, rotate);
game.GlPopMatrix();
[....]
public void DrawItemStack(ModelRef modelref, float size, bool rotate = false)
{
game.GlPushMatrix();
game.GlTranslate(0.5f * size, 0.5f * size, 0.5f * size);
game.GlRotate(180f + 22.6f, 1, 0, 0); // In ortho mode our y-axis is flipped
game.GlRotate(-45.3f + (rotate ? game.CurrentTimeMilliseconds / 50f : 0), 0, 1, 0);
game.GlScale(size * 0.5f, size * 0.5f, size * 0.5f);
game.GlTranslate(-0.5f, -0.5f, -0.5f);
game.LoadCurrentProjectionMatrixUniform(0);
game.LoadCurrentModelviewMatrixUniform(1);
game.Platform.DrawModelNew(modelref); // Calls GL.DrawElements()
game.GlPopMatrix();
}
最后,他是一个动画 gif 旋转时的行为方式:
事实证明这只是一个愚蠢的错误 - 我忘记启用深度遮罩 -.-
我不知道为什么我的模型在透视模式下渲染良好,但在正交模式下却有问题。该模型由 2 个立方体组成,一大一小在上面。在 Ortho 模式下,小立方体渲染在大立方体后面,尽管它不应该:
显然我做对了,因为大立方体呈现完全正确,即使在任何方向旋转时也是如此。也许与在正交模式下深度缓冲区是线性的事实有关? 我在正交模式中的 znear 和 zfar 值是 0.4 和 1000。深度测试已启用。模型(=两个立方体)存储在具有 3 个 vbos(用于顶点、uvs 和 rgbas)的 VAO 中
使用此着色器渲染模型:
顶点:
#version 330
#extension GL_ARB_explicit_attrib_location: enable
layout(location = 0) in vec3 vertexPositionIn;
layout(location = 1) in vec2 uvIn;
layout(location = 2) in vec4 colorIn;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
out vec2 uv;
out vec4 color;
void main(void)
{
uv = uvIn;
color = colorIn;
gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPositionIn, 1.0);
}
片段:
#version 330
in vec2 uv;
in vec4 color;
uniform sampler2D itemTex;
void main () {
gl_FragColor = texture(itemTex, uv) * color;
}
正交模式设置
public void OrthoMode(int width, int height)
{
GlMatrixModeProjection();
GlPushMatrix();
GlLoadIdentity();
GlOrtho(0, width, height, 0, 0.4f, 1001);
LoadCurrentProjectionMatrix();
GlMatrixModeModelView();
GlPushMatrix();
GlLoadIdentity();
GlTranslate(0, 0, -500); // Translate far in the back so we can also render 3d stuff
LoadCurrentModelViewMatrix();
}
绘图调用:
[....]
game.GlPushMatrix();
game.GlTranslate((float)posX, (float)posY, (float)posZ);
DrawItemStack(modelref, size, rotate);
game.GlPopMatrix();
[....]
public void DrawItemStack(ModelRef modelref, float size, bool rotate = false)
{
game.GlPushMatrix();
game.GlTranslate(0.5f * size, 0.5f * size, 0.5f * size);
game.GlRotate(180f + 22.6f, 1, 0, 0); // In ortho mode our y-axis is flipped
game.GlRotate(-45.3f + (rotate ? game.CurrentTimeMilliseconds / 50f : 0), 0, 1, 0);
game.GlScale(size * 0.5f, size * 0.5f, size * 0.5f);
game.GlTranslate(-0.5f, -0.5f, -0.5f);
game.LoadCurrentProjectionMatrixUniform(0);
game.LoadCurrentModelviewMatrixUniform(1);
game.Platform.DrawModelNew(modelref); // Calls GL.DrawElements()
game.GlPopMatrix();
}
最后,他是一个动画 gif 旋转时的行为方式:
事实证明这只是一个愚蠢的错误 - 我忘记启用深度遮罩 -.-