Hi.
I am trying to create a dynamic terrain using vertex buffer objects but I am having a lot of trouble creating the face indices.
The vertex grid is accurate because I used gl_Lines to see it but my function to create the triangle faces is obviously wrong as not a single face shows up.
Does anyone know how to dynamically face a grid of vertexes?
Here is some of my code, please do not judge, I am a beginner in OpenGL.
Function to create the vertex array:
void Terrain::CreateVertexArray( )
{
delete this->Vertices;
this->Vertices = new GLfloat[this->GridWidth * this->GridHeight * 4];
int count = 0;
for( int i = 0; i < this->GridHeight; i++ )
{
for( int j = 0; j < this->GridWidth; j++ )
{
int k = ( i * this->GridHeight ) + this->GridWidth;
this->Vertices[count++] = j;
this->Vertices[count++] = 0.0;
this->Vertices[count++] = i;
this->Vertices[count++] = 1.0;
}
}
}
and here is the face function:
void Terrain::CreateFaceIndices( )
{
delete this->Faces;
int NumberOfVertices = this->GridWidth * this->GridHeight;
int NumberOfTriangles = NumberOfVertices * 2;
this->Faces = new GLuint[NumberOfTriangles * 3];
int counter = 0;
for(int i = 0; i < NumberOfVertices; i++ )
{
this->Faces[counter++] = i;
this->Faces[counter++] = i + 1;
this->Faces[counter++] = i + this->GridWidth;
this->Faces[counter++] = i + 1;
this->Faces[counter++] = i + 1 + this->GridWidth;
this->Faces[counter++] = i + this->GridWidth;
}
}
and just in case someone asks for it, here is my render function:
void Terrain::Render( GUMatrix4 T )
{
int NumberOfVertices = this->GridWidth * this->GridHeight;
int NumberOfIndexPoints = NumberOfVertices * 3;
glUseProgram(Shader);
static GLint mvpLocation = glGetUniformLocation(Shader, "mvpMatrix");
glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, (const GLfloat*)&(T.M));
glBindVertexArray(VertexArrayObject);
glDrawElements(GL_TRIANGLES, sizeof(Faces), GL_UNSIGNED_SHORT, (const GLvoid*)0);
glBindVertexArray(0);
}
Thanks in advance if you can help out.