簡體   English   中英

在OpenGL中制作2D地形的問題

[英]Problems with making 2D terrain in OpenGL

前段時間我問過這個關於如何用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類,但這並不是真正相關,因為我認為它工作正常。 我的問題在於頂點和索引生成。 我也不知道如何調試它,所以任何人都可以看看它,並幫助我找到為什么沒有渲染?

經過一整天,我已經嘗試了一切來解決它,似乎我忘了實際設置 索引網格

mesh.setIndices(tempIn);  

一條缺失的線,幾個小時的痛苦......我是個白痴:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM