LWJGL图像深度测试

时间:2018-07-13 07:09:58

标签: java windows eclipse 3d lwjgl

我在月食中使用LWJGL,并制作了一个3D游戏,该游戏使用始终面对镜头的2D精灵。我遇到了深度测试问题。当前使用此代码绘制面向相机的精灵。

private static final float[] VERTICES = {-0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f, -0.5f};

private RawModel quad;
private SpriteShader shader;

protected SpriteRenderer(Loader loader, Matrix4f projectionMatrix){
    quad=loader.loadToVAO(VERTICES, 2);
    shader=new SpriteShader();
    shader.start();
    shader.loadProjectionMatrix(projectionMatrix);
    shader.stop();
}

protected void render(Map<SpriteTexture,List<Sprite>> sprites, Camera camera){
    Matrix4f viewMatrix = Maths.createViewMatrix(camera);
    prepare();
    for(SpriteTexture texture : sprites.keySet()){
        //bind texture
        GL13.glActiveTexture(GL13.GL_TEXTURE0);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
        GL11.glTexParameterf( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
    for(Sprite sprite : sprites.get(texture)){
        updateModelViewMatrix(sprite.getPosition(),sprite.getRotation(),sprite.getScale(),viewMatrix);
        shader.loadTextureCoordInfo(sprite.getTexOffset1(), sprite.getTexOffset2(), texture.getNumberOfRows(), sprite.getBlend());
        GL11.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, quad.getVertexCount());
    }
    }
    finishRendering();
}

protected void cleanUp(){
    shader.cleanUp();
}

private void updateModelViewMatrix(Vector3f position, float rotation, float scale, Matrix4f viewMatrix){
    Matrix4f modelMatrix = new Matrix4f();
    Matrix4f.translate(position, modelMatrix, modelMatrix);
    modelMatrix.m00 = viewMatrix.m00;
    modelMatrix.m01 = viewMatrix.m10;
    modelMatrix.m02 = viewMatrix.m20;
    modelMatrix.m10 = viewMatrix.m01;
    modelMatrix.m11 = viewMatrix.m11;
    modelMatrix.m12 = viewMatrix.m21;
    modelMatrix.m20 = viewMatrix.m02;
    modelMatrix.m21 = viewMatrix.m12;
    modelMatrix.m22 = viewMatrix.m22;
    Matrix4f.rotate((float) Math.toRadians(rotation), new Vector3f(0,0,1), modelMatrix, modelMatrix);
    Matrix4f.scale(new Vector3f(scale,scale,scale), modelMatrix, modelMatrix);
    Matrix4f modelViewMatrix=Matrix4f.mul(viewMatrix, modelMatrix, null);
    shader.loadModelViewMatrix(modelViewMatrix);
}

private void prepare(){
    shader.start();
    GL30.glBindVertexArray(quad.getVaoID());
    GL20.glEnableVertexAttribArray(0);
    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

    GL11.glDepthMask(false);
}

private void finishRendering(){
    GL11.glDepthMask(true);

    GL11.glDisable(GL11.GL_BLEND);
    GL20.glDisableVertexAttribArray(0);
    GL30.glBindVertexArray(0);
    shader.stop();
}

深度在所有其他3d模型上都可以正常工作,除了此处绘制的实际图像。最后绘制的图像显示在顶部,而不是对图像本身进行实际的深度测试。这是一个例子。 Angle that shows proper depth when stacking order is complemented。这是 Broken stacking order。同样在3d模型位于两个2d图像this occurs之间的奇怪情况下。

任何帮助都会很棒。谢谢。

0 个答案:

没有答案