• Advertisement
Sign in to follow this  

Problem with terrain rendering (with screenshot)

This topic is 4649 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

Well, when I render heightmaps, things are drawn that shouldn't be there. The things that shouldn't be there are scribbled out with black. this loads the .raw file and creates it display list...
BOOL cTerrain :: LoadRawFile(LPSTR strName, int nSize, int ID, std::string TexBase, std::string TexDetail, int MapX, int MapY)
{
	FILE *pFile = NULL;

	// Let's open the file in Read/Binary mode.
	pFile = fopen( strName, "rb" );

	// Check to see if we found the file and could open it
	if ( pFile == NULL )	
	{
		// Display our error message and stop the function
		MessageBox(HWND_DESKTOP, "Can't find the height map!", "Error", MB_OK);
		return FALSE;
	}

	TERRAIN tmpTerrain;

	terrain.push_back(tmpTerrain);
	TerrainCount += 1;

	terrain[TerrainCount - 1].HeightMap = new BYTE[MAP_SIZE * MAP_SIZE];
	terrain[TerrainCount - 1].MAPSIZE = nSize;
	terrain[TerrainCount - 1].ID = ID;
	terrain[TerrainCount - 1].TexBase = TexBase;
	terrain[TerrainCount - 1].TexDetail = TexDetail;
	terrain[TerrainCount - 1].x = MapX;
	terrain[TerrainCount - 1].y = MapY;

	// Here we load the .raw file into our pHeightMap data array.
	// We are only reading in '1', and the size is the (width * height)
	fread( terrain[TerrainCount - 1].HeightMap, 1, MAP_SIZE * MAP_SIZE, pFile );

	// After we read the data, it's a good idea to check if everything read fine.
	int result = ferror( pFile );

	// Check if we received an error.
	if (result)
	{
		MessageBox(NULL, "Can't get data!", "Error", MB_OK);
	}

	if(!bmp.FindTexture("Data/TerGrass.bmp"))
	{
		bmp.LoadGLTextures("Data/TerGrass.bmp",3);
	}

	if(!bmp.FindTexture("Data/Vines.bmp"))
	{
		bmp.LoadGLTextures("Data/Vines.bmp",3);
	}

	terrain[TerrainCount - 1].MapList = glGenLists(1);

	glNewList(terrain[TerrainCount - 1].MapList,GL_COMPILE);

		glEnable(GL_FOG);

		int X = terrain[TerrainCount - 1].x, Y = terrain[TerrainCount - 1].y;						// Create some variables to walk the array with.
		int x, y, z;							// Create some variables for readability
		bool bSwitchSides = false;
		BYTE *pHeightMap;

		pHeightMap = terrain[TerrainCount - 1].HeightMap;

		// Activate the first texture ID and bind the tree background to it
		glActiveTextureARB(GL_TEXTURE0_ARB);
		glEnable(GL_TEXTURE_2D);

		if(bmp.FindTexture(terrain[TerrainCount - 1].TexBase))
		{
			glBindTexture(GL_TEXTURE_2D, bmp.Texture(terrain[TerrainCount - 1].TexBase));
		}

			// Activate the second texture ID and bind the fog texture to it
			glActiveTextureARB(GL_TEXTURE1_ARB);
			glEnable(GL_TEXTURE_2D);
				
			// Here we turn on the COMBINE properties and increase our RGB
			// gamma for the detail texture.  2 seems to work just right.
			glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
			glTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 2);
					
			// Bind the detail texture
			if(bmp.FindTexture(terrain[TerrainCount - 1].TexDetail))
			{
				glBindTexture(GL_TEXTURE_2D, bmp.Texture(terrain[TerrainCount - 1].TexDetail));
			}
				
			// Now we want to enter the texture matrix.  This will allow us
			// to change the tiling of the detail texture.
			glMatrixMode(GL_TEXTURE);

				// Reset the current matrix and apply our chosen scale value
				glLoadIdentity();
				glScalef((float)4, (float)4, 1);

			// Leave the texture matrix and set us back in the model view matrix
			glMatrixMode(GL_MODELVIEW);

		// We want to render triangle strips
		glBegin( GL_TRIANGLE_STRIP );			

		// Go through all of the rows of the height map
		for ( X = terrain[TerrainCount - 1].x; X <= terrain[TerrainCount - 1].x + MAP_SIZE; X += STEP_SIZE )
		{
			// Check if we need to render the opposite way for this column
			if(bSwitchSides)
			{	
				// Render a column of the terrain, for this current X.
				// We start at MAP_SIZE and render down to 0.
				for ( Y = MAP_SIZE + terrain[TerrainCount - 1].y; Y >= terrain[TerrainCount - 1].y; Y -= STEP_SIZE )
				{
					// Get the (X, Y, Z) value for the bottom left vertex		
					x = X;							
					y = Height(pHeightMap, X, Y, TerrainCount - 1 );	
					z = Y;							

					// Set the fog coordinate for this vertex, depending on it's height
					// and the current depth of the fog.
					SetFogCoord(g_FogDepth, (float)y);


					// Set the current texture coordinate and render the vertex
					SetTextureCoord( (float)x, (float)z, TerrainCount - 1 );
					glVertex3i(x, y, z);		

					// Get the (X, Y, Z) value for the bottom right vertex		
					x = X + STEP_SIZE; 
					y = Height(pHeightMap, X + STEP_SIZE, Y, TerrainCount - 1 ); 
					z = Y;

					// Set the fog coordinate for this vertex
					SetFogCoord(g_FogDepth, (float)y);

					// Set the current texture coordinate and render the vertex
					SetTextureCoord( (float)x, (float)z, TerrainCount - 1 );
					glVertex3i(x, y, z);		
					}
				}
				else
				{	
					// Render a column of the terrain, for this current X.
					// We start at 0 and render down up to MAP_SIZE.
					for ( Y = terrain[TerrainCount - 1].y; Y <= MAP_SIZE + terrain[TerrainCount - 1].y; Y += STEP_SIZE )
					{
						// Get the (X, Y, Z) value for the bottom right vertex		
						x = X + STEP_SIZE; 
						y = Height(pHeightMap, X + STEP_SIZE, Y, TerrainCount - 1 ); 
						z = Y;

						// Set the fog coordinate for this vertex
						SetFogCoord(g_FogDepth, (float)y);


						// Set the current texture coordinate and render the vertex
						SetTextureCoord( (float)x, (float)z, TerrainCount - 1 );
						glVertex3i(x, y, z);

						// Get the (X, Y, Z) value for the bottom left vertex		
						x = X;							
						y = Height(pHeightMap, X, Y, TerrainCount - 1 );	
						z = Y;							

						// Set the fog coordinate for this vertex
						SetFogCoord(g_FogDepth, (float)y);
						// Set the current texture coordinate and render the vertex
						SetTextureCoord( (float)x, (float)z, TerrainCount - 1 );
						glVertex3i(x, y, z);		
					}
				}

				// Switch the direction the column renders to allow the fluid tri strips
				bSwitchSides = !bSwitchSides;
			}

			// Stop rendering triangle strips
			glEnd();

			// Turn the second multitexture pass off
			glActiveTextureARB(GL_TEXTURE1_ARB);
			glDisable(GL_TEXTURE_2D);

		// Turn the first multitexture pass off
		glActiveTextureARB(GL_TEXTURE0_ARB);		
		glDisable(GL_TEXTURE_2D);
		glDisable(GL_FOG);
	glEndList();
	

	// Close the file.
	fclose(pFile);

	return TRUE;
}

Anyone know why this could be happening? Jake

Share this post


Link to post
Share on other sites
Advertisement
you are going outside the bounds of your array and reading an extra vert for the width and one for the height.

easiest way to fix it is to clamp your height function to return a 0 or the last position in this case[smile]. Just check if x >= width and if the final index is larger than the size of the array.

HTH

Share this post


Link to post
Share on other sites
Try this, it could be a quick fix:

Change

for ( X = terrain[TerrainCount - 1].x; X <= terrain[TerrainCount - 1].x + MAP_SIZE; X += STEP_SIZE )

to

for ( X = terrain[TerrainCount - 1].x; X < terrain[TerrainCount - 1].x + MAP_SIZE; X += STEP_SIZE )

and

for ( Y = MAP_SIZE + terrain[TerrainCount - 1].y; Y >= terrain[TerrainCount - 1].y; Y -= STEP_SIZE )

to

for ( Y = MAP_SIZE + terrain[TerrainCount - 1].y; Y > terrain[TerrainCount - 1].y; Y -= STEP_SIZE )

Share this post


Link to post
Share on other sites
I've been trying all your suggestions and the sort of work. This is what I think the problem is...

It seems the the map is sort of draw translated. Not actual translated using openGL but by the way it is rendered. It seems that it shifts -16 units left and the parts that where not drawn at the beginning where drawn on the other side. It's the same with the z-axis. It's shifted 16 units and it is draw on the other side. Even thought the heights have been shifted the terrain has not been shifted so it looks like it is scrolling a single terrain (repeating landscape).



I hope this is clear. So I thought that to solve the problem, I just need to shift it back the other way lol, but I don't know how to do that.

Jake

Share this post


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

  • Advertisement