I will try to explain how the engine I am developing works so it'll be easier for you to see what am I doing wrong.
I have a class named CModel, which stores the local matrix of the model, a vector with the position it was in the 3D design program, the vertex buffer, the index buffer and an instance of the material the object uses. I initialize a different CModel instance for each model in the scene.
I initialize each object and translate them to the position (0, 0, 0) using their local matrix, which will position them where they were in the 3D modeling program.
I then call the shader for each object in the scene (is this correct?) and pass in 3 matrices, the local matrix from the model, the view matrix from the camera and the projection matrix, also from the camera.
Here is the Render function:
bool CGraphics::Render(){unsigned int stride = sizeof(SimpleVertex);unsigned int offset = 0;// Clear the render targetD3Ddeviceptr->ClearRenderTargetView(RenderTargetptr, D3DXCOLOR(0.0f, 0.0f, 0.2f, 0.0f));D3Ddeviceptr->ClearDepthStencilView(DepthStencilViewptr, D3D10_CLEAR_DEPTH, 1.0, 0);// Render the first object of the arrayfor (unsigned int count = 0; count < models.size(); count++){ shader->RenderObject(models[count], camera, stride, offset); //shader->RenderBitmap(bitmap, camera);}// Swap the buffersswapChainptr->Present(0, 0);return true;}Here is the RenderObject function:
void CShader::RenderObject(CModel *model, CCamera *camera, unsigned int stride, unsigned int offset){pEffectDevice->IASetVertexBuffers(0, 1, model->ppGetVertexBuffer(), &stride, &offset);pEffectDevice->IASetIndexBuffer(model->pGetIndexBuffer(), DXGI_FORMAT_R32_UINT, 0); [/color][color=#000000]D3D10_TECHNIQUE_DESC technique;[/color][color=#000000]pWorldMatrixVariable->SetMatrix( (float*) &model->worldMatrix);pViewMatrixVariable->SetMatrix( (float*) &camera->viewMatrix );pProjectionMatrixVariable->SetMatrix( (float*) &camera->projectionMatrix );[/color][color=#000000]for (unsigned int t = 0; t < model->GetpMaterial()->pGetTextures()->size(); t++){ pTextureVariable->SetResource( model->GetpMaterial()->pGetTextureByIndex(t) );}[/color][color=#000000]pTechnique->GetDesc(&technique);[/color][color=#000000]for (UINT p = 0; p < technique.Passes; ++p){ pTechnique->GetPassByIndex(p)->Apply(0); pEffectDevice->DrawIndexed(model->GetNumIndices(), 0, 0);}[/color][color=#000000]return;}Here is my shader:
Texture2D tex2D;SamplerState linearSampler{Filter = MIN_MAG_MIP_LINEAR;AddressU = Wrap;AddressV = Wrap;};// --------------------------------------------------------------// GLOBALS// --------------------------------------------------------------matrix World;matrix View;matrix Projection;float4 AmbientColor = float4(0.35f, 0.35f, 0.35f, 1.0f);float4 DiffuseColor = float4(0.0f, 0.0f, 1.0f, 1.0f);float3 LightDirection = float3(0.0f, 0.0f, 1.0f);//--------------------------------------------------------------------------------------struct VS_INPUT{ float4 Pos : POSITION;float2 TexCoord : TEXCOORD;float3 Normal : NORMAL;};struct PS_INPUT{ float4 Pos : SV_POSITION;float2 TexCoord : TEXCOORD0;float3 Normal : NORMAL;float4 Color : COLOR0;};// Lighting function// -----------------------------------------------------float4 CalcPhongLighting( float4 LColor, float3 Normal, float3 LRay, float3 Eye, float3 Phi ){float4 Ia = AmbientColor;float4 Id = saturate( dot( Normal, normalize( LRay ) ) );return Ia * Id;}//--------------------------------------------------------------------------------------// Vertex Shader//--------------------------------------------------------------------------------------PS_INPUT VS( VS_INPUT input ){float4 WorldPos;float3 eye = (0.0f, 4.0f, -50.0f);input.Pos.w = 1.0f; PS_INPUT output = (PS_INPUT)0; output.Pos = mul( input.Pos, World ); output.Pos = mul( output.Pos, View ); output.Pos = mul( output.Pos, Projection );output.TexCoord = input.TexCoord;float3 Normal = normalize( mul( input.Normal, (float3x3) World ) );float3 View = normalize( eye - (float3) input.Pos );float3 Phi = reflect( normalize( LightDirection ), Normal );output.Color = CalcPhongLighting( DiffuseColor, Normal, LightDirection, View, Phi ); return output;}//--------------------------------------------------------------------------------------// Pixel Shader//--------------------------------------------------------------------------------------float4 PS( PS_INPUT input ) : SV_TARGET{return tex2D.Sample( linearSampler, input.TexCoord ) * input.Color;}//--------------------------------------------------------------------------------------technique10 Render{ pass P0 { SetVertexShader( CompileShader( vs_4_0, VS() ) ); SetGeometryShader( NULL ); SetPixelShader( CompileShader( ps_4_0, PS() ) ); }}I simply don't know how to transform all the local matrices from each mesh to a single world matrix (which is the one I should pass to the shader, I suppose)
This is how the scene looks like when all the meshes have been set to their (0, 0, 0)
Spoiler
Thanks in advance.







