2017-12-05 15 views
1

OpenGL에서 VAO 및 삼각형 스트립을 사용하여 메쉬 그리드를 렌더링하는 데 문제가 있습니다.삼각형 스트립 렌더링을 사용하는 메쉬 격자가 잘못 렌더링 되었습니까?

여기 정점과 인덱스를 생성하여 각각의 버퍼에 저장하는 코드입니다.

다음
void HeightField::submitTriangles(void){ 
if (m_vao == UINT32_MAX) { 
    std::cout << "No vertex array is generated, cannot draw anything.\n"; 
    return; 
} 

glEnable(GL_PRIMITIVE_RESTART); 
glPrimitiveRestartIndex(9999); 
glBindVertexArray(m_vao); 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer); 
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
glDrawElements(GL_TRIANGLE_STRIP, m_numIndices, GL_UNSIGNED_INT, 0); 
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 
//glDisable(GL_PRIMITIVE_RESTART); } 

그것이 16 × 16 그리드를 렌더링하려고 어떻게 생겼는지의 스크린 샷 :

void HeightField::generateMesh(int tesselation) { 
// generate a mesh in range -1 to 1 in x and z 
int verticesPerRow = sqrt(tesselation/2) + 1; 
float* verts = NULL; 
verts = new float[verticesPerRow*verticesPerRow*3]; 

//Generate Vertices 
float z = 1; 
int idx = 0; 
for(int j = 0; j < verticesPerRow; j++) { 
    float x = -1; 
    for (int k = 0; k < verticesPerRow; k++) { 
     verts[idx++] = x; 
     verts[idx++] = 0; 
     verts[idx++] = z; 
     x += (2.0f/float(verticesPerRow-1)); 
    } 
    z -= (2.0f/float(verticesPerRow-1)); 
} 

printf("\n VERTICES \n"); 
for (int o = 0; o < verticesPerRow*verticesPerRow*3; o++) { 
    printf("%f, ", verts[o]); 
    if (o % 3 == 2) { 
     printf("\n"); 
    } 
} 


//Generate indices 

int rows = sqrt(tesselation/2); 
int columns = rows+1; 
int* indices = NULL; 
int indicesAmount = 2 * rows*columns + rows; 
indices = new int[indicesAmount]; 
idx = 0; 
for (int r = 0; r < rows; r++) { 
    for (int c = 0; c < columns; c++) { 

     indices[idx++] = (r + 1) * columns + c; 
     indices[idx++] = r * columns + c; 
     //printf("%i, %i, ", indices[idx - 2], indices[idx - 1]); 
     if (r == rows - 1) { 
      indices[idx] = (r + 1) * columns + c - 1; 
     } 
    } 

    if (r < rows - 1) { 
     indices[idx++] = 9999; 
     //printf(", %i, ", indices[-1]); 
    } 
} 

for (int o = 0; o < indicesAmount; o++) { 
    printf("%i, ", indices[o]); 
    if (o % 3 == 2) { 
     printf("\n"); 
    } 
} 

m_numIndices = indicesAmount; 

printf("Vertices Per Row: %i \n", verticesPerRow); 
printf("Number of indices: %i \n", m_numIndices); 

glGenVertexArrays(1, &m_vao); 
glBindVertexArray(m_vao); 

glGenBuffers(1, &m_positionBuffer);             // Create a handle for the vertex position buffer 
glBindBuffer(GL_ARRAY_BUFFER, m_positionBuffer);         // Set the newly created buffer as the current one 
glBufferData(GL_ARRAY_BUFFER, verticesPerRow*verticesPerRow * 3, verts, GL_STATIC_DRAW);  // Send the vetex position data to the current buffer 
glVertexAttribPointer(0, 3, GL_FLOAT, false/*normalized*/, 0/*stride*/, 0/*offset*/); 
glEnableVertexAttribArray(0); 

glGenBuffers(1, &m_uvBuffer);             // Create a handle for the vertex position buffer 
glBindBuffer(GL_ARRAY_BUFFER, m_uvBuffer);         // Set the newly created buffer as the current one 
glBufferData(GL_ARRAY_BUFFER, verticesPerRow*verticesPerRow * 2, texCoords, GL_STATIC_DRAW);  // Send the vetex position data to the current buffer 
//glVertexAttribPointer(2, 2, GL_FLOAT, false/*normalized*/, 0/*stride*/, 0/*offset*/); 
//glEnableVertexAttribArray(2); 

glGenBuffers(1, &m_indexBuffer);             // Create a handle for the vertex position buffer 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer);         // Set the newly created buffer as the current one 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_numIndices, indices, GL_STATIC_DRAW);  // Send the vetex position data to the current buffer} 

는 여기를 렌더링하는 내 코드입니다. 나는 행과 내가 렌더링하려고 열 수를 늘리면

enter image description here

, 나는 더 많은 행과 열이 나는 그들이 원하는 방식을 찾고 얻을 수 있지만, 전체 그림의 "윤곽"동일 보이는 당신이 그림에서 보는 모양과 같은 다이아몬드를 가졌습니다.

답변

0

문제 Appearently는 바이트 크기, 그리고 어레이의 길이 (바보 ME) 예상

glBufferData(GL_ARRAY_BUFFER, verticesPerRow*verticesPerRow * 3, verts, GL_STATIC_DRAW); 

로했다. 상기 라인 변경

다른 버퍼의

glBufferData(GL_ARRAY_BUFFER, verticesPerRow*verticesPerRow * 3 * sizeof(float), verts, GL_STATIC_DRAW);

와 마찬가지로 문제를 해결.