【问题标题】:Problems with making 2D terrain in OpenGL在 OpenGL 中制作 2D 地形的问题
【发布时间】:2012-05-04 19:55:27
【问题描述】:

前段时间我问this question关于如何用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 类,但这并不相关,因为我相信它可以正常工作。我的问题在于顶点和索引的生成。我真的不知道如何调试它,所以任何人都可以看看它,并帮助我找出为什么什么都没有呈现?

【问题讨论】:

  • 您确定没有渲染任何内容吗?可能是您相机的位置。

标签: java opengl libgdx terrain vertex-array


【解决方案1】:

一整天过去了,我已经尝试了一切来解决它,我似乎忘记真正设置 indices 到 mesh时间>。

mesh.setIndices(tempIn);  

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多