Hi! I'd like to start off saying i don't have any programming training and very few knowledge, but i have messed around with several programming languages, designing tools to aid my everyday job (so i am a total amateur).
This time i'm working on something harder - it's a direct3d - mfc software and i am having lots of problems with it, since directx seems a bit hard.
Anyways... I have a direct3d environment in a mfc window and i would like to draw the coordinate system axes in the corner of the screen, as any 3d software has. I thought this would be no problem, since i found this topic, and that worked great but problems occurred when i started moving the camera around. I need the object to appear to be in the same spot, no matter how i pan, zoom or rotate the world (actually, i do these operations on the view matrix).
But it seems that i'm doing something wrong, and i was hoping someone could point me on the right direction, since the object i'm drawing is not scaled accordingly when i zoom, but it works perfect when panning or rotating.
">youtube video to show you the symptoms.
This is my code for drawing the object:
void CDEMView::DrawSomeBox()
{
// Define the needed matrices - object world, view and project
D3DXMATRIX matObjectWorld;
D3DXMatrixIdentity (&matObjectWorld); // object world matrix
D3DXMATRIX matView;
D3DXMatrixIdentity (&matView); // view matrix
D3DXMATRIX matProjection;
D3DXMatrixIdentity (&matProjection); // projection matrix
// Get the needed matrices
_device->GetTransform(D3DTS_VIEW, &matView);
_device->GetTransform(D3DTS_PROJECTION, &matProjection);
// Get the viewport
D3DVIEWPORT9 viewport;
_device->GetViewport(&viewport);
// Get the center point of the object
D3DXVECTOR3* p_centerPoint = BoxCenterVector; // this is from an external variable
// Get the point on the creen that is the screen projection of the object
D3DXVECTOR3 projectPoint;
D3DXVec3Project(&projectPoint, p_centerPoint ,&viewport, &matProjection, &matView, &matObjectWorld);
// choose the screen point where the object is to be drawn, relative to the Viewport's dimensions
D3DXVECTOR3 screenPoint;
screenPoint.x = 0.1*viewport.Width; // x position (horizontal) is 10% of the width of the screen (0% is left, 100% is right)
screenPoint.y = 0.9*viewport.Height; // y position (vertical) is 90% of the height of the screen (0% is top, 100% is bottom)
screenPoint.z = projectPoint.z; // 1-projectPoint.z*60/(-zoom);
//transform the screen position to a world position
D3DXVECTOR3 worldPoint;
D3DXVec3Unproject( &worldPoint, &screenPoint, &viewport, &matProjection, &matView, &matObjectWorld );
// now define how much to translate the box in order to get it to the point we want it to be (WorldPoint)
float transX, transY, transZ;
transX = worldPoint.x;
transY = worldPoint.y;
transZ = worldPoint.z;
// define a mesh to store the object into and create the object
ID3DXMesh* _SomeBox;
float boxSize = 2.0f;
D3DXCreateBox(_device,boxSize,boxSize,boxSize,&_SomeBox,NULL);
// define a material and set its color
D3DMATERIAL9 mat;
// Set the RGBA for diffuse reflection.
mat.Diffuse.r = 255;
mat.Diffuse.g = 0;
mat.Diffuse.b = 0;
mat.Diffuse.a = 0.5;
_device->SetMaterial(&mat);
_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); // D3DFILL_SOLID
// apply the translation matrix
D3DXMatrixTranslation(&matObjectWorld, transX, transY, transZ);
_device->SetTransform(D3DTS_WORLD, &matObjectWorld);
// draw the object
_SomeBox->DrawSubset(0);
// release the mesh
_SomeBox->Release();
// some debugging variables
debug1 = transX;
debug2 = transY;
debug3 = transZ;
debug4 = boxSize;
//debug5 = scaleFactor;
manole = 0;
}
Any help would be greatly appreciated. Thanks!
Edit: made a small change to the code to reflect my software.