Sign in to follow this  

Direct3D setting mesh coordinates

This topic is 3717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I have defined a Model class that holds the mesh, material and texture for my models. I cant really wrap my head around how to set the mesh coordinates in the engine. VOID GraphicHandler::SetupMatrices() { // Set up world matrix D3DXMATRIXA16 matWorld; D3DXMatrixRotationY( &matWorld, timeGetTime()/1000.0f ); g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ); D3DXVECTOR3 vEyePt( 1.0f, 1.0f,-5.0f ); D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f ); D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f ); D3DXMATRIXA16 matView; D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec ); g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView ); D3DXMATRIXA16 matProj; D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 80.0f ); g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); } VOID GraphicHandler::Render() { // Clear the backbuffer and the zbuffer g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // Setup the world, view, and projection matrices SetupMatrices(); // Meshes are divided into subsets, one for each material. Render them in // a loop instances->renderAll(); // End the scene g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } SetupMatrices sets up the world and camera as far as I know, the Render function renders all models contained in the container instances. Heres the render for a Model: VOID Model::Render() { for( DWORD i=0; i<this->d_wNumOfMaterials; i++ ) { // Set the material and texture for this subset (*g_pd3dDevice)->SetMaterial( &g_pMeshMaterials[i] ); (*g_pd3dDevice)->SetTexture( 0, g_pMeshTextures[i] ); // Draw the mesh subset g_pMesh->DrawSubset( i ); } } Best thing would be if I had 3 floats as private variables inside the Model class that will set the position of the mesh in space. Something like: void Model::setCoords(float x, float y, float z) { this->x = x; this->y = y; this->z = z; } and in Render have these coords interpreted and rendered at the appropriate place. I looked at LockVertexBuffer to get access to the vertex coords, but cant really seem to understand how to use it. Also the SetTransform() seems like a bad way to go since all objects inside the D3DDevice gets transformed if im not mistaking, I want each Model to be able to have independent coordinates. Any ideas?

Share this post


Link to post
Share on other sites
Quote:
Original post by MickeMan
I looked at LockVertexBuffer to get access to the vertex coords, but cant really seem to understand how to use it.

Also the SetTransform() seems like a bad way to go since all objects inside the D3DDevice gets transformed if im not mistaking, I want each Model to be able to have independent coordinates.
SetTransform() is what you want. Usually you'll set the world matrix to the appropriate matrix, draw an object then repeat for the next object, setting the matrix before each one.

To clarify, SetTransform() only affects objects not yet drawn; when you draw something, it's drawn with the currently active transforms. Calling SetTransform() again won't change any of the already drawn objects.

Locking the vertex buffer is far more expensive to do, and a lot more hassle.

Share this post


Link to post
Share on other sites
Quote:
Original post by MickeMan
Cant I pass along my LPD3DXMESH to the SetTransform() method in some way?

No, why would you want to? You call SetTransform(), then render the mesh (With DrawSubset() or whatever).

Share this post


Link to post
Share on other sites
You see that "matWorld" matrix you're setting in your SetupMatrices method? That is of course, your world matrix. The idea is that you'll want to store a world matrix in each instance of you Model class, and this world matrix will represent the current position and orientation of your model. Then in the Render method of your model class, you can apply the world matrix using SetTransform like this:



VOID Model::Render()
{
//set the world matrix for this model
(*g_pd3dDevice)->SetTransform( D3DTS_WORLD, &matWorld );

for( DWORD i=0; i<this->d_wNumOfMaterials; i++ )
{
// Set the material and texture for this subset
(*g_pd3dDevice)->SetMaterial( &g_pMeshMaterials[i] );
(*g_pd3dDevice)->SetTexture( 0, g_pMeshTextures[i] );

// Draw the mesh subset
g_pMesh->DrawSubset( i );
}
}






As for how you construct your world matrix and handle rotations and translations, there's a few ways of going about it. Either way you'll probably need to create some methods for handling these transformations, so that you can alter or reconstruct your world matrix when movement or a change in rotation is requested. For example say you wanted to allow for translation, but you still wanted to keep track of the model's position. You could do that like this:



VOID Model::Translate (D3DXVECTOR3* pVec)
{
//Method 1: create a new matrix and multiply it with the old world matrix
D3DXMATRIXA16 translationMatrix;
D3DXMatrixTranslation(&translationMatrix, pVec->x, pVec->y, pVec->z);
worldMatrix = worldMatrix * translationMatrix;

//Method 2: directly alter the translation portion of the world matrix
worldMatrix._41 += pVec->x;
worldMatrix._42 += pVec->y;
worldMatrix._43 += pVec->z;

//if you're keeping track of the position, update it
position += (*pVec);
}






Anyway I hope this helps point you in the right direction. If don't quite understand what I'm showing you here, feel free to ask some more questions or to read up on how vectors are matrices are used in 3D graphics. This stuff is the very basics of 3D work, so its imperative that you get a good grasp on it early.

Oh and I almost forgot to mention...you'll probably also want to wrap up your view and viewProjection matrices in some sort of camera class, so that you can position and orient your virtual camera in your 3D world and have the class handle updating or creating the view matrices accordingly.

[Edited by - MJP on October 12, 2007 6:42:20 PM]

Share this post


Link to post
Share on other sites

This topic is 3717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this