Libgdx,Mesh在相机移动时变形

时间:2014-06-12 12:30:13

标签: java camera libgdx mesh

我正在绘制一个简单的网格,它显示正常,但是当相机向右或向左移动时,它会变形并且不像精灵那样移动。

不移动相机link http://aetos.it.teithe.gr/~sskourti/temp/pic1.png

的外观

它如何移动它link http://aetos.it.teithe.gr/~sskourti/temp/pic2.png

相机移动时所需的效果link http://aetos.it.teithe.gr/~sskourti/temp/pic3.png

我缺少什么?

TestMesh mesh;
PerspectiveCamera camera;
ShaderProgram shader;

public TestScreen(){

    mesh = new TestMesh();

    camera = new PerspectiveCamera(67f, 2f*((float)Gdx.graphics.getWidth()/Gdx.graphics.getHeight()),2f);
    camera.position.set(0f, 0f, 1.5f);
    shader = new ShaderProgram(Gdx.files.internal("shaders/vertex.vsgl"), Gdx.files.internal("shaders/fragment.fsgl"));
}

public void render(float delta) {
    Gdx.gl.glClearColor(1f, 1f, 1f, 1f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    camera.update();


    shader.begin();
    shader.setUniformMatrix("u_projectionViewMatrix", camera.combined);
    mesh.render(shader);
    shader.end();

    if(Gdx.input.isKeyPressed(Keys.LEFT)){
        camera.position.set(camera.position.x-1f, camera.position.y, camera.position.z);
    }else if(Gdx.input.isKeyPressed(Keys.RIGHT)){
        camera.position.set(camera.position.x+1f, camera.position.y, camera.position.z);
    }

} 

TestMesh类,它扩展了MeshGameObject

public TestMesh(){
    addVertex(-0.5f, 0.5f,-2f);
    addVertex( 0.5f, 0.5f,-2f);
    addVertex( 0.5f,-0.5f,-2f);
    addVertex(-0.5f,-0.5f,-2f);


    addIndices(new short[] {0,1,2,3,0});
    create();
}

MeshGameObject

private Mesh mesh;
private float vertices[];
private Array<Float> v;
private short indeces[];

public MeshGameObject(){
    v = new Array<Float>();
}

public void create(){
    vertices = new float[v.size];
    for(int i=0;i<vertices.length;i++){
        vertices[i] = v.get(i).floatValue();
    }

    mesh = new Mesh(true, vertices.length/7, indeces.length, new VertexAttribute(Usage.Position,3,"a_position"), VertexAttribute.  ColorUnpacked());

    mesh.setVertices(vertices);
    mesh.setIndices(indeces);
}

public void render(ShaderProgram shader){
    mesh.render(shader,GL20.GL_TRIANGLE_STRIP,0,indeces.length);
}

protected void addVertex(float x,float y,float z){
    v.add(new Float(x));    //x
    v.add(new Float(y));    //y
    v.add(new Float(z));    //z
    v.add(new Float(0));    //r
    v.add(new Float(0));    //g
    v.add(new Float(0));    //b
    v.add(new Float(1));    //a
}

protected void addIndices(short[] indeces){
    this.indeces = indeces;
}

顶点着色器

attribute vec4 a_position;
attribute vec4 a_color;

uniform mat4 u_projectionViewMatrix;

varying vec4 v_color;

void main()
{
v_color = a_color;
    gl_Position = a_position * u_projectionViewMatrix;
}

片段着色器

varying vec4 v_color;

void main()
{
    gl_FragColor = v_color;
} 

0 个答案:

没有答案