Skip to main content
GameDev.net gamedev.net
🔒 Locked

VBO and heightmap

Started by muster May 11, 2009 at 3:10 AM 3 replies 2.1k views
Original Post
muster
muster
hey all. I want to load heightmap of type ".bmp" into my scene using vertex buffer object. The thing is from what i got from nehe tutorial the scene is loaded successfully but using gldrawarray. I want to load the scene as gldrawelements to improve performance more. The problem is how can i get the index array for each vertex loaded from heightmaps. I tried google but nothing, can someone help. tnx in advance...
Lord_Evil
Lord_Evil
Assuming each quad is build the same way, just put the vertices into your VBO, setting y to the heightmap value. Then build an index buffer with 6 x num_quads indices and fill them, for example by iterating over each quad, like this:

for(int x = 0; x < (width - 1); x++){ for(int y = 0; y < (height - 1); y++) {   //add index y * width + x   //add index y * width + (x + 1)   //add index (y + 1) * width + x   //add index (y + 1) * width + x   //add index y * width + (x + 1)   //add index (y + 1) * width + (x + 1) }}
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
muster
muster
tnx for the reply. actually i'm drawing triangles not quad and as u were saying its for quad. i'm working according to the sample of nehe leson 45: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=45

IMO the index should be implemented in this code but i don't know how. hope you can help.
for( nZ = 0; nZ < m_pTextureImage->sizeY; nZ += (int) flResolution )	{		for( nX = 0; nX < m_pTextureImage->sizeX; nX += (int) flResolution )		{			for( nTri = 0; nTri < 6; nTri++ )			{				// Using This Quick Hack, Figure The X,Z Position Of The Point				flX = (float) nX + ( ( nTri == 1 || nTri == 2 || nTri == 5 ) ? flResolution : 0.0f );				flZ = (float) nZ + ( ( nTri == 2 || nTri == 4 || nTri == 5 ) ? flResolution : 0.0f );				// Set The Data, Using PtHeight To Obtain The Y Value				m_pVertices[nIndex].x = flX - ( m_pTextureImage->sizeX / 2 );				m_pVertices[nIndex].y = PtHeight( (int) flX, (int) flZ ) *  flHeightScale;				m_pVertices[nIndex].z = flZ - ( m_pTextureImage->sizeY / 2 );				// Stretch The Texture Across The Entire Mesh				m_pTexCoords[nIndex].u = flX / m_pTextureImage->sizeX;				m_pTexCoords[nIndex].v = flZ / m_pTextureImage->sizeY;				// Increment Our Index				nIndex++;			}		}	}
Lord_Evil
Lord_Evil
Sorry for being a bit unclear. Saying "quad" I meant a quad made of two triangles, like so:
+---+---+|\  |\  || \ | \ ||  \|  \|+---+---+|\  |\  || \ | \ ||  \|  \|+---+---+

(4 quads)

You can use the code below to fill the VBO, just call the code inside the for( nTri = 0; nTri < 6; nTri++ ) loop only once. I'd also suggest incrementing nZ and nX by 1 (i.e. use flResolution = 1.0f), for sake of simplicity.

Then build the index buffer using the method I outlined above.

It might help to do some manual calculations on a piece of paper to get an understanding of what's going on [smile].
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
muster
muster
tnx alot, its clear now :).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.