Multi-texturing a mesh
After some fruitless searching I have found no clear answer to a multitexturing problem I have. I figure it must be so straightforward that no one has bothered to write it down.
How can I apply multiple textures to a mesh, with different textures associated with different vertices? My particular problem is related to a terrain engine, but I will reduce it to a simple example. Suppose I have a mesh of 4 vertices (ie 2 triangles); I also have a map-type file (a .tga in my case) whose values indicate which textures should be applied to each vertex. So:
(0,0) = grass
(1,0) = stone
(0,1) = earth
(1,1) = water
Do I declare which texture should be used when the vertices or mesh are created? Is this done during the rendering, and if so how do I weight individual vertices with the correct texture? This last bit is the element that I'm really struggling with, I think (but I'm confused, so who knows!).
I assume that texture stages should be used or pixel shaders. I just don't see how.
Thanks for your reply. I guess I was kind of hoping to avoid shaders for the moment and use a more old-fashioned approach like some sort of single pass multi-texturing (if appropriate), mainly because shaders make my head spin! In addition to your example, I also found this project (
http://www.codeproject.com/cs/media/terrainrendering.asp), which is quite informative on using shaders for multitexturing, and is written in C#.
Your mesh has this in it
4 vertex FVF in the Vertex Buffer
6 index in the Index Buffer
2 attribut in the Attribut Buffer (if both value are 0 the DrawSubSet(0) will draw both triangle with Material/texture 0)
The attribut buffer is not well known, it exist when you create the mesh, you get 1 value for each triangle
So you set a value for each of your triangle in the mesh, you lock to a ptr and you set one value for each triangle (faces)
http://www.gamedev.net/community/forums/topic.asp?topic_id=202452
You mesh will have 4 vertex where you can set many U,V coordinate in the FVF
D3DTSS_TEXCOORDINDEX - you can specify up to 8 texture coordinate sets in your vertex declaration. Here you can define which of those sets should be used for this stage. An advanced method is to let Direct3D generate the coordinates e.g. use the camera space vertex normal.
http://www.toymaker.info/Games/html/texture_states.html
Here is one way to do what you want
============
You loop for 4 stages of multitexture (grass, rock, snow, dirt etc...)
in the loop you set the D3DTSS_TEXCOORDINDEX
To use the U,V 0,1,2 or 3 of the 4 pair of coordinated that you stored in the FVF
You set the Stage ColorOp and AlphaOp etc
you call the Loop for each i attribut of the Mesh
You set the texture i for this material, since you know what stage you are in
you choose the grass, rock etc texture to set here for the corresponding multitexture stage
You call pMesh->DrawSubSet(i) for drawing each triangle that as a i for attribut
You loop the attribut i
You loop the Stage of the multitexture
============
Your attribut buffer assign a material to each faces (triangle)
If each pass all triangle have the same texture all the attribut buffer value can be 0
your texture will change for each stage not really for each group of triangle in the mesh
Think of your mesh as slice of texture using different U,V sliding and blending
on the same vertex, the x,y,z of the vertex don't change, only the U,V for each stage
Texture are associated with faces not vertex...
If you really want to multitexture this way don't forget to use 4X or 2X Texture Modulation Op
Because many stage will render the result very dark...
I Hope any of this could help :-)
erm ....
If i asked for the stupid persons explanation of this would that be a bad thing ?
I started reading up on xna about a month ago and have some terrain which i generate. Im just looking to blend textures / use multitexturing to acheive a varied more realistic landscape.
I have no knowledge of HLSL and would love to know where i start to learn how to use it if anyone has any links to the "idiots guide to HLSL" web site id love to see it.
:)
i have my mesh textured all in one texture so far. Im gutted i managed to even get this far to be honest lol