Sign in to follow this  
MrSparkle

Accessing FVFData from Shaders

Recommended Posts

Hi, how can I use different texture coordinates stored in the FVFData in the shader? Using a structure like struct VertexInput { float3 pos : POSITION; float2 texCoord01 : TEXCOORD0; float2 texCoord02 : TEXCOORD1; float2 texCoord03 : TEXCOORD2; float3 normal : NORMAL; }; texCoord02 and texCoord03 will produce crap, only texCoord01 works. I use the Panda DirectX Exporter for 3ds max and the texture coordinates are written to the fx file (in the FVFData section). What have I done wrong? Thanks, Christian

Share this post


Link to post
Share on other sites
You'd access texCoord02 and texCoord03 with In.texCoord02 and In.texCoord03 or t1 and t2.

For the producing crap thing I'd guess maybe you're not loading textures into a stage you're trying to use, but it's pure speculation.

I may be wrong, but it feels like you left something out.

Share this post


Link to post
Share on other sites
Texture coordinates are effectively meaningless without the sampler and texture information. All they are is offsets into some data - until you know what that data is then you can't really tell what you'll get back.

What exactly is "producing crap"? Any chance of a more scientific description? [grin]

If you've not bound a texture to the sampler then you should get a default value returned (either [0,0,0,0] or [1,1,1,1]) and not just noise.

If you have got a texture bound to the sampler then you'll be pulling in valid texels (unless you're going over the max repeat cap) but it might indicate that the texture coordinates are wrong.

Without more information it could be a simple case that 3dsmax/panda has picked up 3 sets of TC's from somewhere even though they aren't actually being used. Maybe the result doesn't show up in Max even though they are technically there.

hth
Jack

Share this post


Link to post
Share on other sites
Quote:
Original post by jollyjeffers
What exactly is "producing crap"? Any chance of a more scientific description? [grin]


;) Yes, I try it... Crap means, in the FX Composer it is really noisy (because there is only one texture coordinate available). In 3ds max and in my application, there is no texture visible if I use the second or third texture coordinate set.

My IN and OUT structures look like this:

struct VertexInput
{
float3 pos : POSITION;
float2 texCoord01 : TEXCOORD0;
float2 texCoord02 : TEXCOORD1;
float2 texCoord03 : TEXCOORD2;
};

struct VertexOutput
{
float4 pos : POSITION;
float2 texCoord01 : TEXCOORD0;
float2 texCoord02 : TEXCOORD1;
float2 texCoord03 : TEXCOORD2;
};


In the vertex Shader, I write the texture coordinates to the OUT structure:

Out.texCoord01 = In.texCoord01;
Out.texCoord02 = In.texCoord02;
Out.texCoord03 = In.texCoord03;

And finally, in the pixel shader, I try to apply this to the TextureSampler, I want to use:

float4 textureColor1 = tex2D(DiffuseTextureSampler, In.texCoord01);
float4 textureColor2 = tex2D(DiffuseTextureSampler, In.texCoord02);
float4 textureColor3 = tex2D(DiffuseTextureSampler, In.texCoord03);

If the PixelShader returns textureColor1, the texture looks fine. textureColor2 and textureColor3 wont give any result (the model is uniformly colored without any texture).

Because the first texture coordinate gives perfect results, I don't know what I could try, and I have definitely no idea what could be missing.

Thanks for your ideas!

Christian




Share this post


Link to post
Share on other sites
Try this:

Out.texCoord01 = In.texCoord01;
Out.texCoord02 = In.texCoord01;
Out.texCoord03 = In.texCoord01;

1) If textureColor2, and textureColor3 work when you use the first set of texture coordinates, then there is likely a problem with the texture coordinates in the vertices in the mesh, in the vertices in your vertex buffer or with the vertex declaration.

2) If that still produces the same result, I'd take a look at the SetTexture() calls in your application - and also for any SetSamplerState() (the addressing modes in particular) and SetTextureStageState() calls (D3DTSS_TEXCOORDINDEX and D3DTSS_TEXTURETRANSFORMFLAGS in particular).

Share this post


Link to post
Share on other sites
Hi Sica,
I already tried this and as I expected, it gives the same results for all texture coordinates. Changing Out.texCoord0X to In.texCoord02 or In.texCoord03 will produce crap, as expected, too.

Quote:

1) If textureColor2, and textureColor3 work when you use the first set of texture coordinates, then there is likely a problem with the texture coordinates in the vertices in the mesh, in the vertices in your vertex buffer or with the vertex declaration.


How can I find out? The texture coordinates are written to the .x file with the Panda Exporter. The Exporter stored the first texture coordinates to the MeshTextureCoords section, the second and the third to the FVFData section. I can see this, because I used the ASCII format and opened the .x file with the text editor.

I think that DirectX doesn't read the texture coordinates from the FVFData, but I don't know how to tell DirectX to do this. In another thread, someone posted that "D3DXLoadMeshFromX() should load mesh correctly with multiple coordinates". But does this also involve texture coordinates stored in the FVFData?

Thanks,
Christian

Share this post


Link to post
Share on other sites
Quote:
Original post by MrSparkle
Quote:

1) If textureColor2, and textureColor3 work when you use the first set of texture coordinates, then there is likely a problem with the texture coordinates in the vertices in the mesh, in the vertices in your vertex buffer or with the vertex declaration.


How can I find out? The texture coordinates are written to the .x file with the Panda Exporter. The Exporter stored the first texture coordinates to the MeshTextureCoords section, the second and the third to the FVFData section. I can see this, because I used the ASCII format and opened the .x file with the text editor.


Personally, the first thing I'd do us call ID3DXBaseMesh::GetDeclaration() immediately after loading the mesh. Then check the values of the declaration in the debugger to ensure there are definitely 3 occurrences of D3DDECLUSAGE_TEXCOORD in the vertices of the mesh. You could do the same with ID3DXBaseMesh::GetFVF() and check for D3DFVF_TEX3 if you feel more comfortable with FVF codes.

Quote:
I think that DirectX doesn't read the texture coordinates from the FVFData, but I don't know how to tell DirectX to do this. In another thread, someone posted that "D3DXLoadMeshFromX() should load mesh correctly with multiple coordinates". But does this also involve texture coordinates stored in the FVFData?


Direct3D FVF codes themselves do support multiple sets of texture coordinates - unfortunately I don't personally know enough about the nuances of .X files and the Panda exporter to know whether you should be getting them in your input data.


Share this post


Link to post
Share on other sites
Thank you S1ca!

The more I read through articles and the DirectX documentation on this topic, the more I wonder if I can do this with Managed DirectX.

At the moment, I don't have any vertex declarations and that stuff implemented in my application. I simple load the mesh from a file and render it using Vertex and Pixel Shaders. I load the mesh like that:
Quote:

d3dMesh = Mesh.FromFile(
Path,
MeshFlags.Managed,
RenderForm.DirectXDevice,
out adjacencyBuffer,
out ExtendedMaterials,
out EffectParameters);

d3dMesh.OptimizeInPlace(
MeshFlags.OptimizeCompact |
MeshFlags.OptimizeAttributeSort |
MeshFlags.OptimizeVertexCache,
adjacencyBuffer);


In the render loop, I simply call mesh.DrawSubset() for each subset ans shader pass.

At which point I have to tell the DirectX Device or the Mesh to use the FVFData as additional texture coordinates?

I'm sorry for this noob question but I couldn't find any information in the DirectX docs.

Thanks,
Christian

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