Sign in to follow this  
Zed-ex

Different Texture for different vertices

Recommended Posts

Sorry if this should be somewhere else or if it is terribly noobish I'm a computer program analyst student. I recently finished a basic directx course. This fall I will be starting a course in directx game development. I am trying to get ahead of the class and really knock the socks off of my proffesors. I have made a terrain generating program that takes a .raw file and turns it into a 3D terrain. After alot of work I got normals and texture working. I then diverted for awhile and did other things like collision detection basic physics 1st person camera(sorry not important just trying to let you know my level of understanding of the direct api) Anyways. What I want to do now is change it so that not all of the terrain has one texture. Currently it is all grass. if you want a pic visit www.zed-ex.com( ya I'm not really into web design if you can tell. be kind please) anyways. I basically want to do something where depending on the height of the vertices the texture is different if(y < 25) sand else if (y > 25 && y < 175) grass else if (y > 175 < 210) rock else snow Thats the idea. I have done alot of research to get where I am with this project but have found nothing in the way of something that would let me do this. The texture co-ordinates are held within the vertex but the texture is set write before drawing using pdev->SetTexture( 0, m_texture ); So I am unsure how to get different textures on one set of vertices Not asking for the code btw just the concept I should research. Done a bit of work at looking at HLSL for vertex shading but I only found how to change the texture co-ord not the actual texture Sorry again if this is really beginner...

Share this post


Link to post
Share on other sites
every triangle in a triangle list has it's own set of 3 UV coordinates. thus each triangle can point to a space on a texture regardless of what the triangle is doing next to it. instead of loading up a bunch of textures, just make one big texture and split it up into different things, such as dirt, rocks, grass, thick grass, cobblestones, whatever. If you grid out a single texture into say 4 rows and 4 columns, making 16 spots (granted a larger single texture instead of 16 smaller ones).

lets say you wanted to use the top left grid, well your UV would be between 0 and 0.25

the next one over would be u 0.25 to 0.50 and v 0.00 to 0.25

Share this post


Link to post
Share on other sites
I've not done any DirectX programming but I've written plenty of HLSL shaders, and what you are after is very easy to achieve in shaders, so the terrain could have a single material on it.

You could have a vec3/float3 in the Vertex shader with .xyz as seperate factors for the sand/grass/rock/snow that you mentioned. Use this in the PixelShader to lerp between the texture maps!

perhaps: lerp(lerp(lerp(sand, grass, Height.x), rock, Height.y), snow, Height.z);

In addition, you could make the blend values based on Dot( N, WorldUpVector) so that rock will blend into grass depending on slope.

Sorry I can't help with the Dx side of things! :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Pheonixx1980
every triangle in a triangle list has it's own set of 3 UV coordinates. thus each triangle can point to a space on a texture regardless of what the triangle is doing next to it. instead of loading up a bunch of textures, just make one big texture and split it up into different things, such as dirt, rocks, grass, thick grass, cobblestones, whatever. If you grid out a single texture into say 4 rows and 4 columns, making 16 spots (granted a larger single texture instead of 16 smaller ones).

lets say you wanted to use the top left grid, well your UV would be between 0 and 0.25

the next one over would be u 0.25 to 0.50 and v 0.00 to 0.25


Honestly it didn't even cross my mind to do it that way.

I was attempting this morning to figure out how to make a texture that would let me do this but I have run into a problem

Every tringle is made up of 3 co-ordinates but each of those co-ordinates are shared with six other traingles.

Therefore after trying it out I don't see how it is possible to do it that way.

Thank you for the suggestion however it's got me thinking :D

urgrund

Now your solution on the other hand seems like it can work. I may have been going about this wrong the whole time. I was thinking vertex shaders. But since you brought it up vertex shaders do seem alittle out of place. It seems I want to be working with the pixels not the vertices.

I haven't covered HLSL in school yet. I've been trying to learn HLSL but so far I have only gotten as far as reading it.

I like your idea but alot of it seems foreign. Could you possible elaborate and dumb it down for us who are not as skilled? :D

Much appreciated





Share this post


Link to post
Share on other sites
if you are using a primitive triangle list, each triangle may be physically located next to each other, and part of the same object, but the actual triangles are independent. a triangle fan and triangle list does have that limitation.

there is more then one reason to learn about this UV method. in order to prevent tile pattern repetition, you can flip, flop, flipflop, and scale the UV mapping to get a different feel from the same texture, or even have the triangle point to different variations of the same 'grass'. You essentially quadrouple your texture base as well as provide the ability to scale the UV on longer sided triangles.

that shading stuff is really cool for what it is worth, but I always feel that working from a solid foundation is better then slapping a nice house on an incomplete foundation. if you're going to have UV coordinates anyway, it seems like a waste not to utilize them.

Share this post


Link to post
Share on other sites
You can use a texture atlas. This means packing several images into one textures. This way playing with texture coordinates lets you select "different textures". (Look it up in a search engine for more info.)

Regarding the same vertex being used in several triangles: that's a classic problem, and the standard solution is duplicating vertices.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this