Original Post
Hi,
I''ve been messing with the camera for a while, and I''ve noticed some things that I''ve got questions on. For the sake of simplicity, I''m restricting the camera to be fixed to the z axis.
Ok, so say we have an object centered at the world origin, and at 1.0 on the z-axis. In all these questions the cameras look-at point is the origin.
If I place the camera''s eye point at 0,0,-1, this makes it so the camera looks into the screen. Since the object is at 1.0 on the z-axis, its two units away. According to the DX docs on the construction of the view matrix, the translation for this object would be -(n dot c), the dot product between the camera direction vector (n) and the camera world position (c). The camera direction vector is the lookat point - the eyepoint, in this case (0,0,0) - (0,0,-1) = (0,0,1). So,
-(n dot c) = -[(0,0,1) dot (0,0,-1)] = -(0,0,-1) = (0,0,1).
So in camera space, the object will be at (0,0,2), which is correct, since its two units away.
So, if I then place the cameras eye point at (0,0,1) and do those calculations, we end up with the same translation of (0,0,1) on the z-axis. The calculations I did are -(n dot c) = -[(0,0,-1) dot (0,0,1)] = -(0,0,-1) = (0,0,1). According to that, in view space the object should be at 2 on the z-axis, but when I run the program and place the camera at (0,0,2) so the object is 1 unit away, it works normally. How can this be? Did I calculate something wrong? Is there information missing from the docs?
Another thing that is pretty weird, if I translate my object by +1 unit on the z-axis, apply the view matrix, and then translate back 1 unit (combining these into one matrix or T-1 * V * T), nothing happens when the camera is at (0,0,-1) or anywhere on the negative z axis. This is what should happen, since the translations should have a total effect of 0. When I place the camera at (0,0,1) or anywhere positive, something DOES happen. The object appears to be 2 units further than it should be into the screen. So if I set the camera to (0,0,2), the object gets displayed as if its 3 units away, even though its 1 unit away (or should be). The object is displayed as if the camera were at (0,0,-2). How the heck can that happen? Something to do with what I noticed previously maybe?
Some other observations:
If I seperate out the first translation, so I would do T and then T(-1) * V, regardless of where the camera is on the axis the proper thing is displayed (in other words theres no difference with or without the translations, as it should be). Something weird happening when I''m combing them?
I''m pretty sure my code is just fine, and this message is long enough as is, but heres the transformation code that I was referring to when I talked about changing stuff above, if that helps.
//heres the verts I''m using
CUSTOMVERTEX g_Vertices[] =
{
{ -0.3f, -0.3f, 1.0f, 0xff00ff00, },
{ 0.3f, -0.3f, 1.0f, 0xff0000ff, },
{ -0.3f, 0.3f, 1.0f, 0xffff0000, },
{ 0.3f, 0.3f, 1.0f, 0xffffffff, },
};
D3DXMATRIX matWorld;
D3DXMatrixTranslation( &matWorld, 0.0f, 0.0f, 0.0f );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
D3DXMATRIX matView;
D3DXMATRIX matTrans, matTrans2;
D3DXVECTOR3 vecEye( 0.0f, 0.0f, 2.0f );
D3DXMatrixTranslation( &matTrans, 0.0f, 0.0f, 1.0f );
D3DXMatrixTranslation( &matTrans2, 0.0f, 0.0f, -1.0f );
D3DXMatrixLookAtLH( &matView, &vecEye,
&D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
&D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );
//uncomment those multiplies if I want to experiment
D3DXMATRIX matTemp;
//D3DXMatrixMultiply( &matTemp, &matView, &matTrans );
//D3DXMatrixMultiply( &matView, &matTrans2, &matView );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 4.0f / 3.0f, 1.0f, 1000.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
Thanks a ton if anyone has some answers to these odd problems! If no one does am I safe to assume that this is a bug with Direct X? I very seriously doubt thats the case, something must be up!
Best Regards,
Tom Rhodes