2017-06-16 6 views
-1

私のボクセルエンジンのチャンクは、何らかの奇妙な理由で描画されません。私はRenderDocでデバッグしましたが、すべてうまく見えます。私は自分の機能をどのようにセットアップするかによって、何かが間違っていると思っています。私のVoxelエンジンでglDrawArraysが動作しません

void Chunk::createMesh() // called once, right before the render loop 
{ 
    int i = 0; 
    this->shader.loadShader("chunk.vs", "chunk.fs"); 

    for (int x = 0; x < CHUNK_SIZE; x++) 
    { 
     for (int y = 0; y < CHUNK_HEIGHT; y++) 
     { 
      for (int z = 0; z < CHUNK_SIZE; z++) 
      { 
       GLuint currentBlock = this->blockCur[x][y][z]; 

       this->vertex[i++] = byte4(x, y, z, currentBlock); 
       this->vertex[i++] = byte4(x, y, z + 1, currentBlock); 
       this->vertex[i++] = byte4(x, y + 1, z, currentBlock); 
       this->vertex[i++] = byte4(x, y + 1, z, currentBlock); 
       this->vertex[i++] = byte4(x, y, z + 1, currentBlock); 
       this->vertex[i++] = byte4(x, y + 1, z + 1, currentBlock); 

       // View from positive x 
       this->vertex[i++] = byte4(x + 1, y, z, currentBlock); 
       this->vertex[i++] = byte4(x + 1, y + 1, z, currentBlock); 
       this->vertex[i++] = byte4(x + 1, y, z + 1, currentBlock); 
       this->vertex[i++] = byte4(x + 1, y + 1, z, currentBlock); 
       this->vertex[i++] = byte4(x + 1, y + 1, z + 1, currentBlock); 
       this->vertex[i++] = byte4(x + 1, y, z + 1, currentBlock); 

       currentBlock++; 
      } 
     } 
    } 

    this->elements = i; 

    glGenVertexArrays(1, &this->vao); 

    glBindVertexArray(this->vao); 
    glGenBuffers(1, &this->vbo); 
    glBindBuffer(GL_ARRAY_BUFFER, vbo); 
    glBufferData(GL_ARRAY_BUFFER, this->elements * sizeof(this->vertex), this->vertex, GL_STATIC_DRAW); 

    glGenBuffers(1, &cubeColor); 
    glBindBuffer(GL_ARRAY_BUFFER, cubeColor); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(color), color, GL_STATIC_DRAW); 


    this->attribCoord3dChunk = glGetAttribLocation(this->shader.program, "coord3d"); 
    this->attribMVPChunk = glGetUniformLocation(this->shader.program, "mvp"); 
    this->attribColor = glGetUniformLocation(this->shader.program, "f_color"); 

    if (this->attribCoord3dChunk < 0) 
    { 
     std::cout << "attribCoord3dChunk was negative!" << std::endl; 
    } 

    std::cout << this->attribMVPChunk << std::endl; 

    glBindBuffer(GL_ARRAY_BUFFER, this->vbo); 
    glVertexAttribPointer(
     this->attribCoord3dChunk, // attribute 
     3,     // number of elements per vertex, here (R,G,B) 
     GL_FLOAT,   // the type of each element 
     GL_FALSE,   // take our values as-is 
     0,     // no extra data between each position 
     (GLvoid*)0     // offset of first element 
    ); 
    glEnableVertexAttribArray(this->attribCoord3dChunk); 

    glBindBuffer(GL_ARRAY_BUFFER, this->chunkColor); 
    glVertexAttribPointer(
     this->attribColor, // attribute 
     3,     // number of elements per vertex, here (x,y,z) 
     GL_FLOAT,   // the type of each element 
     GL_FALSE,   // take our values as-is 
     0,     // no extra data between each position 
     (GLvoid*)0     // offset of first element 
    ); 
    glEnableVertexAttribArray(this->attribColor); 

    glBindVertexArray(0); 

    this->loaded = true; 
} 

void Chunk::render() // called every frame 
{ 
    glBindVertexArray(this->vao); 
    glEnableVertexAttribArray(this->attribCoord3dChunk); 
    model = glm::translate(glm::mat4(1.0f), glm::vec3(1, 1, 1)); 
    glm::mat4 mvp = gameManager->projection * gameManager->view * model; 
    glUniformMatrix4fv(this->attribMVPChunk, 1, GL_FALSE, glm::value_ptr(mvp)); 

    glBindBuffer(GL_ARRAY_BUFFER, this->vbo); 
    glDrawArrays(GL_TRIANGLES, 0, this->elements); 
    glDisableVertexAttribArray(this->attribCoord3dChunk); 
    glBindVertexArray(0); 
} 

byte4のtypedefのは、私はレンダリングループの前に、一度createMeshを呼び出す

typedef glm::tvec4<GLbyte> byte4; 

次のとおりです。ここで

はコードです。 また、すべてのフレームをレンダリングします。

答えて

-1

固定!現在のコード:

void Chunk::genChunk() 
{ 
    int i = 0; 
    this->shader.loadShader("chunk.vs", "chunk.fs"); 

    for (int x = 0; x < CHUNK_SIZE; x++) 
    { 
     for (int y = 0; y < CHUNK_HEIGHT; y++) 
     { 
      for (int z = 0; z < CHUNK_SIZE; z++) 
      { 
       vertices.push_back(x); 
       vertices.push_back(y); 
       vertices.push_back(z); 
       vertices.push_back(x); 
       vertices.push_back(y); 
       vertices.push_back(z + 1); 
       vertices.push_back(x); 
       vertices.push_back(y + 1); 
       vertices.push_back(z + 1); 
       vertices.push_back(x); 
       vertices.push_back(y + 1); 
       vertices.push_back(z); 

       vertices.push_back(x + 1); 
       vertices.push_back(y); 
       vertices.push_back(z); 
       vertices.push_back(x + 1); 
       vertices.push_back(y); 
       vertices.push_back(z + 1); 
       vertices.push_back(x + 1); 
       vertices.push_back(y + 1); 
       vertices.push_back(z + 1); 
       vertices.push_back(x + 1); 
       vertices.push_back(y + 1); 
       vertices.push_back(z); 

       vertices.push_back(x); 
       vertices.push_back(y + 1); 
       vertices.push_back(z); 
       vertices.push_back(x + 1); 
       vertices.push_back(y + 1); 
       vertices.push_back(z); 
       vertices.push_back(x + 1); 
       vertices.push_back(y + 1); 
       vertices.push_back(z + 1); 
       vertices.push_back(x); 
       vertices.push_back(y + 1); 
       vertices.push_back(z + 1); 

       vertices.push_back(x); 
       vertices.push_back(y); 
       vertices.push_back(z); 
       vertices.push_back(x + 1); 
       vertices.push_back(y); 
       vertices.push_back(z); 
       vertices.push_back(x + 1); 
       vertices.push_back(y); 
       vertices.push_back(z + 1); 
       vertices.push_back(x); 
       vertices.push_back(y); 
       vertices.push_back(z + 1); 

       vertices.push_back(x); 
       vertices.push_back(y); 
       vertices.push_back(z + 1); 
       vertices.push_back(x + 1); 
       vertices.push_back(y); 
       vertices.push_back(z + 1); 
       vertices.push_back(x + 1); 
       vertices.push_back(y + 1); 
       vertices.push_back(z + 1); 
       vertices.push_back(x); 
       vertices.push_back(y + 1); 
       vertices.push_back(z + 1); 

       vertices.push_back(x); 
       vertices.push_back(y); 
       vertices.push_back(z); 
       vertices.push_back(x + 1); 
       vertices.push_back(y); 
       vertices.push_back(z); 
       vertices.push_back(x + 1); 
       vertices.push_back(y + 1); 
       vertices.push_back(z); 
       vertices.push_back(x); 
       vertices.push_back(y + 1); 
       vertices.push_back(z); 
      } 
     } 
    } 

    this->elements = i; 

    glGenVertexArrays(1, &this->vao); 

    glBindVertexArray(this->vao); 
    glGenBuffers(1, &this->vbo); 
    glBindBuffer(GL_ARRAY_BUFFER, vbo); 
    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices.front(), GL_STATIC_DRAW); 

    glGenBuffers(1, &this->chunkColor); 
    glBindBuffer(GL_ARRAY_BUFFER, this->chunkColor); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(color), color, GL_STATIC_DRAW); 

    this->attribCoord3dChunk = glGetAttribLocation(this->shader.program, "coord3d"); 
    this->attribMVPChunk = glGetUniformLocation(this->shader.program, "mvp"); 
    this->attribColor = glGetAttribLocation(this->shader.program, "v_color"); 

    if (this->attribCoord3dChunk < 0) 
    { 
     std::cout << "attribCoord3dChunk was negative!" << std::endl; 
    } 
    else if (this->attribColor < 0) 
    { 
     std::cout << "v_color was negative!" << std::endl; 
    } 

    glBindBuffer(GL_ARRAY_BUFFER, this->vbo); 
    glVertexAttribPointer(
     this->attribCoord3dChunk, // attribute 
     3,     // number of elements per vertex, here (R,G,B) 
     GL_FLOAT,   // the currentBlock of each element 
     GL_FALSE,   // take our values as-is 
     0,     // no extra data between each position 
     (GLvoid*)0     // offset of first element 
    ); 

    glBindBuffer(GL_ARRAY_BUFFER, this->chunkColor); 
    glVertexAttribPointer(
     this->attribColor, // attribute 
     3,     // number of elements per vertex, here (x,y,z) 
     GL_FLOAT,   // the currentBlock of each element 
     GL_FALSE,   // take our values as-is 
     0,     // no extra data between each position 
     (GLvoid*)0     // offset of first element 
    ); 

    glBindVertexArray(0); 

    this->loaded = true; 
} 

void Chunk::render() 
{ 
    glBindVertexArray(this->vao); 
    glEnableVertexAttribArray(this->attribCoord3dChunk); 
    glEnableVertexAttribArray(this->attribColor); 

    model = glm::translate(glm::mat4(1.0f), glm::vec3(1, 1, 1)); 
    glm::mat4 mvp = gameManager->projection * gameManager->view * model; 
    glUniformMatrix4fv(this->attribMVPChunk, 1, GL_FALSE, glm::value_ptr(mvp)); 

    glBindBuffer(GL_ARRAY_BUFFER, this->vbo); 
    glDrawArrays(GL_QUADS, 0, this->vertices.size()); 

    glDisableVertexAttribArray(this->attribCoord3dChunk); 
    glDisableVertexAttribArray(this->attribColor); 
    glBindVertexArray(0); 
} 
+2

今後の読者には、エラーの内容と修正のためにどのような変更を加えたのかを指摘できれば便利です。 – HolyBlackCat

関連する問題