1

前段时间我问过这个问题,关于如何使用 opengl 顶点制作 2D 地形。我得到了一个很好的答案,但是在尝试时它没有画出任何东西,我不知道出了什么问题,或者如何解决它。

我现在有这个:

public class Terrain extends Actor {

Mesh mesh;
private final int LENGTH = 1500; //length of the whole terrain

public Terrain(int res) {

    Random r = new Random();

    //res (resolution) is the number of height-points 
    //minimum is 2, which will result in a box (under each height-point there is another vertex)
    if (res < 2)
        res = 2;

    mesh = new Mesh(VertexDataType.VertexArray, true, 2 * res, 50, new VertexAttribute(Usage.Position, 2, "a_position")); 

    float x = 0f;     //current position to put vertices
    float med = 100f; //starting y
    float y = med;

    float slopeWidth = (float) (LENGTH / ((float) (res - 1))); //horizontal distance between 2 heightpoints


    // VERTICES
    float[] tempVer = new float[2*2*res]; //hold vertices before setting them to the mesh
    int offset = 0; //offset to put it in tempVer

    for (int i = 0; i<res; i++) {

        tempVer[offset+0] = x;      tempVer[offset+1] = 0f; // below height
        tempVer[offset+2] = x;      tempVer[offset+3] = y;  // height

        //next position: 
        x += slopeWidth;
        y += (r.nextFloat() - 0.5f) * 50;
        offset +=4;
    }
    mesh.setVertices(tempVer);


    // INDICES
    short[] tempIn = new short[(res-1)*6];
    offset = 0;
    for (int i = 0; i<res; i+=2) {

        tempIn[offset + 0] = (short) (i);       // below height
        tempIn[offset + 1] = (short) (i + 2);   // below next height
        tempIn[offset + 2] = (short) (i + 1);   // height

        tempIn[offset + 3] = (short) (i + 1);   // height
        tempIn[offset + 4] = (short) (i + 2);   // below next height
        tempIn[offset + 5] = (short) (i + 3);   // next height

        offset+=6;
    }
}

@Override
public void draw(SpriteBatch batch, float delta) {
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    mesh.render(GL10.GL_TRIANGLES);
}

这是由 Libgdx 渲染的,它也提供了类 Mesh,但这并不真正相关,因为我相信它可以正常工作。我的问题在于顶点和索引的生成。我也不知道如何调试它,所以任何人都可以看看它,并帮助我找出为什么什么都没有渲染?

4

1 回答 1

3

一整天过去了,我已经尝试了一切来解决它,似乎我忘了实际索引设置为mesh

mesh.setIndices(tempIn);  

一条缺失的线,数小时的痛苦……我是个白痴:)

于 2012-05-04T23:47:30.973 回答