2017-06-08 13 views
0

私はVBOs、VAOs、およびIndexesのかなり新しいです。私は単一のキューブをレンダリングできましたが、今はキューブの塊をレンダリングしようとしています。私の目標は、ゆっくりとボクセルエンジンを作ることです。チャンククラスに問題があります。何らかの理由で何も表示されません。誰でも素早く見て、何が間違っているのか分かり、それを私に指摘できますか?乾杯glDrawElementsは何も描画しません

class Chunk { 

private IntBuffer vaoID; 
private IntBuffer vboID; 
private IntBuffer indexID; 


public void createChunkVBO() { 

    FloatBuffer vertices = BufferUtils.createFloatBuffer(16 * 256 * 16 * 3 * 8); 
    FloatBuffer colors = BufferUtils.createFloatBuffer(16 * 256 * 16 * 4 * 8); 
    FloatBuffer indices = BufferUtils.createFloatBuffer(16 * 256 * 16 * 4 * 6); 

    vaoID = BufferUtils.createIntBuffer(1); // Create a buffer for the Vertex Array Object 
    vboID = BufferUtils.createIntBuffer(1); // Create a buffer for the Vertex Buffer Object 
    indexID = BufferUtils.createIntBuffer(1); // Create a buffer for the Vertex Indices 


    for (int x = 0; x < 16; x++) { 
     for (int y = 0; y < 256; y++) { 
      for (int z = 0; z < 16; z++) { 
       System.out.println(x + ", " + y + ", " + z); 
       vertices.put(x + World.BLOCK_SIZE); 
       vertices.put(y); 
       vertices.put(z + World.BLOCK_SIZE); 

       vertices.put(x); 
       vertices.put(y); 
       vertices.put(z + World.BLOCK_SIZE); 

       vertices.put(x); 
       vertices.put(y); 
       vertices.put(z); 

       vertices.put(x + World.BLOCK_SIZE); 
       vertices.put(y); 
       vertices.put(z); 

       vertices.put(x + World.BLOCK_SIZE); 
       vertices.put(y + World.BLOCK_SIZE); 
       vertices.put(z + World.BLOCK_SIZE); 

       vertices.put(x); 
       vertices.put(y + World.BLOCK_SIZE); 
       vertices.put(z + World.BLOCK_SIZE); 

       vertices.put(x); 
       vertices.put(y + World.BLOCK_SIZE); 
       vertices.put(z); 

       vertices.put(x + World.BLOCK_SIZE); 
       vertices.put(y + World.BLOCK_SIZE); 
       vertices.put(z); 


       colors.put(1f); 
       colors.put(0f); 
       colors.put(0f); 
       colors.put(1f); 

       colors.put(1f); 
       colors.put(0f); 
       colors.put(0f); 
       colors.put(1f); 

       colors.put(1f); 
       colors.put(0f); 
       colors.put(0f); 
       colors.put(1f); 

       colors.put(1f); 
       colors.put(0f); 
       colors.put(0f); 
       colors.put(1f); 

       colors.put(1f); 
       colors.put(0f); 
       colors.put(0f); 
       colors.put(1f); 

       colors.put(1f); 
       colors.put(0f); 
       colors.put(0f); 
       colors.put(1f); 

       colors.put(1f); 
       colors.put(0f); 
       colors.put(0f); 
       colors.put(1f); 

       colors.put(1f); 
       colors.put(0f); 
       colors.put(0f); 
       colors.put(1f); 


       indices.put(0 + x * y * z); 
       indices.put(1 + x * y * z); 
       indices.put(2 + x * y * z); 
       indices.put(3 + x * y * z); 


       indices.put(4 + x * y * z); 
       indices.put(5 + x * y * z); 
       indices.put(2 + x * y * z); 
       indices.put(3 + x * y * z); 


       indices.put(1 + x * y * z); 
       indices.put(3 + x * y * z); 
       indices.put(7 + x * y * z); 
       indices.put(5 + x * y * z); 


       indices.put(0 + x * y * z); 
       indices.put(3 + x * y * z); 
       indices.put(4 + x * y * z); 
       indices.put(7 + x * y * z); 


       indices.put(0 + x * y * z); 
       indices.put(1 + x * y * z); 
       indices.put(6 + x * y * z); 
       indices.put(7 + x * y * z); 


       indices.put(4 + x * y * z); 
       indices.put(5 + x * y * z); 
       indices.put(6 + x * y * z); 
       indices.put(7 + x * y * z); 

      } 
     } 
    } 


    glGenVertexArrays(vaoID); // Create an id for the VAO 
    glBindVertexArray(vaoID.get(0)); // Bind the VAO so it remembers all the Attributes (none right now) 

    glGenBuffers(vboID); // Create an id for the VBO 
    glBindBuffer(GL_ARRAY_BUFFER, vboID.get(0)); // Bind the VBO so we can put data into it 

    glBufferData(GL_ARRAY_BUFFER, 16 * 256 * 16 * 8 * 7 * Float.SIZE, GL_STATIC_DRAW); // We make an empty buffer with a specific size in bytes 
                     // 8 * 7 * sizeof(float) 
                     // 8 = number of vertices, 7 = xyzrgba 

    glBufferSubData(GL_ARRAY_BUFFER, 0, vertices); // Put the vertices at the beginning of the buffer 
    glBufferSubData(GL_ARRAY_BUFFER, 16 * 256 * 16 * 8 * 3 * Float.SIZE, colors); // Put the colors after the vertices 

    glGenBuffers(indexID); // Create an id for the Index Buffer 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexID.get(0)); // Bind the Index Buffer so we can put data into it 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW); // Store the indices inside the currently bound Index Buffer 

} 

public void drawChunk() { 

    glEnableClientState(GL_VERTEX_ARRAY); // Enable the Vertex Array 
    glEnableClientState(GL_COLOR_ARRAY); // Enable the Color Array 

    glVertexPointer(3, GL_FLOAT, 0, 0); 
    glColorPointer(4, GL_FLOAT, 0, 16 * 256 * 16 * 8 * 3 * Float.SIZE); // Position of the colors in the currently bound buffer 

    glDrawElements(GL_QUADS, 24 * 16 * 256 * 16, GL_UNSIGNED_INT, 0); // Draws the elements from the Index Buffer 

} 


} 

答えて

2

私は質問が非常に具体的ではないとして、私は、すぐに解決策を提供できることをわからないんだけど、私はあなたの問題を解決するのに役立つかもしれないいくつかの一般的なアドバイスを提供することができます

  1. をチャンククラスは1つのvaoIDしか持たないようにしてください(チャンク全体を1回のドローコールでレンダリングする必要があります)。その1つのvaoIDに関連付けられた複数のvboIDを持つことができます。重要なのは、IntBuffersを使用してそれらを格納する必要はなく、それぞれのvboIDに明示的に名前を付けると、通常は物事をより整理した状態に保つことです。
  2. BufferUtilsを使用してFloatBufferオブジェクトを作成し、データをロードした後で、それらのすべてで.flip()を呼び出して、OpenGLで使用できる状態になっていることを確認する必要があります。

  3. プログラムの残りの部分の一般的な礼儀として、drawメソッドで有効にしてバインドされているVAOSのバインドを解除する必要があります。 (VAOがバインドされている間にバッファオブジェクトをバインドすると、VAOがバインドされているときだけそのバッファがバインドされていると思います。これをバックアップするためのドキュメントが見つからないため、安全です。あなたは彼らと終わった後)

    以下
  4. 私はあなたのチャンククラスの作業実施を与えるべきであると考えているものです:

    class Chunk { 
    
    private int vaoID; 
    private int vboID; 
    private int indexID; 
    
    
    public void createChunkVBO() { 
    
        FloatBuffer vertices = BufferUtils.createFloatBuffer(16 * 256 * 16 * 3 * 8); 
        FloatBuffer colors = BufferUtils.createFloatBuffer(16 * 256 * 16 * 4 * 8); 
        FloatBuffer indices = BufferUtils.createFloatBuffer(16 * 256 * 16 * 24); 
    
        // I am assuming that all of this is generated properly 
        for (int x = 0; x < 16; x++) { 
         for (int y = 0; y < 256; y++) { 
          for (int z = 0; z < 16; z++) { 
           System.out.println(x + ", " + y + ", " + z); 
           vertices.put(x + World.BLOCK_SIZE); 
           vertices.put(y); 
           vertices.put(z + World.BLOCK_SIZE); 
    
           vertices.put(x); 
           vertices.put(y); 
           vertices.put(z + World.BLOCK_SIZE); 
    
           vertices.put(x); 
           vertices.put(y); 
           vertices.put(z); 
    
           vertices.put(x + World.BLOCK_SIZE); 
           vertices.put(y); 
           vertices.put(z); 
    
           vertices.put(x + World.BLOCK_SIZE); 
           vertices.put(y + World.BLOCK_SIZE); 
           vertices.put(z + World.BLOCK_SIZE); 
    
           vertices.put(x); 
           vertices.put(y + World.BLOCK_SIZE); 
           vertices.put(z + World.BLOCK_SIZE); 
    
           vertices.put(x); 
           vertices.put(y + World.BLOCK_SIZE); 
           vertices.put(z); 
    
           vertices.put(x + World.BLOCK_SIZE); 
           vertices.put(y + World.BLOCK_SIZE); 
           vertices.put(z); 
    
    
           colors.put(1f); 
           colors.put(0f); 
           colors.put(0f); 
           colors.put(1f); 
    
           colors.put(1f); 
           colors.put(0f); 
           colors.put(0f); 
           colors.put(1f); 
    
           colors.put(1f); 
           colors.put(0f); 
           colors.put(0f); 
           colors.put(1f); 
    
           colors.put(1f); 
           colors.put(0f); 
           colors.put(0f); 
           colors.put(1f); 
    
           colors.put(1f); 
           colors.put(0f); 
           colors.put(0f); 
           colors.put(1f); 
    
           colors.put(1f); 
           colors.put(0f); 
           colors.put(0f); 
           colors.put(1f); 
    
           colors.put(1f); 
           colors.put(0f); 
           colors.put(0f); 
           colors.put(1f); 
    
           colors.put(1f); 
           colors.put(0f); 
           colors.put(0f); 
           colors.put(1f); 
    
    
           indices.put(0 + x * y * z); 
           indices.put(1 + x * y * z); 
           indices.put(2 + x * y * z); 
           indices.put(3 + x * y * z); 
    
    
           indices.put(4 + x * y * z); 
           indices.put(5 + x * y * z); 
           indices.put(2 + x * y * z); 
           indices.put(3 + x * y * z); 
    
    
           indices.put(1 + x * y * z); 
           indices.put(3 + x * y * z); 
           indices.put(7 + x * y * z); 
           indices.put(5 + x * y * z); 
    
    
           indices.put(0 + x * y * z); 
           indices.put(3 + x * y * z); 
           indices.put(4 + x * y * z); 
           indices.put(7 + x * y * z); 
    
    
           indices.put(0 + x * y * z); 
           indices.put(1 + x * y * z); 
           indices.put(6 + x * y * z); 
           indices.put(7 + x * y * z); 
    
    
           indices.put(4 + x * y * z); 
           indices.put(5 + x * y * z); 
           indices.put(6 + x * y * z); 
           indices.put(7 + x * y * z); 
    
          } 
         } 
        } 
    
        vertices.flip(); 
        colors.flip(); 
        indices.flip(); 
    
    
        glGenVertexArrays(vaoID); // Create an id for the VAO 
        glBindVertexArray(vaoID); // Bind the VAO so it remembers all the Attributes (none right now) 
    
        glGenBuffers(vboID); // Create an id for the VBO 
        glBindBuffer(GL_ARRAY_BUFFER, vboID); // Bind the VBO so we can put data into it 
    
        glBufferData(GL_ARRAY_BUFFER, 16 * 256 * 16 * 8 * 7 * Float.SIZE, GL_STATIC_DRAW); // We make an empty buffer with a specific size in bytes 
                        // 8 * 7 * sizeof(float) 
                        // 8 = number of vertices, 7 = xyzrgba 
        // I have not used subdata like this before so I will assume this is correct. 
        glBufferSubData(GL_ARRAY_BUFFER, 0, vertices); // Put the vertices at the beginning of the buffer 
        glBufferSubData(GL_ARRAY_BUFFER, 16 * 256 * 16 * 8 * 3 * Float.SIZE, colors); // Put the colors after the vertices 
    
    
        glGenBuffers(indexID); // Create an id for the Index Buffer 
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexID); // Bind the Index Buffer so we can put data into it 
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW); // Store the indices inside the currently bound Index Buffer 
    
    } 
    
    public void drawChunk() { 
    
        glEnableClientState(GL_VERTEX_ARRAY); // Enable the Vertex Array 
        glEnableClientState(GL_COLOR_ARRAY); // Enable the Color Array 
    
        glVertexPointer(3, GL_FLOAT, 0, 0); 
        glColorPointer(4, GL_FLOAT, 0, 16 * 256 * 16 * 8 * 3 * Float.SIZE); // Position of the colors in the currently bound buffer 
    
        glDrawElements(GL_QUADS, 8 * 16 * 256 * 24, GL_UNSIGNED_INT, 0); // Draws the elements from the Index Buffer 
    } 
    } 
    

ご質問があればお気軽にご連絡ください私が言ったことは何でも。

免責事項:私はOpenGLまたはLWJGLの専門家ではありません。これらの答えは私の教育的/個人的な経験からほぼ純粋に来るので、塩の穀物で私の答えを取ってください。

+1

あなたの素晴らしい答えをありがとう!私は実際にFloatBuffersを反転することを忘れてしまった。それを部分的に反転させると、問題が部分的に修正されます。私がglDrawArraysを使うとき、私は四辺形を正しくレンダリングします。しかし、インデックスを使用したいので、1キューブあたり2つのクワッドしかありません。 glDrawElementsは何らかの理由で動作していないようです。私の推測では、私は各キューブのインデックスを追加することを台無しにしています。すべての偉大なヒントをありがとう!あなたが私よりも一般的にOpenGLでもっと経験があるので、私がインデックスをどのように格納するかを見てみる可能性はありますか?とても感謝しております! –

+0

私はあなたのコメントを読んで、まもなくテストします。私は自宅のデスクトップでEclipseとJDKがインストールされている必要があります。私は簡単なテストを行うこともできます...私はあなたに戻って、私がそれを見たときに私の答えを編集します。 –

+0

私は仕事と学校の間でタイムリーにこれに答える時間がないことをお詫びしなければならない。インデックスが初期化された行とglDrawElementsの行の両方でインデックスバッファサイズを修正しました。これはあなたの問題を解決する可能性があります。もしそうでなければ、最初に小さな例から始まり、次にチャンクまであなたの方法を試してみることをお勧めします。例えば、キュ​​ーブで開始し、次にキューブのライン、次にキューブのグリッド、次いでキューブの塊を含む。 –

関連する問題