1

我正在从 .bmp 文件生成地形,作为战略游戏的早期先驱。在我的代码中,我将 BMP 文件加载为 openGL 纹理,然后使用双循环生成坐标 (x, y redChannel)。然后我通过再次双循环并为 (x,y) 到 (x+1, y+1) 之间的正方形生成三角形来创建索引。但是,当我运行代码时,我最终会得到一个额外的三角形,从一行的末尾到下一行的开头,我似乎无法解决这个问题。这只发生在我使用不同的高度和足够大的地图时,或者至少它不可见。

这是代码:

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

似乎也有其中之一朝着另一个方向发展,在最右边,但我现在假设它可能与同一问题有关。

4

1 回答 1

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

这些可能也值得一看。您可以为许多程序对象使用条带原语保存相当多的数据:

于 2013-10-23T15:18:13.583 回答