LibGDX Shader不起作用

时间:2015-12-24 06:10:07

标签: java opengl libgdx

我正在尝试使用LibGDX渲染独立的Cube Mesh片段,为了使它们独立我为网格封装创建了一个Cube类。这是我写的代码

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Matrix4;

public class Cube {

private static final String VERTEX_SHADER =
        "attribute vec4 position;\n" +
                "attribute vec4 color;\n" +
                "uniform mat4 projection;\n" +
                "uniform mat4 transformation;\n" +
                "varying vec4 vColor;\n" +
                "void main(){\n" +
                "   vColor = color;\n" +
                "   gl_Position = projection * transformation * position;\n" +
                "}";

private static final String FRAGMENT_SHADER =
        "#ifdef GL_ES\n" +
                "precision mediump float;\n" +
                "#endif\n" +
                "varying vec4 vColor;\n" +
                "void main(){\n" +
                "   gl_FragColor = vColor;\n" +
                "}";

private static final float[] vertxs = {
        //0            1
        -1f, -1f, -1f, 1f, -1f, -1f,
        //2            3
        1f,  1f,  -1f, -1f, 1f, -1f,
        //4           5
        -1f, -1f, 1f, 1f, -1f, 1f,
        //6           7
        1f,  1f,  1f, -1f, 1f, 1f,
};

private static Matrix4 projection = new Matrix4();
static{
    final float aspect = Gdx.graphics.getWidth() / (float) Gdx.graphics.getHeight();
    projection.setToProjection(1.0f, 20.0f, 60.0f, aspect);
}

private static final short[] indicies = {
    1, 3, 0, 1, 2, 3, //front
    1, 6, 2, 1, 5, 6, //right
    2, 7, 3, 1, 6, 7, //top
    4, 7, 6, 4, 5, 6, //back
    7, 4, 3, 4, 3, 0, //left
    0, 4, 1, 4, 5, 1  //bottom
};

private static ShaderProgram createProgram() {

    ShaderProgram.pedantic = false;
    final ShaderProgram program = new ShaderProgram(VERTEX_SHADER, FRAGMENT_SHADER);
    final String log = program.getLog();
    if (!program.isCompiled()) {
        System.out.println("Failed to compile Cube Shader Program");
    }
    if (log != null && !log.isEmpty()) {
        System.out.println(log);
    }
    return program;
}

public static final ShaderProgram program = createProgram();


private Mesh mesh;
private Color color;
private Matrix4 transform;

public Cube(final Color color){
    this.color = color;
    mesh = new Mesh(true, 24, 36,
            new VertexAttribute(VertexAttributes.Usage.Position, 3, "position"),
            new VertexAttribute(VertexAttributes.Usage.ColorUnpacked, 3, "color"));
    mesh.setVertices(vertxs).setIndices(indicies);
    mesh.getVertexAttribute(VertexAttributes.Usage.Position).alias = "position";

    transform = new Matrix4();
}

public void addTransform(final Matrix4 additionalTrans){
    transform.mul(additionalTrans);
}

public Matrix4 getTransform(){
    return transform;
}

public Color getColor() {
    return color;
}

public void render(){
    Gdx.gl20.glDepthMask(true);
    Gdx.gl20.glEnable(GL20.GL_BLEND);
    Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_CONSTANT_ALPHA);

    program.begin();
    program.setUniform4fv("projection", projection.getValues(), 0, projection.getValues().length);
    program.setUniform4fv("transformation", transform.getValues(), 0, transform.getValues().length);
    program.setAttributef("color", color.r, color.g, color.b, color.a);
    mesh.render(program, GL20.GL_TRIANGLES);
    program.end();
}

}

和调用类

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;

public class MeshStudies extends ApplicationAdapter {
private Cube cube;

@Override
public void create() {
    cube = new Cube(Color.BLUE);
}

@Override
public void render() {
    Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl20.glClearColor(0.5f, 0.3f, 0.8f, 1f);
    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
    cube.render();
}
}

当我运行此代码时,我得到一个黑色的空白屏幕,没有任何Shader编译错误或提示。

请有人帮我弄清楚出了什么问题?如果任何有经验的LibGDX用户可以告诉我是否有更有效的方法来设计这个程序(在每个Cube类中保存转换矩阵是必要的),但是这个代码实际上是否在OpenGL透视图中有效地呈现每一件东西会更好?

由于

0 个答案:

没有答案