   Problem with Vertex Declaration and VertexShader
  Hey I've got a little Problem here.I'm using Direct3D9 (C++) and an Effect file (HLSL).I'm trying to get a directional Light in there but I keep getting an error on the Vertex Declaration.Direct3D9: (ERROR) :Vertex shader function usage (D3DDECLUSAGE_NORMAL,0) does not have corresponding usage in the current vertex declarationMy VertexDeclaration looks like this:const D3DVERTEXELEMENT9 declarationMesh[] ={{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},{0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},{0, 20, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},D3DDECL_END()};And here's my Vertex Shader and the I/O structs:struct PositionTextureLightVSOutput{float4 Position: POSITION;float2 TexCoord: TEXCOORD0;float3 LightDirection   : TEXCOORD1;float3 Normal: TEXCOORD2;};struct PositionTextureLightPSOutput{float4 Color: COLOR0;};PositionTextureLightVSOutput PositionTextureLightVS(float4 inPos : POSITION, float2 inTexCoord : TEXCOORD0, float3 inNormal : NORMAL){PositionTextureLightVSOutput output = (PositionTextureLightVSOutput)0;float4x4 ViewProjection = mul(xView, xProj);float4x4 WorldViewProjection = mul(xWorld, ViewProjection);output.Position = mul(inPos, WorldViewProjection);output.TexCoord = inTexCoord;output.LightDirection = normalize(lightDirection);output.Normal = normalize(mul(xWorldInverseTranspose, inNormal));return output;}PositionTextureLightPSOutput PositionTextureLightPS(float2 inTexCoord : TEXCOORD0, float3 inLightDirection : TEXCOORD1, float3 inNormal : TEXCOORD2){PositionTextureLightPSOutput output = (PositionTextureLightPSOutput)0;output.Color = ambientColor * ambientIntensity;output.Color += tex2D(MeshTextureSampler, inTexCoord);output.Color += saturate(dot(inLightDirection, inNormal));return output;}Any ideas ?Also, what would I need to do in the DX9 code to create a directional light in combination with the shader?[Edited by - lipsryme on April 4, 2010 2:32:15 PM]
  The declarations look okay to me, this usually happens when you don't have the correct D3DECLUSAGE_NORMAL enum in the declaration - are you sure you're setting the vertex declaration to 'declarationMesh'?  Can we see some drawing/setup code?   ;  Hmm, well here's the Render Function:void CGraphics::Render(){if(IsDeviceResponding()){m_pD3DDev9->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);m_pD3DDev9->BeginScene();// Draw Header FontDrawText_Header();// Set Technique and VariablesD3DXMatrixTranslation(&m_matWorld, 30.0, -30.0f, 0.0f);m_pD3DEffect->SetMatrix("xWorld", &m_matWorld);m_pD3DEffect->SetMatrix("xView", &m_matView);m_pD3DEffect->SetMatrix("xProj", &m_matProj);m_pD3DEffect->SetMatrix("xWorldInverseTranspose", &m_matWorldInverseTranspose);if(SUCCEEDED(m_pD3DEffect->SetTechnique("PositionColor"))){UINT numberOfPasses;m_pD3DEffect->Begin(&numberOfPasses, NULL);for(UINT i = 0; i < numberOfPasses; i ++){m_pD3DEffect->BeginPass(i);// --> Draw Terrain <--m_pCTerrain->RenderTerrain(m_pD3DDev9);m_pD3DEffect->EndPass();}m_pD3DEffect->End();}// Set new Technique and Variables//D3DXMatrixIdentity(&m_matWorld);D3DXMATRIX matRotationX, matScale, matTranslation;D3DXMatrixScaling(&matScale, 15.0f, 15.0f, 15.0f);D3DXMatrixRotationX(&matRotationX, D3DXToRadian(90));D3DXMatrixTranslation(&matTranslation, 0.0f, 0.0f, 15.0f);m_matWorld = matScale * matRotationX * matTranslation;m_pD3DEffect->SetMatrix("xWorld", &m_matWorld);if(SUCCEEDED(m_pD3DEffect->SetTechnique("PositionTextureLight"))){UINT numberOfPasses;m_pD3DEffect->Begin(&numberOfPasses, NULL);for(UINT i = 0; i < numberOfPasses; i ++){m_pD3DEffect->BeginPass(i);// --> Draw Mesh here <--m_pCMesh->RenderMesh(m_pD3DDev9);m_pD3DEffect->EndPass();}m_pD3DEffect->End();}m_pD3DDev9->EndScene();m_pD3DDev9->Present(NULL, NULL, NULL, NULL);}}And the specific RenderMesh function is this one:void CMesh::RenderMesh(LPDIRECT3DDEVICE9EX device){device->SetVertexDeclaration(m_pD3DVertexDecl9);for(DWORD i = 0; i < m_dwNumberMaterials; i++){device->SetMaterial(&m_pD3DMeshMaterials);device->SetTexture(0, m_pD3DMeshTextures);m_pD3DMesh->DrawSubset(i);}}Also this one:HRESULT CMesh::InitializeVertexDeclaration(LPDIRECT3DDEVICE9EX device){// Create VertexDeclarationif(FAILED(device->CreateVertexDeclaration(declarationMesh, &m_pD3DVertexDecl9))){MessageBox(NULL, L"Failed to create VertexDeclaration", L"CreateVertexDeclaration() - HRESULT", MB_OK);return E_FAIL;}return S_OK;}there's no problem with the calling process since it worked before without the Normal/Lighting code.   ;  I noticed that you're using DrawSubset.  I have a feeling that you will need to ensure that your mesh has the necessary vertex buffers set correctly with regard to vertex stride.  You might want to take a closer look at how DrawSubset works internally.How are you setting your mesh up?   ;  Nope I'm using D3DXMESH_SYSTEMEM.anyway here's the mesh loading code:HRESULT CMesh::LoadMesh(LPDIRECT3DDEVICE9EX device, LPCWSTR MeshLocation){if(FAILED(InitializeVertexDeclaration(device))){MessageBox(NULL, L"Failed to initialize VertexDeclaration (CMesh)", L"InitializeVertexDeclaration() - HRESULT", MB_OK);return E_FAIL;}if(FAILED(D3DXLoadMeshFromX(MeshLocation,D3DXMESH_SYSTEMMEM,device,NULL,&m_pD3DMaterialBuffer,NULL,&m_dwNumberMaterials,&m_pD3DMesh))){MessageBox(NULL, L"Failed to load mesh from file", L"D3DXLoadMeshFromX() - HRESULT", MB_OK);return E_FAIL;}// extract Material information from the X file Buffer to our own Material bufferD3DXMATERIAL* pMaterials = (D3DXMATERIAL*)m_pD3DMaterialBuffer-&gt;GetBufferPointer();m_pD3DMeshMaterials = new D3DMATERIAL9[m_dwNumberMaterials];m_pD3DMeshTextures = new LPDIRECT3DTEXTURE9[m_dwNumberMaterials];for(DWORD i = 0; i &lt; m_dwNumberMaterials; i++){// copy the materialm_pD3DMeshMaterials = pMaterials.MatD3D;// Set ambient color for materialsm_pD3DMeshMaterials.Ambient = m_pD3DMeshMaterials.Diffuse;// Create texture for mesh (if it exists)m_pD3DMeshTextures = NULL;if(pMaterials.pTextureFilename){if(FAILED(D3DXCreateTextureFromFileA(device, pMaterials.pTextureFilename, &m_pD3DMeshTextures))){MessageBox(NULL, L"Failed to create Texture from file", L"D3DXCreateTextureFromFile() - HRESULT", MB_OK);return E_FAIL;}}}return S_OK;};  Are you sure your mesh file contains the correct vertex declaration?  I don't know an awful lot about meshes, but I'd say your mesh file is being loaded without normals and you're trying to set the vertex declaration to include them.   ;Quote:Original post by RobMaddisonAre you sure your mesh file contains the correct vertex declaration?  I don't know an awful lot about meshes, but I'd say your mesh file is being loaded without normals and you're trying to set the vertex declaration to include them.I solved it! Yeah that's sort of exactly what was wrong.The stupid DirectX Sample Mesh (tiger.x) did not have normals.So the VS was waiting for a normal which he(she, it ? xD) did not get.While this isn't really an error(?) I fixed it now ;)   