Head tracking projection matrix
Hi,
I have been mucking around re-writing Jonny Lee's wiimote vr desktop in c++. Could somebody please explain why, when you move your head CLOSER to the screen, the back wall appears FURTHER away?!
Here is the code I am using to Render a single frame. headDist, headX, and headY are values for the head being tracked. They are respectivelt the distance of head from screen, head x position and headY position.
//-------------------------------------------------
void RenderFrame()
{
float screenAspect = (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT;
// clear the window
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene(); // begins the 3D scene
// select which vertex format we are using
d3ddev->SetFVF(CUSTOMFVF);
// select the vertex buffer to display
d3ddev->SetStreamSource(0, vertexBuffer, 0, sizeof(CUSTOMVERTEX));
RenderWorldTransform(screenAspect);
if(world._wiimoteConnected)
{
RenderViewTransform(world.GetHeadX(), world.GetHeadY(), world.GetHeadDist());
RenderProjTransform(world.GetHeadX(), world.GetHeadY(), world.GetHeadDist(), screenAspect);
}
else
{
RenderViewTransform(0, 0, 90.0);
RenderProjTransform(0, 0, 90.0, screenAspect);
}
d3ddev->EndScene(); // ends the 3D scene
d3ddev->Present(NULL, NULL, NULL, NULL); // displays the created frame on the screen
return;
}
void RenderWorldTransform(float screenAspect)
{
int NumberOfLines;
float boxdepth;
D3DXMATRIX matTranslate1;
D3DXMATRIX matTranslate2;
D3DXMATRIX matTranslate3;
D3DXMATRIX matTranslate4;
D3DXMATRIX matTranslate5;
D3DXMATRIX matTranslate6;
D3DXMATRIX matTranslate7;
D3DXMATRIX matScale1;
D3DXMATRIX matScale2;
D3DXMATRIX matScale3;
D3DXMATRIX matScale4;
D3DXMATRIX matRot1;
D3DXMATRIX matRot2;
D3DXMATRIX flipY;
boxdepth = 15;
NumberOfLines = 18;
D3DXMatrixTranslation(&matTranslate1, -0.5f, -0.5f, -1*boxdepth/2);
D3DXMatrixTranslation(&matTranslate2, -0.5f, -0.5f, 0);
D3DXMatrixTranslation(&matTranslate3, 0.5f * screenAspect, 0, -.5f*boxdepth/2);
D3DXMatrixTranslation(&matTranslate4, -1.0f * screenAspect, 0, 0);
D3DXMatrixTranslation(&matTranslate5, 0, 0.5f, -0.5f*boxdepth/2);
D3DXMatrixTranslation(&matTranslate6, 0, -1.0f, 0);
D3DXMatrixTranslation(&matTranslate7, -0.5f, -0.25f, 3.0f);
D3DXMatrixScaling(&matScale1, screenAspect, 1.0f, 1.0f);
D3DXMatrixScaling(&matScale2, 1 * boxdepth / 2, 1.0f, 1.0f);
D3DXMatrixScaling(&matScale3,screenAspect , 1 * boxdepth / 2, 1.0f);
D3DXMatrixScaling(&matScale4, screenAspect/2, screenAspect/2, 1.0f);
D3DXMatrixRotationY(&matRot1, D3DXToRadian(90));
D3DXMatrixRotationX(&matRot2, D3DXToRadian(90));
D3DXMatrixRotationY(&flipY, D3DXToRadian(180));
//draw back wall
d3ddev->SetTransform(D3DTS_WORLD, &(matTranslate1 * matScale1) );
d3ddev->DrawPrimitive(D3DPT_LINELIST,0,NumberOfLines);
//draw left wall
d3ddev->SetTransform(D3DTS_WORLD, &(matTranslate2 * matScale2 * matRot1 * matTranslate3) );
d3ddev->DrawPrimitive(D3DPT_LINELIST, 0, NumberOfLines);
//draw right wall
d3ddev->SetTransform(D3DTS_WORLD, &(matTranslate2 * matScale2 * matRot1 * matTranslate3 * matTranslate4) );
d3ddev->DrawPrimitive(D3DPT_LINELIST, 0, NumberOfLines);
//draw top wall
d3ddev->SetTransform(D3DTS_WORLD, &(matTranslate2 * matScale3 * matRot2 * matTranslate5) );
d3ddev->DrawPrimitive(D3DPT_LINELIST, 0, NumberOfLines);
//draw bottom wall
d3ddev->SetTransform(D3DTS_WORLD, &(matTranslate2 * matScale3 * matRot2 * matTranslate5 * matTranslate6) );
d3ddev->DrawPrimitive(D3DPT_LINELIST, 0, NumberOfLines);
}
void RenderProjTransform(float headX, float headY, float headDist, float screenAspect)
{
D3DXMATRIX matProjection;
float nearPlane = .05f;
D3DXMatrixPerspectiveOffCenterLH(&matProjection,
nearPlane*((-.5f * screenAspect) + headX)/headDist,//Minimum x-value of the view volume.
nearPlane*((.5f * screenAspect) + headX)/headDist, //Maximum x-value of the view volume.
nearPlane*(-.5f - headY)/headDist, //Minimum y-value of the view volume.
nearPlane*(.5f - headY)/headDist, //Maximum y-value of the view volume.
nearPlane, //Minimum z-value of the view volume.
100); //Maximum z-value of the view volume.
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);
}
void RenderViewTransform(float headX, float headY, float headDist)
{
D3DXMATRIX matView; // the view transform matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (headX, headY, headDist), // the camera position
&D3DXVECTOR3 (headX, headY, 0.0f), // the look-at position
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView
}
//--------------------------------------------------
[Edited by - joe90p on December 27, 2009 5:19:18 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement