PDA

View Full Version : Drag and Drop Meshes


durban
09-11-2005, 05:49 PM
Hello, I'm pretty new to DirectX and I still haven't gathered how all the functions work yet. My question lies in Transforming from Model Space to Screen space then adjusting a Meshes position from screen space coordinates. Here's some code:



_device->GetTransform(D3DTS_PROJECTION, &proj);
_device->GetTransform(D3DTS_VIEW, &view);

GetCursorPos(&ptCursor);
ScreenToClient(GetCapture(), &ptCursor);

numVerts = mesh->_mesh->GetNumVertices();
fvf = mesh->_mesh->GetFVF();
vertSize = D3DXGetFVFVertexSize(fvf);

mesh->_mesh->LockVertexBuffer(0, &ptr)

for( DWORD i=0; i<numVerts; i++ )
{
D3DXVECTOR3* vPtr = (D3DXVECTOR3*)ptr;
D3DXVECTOR3 temp;
D3DXVec3TransformCoord(&temp, vPtr, &view); // Is this right?
D3DXVec3TransformCoord(&temp, vPtr, &proj); // as well as this?
vPtr->x += temp.x*ptCursor.x; // this doesn't work
}


I read a post here that said you multiply the Vertex by the view matrix, then the projection matrix...

Trip99
09-20-2005, 09:49 AM
Hi, model space is how you define your model in an editor e.g. 3ds max and normally 0,0,0 is the centre. To put this model in your game world you will obviously need to move it so you apply a world matrix transformation that takes the vertices from model space to world space. You also want to view the world from a set position via a camera and so you next need to transform from world space into view space by setting the view matrix. Finally the 3D data needs to be converted to 2D screen data. This is done by applying a perspective transform and takes the data from view space to screen space.

So the calc is view matrix * world matrix * projection matrix

So there are three matrices, world, view and perspective. These can all be set in Direct3D by using SetTransform and they will cause the vertices to be transformed by Direct3D or the graphics card (if HW support available).

Doing it manually as you are above is very slow, so unless you have a particular reason to do it like this I would just set the matrix.

bladder
09-20-2005, 07:26 PM
I think he's tring to click on a mesh and drag around?

Algo would be something like:
- click and check if a mesh is selected
- while mouse button is down
- - transform mouse coord to world space coord
- - set world transform of object to calculated world space coord