Help triangulating my Vertex array.

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.


	for (int y = 0; y < this->GridHeight-1; y++) {
		for (int x = 0; x < this->GridWidth-1; x++) {
			int i0 = y * this->GridWidth + x;
			int i1 = i0 + this->GridWidth;
			this->Faces[counter++] = i0;
			this->Faces[counter++] = i0 + 1;
			this->Faces[counter++] = i1 + 1;
			this->Faces[counter++] = i1 + 1;
			this->Faces[counter++] = i1;
			this->Faces[counter++] = i0;
		}
	}

Should be:


	int NumberOfTriangles = (this->GridWidth-1) * (this->GridHeight-1) * 2;

The second argument should be the number of indices, not the number of bytes.

Beyond that, if you aren’t getting anything, I’d check that the VAO has been set up correctly, e.g. the relevant arrays have been defined and enabled.

Hi GClements.

Thank you for the reply.
I figured out the:

int NumberOfTriangles = (this->GridWidth-1) * (this->GridHeight-1) * 2;

after putting all the numbers into excel and finding a pattern. (Wish I had just check back here first).

I tried your pattern for setting up the face indices but it still will not render the grid. I am stumped.
I am sure that everything is set correctly, because I made debugging render function that uses glBegin(GL_LINES) to render the grid that way and it looks fine.
I have rendered it vert to vert to make sure they were fine, then I made one to render vert to vert using the indices buffer as a reference to the verts and it is all triangulating properly.

I have checked my render function against a template one for a cube and that looks fine too.

I am lost with it, but thank for the reply anyway.