2012-05-04 17 views
1

ある程度前に、OpenGL頂点で2D地形を作る方法について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、によってレンダリングされているが、私は正常に動作していると考えているので、これは本当に関係ありません。私の問題は、頂点とインデックスの生成によって決まります。私は実際にそれをデバッグする方法も知らないので、誰もそれを見て、何もレンダリングされていない理由を見つけるのを助けることができますか?丸一日が経過している、と私はそれを解決するためにすべてを試みた後

+0

よ、あなたは何もレンダリングされていないことを確認しています?あなたのカメラの位置かもしれません。 – PaulG

答えて

3

、私が実際にメッシュインデックスを設定するのを忘れたように見えました。

mesh.setIndices(tempIn); 
ライン、痛みの時間が不足して

一つ...私は馬鹿:)

+4

それはプログラミングです。 :) – Matsemann

関連する問題