Проблемы с созданием 2D-ландшафта в OpenGL

Некоторое время назад я задал этот вопрос о как сделать 2D ландшафт с вершинами opengl. Я получил хороший ответ, но при проверке он ничего не нарисовал, и я не могу понять, что не так или как это исправить.

У меня сейчас это:

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, но это не очень важно, так как я считаю, что это работает нормально. Мои проблемы заключаются в генерации вершин и индексов. Я тоже не знаю, как его отлаживать, так что может кто-нибудь взглянуть на него и помочь мне понять, почему ничего не отображается?


person user717572    schedule 04.05.2012    source источник
comment
вы уверены, что ничего не рендерится?? Может быть положение вашей камеры.   -  person PaulG    schedule 04.05.2012


Ответы (1)


После того, как прошел целый день, и я перепробовал все, чтобы решить эту проблему, оказалось, что я действительно забыл установить индексы для сетки.

mesh.setIndices(tempIn);  

Одна пропущенная строчка, часы боли... я идиот :)

person user717572    schedule 04.05.2012