【问题标题】:Opengl nothing is being written to depth bufferOpengl没有任何东西被写入深度缓冲区
【发布时间】:2016-03-06 08:17:36
【问题描述】:

我正在尝试对 2D 等距游戏实施深度测试。为了让某些东西正常工作,我从这个示例开始,但我无法让它正常工作。

我正在尝试按特定顺序绘制 2 张图像。

first.png

秒.png

first.png 先绘制, second.png 绘制在顶部。使用片段着色器,我计算出红色的深度低于绿色,因此在红色片段上绘制时应该丢弃绿色片段。最终结果是,当 second.png 直接绘制在 first.png 之上时,生成的正方形仅显示为红色。

在渲染函数结束时,我得到了深度缓冲区的像素,并在它们上循环检查值是否已从默认值更改。似乎无论我做什么,深度缓冲区中的值都不会改变。

深度测试本身正在工作,如果我将绿色片段设置为depth=1.0,将红色片段设置为depth=0.0,并且我的深度函数是GL_LESS,则只绘制红色片段,但深度缓冲区没有改变。

代码是Java,但是OpenGL函数是一样的。

    private SpriteBatch mBatch;

private Texture mTexture1;
private Texture mTexture2;

@Override
public void create() {

    mBatch = new SpriteBatch();
    mBatch.setShader(new ShaderProgram(Gdx.files.internal("test.vsh"), Gdx.files.internal("test.fsh")));

    mTexture1 = new Texture("first.png");
    mTexture2 = new Texture("second.png");

    Gdx.gl20.glEnable(GL20.GL_DEPTH_TEST);
    Gdx.gl20.glDepthFunc(GL20.GL_LESS);
    Gdx.gl20.glDepthMask(true);

}

@Override
public void render() {

    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    mBatch.begin();

    float scale = 4.0f;

    float x = Gdx.graphics.getWidth() / 2;
    float y = Gdx.graphics.getHeight() / 2;

    mBatch.draw(mTexture1, x - mTexture1.getWidth() / 2 * scale, y - mTexture1.getHeight() / 2 * scale,
            mTexture1.getWidth() * scale, mTexture1.getHeight() * scale);

    mBatch.flush();

    mBatch.draw(mTexture2, x - mTexture2.getWidth() / 2 * scale, y - mTexture2.getHeight() / 2 * scale,
            mTexture2.getWidth() * scale, mTexture2.getHeight() * scale);

    mBatch.end();

    int width = Gdx.graphics.getWidth();
    int height = Gdx.graphics.getHeight();
    FloatBuffer buffer = BufferUtils.newFloatBuffer(width * height);
    Gdx.gl20.glReadPixels(0, 0, width, height, GL20.GL_DEPTH_COMPONENT, GL20.GL_FLOAT,
            buffer);

    for (int i = 0; i < width * height; i++) {
        float pixel = buffer.get(i);
        if (pixel != 1.0f && pixel != 0.0f) {
            // why is this never thrown??
            // it means depth buffer wasn't changed.
            throw new IllegalStateException("OMG IT WORKS!! " + pixel);
        }
    }

    if (Gdx.gl20.glGetError()!=0) {
        throw new Error("OPENGL ERROR: " + Gdx.gl20.glGetError());
    }
}

顶点着色器

#ifdef GL_ES
precision mediump float;
#endif

attribute vec3 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;

uniform mat4 u_projTrans;
varying vec4 v_color;
varying vec2 v_texCoord;

void main()
{
    gl_Position = u_projTrans * vec4(a_position, 1);
    v_color = a_color * 2.0;
    v_texCoord = a_texCoord0;
}

片段着色器

#ifdef GL_ES
precision mediump float;
#endif

uniform sampler2D u_texture;

varying vec4 v_color;
varying vec2 v_texCoord;

void main()
{

    vec4 texel = v_color * texture2D(u_texture, v_texCoord);

    if (texel.r > texel.g)
    {
        gl_FragDepth = 0.0;
    }
    else
    {
        gl_FragDepth = 0.5;
    }


    gl_FragColor = texel;
}

【问题讨论】:

    标签: java opengl-es libgdx opengl-3


    【解决方案1】:

    好的,我找到了问题。

    SpriteBatch.begin() 会

    glDepthMask(假)

    将 glDepthMask 设置为 false 可防止 OpenGL 写入深度缓冲区。

    解决办法是在SpriteBatch.begin()之后调用glDepthMask(true)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多