   HLSL Texture render to Mesh
  Hey, I'm having some problems with rendering a texture onto a mesh, in DirectX9 with HLSL, I create the vertex decl as such:const D3DVERTEXELEMENT9 g_aVertexDeclaration[4] ={{0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   0},{0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},D3DDECL_END()};And I render it with this HLSL codeuniform extern float3 g_fModelColour;uniform extern float  g_fModelReflectivity;uniform extern float4x4 g_mWorld;uniform extern float4x4 g_mWorldViewProj;uniform extern float3 g_vEyePt;uniform extern textureCUBE g_tCube;uniform extern texture2D g_tTexture;samplerCUBE CubeSampler = sampler_state{    Texture = <g_tCube>;    MinFilter = Linear;    MagFilter = Linear;    MipFilter = Linear;};sampler2D TextureSampler = sampler_state{    Texture = <g_tTexture>;    MinFilter = Linear;    MagFilter = Linear;    MipFilter = Linear;};//-----------------------------------------------------------------------------// Name: SceneVS// Type: Vertex Shader// Desc: //-----------------------------------------------------------------------------struct SceneVS_Input{    float3 Pos : POSITION;    float3 Normal : NORMAL;    float2 TexCoord : TEXCOORD;};struct SceneVS_Output{    float4 Pos : POSITION;    // Transformed position    float3 Normal_World : TEXCOORD0;    // Normal (world space)    float3 ViewVec_World : TEXCOORD1; // View vector (world space)    float2 TexCoord : TEXCOORD2;};SceneVS_Output SceneVS(SceneVS_Input Input){    SceneVS_Output Output;   Output.Pos = mul(float4(Input.Pos, 1.0f), g_mWorldViewProj);   Output.Normal_World = mul(Input.Normal, (float3x3)g_mWorld);   float4 pos_World = mul( float4(Input.Pos, 1.0f), g_mWorld );    Output.ViewVec_World = pos_World.xyz - g_vEyePt;   Output.TexCoord = Input.TexCoord;   return Output;}//-----------------------------------------------------------------------------// Name: ScenePS// Type: Pixel Shader// Desc: Environment Mapping and simplified hemispheric lighting//-----------------------------------------------------------------------------float4 ScenePS(SceneVS_Output Input) : COLOR{    // Sample the environment map    float3 vReflect = reflect(Input.ViewVec_World, Input.Normal_World);    float4 vEnvironment = texCUBE(CubeSampler, vReflect);   // Simple overhead lighting    float3 vColour = saturate( g_fModelColour * Input.Normal_World.y );   // Add in reflection    vColour = lerp( vColour, vEnvironment.rgb, g_fModelReflectivity );   vColour *= tex2D(TextureSampler, Input.TexCoord);   return float4( vColour, 1.0f );}//-----------------------------------------------------------------------------// Technique: EnvironmentMapping// Desc: //-----------------------------------------------------------------------------technique EnvironmentMapping{    pass p0    {   VertexShader = compile vs_2_0 SceneVS();   PixelShader = compile ps_2_0 ScenePS();    }}But when I try to render it using DrawIndexedPrimitive, the function fails and I don't see anything where my mesh should be, when I remove the texture code from the hlsl it renders fine with just environment mapping, here's the debug output info;First-chance exception at 0x7c812afb in RTHDRIBLEngine.exe: Microsoft C++ exception: long at memory location 0x0012f728..Direct3D9: (ERROR) :DrawIndexedPrimitive failed.Direct3D9: (ERROR) :Vertex shader function usage (D3DDECLUSAGE_TEXCOORD, 0) does not have corresponding usage in the current vertex declarationDirect3D9: (INFO) :The vertex declaration is (Stream, Offset, Type, Method, Usage, UsageIndex):Direct3D9: (INFO) :0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0Direct3D9: (INFO) :0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0First-chance exception at 0x7c812afb in Engine.exe: Microsoft C++ exception: long at memory location 0x0012f728..Direct3D9: (ERROR) :DrawIndexedPrimitive failed.
  Well what you have looks correct.  If you could post your render loop where you set the parameters in your shader as well as where you initialize your handles I could tell you more.  Also, are you sure that you are actually setting your vertex declaration with IDirect3DDevice9::SetVertexDeclaration and that you are not overwriting it with another vertex declaration later on in the program?John   ;  Hey, here's the render loop code:/**********************************************************************************Render**********************************************************************************/HRESULT Mesh::Render(D3DXMATRIX*  pmWorld,  D3DXMATRIX*  pmWorldViewProj,  D3DXVECTOR3* pvEye,  LPDIRECT3DCUBETEXTURE9 pCubeTexture){HRESULT hr;UINT uiPassCount, uiPass;D3DXHANDLE hHandle = m_pEffect->GetTechniqueByName("EnvironmentMapping");hr = m_pEffect->SetTechnique(hHandle);CHECK_RESULT(hr, "Could not set technique");hr = m_pEffect->SetValue("g_vEyePt", pvEye, sizeof(D3DXVECTOR3)); CHECK_RESULT(hr, "Could not set value 1");hr = m_pEffect->SetValue("g_fModelColour", &m_vColour, sizeof(D3DXVECTOR3));CHECK_RESULT(hr, "Could not set value 2, RENDER");hr = m_pEffect->SetMatrix("g_mWorldViewProj", pmWorldViewProj);CHECK_RESULT(hr, "Could not set value 3");hr = m_pEffect->SetMatrix("g_mWorld", pmWorld);CHECK_RESULT(hr, "Could not set value 4");hr = m_pEffect->SetTexture("g_tCube", pCubeTexture);CHECK_RESULT(hr, "Could not set value 5");hr = m_pEffect->SetFloat("g_fModelReflectivity", m_fModelReflectivity);CHECK_RESULT(hr, "Could not set value 6");hr = m_pEffect->SetTexture("g_tTexture", m_pTexture);CHECK_RESULT(hr, "Could not set value 7");hr = m_pd3dDevice->SetVertexDeclaration(m_pVertexDecl);CHECK_RESULT(hr, "Could not set VertexDecl");hr = m_pEffect->Begin(&uiPassCount, 0);CHECK_RESULT(hr, "Could not begin effect");for(uiPass = 0; uiPass < uiPassCount; uiPass++){m_pEffect->BeginPass(uiPass);m_pMesh->DrawSubset(0);m_pEffect->EndPass();}m_pEffect->End();return S_OK;};  Hmm, as far as I can tell, your code is correct.  As long as hr is S_OK everything should be working...  The only advice I can give you is to setting the cullmode to none and making it wireframe.  You could also try simply returning float4(1,1,1,1) from your pixel shader to make sure that the shader is rendering.John   ;  Hi,I see that you've just created a VERTEXELEMENT. But did you create and set a declaration over this VERTEXELEMENT? Or, did you clone your mesh /w that declaration?If no, you need to clone your mesh so that final mesh's vertex declaration can contain POS, NOR and TEXCOORD data types/elements:LPD3DXMESH YourCloneMesh = NULL;m_pYourMesh->CloneMesh (m_mYourMesh->GetOptions(), g_aVertexDeclaration, m_pYourD3DDevice, &YourCloneMesh);And render via YourCloneMesh->DrawSubset(AnySubSetNumber);Hope this helps.Regards,Rohat.   ;  Hey programci_84, that seems to have worked, but I'm not sure if the texture is being rendered properly as with out the texture I get this:http://img13.imageshack.us/img13/5442/59317968.pngand with I get thishttp://img13.imageshack.us/img13/1088/85860625.pngand the texture I pass in is a brick texture and all I get is that it is slightly darker over the entire mesh.   ;  I think that it was about your shader. You've not normalized all the vectors (i.e. Normal and Reflection vectors).Also, I think that you must perform linear interpolation (lerp) after multiplying sampled texture color. I mean, just swap the line contains lerp(...) and the line contains tex2D (...) (The last 3rd and 2nd lines of pixel shader).Hope you can understand (my bad English) :)   ;  Don't worry about your English, it's hard enough for those of us who are native speakers :PI'm not sure I understand, perhaps some code would make it easier to understand.Thanks   ;  Alright,I only write the last three lines of pixel shader:float4 ScenePS(SceneVS_Output Input) : COLOR{    ...    vColour = tex2D(TextureSampler, Input.TexCoord);    // Add in reflection    vColour *= lerp( vColour, vEnvironment.rgb, g_fModelReflectivity );   return float4( vColour, 1.0f );}Also, it would be better if you used AddressU = WRAP; and AddressV = WRAP; lines in sampler state definitions and normalized Output.Normal_World vector by using normalize() function.Hope this helps.   ;  Hey,it doesn't seem to have fixed the problem, this is what I am now getting;http://img17.imageshack.us/img17/4259/60514861.pngthanks   