Advertisement Jump to content
Sign in to follow this  

Optimizing Heightmap

This topic is 4876 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

glBindTexture(GL_TEXTURE_2D, groundTexture);
	for (int z = 0; z < Size - 1; z++)
		for (int x = 0; x < Size; x++)
			// render two vertices of the strip at once
			float scaledHeight = heightmap[z * Size + x] / ScaleFactor;
			float nextScaledHeight = heightmap[(z + 1)* Size + x] / ScaleFactor;
			float color = 0.5f + 0.5f * scaledHeight / MaxHeight;
			float nextColor = 0.5f + 0.5f * nextScaledHeight / MaxHeight;

			glColor3f(color, color, color);
			glTexCoord2f((GLfloat)x/Size*8, (GLfloat)z/Size*8);
			glVertex3f(static_cast<GLfloat>(x - Size/2), scaledHeight, static_cast<GLfloat>(z - Size/2));

			glColor3f(nextColor, nextColor, nextColor);
			glTexCoord2f((GLfloat)x/Size*8, (GLfloat)(z+1)/Size*8);
			glVertex3f(static_cast<GLfloat>(x - Size/2), nextScaledHeight, static_cast<GLfloat>(z + 1 - Size/2));
This taking 15 milliseconds a frame!!! Far to long is there a way I can speed this up? Any help would be appreciated.

Share this post

Link to post
Share on other sites
Well, two things should help. Right now your code, a) is making a lot of uneeded calculations each frame and b) is setting all these data through AGP to the card.

You should run a preprocessing step when loading the heightmap, which calculates all needed data (ie. vertex positions / colors / texture coordinates), and store the results in an array. Then you should look up Vertex Arrays (VA) and Vertex Buffer Objects (VBO). Using immediate mode (glBegin / glEnd) means a lot of function calls that stall the pipeline - VAs and VBOs need just one function call.

Share this post

Link to post
Share on other sites
I actually begun putting the values in a vertex array just after I posted the original message. I know render using glDrawElements making it quite a bit quicker.

Although, if anyone does know how to find the size of a file without having to load it into a buffer just using from a FILE (_iobuf in stdio.h) type that would be appreciated.

Share this post

Link to post
Share on other sites
I'm not sure this is the best way to do it, but I've seen it done like this:

// Open the file
FILE *file = fopen("testfile", "rb");

// Get the length of the file
fseek(file, 0, SEEK_END);
long length = ftell(file);

That piece of code even puts the file pointer at the beginning of the file once it's finished.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!