0

我在 LibGDX 中做一些基本代码并想要渲染一个绿色球体网格,但是我设置为 LEQUAL 的深度测试有一个错误。错误是它似乎像 GEQUAL 一样将它们呈现在前面。

这是截图 - https://www.imageupload.co.uk/image/5ot2

这是代码 -

MyGdxGame.java

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.graphics.g3d.utils.DefaultTextureBinder;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
import com.badlogic.gdx.utils.Array;

public class MyGdxGame extends ApplicationAdapter {
    Environment env;

    PerspectiveCamera camera;
    CameraInputController camController;

    ModelBatch modelBatch;
    Array<ModelInstance> instances;
    CustomShader shader;

    @Override
    public void create () {
        env = new Environment();
        env.set(new ColorAttribute(ColorAttribute.AmbientLight,
                0.5f, 0.5f, 0.5f, 1.0f));
        env.add(new DirectionalLight().set(0.7f, 0.7f, 0.7f,
                1.0f, 1.0f, 1.0f));

        camera = new PerspectiveCamera(60.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        camera.position.set(0.0f, 5.0f, 0.0f);
        camera.lookAt(10.0f, 5.0f, 10.0f);
        camera.near = 0.0f;
        camera.far = 300.0f;
        camera.update();
        camController = new CameraInputController(camera);
        Gdx.input.setInputProcessor(camController);

        modelBatch = new ModelBatch();
        instances = new Array<ModelInstance>();

        ModelBuilder mb = new ModelBuilder();
        for(float x = 0.0f; x < 100.0f; x += 10.0f) {
            for(float z = 0.0f; z < 100.0f; z += 10.0f) {
                Model model = mb.createSphere(5.0f, 5.0f, 5.0f, 20, 20,
                        new Material(),
                        Usage.Position | Usage.Normal | Usage.TextureCoordinates);
                ModelInstance instance = new ModelInstance(model);
                instance.transform.setTranslation(x, 0.0f, z);
                instances.add(instance);
            }
        }

        shader = new CustomShader();
        shader.init();
    }

    @Override
    public void dispose () {
        instances.clear();
    }

    @Override
    public void render () {
        camController.update();

        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

        modelBatch.begin(camera);
        for(ModelInstance instance : instances) {
            modelBatch.render(instance, shader);
        }
        modelBatch.end();
    }
}

CustomShader.java

package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g3d.Renderable;
import com.badlogic.gdx.graphics.g3d.Shader;
import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.utils.GdxRuntimeException;

public class CustomShader implements Shader {
    ShaderProgram program;
    int u_worldTrans, u_projViewTrans;

    @Override
    public void dispose() {
        program.dispose();
    }

    @Override
    public void init() {
        String vert = Gdx.files.internal("shaders/custom.vertex.glsl").readString();
        String frag = Gdx.files.internal("shaders/custom.fragment.glsl").readString();
        program = new ShaderProgram(vert, frag);
        if(!program.isCompiled()) {
            throw new GdxRuntimeException(program.getLog());
        }

        // Cache uniform positions
        u_worldTrans = program.getUniformLocation("u_worldTrans");
        u_projViewTrans = program.getUniformLocation("u_projViewTrans");
    }

    @Override
    public int compareTo(Shader other) {
        return 0;
    }

    @Override
    public boolean canRender(Renderable instance) {
        return true;
    }

    @Override
    public void begin(Camera camera, RenderContext context) {
        program.begin();
        program.setUniformMatrix(u_projViewTrans, camera.combined);
        context.setDepthTest(GL20.GL_LEQUAL);//not working/displaying GEQUAL instead?
        context.setCullFace(GL20.GL_BACK);
    }

    @Override
    public void render(Renderable renderable) {
        program.setUniformMatrix(u_worldTrans, renderable.worldTransform);
        renderable.mesh.render(program,
                renderable.primitiveType,
                renderable.meshPartOffset,
                renderable.meshPartSize);
    }

    @Override
    public void end() {
        program.end();
    }

}

custom.vertex.glsl

uniform mat4 u_worldTrans;
uniform mat4 u_projViewTrans;

attribute vec3 a_position;
attribute vec3 a_normal;
attribute vec2 a_texCoord0;

varying vec2 v_texCoord0;

void main() {
    v_texCoord0 = a_texCoord0;
    gl_Position = u_projViewTrans * u_worldTrans * vec4(a_position, 1.0);
}

custom.fragment.glsl

    #ifdef GL_ES
        precision mediump float;
    #endif

    varying vec2 v_texCoord0;

    void main() {
        //gl_FragColor = vec4(v_texCoord0, 0.0f, 1.0f);
        gl_FragColor = vec4(0.0f, (0.5f*v_texCoord0.x)/v_texCoord0.y, 0.0f, 1.0f);
    }
4

0 回答 0