Skip to main content
gamedev.net
🔒 Locked

Optimizing Heightmap

Started by BoReDoM_Inc Sep 11, 2005 at 1:38 AM 4 replies 1.4k views
Original Post
BoReDoM_Inc
BoReDoM_Inc
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.
Fiddler
Fiddler
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.
[OpenTK: C# OpenGL 4.4, OpenGL ES 3.0 and OpenAL 1.1. Now with Linux/KMS support!]
BoReDoM_Inc
BoReDoM_Inc
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.
RAZORUNREAL
RAZORUNREAL
A display list would probably be more convinient in your situation. A little faster too.
___________________________________________________David OlsenIf I've helped you, please vote for PigeonGrape!
James Trotter
James Trotter
I'm not sure this is the best way to do it, but I've seen it done like this:

// Open the fileFILE *file = fopen("testfile", "rb");// Get the length of the filefseek(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.

Topic Locked

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

Sign in to reply to this topic.