1

我正在寻求使用三角形带绘制平坦地形的帮助。如果这是正确的方法吗?

到目前为止,我设法创建了顶点和索引数组,对其进行了几次调试,它似乎生成了正确的值。

起初我尝试用退化的三角形绘制,但我在某处读到这是一种过时的方式,我宁愿切换到原始的重启选项。

请注意,这不是我的项目设置的确切方式......我只是复制了必要的代码

这是地形生成的代码:

this->terrainSize = width * height;
unsigned int numRows = height - 1;                        //!NUMBER OF ROWS OF TRIANGLE STRIPS
unsigned int numColumns = width - 1;                      //!Number of columns of strips
unsigned int numVerticiesPerRow = 2 * width;              //!NUMBER OF VERTICIES PER EVERY ROW
unsigned int numIndPerRow = numColumns * 2 + 2;           //!Number of indices per row
unsigned int numOfStrips = numRows - 1;                   //!Number of splits, after every row


this->numVertices = terrainSize * 8;                        
this->numIndices = numRows * numIndPerRow + numOfStrips;   //!total number of indices


std::vector<float> vertexArray (numVertices);
float* vertexPointer = vertexArray.data();            
float* vertexPointer2 = vertexArray.data();
for( int row = 0; row < height; row++ )              //!Z AXIS
{
    for( int col = 0; col < width; col++ )           //!X AXIS
    {
        float x = (float) ( col - width / 2 );       
        float z = (float) ( row - height / 2 );

        //?Positions
        *vertexPointer = x; ++vertexPointer;           //?1
        *vertexPointer = 0.0f; ++vertexPointer;       //?2
        *vertexPointer = z; ++vertexPointer;          //?3

        //?Normals (lightning)
        *vertexPointer = 0.0f; ++vertexPointer;        //?4 
        *vertexPointer = 1.0f; ++vertexPointer;        //?5 
        *vertexPointer = 0.0f; ++vertexPointer;       //?6

        //?Texture Coordinates
        *vertexPointer = x; ++vertexPointer;           //?7
        *vertexPointer = z; ++vertexPointer;          //?8
        }
    }

//?GENERATE INDICIES
std::vector<unsigned int> indexArray (numIndices);
unsigned int* indexPointer = indexArray.data();
unsigned int* indexPointer2 = indexArray.data();
for( int row = 0; row < height - 1; row++ )   //!Z AXIS
{
    for( int col = 0; col < width; col++ )   //!X AXIS
    {
        *indexPointer = ( row * height ) + col; ++indexPointer;       
        *indexPointer = (( row + 1 )* height ) + col; ++indexPointer;

    }
    if( row < height - 2 )                   **//!Add 0xFFFF on the end of each row** 
    {
        *indexPointer = 0xFFFF; ++indexPointer;             
    }
}
//Pack the data into vertex struct
unsigned int i;
for( i = 0; i < this->numVertices; i=i+0 )
{
    Vertex vertex;
    glm::vec3 vector;

    vector.x = vertexArray[i++];
    vector.y = vertexArray[i++];
    vector.z = vertexArray[i++];
    vertex.Position = vector; 

    vector.x = vertexArray[i++];
    vector.y = vertexArray[i++];
    vector.z = vertexArray[i++];
    vertex.Normal = vector;

    glm::vec2 vec;

    vec.x = vertexArray[i++];
    vec.y = vertexArray[i++];
    vertex.TexCords = vec;

    this->vertexStruct.push_back( vertex );
}
for( i = 0; i < this->numIndices; i++ )
{
    this->indexVector.push_back( indexArray[i] );
}

//Bind VertexArray
VAO.Create();

//Bind VertexBuffer
VBO.Create( vertexArray, numVertices);

//Bind IndexBuffer
IBO.Create( indexArray, numIndices );

//? DEFINE HOW SHOULD THE GPU SHOULD READ THE DATA FROM VERTEX BUFFER
//Position
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, sizeof( Vertex ), (GLvoid*) offsetof( Vertex, Position ) );
glEnableVertexAttribArray( 0 );
//Normal
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, sizeof( Vertex ), (GLvoid*) offsetof( Vertex, Normal ) );
glEnableVertexAttribArray( 1 );
//Texcoord
glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE, sizeof( Vertex ), (GLvoid*) offsetof( Vertex, TexCords ) );
glEnableVertexAttribArray( 2 );
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); //?ENABLE WIREFRAME
glEnable( GL_PRIMITIVE_RESTART );
glPrimitiveRestartIndex( 0xFFFF );
glDrawElements( GL_TRIANGLE_STRIP, this->numVertices, GL_UNSIGNED_INT, this->indexArray );
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); //?DISABLE WIREFRAME
4

0 回答 0