OK, so understanding it and implementing it are two different things. I'm not sure what I'm doing wrong, but this code is throwing errors at runtime. So far, this is how I'm initializing my shader:
void MyWorldObj::initShader( void ){ D3DVERTEXELEMENT9 declaration[] = { { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 }, { 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 }, { 0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 }, { 0, 40, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() }; g_pd3dDevice->CreateVertexDeclaration(declaration, &g_pVertexDeclaration); HRESULT hr; LPD3DXBUFFER pCode; DWORD dwShaderFlags = 0; LPD3DXBUFFER pBufferErrors = NULL; // Assemble the vertex shader from the file hr = D3DXCompileShaderFromFile( "vertex_shader.vsh", NULL, NULL, "main", "vs_1_1", dwShaderFlags, &pCode, &pBufferErrors, &g_pConstantTableVS ); if( FAILED(hr) ) { LPVOID pCompilErrors = pBufferErrors->GetBufferPointer(); MessageBox(NULL, (const char*)pCompilErrors, "Vertex Shader Compile Error", MB_OK|MB_ICONEXCLAMATION); } // Create the vertex shader g_pd3dDevice->CreateVertexShader( (DWORD*)pCode->GetBufferPointer(), &g_pVertexShader ); pCode->Release(); }
And this is the HLSL vertex_shader.vsh file that is loaded by D3DXCompileShaderFromFile:
float4x4 worldViewProj;struct VS_INPUT{ float3 position : POSITION0; float3 normal : NORMAL0; float4 color : COLOR0; float2 texcoord : TEXCOORD0;};struct VS_OUTPUT{ float4 hposition : POSITION0; float3 normal : NORMAL0; float4 color : COLOR0; float2 texcoord : TEXCOORD0;};VS_OUTPUT main( VS_INPUT IN ){ VS_OUTPUT vOut; float4 v = float4( IN.position.x, IN.position.y, IN.position.z, 1.0f ); vOut.hposition = mul( v, worldViewProj ); vOut.normal = IN.normal; vOut.color = IN.color; vOut.texcoord = IN.texcoord; return vOut;}
So far, as far as my understanding, everything should work fine, and the code does compile without errors. Unfortunately D3DXCompileShaderFromFile HRESULT fails with the error: "invalid output semantics 'NORMAL0'" and VS.NET debugger breaks on g_pd3dDevice->CreateVertexShader. What am I missing here? Is the pixel shader neccessary to run without error? From my understanding, this vertex shader is the simplest shader that can be written in HLSL and should just output everything input to it plus transform world coordinates to screen coordinates and thus take over the pipeline. Why can't I implement it? Ho-hum,
I do thank everyone who's given their time to this! As soon as I get this running, I'll be sure to post the working code so everyone can understand how to do this. There just aren't enough
practical tutorials available for "putting it all together" when it comes to shaders. So, once again, thank you!