Texturing Terrain based on Height

Started by
2 comments, last by Xpyder 18 years, 5 months ago
Hey guys I am working on a 3D terrain engine, and so far it loads a wireframe, colored island onto the screen based on a heightmap. The color settings use the height by a few simple ifs:

if (CV[x + (y * WIDTH)].Z == 0) { CV[x + (y * WIDTH)].Color = Colorif0.ToArgb();}
if (CV[x + (y * WIDTH)].Z == 1) { CV[x + (y * WIDTH)].Color = Colorif1.ToArgb();}
if (CV[x + (y * WIDTH)].Z == 2) { CV[x + (y * WIDTH)].Color = Colorif2.ToArgb();}
if (CV[x + (y * WIDTH)].Z >= 3) { CV[x + (y * WIDTH)].Color = Colorif3.ToArgb();}
but now i need to texture it instead of just colour it, so i sorted out the texture coordinates etc so it would basically render the whole thing in one texture, but when i tried to do the same sort of thing by doing the following:

for (int xy = 0; xy < WIDTH * HEIGHT; xy++)
{
  if (vertices[xy].Z == 0) 
  {
   device.SetTexture(0, tWater);
   device.DrawIndexedPrimitives(PrimitiveType.TriangleList,0,0,4,0,                      indices.Length/3);
  }
  if (vertices[xy].Z == 1)
  {
    device.SetTexture(0, tSand);
    device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, indices.Length / 3);
  }
  if (vertices[xy].Z == 2) 
  {
    device.SetTexture(0, tGrass);
    device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, indices.Length / 3);
  }
  if (vertices[xy].Z >= 3) 
  {
  device.SetTexture(0, tRock);
  device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, indices.Length / 3);
  }

It will only render the top-most texture. 
I have tried changing the numVertices parameter in DrawIndexedPrimitives (currently "4") from 1, to the maximum vertices but it has the same result. 

Can anyone offer any pointers (oh god i miss pointers) on this?

             }
JUST-CODE-IT.NETManaged DirectX & C# TutorialsForumsArticlesLinksSamples
Advertisement
Umm... Are your "vertices[xy].Z" all int/long's? Otherwise you're only doing something if it's EXACTLY on that. I would suspect your coordinates were done in floats therefore you need to take into consideration 3.0001 to 3.9999 not just 3.0000.

You gave us nearly no information. Try posting some sample data, or telling us what vertices[xy].Z is, etc. Not using these puppies, "", might be your issue...

You could look at this article from CodeProject where the person have used shaders to render a terrain based on height.

I hope this helps.
Take care.
thanks

Other information like datatypes would not help, "4" was my mistake but not in the code.

Is there anybody i could add to MSN and we can sort it out that way?
JUST-CODE-IT.NETManaged DirectX & C# TutorialsForumsArticlesLinksSamples

This topic is closed to new replies.

Advertisement