2013-10-23 43 views
0

我從.bmp文件生成地形,作爲戰略遊戲的早期前體。在我的代碼中,我將BMP文件作爲openGL紋理加載,然後使用雙循環來生成座標(x,y redChannel)。然後,我再次通過雙循環創建索引,併爲(x,y)到(x + 1,y + 1)之間的平方生成三角形。但是,當我運行代碼時,最終會出現從一行尾到下一行開頭的額外三角形,而我似乎無法解決這個問題。這隻會發生在我使用不同的高度和足夠大的地圖時,或者至少在其他情況下不可見。openGL drawElements - 一個額外的三角形,使用索引數組?

這是代碼:

void Map::setupVertices(GLsizei* &sizeP, GLint * &vertexArray, GLubyte* &colorArray){ 

    //textureNum is the identifier generated by glGenTextures 
    GLuint textureNum = loadMap("heightmap.bmp"); 

    //Bind the texture again, and extract the needed data 
    glBindTexture(GL_TEXTURE_2D, textureNum); 
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width); 
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height); 
    GLint i = height*width; 
    GLubyte * imageData = new GLubyte[i+1]; 
    glGetTexImage(GL_TEXTURE_2D,0,GL_RED, GL_UNSIGNED_BYTE, &imageData[0]); 

    //Setup varibles: counter (used for counting vertices) 
    //VertexArray: pointer to address for storing the vertices. Size: 3 ints per point, width*height points total 
    //ColorArray: pointer to address for storing the color data. 3 bytes per point. 
    int counter = 0; 
    vertexArray = new GLint[height*width*3]; 
    colorArray = new GLubyte[height*width*3]; 

    srand(time(NULL)); 
    //Loop through rows 
    for (int y = 0; y < height; y++){ 
     //Loop along the line 
     for (int x=0; x < width; x++){ 
      //Add vertices: x, y, redChannel 
      //Add colordata: the common-color. 
      colorArray[counter] = imageData[x+y*width]; 
      vertexArray[counter++] = x; 
      colorArray[counter] = imageData[x+y*width]; 
      vertexArray[counter++] = y; 
      colorArray[counter] = imageData[x+y*width];//(float) (rand() % 255); 
      vertexArray[counter++] = (float)imageData[x+y*width] /255 * maxHeight; 
     } 
    } 
    //"Return" total vertice amount 
    sizeP = new GLsizei(counter); 

} 

void Map::setupIndices(GLsizei* &sizeP, GLuint* &indexArray){ 
    //Pointer to location for storing indices. Size: 2 triangles per square, 3 points per triangle, width*height triangles 
    indexArray = new GLuint[width*height*2*3]; 
    int counter = 0; 
    //Loop through rows, don't go to top row (because those triangles are to the row below) 
    for (int y = 0; y < height-1; y++){ 
     //Loop along the line, don't go to last point (those are connected to second last point) 
     for (int x=0; x < width-1; x++){ 
      // 
      // TL___TR 
      // |/| 
      // LL___LR 
      int lowerLeft = x + width*y; 
      int lowerRight = lowerLeft+1; 
      int topLeft = lowerLeft + width+1; 
      int topRight = topLeft + 1; 

      indexArray[counter++] = lowerLeft; 
      indexArray[counter++] = lowerRight; 
      indexArray[counter++] = topLeft; 

      indexArray[counter++] = topLeft; 
      indexArray[counter++] = lowerRight; 
      indexArray[counter++] = topRight; 
     } 
    } 
    //"Return" the amount of indices 
    sizeP = new GLsizei(counter); 
} 

我最終與此代碼得出這樣的:

void drawGL(){ 
    glPushMatrix(); 
    glEnableClientState(GL_VERTEX_ARRAY); 
    glVertexPointer(3,GL_INT,0,mapHeight); 
    glEnableClientState(GL_COLOR_ARRAY); 
    glColorPointer(3,GL_UNSIGNED_BYTE,0,mapcolor); 
    if (totalIndices != 0x00000000){ 
     glDrawElements(GL_TRIANGLES, *totalIndices, GL_UNSIGNED_INT, indices); 
    glDisableClientState(GL_VERTEX_ARRAY); 
    glDisableClientState(GL_COLOR_ARRAY); 
    glPopMatrix(); 
} 

這裏的結果的畫面:

http://s22.postimg.org/k2qoru3kx/open_GLtriangles.gif

而且只藍線和黑色背景。 http://s21.postimg.org/5yw8sz5mv/triangle_Error_Blue_Line.gif

似乎也有其中一個方向朝着另一個方向發展,在最右邊,但我現在假設它可能與同一個問題有關。

+0

INT左上= lowerLeft +寬度+ 1;這個+1是否正確? – Sarien

+0

D'oh!我一定看過那一百遍而沒有注意到。非常感謝! –

回答

0

我會簡化這一部分:

  int lowerLeft = x + width * y; 
     int lowerRight = (x + 1) + width * y; 
     int topLeft = x + width * (y + 1); 
     int topRight = (x + 1) + width * (y + 1); 

問題看起來像topLeft有一個額外的+ 1時,它應該只擁有+ width。 這會導致「頂」頂點都沿着一列移動。您可能沒有注意到網格內的偏移量,正如您所指出的,它們在高度改變之前不可見。

此外,返回new GLsizei(counter)似乎有點圍繞。爲什麼不通過GLsizei& counter

這些也許值得一看。可以節省使用條原語許多程序對象的數據的一個公平的位:

+0

添加了這些建議!非常感謝! –