Jump to content
  • Advertisement
Sign in to follow this  
smallGame

Vertex Terrain for detailed texturing

This topic is 3608 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, I created a terrain, and each vertex of this terrain is composed of U,V texture. But I don't want to have the same texture coordinate for the right top, left top, right bottom, and left bottom polygon. So the solution is to duplicate this vertex 4 times, just for the texture coordinates. I worked on mipmapping and frutrum to optimize my terrain and now I want to texure it I am thinking to mutilply its size by 4!!!! It must be exist an other solution. Any help will be appreciated, Thanks [Edited by - smallGame on November 28, 2008 4:44:35 PM]

Share this post


Link to post
Share on other sites
Advertisement
If I understand I have just to give to the shader data about texture place to allow to calculate texture coordinate.

That it s a good idea, thanks a lot.

Share this post


Link to post
Share on other sites
As far as I understand what you are trying to do, you indeed don't have to duplicate each vertex 4 times. Smarter solutions exist.

To start off, you probably want to render your terrain as one big list of vertices. Just rendering vertices is not what you want though, you want indices as well.

Basically you want to create every vertex once, so something like this:


vertices = new Vertex[vertexDimensions.X * vertexDimensions.Y];

for (int z = 0; z < vertexDimensions.Y; z++)
{
for (int x = 0; x < vertexDimensions.X; x++)
{
vertices[z * vertexDimensions.X + x] = new Vertex(new Vector3((float)x * tileSize, 0.0f, (float)z * tileSize)), new Vector2((float)x * textureScale, (float)z * textureScale));
}
}


This assumes vertices is a single dimension array of Vertex's (lol), and each Vertex is constructed with a Vector3 position and Vector2 texture coordinates. Note that each vertex is created only once, but we are going to use the same vertex for multiple polygons!

By the way, tileSize is the size of one terrain tile, so how far vertices are away from eachother, and vertexDimensions is a Vector2 containing the amount of vertices in the x and z direction. (I'm assuming Y is your up vector). textureScale is just a float value that says how often a texture should be repeated for one tile. For me, this value is always 1.0f or smaller.

The trick comes from the indices: (note that tileDimensions is also a Vector2 like vertexDimensions, but both the X and Y are one lower since you need x+1 vertices to create x tiles in one direction).


indices = new int[6 * tileDimensions.X * tileDimensions.Y];

int currentIndex = 0;
for (int z = 0; z < tileDimensions.Y; z++)
{
for (int x = 0; x < tileDimensions.X; x++)
{
indices[currentIndex] = z * vertexDimensions.X + (x + 1);
currentIndex++;
indices[currentIndex] = z * vertexDimensions.X + x;
currentIndex++;
indices[currentIndex] = (z + 1) * vertexDimensions.X + (x + 1);
currentIndex++;
indices[currentIndex] = (z + 1) * vertexDimensions.X + x;
currentIndex++;
indices[currentIndex] = (z + 1) * vertexDimensions.X + (x + 1);
currentIndex++;
indices[currentIndex] = z * vertexDimensions.X + x;
currentIndex++;
}
}


Basically this loops through all the tiles in the terrain, and creates 6 indices for each tile (each tile is a quad, so 2 triangles, so you need 6 indices of existing vertices.

This gives you what you need (at least I think). Now just draw the vertices using the indices (look it up if you don't know how), and your terrain will be there. Make sure you use the right ordering for indices (clockwise or counter-clockwise depending on your culling).

If you want to use frustum culling for terrain, it gets a little more complicated. You probably want to divide your terrain into smaller parts. The easiest way to do this is to create index buffers for each part, instead of one big index buffer.

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!