Jump to content
  • Advertisement
Sign in to follow this  
BoReDoM_Inc

Optimizing Heightmap

This topic is 4814 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);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	for (int z = 0; z < Size - 1; z++)
	{
		glBegin(GL_TRIANGLE_STRIP);
		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));
		}
		glEnd();
	}
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
Advertisement
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
Thanks,
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);
rewind(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 GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!