chronozphere
24-02-2007, 01:24 PM
Hi PGD'ers. :)
I have discovered something weird.
The Z buffers isn't working properly when you change your projection matrix. I had this problem with my engine so i decided to test it using one of clooties tut's. :)
My engine supports different view/projection matrices to be used in one scene. So it is possible to render with perspective/orthogonal/screen coordinates etc.
But this requires changing the projection matrix, and that delivers a wierd result.
I've used this code to test. (its from the Mesh tutorial in clooties SDK, just replace the render code with this code).
procedure Render;
var
i: DWORD;
matWorld,matProj,matView: TD3DXmatrix;
begin
// Clear the backbuffer and the zbuffer
g_pd3dDevice.Clear(0, nil, D3DCLEAR_TARGET or D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0,0,255), 1.0, 0);
// Begin the scene
g_pd3dDevice.BeginScene;
//setup matrices
D3DXMatrixRotationY(matWorld, timeGetTime/1000.0);
g_pd3dDevice.SetTransform(D3DTS_WORLD, matWorld);
D3DXMatrixLookAtLH(matView, D3DXVector3(0.0, 0.0,-5.0),
D3DXVector3(0.0, 0.0, 0.0),
D3DXVector3(0.0, 1.0, 0.0));
g_pd3dDevice.SetTransform(D3DTS_VIEW, matView);
D3DXMatrixPerspectiveFovLH(matProj, D3DX_PI/4, 1.0, 1.0, 100.0);
g_pd3dDevice.SetTransform(D3DTS_PROJECTION, matProj);
//render first tiger
for i:= 0 to g_dwNumMaterials - 1 do
begin
g_pd3dDevice.SetMaterial(g_pMeshMaterials[i]);
g_pd3dDevice.SetTexture(0, g_pMeshTextures[i]);
g_pMesh.DrawSubset(i);
end;
//translate second tiger
D3DXMatrixTranslation(matWorld, 1,0,0);
g_pd3dDevice.SetTransform(D3DTS_WORLD, matWorld);
//set new projection matrix
D3DXMatrixOrthoLH(matProj, 5.0, 5.0, 1.0, 100.0);
g_pd3dDevice.SetTransform(D3DTS_PROJECTION, matProj);
//render second tiger
for i:= 0 to g_dwNumMaterials - 1 do
begin
g_pd3dDevice.SetMaterial(g_pMeshMaterials[i]);
g_pd3dDevice.SetTexture(0, g_pMeshTextures[i]);
g_pMesh.DrawSubset(i);
end;
g_pd3dDevice.EndScene;
g_pd3dDevice.Present(nil, nil, 0, nil);
end;
The weird thing is that the second tiger is ALWAYS drawn over the first tiger. No matter what value i use for the Z translation of the second tiger. There is no way to make the meshes intersect, it's just like rendering 2d images of a tiger.
The ZBuffer was enabled during these tests.
What is the cause of this?? does D3D reset the zbuffer every time you change the projection matrix??
thanks in advance. ;)
I have discovered something weird.
The Z buffers isn't working properly when you change your projection matrix. I had this problem with my engine so i decided to test it using one of clooties tut's. :)
My engine supports different view/projection matrices to be used in one scene. So it is possible to render with perspective/orthogonal/screen coordinates etc.
But this requires changing the projection matrix, and that delivers a wierd result.
I've used this code to test. (its from the Mesh tutorial in clooties SDK, just replace the render code with this code).
procedure Render;
var
i: DWORD;
matWorld,matProj,matView: TD3DXmatrix;
begin
// Clear the backbuffer and the zbuffer
g_pd3dDevice.Clear(0, nil, D3DCLEAR_TARGET or D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0,0,255), 1.0, 0);
// Begin the scene
g_pd3dDevice.BeginScene;
//setup matrices
D3DXMatrixRotationY(matWorld, timeGetTime/1000.0);
g_pd3dDevice.SetTransform(D3DTS_WORLD, matWorld);
D3DXMatrixLookAtLH(matView, D3DXVector3(0.0, 0.0,-5.0),
D3DXVector3(0.0, 0.0, 0.0),
D3DXVector3(0.0, 1.0, 0.0));
g_pd3dDevice.SetTransform(D3DTS_VIEW, matView);
D3DXMatrixPerspectiveFovLH(matProj, D3DX_PI/4, 1.0, 1.0, 100.0);
g_pd3dDevice.SetTransform(D3DTS_PROJECTION, matProj);
//render first tiger
for i:= 0 to g_dwNumMaterials - 1 do
begin
g_pd3dDevice.SetMaterial(g_pMeshMaterials[i]);
g_pd3dDevice.SetTexture(0, g_pMeshTextures[i]);
g_pMesh.DrawSubset(i);
end;
//translate second tiger
D3DXMatrixTranslation(matWorld, 1,0,0);
g_pd3dDevice.SetTransform(D3DTS_WORLD, matWorld);
//set new projection matrix
D3DXMatrixOrthoLH(matProj, 5.0, 5.0, 1.0, 100.0);
g_pd3dDevice.SetTransform(D3DTS_PROJECTION, matProj);
//render second tiger
for i:= 0 to g_dwNumMaterials - 1 do
begin
g_pd3dDevice.SetMaterial(g_pMeshMaterials[i]);
g_pd3dDevice.SetTexture(0, g_pMeshTextures[i]);
g_pMesh.DrawSubset(i);
end;
g_pd3dDevice.EndScene;
g_pd3dDevice.Present(nil, nil, 0, nil);
end;
The weird thing is that the second tiger is ALWAYS drawn over the first tiger. No matter what value i use for the Z translation of the second tiger. There is no way to make the meshes intersect, it's just like rendering 2d images of a tiger.
The ZBuffer was enabled during these tests.
What is the cause of this?? does D3D reset the zbuffer every time you change the projection matrix??
thanks in advance. ;)