• Advertisement
Sign in to follow this  

[.net] [DirectX]Terrain Generator

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

Hi everyone ! I'm now looking at the Terrain Generator part of the tutorial... but I've got a problem. I don't understand the maths behind the following snippets, could someone explain to me what's going on because I really can't understand that code... :(
public void VertexBufferSetUp()
{
	VBuf = new VertexBuffer(typeof(CustomVertex.PositionColored), WIDTH*HEIGHT, D3DDevice,
		Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);

	int index = 0;
	Vertices = new CustomVertex.PositionColored[WIDTH*HEIGHT];
	for(int x = 0; x < WIDTH; ++x)
	{
		for(int y = 0; y < HEIGHT; ++y)
		{
			index = x+y*WIDTH;
			Vertices[index].Position = new Vector3(x, y, 0);
			Vertices[index].Color = Color.White.ToArgb();
		}
	}

	VBuf.SetData(Vertices, 0, LockFlags.None);
}

  private void IndicesDeclaration()
  {
    ib = new IndexBuffer(typeof(int), (WIDTH-1)*(HEIGHT-1)*6, device, Usage.WriteOnly, Pool.Default);
    indices = new int[(WIDTH-1)*(HEIGHT-1)*6];
    for (int x=0;x<WIDTH-1;x++)
    {
      for (int y=0; y<HEIGHT-1;y++)
      {
        indices[(x+y*(WIDTH-1))*6] = (x+1)+(y+1)*WIDTH;
        indices[(x+y*(WIDTH-1))*6+1] = (x+1)+y*WIDTH;
        indices[(x+y*(WIDTH-1))*6+2] = x+y*WIDTH;
 
        indices[(x+y*(WIDTH-1))*6+3] = (x+1)+(y+1)*WIDTH;
        indices[(x+y*(WIDTH-1))*6+4] = x+y*WIDTH;
        indices[(x+y*(WIDTH-1))*6+5] = x+(y+1)*WIDTH;
      }
    }
    ib.SetData(indices, 0, LockFlags.None);
  }


Thank you all. [Edited by - White Scorpion on January 3, 2005 11:58:13 AM]

Share this post


Link to post
Share on other sites
Advertisement
I think your difficulty in understanding is the way that it is accessing a single-dimension array using X and Y coordinates, because that's the only part with numbers in the code snippets really. :)

Say you want a 16x16 grid (of vertices, let's say) being stored in a single-dimension array. The area of 16x16 is 256, so the array will have 256 elements. Let's say we want to refer to X=0 Y=1, which is one down from the top-left corner. So we multiply the Y by the width of the grid to move 'down' that amount, and then add X to move 'across' that amount. Location = (Y*WIDTH)+X

I hope that helps. I had some trouble too when I first started playing with heightmaps and had to work with a single-dimension array rather than the two-dimension arrays that I had been used to. :)

[Edited by - HopeDagger on January 2, 2005 11:30:55 PM]

Share this post


Link to post
Share on other sites
Yes yes thank you a lot mate ! :D But what about the second one ? I don't understand
indices[(x+y*(WIDTH-1))*3] = ...
nor
indices = new short[(WIDTH-1)*(HEIGHT-1)*6];

Share this post


Link to post
Share on other sites
indices[(x+y*(WIDTH-1))*3] = ...


The first part, (x+y*(WIDTH-1)) I covered above. It's subtracting one from the width because the last vertices on the end aren't needed. The value is being multiplied by 3 because there are 3 vertices per triangle.

indices = new short[(WIDTH-1)*(HEIGHT-1)*6];


Like I said above, it's WIDTH-1 (or HEIGHT-1) because the last row/column isn't needed, since the second-last triangles will cover the final row/column. It is multiplied by 6 because each square block on the 'grid' is 2 triangles (6 vertices).

Hope that helps! :)

Share this post


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

  • Advertisement