I've been working on my own 3D Engine using Direct3D. This went pretty good, until I ran into an issue with the World View and Projection Matrices. I'am using code from previous projects that I worked, but doesn't in the project. So I decided to use the Direct3D functions D3DXMatrixLookAtLH and D3DXMatrixPerspectiveFovLH.
This gives a working matrices, but if by rotating or translating the camera I move geometry close to the edge of the screen, it stretches that entire object in the horizontal axis, now i'm not sure if this is correct or not.
Cube in the middle of the screen looks normal:
But if I move it to the side it starts to stretch:
This is the View matrix that I get in such a case:
-0.81964797 -0.017183444 -0.57260966 0.00000000
0.00000000 0.99954993 -0.029995499 0.00000000
0.57286739 -0.024585750 -0.81927919 0.00000000
-1.1457349 0.049171507 1.6385586 1.0000000
I also noticed that between the WVP matrix in the application and the matrix the shader receives it gets transposed. ( I use memcpy to get it in the CBuffer, so that shouldn't be the problem) This is not a major issue, I just transpose it before it gets send, but I would like to know if this is common.
this is my shader:
cbuffer WVP :cb0
{
float4x4 WVP;
};
struct VOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
VOut VShader(float3 position : POSITION, float4 color : COLOR)
{
VOut output;
output.position = mul( float4(position, 1.0f), WVP);
output.color = color;
return output;
}
float4 PShader(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
{
return color;
}
This is the code in that happens every frame to update the View matrix according to input:
// reset the vectors so the changes don't stack
vcPos.Set(x, y, z);
vcDir.Set(0.0f, 0.0f, -1.0f);
// Rotate the dir vector
Matrix3D m;
m.Rota(Looky/100, 0.0f, Lookx/100);
vcDir.RotateWith(m);
// add the pos to the dir vector so it points to in a direction rather then a point in the world
vcDir += vcPos;
// Set and get the View matrix
g_pDevice->SetViewMatrix(vcRight, vcUp, vcDir, vcPos );
g_pDevice->GetViewProjMatrix(&g_mView, NULL);
// Multiply them
g_mWVP = g_mWorld * g_mView * g_mProj;
// Transpose them so the shader tranposes them back.
Transpose(&g_mWVP);
And this is the behind the scenes part of it:
View:
D3DXMatrixLookAtLH(&m_mView, &D3DXVECTOR3(vcPos.x, vcPos.y, vcPos.z),
&D3DXVECTOR3(vcDir.x, vcDir.y, vcDir.z), &D3DXVECTOR3(vcUp.x, vcUp.y, vcUp.z)); // The .x/.y/.z has to do with some compatability stuff
Proj:
D3DXMatrixPerspectiveFovLH(&m_mProj, FOV, Aspect, m_fNear, m_fFar); // Straight
forward
D3DXMatrixPerspectiveFovLH(&m_mProj, 90.0, Aspect, 0.1f, 100.0f); // With values
Update2: Complete rewrite with new information
Update3: I'm having problems with the View matrix and maybe the Projection. The World matrix works fine.
Update4:
After changing some more code, I now have something that seems to be working! The only Problem I have right now is the stretching to the side.