Terrain Rendering Using Vertex Array.

I need to ask someone on this forum to please help me out. I read the red book pdf file, particular interest - vertex arrays, and i did not understand it well, or no… did not understand it at all. I want to render a terrain, automaticaly, no heightmap, by constructing a vertex array… and through a loop or something - fill it in with triangles. And the indecies for the vertices in this array of triangles, to be able to later compare to selection.

Can someone help me do it? But if they have time, to rather tutor me in this thread on how i would do it or point me to a tut, coz i just cant get these vertex arrays, yet i dig the possibilities.

Oh yeah, the height values dont really matter right now, they will be changed by the user.



Ok, then. Can someone post a code for a flat, rendered terrain (height value is 0, composed of triangles) using vertex arrays please? I really want to… need to know how its done. Please! I dont think its a lot. Is it? :frowning:



EDIT: I just cant figure out how to use vertex arrays, and what would the loop look like, to do it all automaticaly (rendering)

Here’s a simple snippet that initializes the positions and indices of a terrain triangle mesh. This is intended to be informative, not normative :stuck_out_tongue: .

// Size of a single quad side in world space.
const int grid = 64;
// Simple square mesh of terrian vertices.
const int width = 256 + 1; 
Vector3 verts[width * width];
// 3 indices for each of the 2 triangles 
// in each quad.
const int numIndices = (width-1)*(width-1)*2*3;
unsigned int indices[numIndices];
// Build the verts and triangle index list.
Vector3*      v     = verts;
unsigned int* index = indices;
for( int y = 0; y < width; y++ )
    for( int x = 0; x < width; x++, v++ )
        // Set the position in world.	
	v->x = (x - width / 2) * grid;
	v->y = (y - width / 2) * grid;
        v->z = 0; 
	// Create indices for this quad.
	if( y < width - 1 && x < width - 1 )
            // CCW winding for triangles.
            unsigned int ofs = v - verts;
	    // Triangle 1.
            *index++ = ofs;
	    *index++ = ofs + 1;
	    *index++ = ofs + width;
            // Triangle 2.
	    *index++ = ofs + 1;
	    *index++ = ofs + width + 1;
	    *index++ = ofs + width;
// Later on in some draw function...
// Give the GL a pointer to our vertex data.
glVertexPointer( 3, GL_FLOAT, sizeof(Vector3), verts );
// Enable vertex arrays.
glEnableClientState( GL_VERTEX_ARRAY );
// Draw the triangles using our indices.
glDrawElements( GL_TRIANGLES, numIndices,  GL_UNSIGNED_INT, indices );
// Disable vertex arrays.
glDisableClientState( GL_VERTEX_ARRAY );

Thanks! This will help me a lot in my future research! Thanq.


Do I gain anything by calling glDisableClientState( GL_VERTEX_ARRAY ) after I am done with the drawing?

Do I gain anything by calling glDisableClientState( GL_VERTEX_ARRAY ) after I am done with the drawing?
Just the statisfaction of knowing it’s disabled.