2014-10-17 38 views
1

所以我組建了一個高度貼圖渲染,將完成大部分的工作詮釋他的頂點着色器,但首先我當然生成網格渲染,此刻我正在玩圍繞着openGL和C++的上限來看看我可以渲染多少密度的網格(所以我稍後在LoD網格劃分方面有了一些進展)C++ OpenGL的大網眼缺少三角形

ANYWAY!切入問題;

在我測試了一個32位,64位和128位的meshResolution之後,我注意到了這個問題我遇到了運行時崩潰,我使用自制類「indexFace」來阻止它們,它包含6個索引以降低數組長度,問題在於128分辨率只有網格的第三個實際上顯示,我在想,如果有OpenGL的能有多少指標呈現或持有使用1套BufferObjects,或者如果它的一個問題,我處理事情的C++方面的限制。

我通過以下生成網格:

void HeightMapMesh::GenerateMesh(GLfloat meshScale, GLushort meshResolution) 
{ 
    GLushort vertexCount = (meshResolution + 1) * (meshResolution + 1); 
    Vertex_Texture* vertexData = new Vertex_Texture[vertexCount]; 
    GLushort indexCount = (meshResolution * meshResolution) * 6; 

//indexFace holds 6 GLushort's in an attempt to overcome the array size limit 
    indexFace* indexData = new indexFace[meshResolution * meshResolution]; 
    GLfloat scalar = meshScale/((GLfloat)meshResolution); 
    GLfloat posX = 0; 
    GLfloat posY = 0; 
    for (int x = 0; x <= meshResolution; x++) 
    { 
     posX = ((GLfloat)x) * scalar; 
     for (int y = 0; y <= meshResolution; y++) 
     { 
      posY = ((GLfloat)y) * scalar; 
      vertexData[y + (x * (meshResolution + 1))] = Vertex_Texture(posX, posY, 0.0f, x, y); 
     } 
    } 
    GLint indexPosition; 
    GLint TL, TR, BL, BR; 
    for (int x = 0; x < meshResolution; x++) 
    { 
     for (int y = 0; y < meshResolution; y++) 
     { 
      indexPosition = (y + (x * (meshResolution))); 
      BL = y + (x * (meshResolution + 1)); 
      TL = y + 1 + (x * (meshResolution + 1)); 
      BR = y + ((x + 1) * (meshResolution + 1)); 
      TR = y + 1 + ((x + 1) * (meshResolution + 1)); 
      indexData[indexPosition] = indexFace(
        BL, TR, TL, 
        BL, BR, TR 
        ); 
     } 
    } 
    mesh.Fill(vertexData, vertexCount, (void *)indexData, indexCount, GL_STATIC_DRAW, GL_STATIC_DRAW); 
    delete [] vertexData; 
    delete [] indexData; 
} 

//This is for mesh.Fill() 
    void Fill(T* vertData, GLushort vertCount, void* indData, GLushort indCount, GLenum vertUsage, GLenum indUsage) 
    { 
     indexCount = indCount; 
     vertexCount = vertCount; 
     glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectID); 
     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObjectID); 
     glBufferData(GL_ARRAY_BUFFER, sizeof(T) * vertexCount, vertData, vertUsage); 
     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * indexCount, indData, indUsage); 
    } 

回答

1

它,因爲你做了索引的短褲。

例如:GLushort indexCount = (meshResolution * meshResolution) * 6;meshResolution的值105達到USHRT_MAX。 (105 * 105 * 6 = 66150> 65535)

使用整數作爲指標。所以處處改變你的指數爲無符號整數,並做最後的繪製調用是這樣的:

glDrawElements(GL_QUADS, indCount, GL_UNSIGNED_INT, indices); //do this 
//glDrawElements(GL_QUADS, indCount, GL_UNSIGNED_SHORT, indices); //instead of this 
//also GL_QUADS is deprecated but it seems your data is in that format so I left it that way 

您可以節省一堆指標,如果你畫了GL_TRIANGLE_STRIP!而非或更好,但這樣做對GPU鑲嵌,因爲這是像它的完美用例。

+1

小幅回調,而USHORT和uint是我的問題,那是因爲我使用USHORT來繞過索引計數,頂點還是很好的USHORT範圍內,因此是安全的,以保持指數爲ushorts,感謝雖然由於某種原因,我很困惑短褲的限制與整數和長整數。 GL_TRIANGLE_STRIP我會仰視它的順序總是讓我感到困惑,當你有多個行,但研究熟能生巧我猜想,再次感謝。 – 2014-10-17 13:41:36