FVF and Shaders

Started by
12 comments, last by ET3D 18 years ago
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
Advertisement
You need to use declarations:

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

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

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

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?
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 hereg_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

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Yeah isn't the Fixed-Function Pipeline being made redundant in DX10?
It's not a bug... it's a feature!
Quote:Original post by Dom_152
Yeah isn't the Fixed-Function Pipeline being made redundant in DX10?
Most definitely is [grin]

Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

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.
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!

It's not a bug... it's a feature!
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

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

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,&currentEffect->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?

This topic is closed to new replies.

Advertisement