Jump to content
  • Advertisement
Sign in to follow this  
liamf1986

Multi texture terrain

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

Hellos,

I'm working on a multi texture terrain in C++ with Direct x.
I did in it XNA about a year ago following this tutorial http://www.riemers.net/eng/Tutorials/XNA/Csharp/series4.php
and this is a port of the final result.

I'm using 4 textures, a different texture should be shown dependant on the height of the vertex being processed. Its not working however the weighting for texture 3 and 4 seems to be getting incorrect information within the shader.

I'm fairly certain the problem is with my vertex declaration leading to information being passed to the shader incorrectly. I just can't seem to isolate the error.

I'm using the following Vertex declaration,
#define FVF_MULTITEXLITVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 | D3DFVF_TEX2)
typedef struct _MULTITEXLITVERTEX
{
Vector3 vecPosition;
Vector3 vecNormal;
Vector2 vecTexCoords;
Vector4 vecTextureWeights;
} MULTITEXLITVERTEX;


and below is the vertex shader code,
MTVertexToPixel MultiTexturedVS( float4 inPos : POSITION, float3 inNormal: NORMAL, float2 inTexCoords: TEXCOORD0, float4 inTexWeights: TEXCOORD1)
{
// unimportant code hidden
Output.TextureWeights = inTexWeights;

return Output;
}


and finally my redering code,

void Terrain::Render(Camera cam)
{
// Set terrains VertexBuffer to the Graphics card
Engine_Settings.D3D_Device->SetStreamSource(0, terrainVertexBuffer, 0, sizeof(MULTITEXLITVERTEX));
Engine_Settings.D3D_Device->SetFVF(FVF_MULTITEXLITVERTEX);
// Set the terrains IndexBuffer to the Graphics card
Engine_Settings.D3D_Device->SetIndices(terrainIndexBuffer);

terrainEffect.Begin();
for (unsigned short i = 0; i < terrainEffect.GetPasses(); i++)
{
terrainEffect.BeginPass(i);
Engine_Settings.D3D_Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, iNumVertices, 0, (map.GetWidth()-1)*(map.GetHeight()-1)*2);
terrainEffect.EndPass();
}
terrainEffect.End();
}


Any help is greatly appreciated thanks in advance ^^

Share this post


Link to post
Share on other sites
Advertisement
If you use flexible vertex formats D3DFVF_TEXx only defines how many different texture coordinates you use. In your case you need only D3DFVF_TEX2. But if your texture coordinates have more than two components you need to use the D3DFVF_TEXCOORDSIZEN macro. In your case D3DFVF_TEXTUREFORMAT4 (1).
Anyway instead of using FVF you better create a vertex declaration. This new way of defining the format of a vertex buffer is more flexible.

Share this post


Link to post
Share on other sites
Hey Demirug, thanks for the reply.

I had a feeling i didn't quite understand how the D3DFVF_TEXx flags were working.

I did experiment with my own vertexDeclaration this morning sometime, it looked something like this.

const D3DVERTEXELEMENT9 declaration[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION,0},
{0, sizeof(float) * 3, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
{0, sizeof(float) * 6, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
{0, sizeof(float) * 8, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
D3DDECL_END()
};

As its only the second time i've used this (the first being in my mesh class) i was confused how to call CreateVertexBuffer(). I tried the following

Device->CreateVertexBuffer(iNumVertices*sizeof(MULTITEXLITVERTEX), 0, D3DXGetDeclVertexSize(declaration, 0), D3DPOOL_DEFAULT, &terrainVertexBuffer, NULL)

The part I was unsure about being the 3 passed item where it requests the FVF.
When that didn't seem to work I wussed out and when back to using FVF XD

I'll have to look into it more

Share this post


Link to post
Share on other sites
Just use 0 as FVF when creating a vertex buffer. You only need to define a FVF for a vertex buffer if you want to use it as target for ProcessVertices. As this function is hardly used you should be fine if you create all your buffers with a FVF value of 0.

Share this post


Link to post
Share on other sites
Oh I see.

Got it rendering with the vertexDeclaration now and removed all the FVF stuff. This hasn't fixed the problem, in fact it's lost all texture now. But I'm sure thats just my lack of experience with the vertexDeclaration hopefully I'll find my mistake somewhere.

Nice that I understand the FVF stuff alittle better now thanks for that :)

Share this post


Link to post
Share on other sites
Maybe you missed a SetVertexDeclaration. As FVF and vertex declaration are responsible for the same job setting one of them will have an impact of the other one. So after you use a SetFVF you need to call SetVertexDeclaration to switch back to a vertex declaration.

Share this post


Link to post
Share on other sites
Yup you were right that was it ^^

I was missing a couple of other things aswell. For example I was never calling CreateVertexDeclaration() I was simply declaring the VertexDeclaration as above and then attempting to use it ....

thanks for all the help its working well enough now :)

For anyone else having trouble with VertexDeclarations I remembered reading a nice tutorial that gives the basics of using them and managed to track it down. You can find it at:- http://www.two-kings.de/tutorials/dxgraphics/dxgraphics18.html

Thanks again Demirug

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!