Sign in to follow this  

FVF and Shaders

This topic is 4272 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 im writting an app which will use HLSL shaders, im loading my meshes into ID3DXMESH, when i run my shader it says that the COLOUR0 is not stored in my fvf. so i made my own vertex structure: struct CUSTOMVERTEX { D3DXVECTOR3 p; // Vertex position D3DXVECTOR3 n; // Vertex normal D3DXVECTOR3 d; // Diffuse Colour float tu,tv; // Texture co-ordinate }; #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1 ) but how do i apply this to the device or the mesh when i load it or do i have to clone the mesh into a new one with my custom vertex format? thankx

Share this post


Link to post
Share on other sites
You need to use declarations:

D3DVERTEXELEMENT9 structure -> IDirect3DDevice9::CreateVertexDeclaration() -> IDirect3DDevice9::SetVertexDeclaration().

FVF's are for the legacy fixed-function pipeline.

hth
Jack

Share this post


Link to post
Share on other sites
like this?


LPDIRECT3DVERTEXDECLARATION9 pVertexDeclaration = NULL;

const D3DVERTEXELEMENT9 declaration[] =
{
{ 0, 0 * sizeof(float),D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_POSITION, 0 },
{ 0, 3 * sizeof(float),D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },D3DDECL_END()};

base->D3dDevice->CreateVertexDeclaration( declaration, &pVertexDeclaration );

if i do that when do i actually set it? every render or once at the beginning?

Share this post


Link to post
Share on other sites
D3DVERTEXELEMENT9 decl[] = 
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
{ 0, 24, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 0, 36, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
};

LPDIRECT3DVERTEXDECLARATION9 pVertexDecl = NULL;

g_pd3dDevice->CreateVertexDeclaration( decl, &pVertexDecl );

g_pd3dDevice->SetVertexDeclaration( pVertexDecl );

// do normal rendering here
g_pMesh->DrawSubset( 0 );

// Make sure you SAFE_RELEASE() pVertexDecl later on!



Your usage of a float3 for colours doesn't match the fixed function btw. It's expected a DWORD. Shouldn't matter for programmable pipeline though.

hth
Jack

Share this post


Link to post
Share on other sites
First, note that the colour in the FVF is of type D3DCOLOR, i.e. a DWORD, and not a 3 component vector.

When using a mesh, you can use the CloneMeshFVF method to get a new mesh with the FVF you want.

However, if your object doesn't have colour information, I don't see the point in moving it to a format with colour unless you're going to fill that colour in, in which case it'd be better to create the object with the colour in place.

Share this post


Link to post
Share on other sites
Quote:
Original post by jollyjeffers
Most definitely is [grin]

Jack


Oh dear, I've only just got to grips with FFP, I don't know anything about the Programmable one!

Share this post


Link to post
Share on other sites
Quote:
Original post by Dom_152
Oh dear, I've only just got to grips with FFP, I don't know anything about the Programmable one!
Well you can stick with FFP for now if you don't want to be using any advanced features of D3D9 and aren't interested in D3D10. But for the most part the FFP has been stale for a few years now - you can do some clever stuff with it, but for the most part you need to use shaders to leverage the true power of modern GPU's [smile]

If you dig into HLSL and the effects framework it shouldn't be too hard. Especially if you use the effects framework you can mix fixed and programmable code, which should help you transition between them as you learn...

hth
Jack

Share this post


Link to post
Share on other sites
okay i created and set the declaration as you said but ive still got this message coming up when it gets to drawing the subsets.


D3DVERTEXELEMENT9 decl[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
{ 0, 24, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 0, 36, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
};

LPDIRECT3DVERTEXDECLARATION9 pVertexDecl = NULL;

base->D3dDevice->CreateVertexDeclaration( decl, &pVertexDecl );

base->D3dDevice->SetVertexDeclaration( pVertexDecl );


.... do all semantics stuff ...

if (FAILED(currentEffect->pEffect->FindNextValidTechnique(NULL,¤tEffect->hTech)))
{
base->messageBox(NULL,"no valid techniques found");
}

if (SUCCEEDED(currentEffect->pEffect->SetTechnique(currentEffect->hTech)))
{
if (SUCCEEDED(currentEffect->pEffect->Begin(&passes,0)))
{
for (UINT i=0;i<passes;i++)
{
currentEffect->pEffect->Pass(i);
Subsets[currentSubset]->mesh->DrawSubset(Subsets[currentSubset]->SubsetNumber); // draw the subset
}
currentEffect->pEffect->End();
}else{
base->messageBox(NULL,"Could not begin pass");
}
}else{
base->messageBox(NULL,"could not set technique");
}




Direct3D9: (ERROR) :Vertex shader function usage (D3DDECLUSAGE_COLOR,0) does not have corresponding usage in the current vertex declaration

is this to do with using vectors instead of DWORDS for colours?

Share this post


Link to post
Share on other sites
It has to do with the mesh. As I said before, your mesh doesn't have colour information. DrawSubset will set a vertex declaration suitable to the vertex buffer it has. The declaration you created doesn't matter at all.

I told you before how you can add colour information to the mesh. I also told you that I think the problem is with trying to use colour where it doesn't exist. My guess is that you're either not using the colour at all, so drop it from the shader, or you're using it erroneously, since you don't have colour and yet you're trying to use it.

Stop fiddling with the vertex declaration and start looking at the shader.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dom_152
Sorry to go off topic again but you mean like mix Shader usage with FVF?


Shaders and FVF mix pretty well. You can define your vertex structure with an FVF and use a shader. You can also use a vertex declaration with the fixed function vertex pipeline (in fact, there's one fixed function feature -- vertex tweening -- which has to have a vertex declaration, since FVF is not enough to describe vertices for it).

It's still a good idea to get used to vertex declarations, but FVF in itself is not a problem.

Share this post


Link to post
Share on other sites

This topic is 4272 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.

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