Archived

This topic is now archived and is closed to further replies.

Placing Meshes

This topic is 5624 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

Using this code to render a Mesh loaded from a *.x file with D3DX...
  	HRESULT hr;
    // Meshes are divided into subsets, one for each material. Render them in

    // a loop

    for( DWORD i=0; i<materialCount; i++ )
    {
        // Set the material and texture for this subset

		hr = d3dDevice->SetMaterial( &materials[i] );
		if (failed(hr)) FatalError(hr, "device->SetMaterial. Error in CMesh::Render");

        hr = d3dDevice->SetTexture( 0, textures[i] );
		if (failed(hr)) FatalError(hr, "device->SetTexture. Error in CMesh::Render");
        
        // Draw the mesh subset

        hr = mesh->DrawSubset( i );
		if (failed(hr)) FatalError(hr, "mesh->DrawSubset. Error in CMehs::Render.");
    }
  
How do I set where the mesh is in the world. Like for example render a mesh at -5,-5,3, and another one at 0,0,0 and at 3,1,2 and such. I think I need some code before doin something with the world matrix, but I cant quite figure it out from the tutorials i''ve been reading.

Share this post


Link to post
Share on other sites
"I think I need some code before doin something with the world matrix"

Your right, just set the last column of the matrix like this:
worldMat._41 = -5;
worldMat._42 = -5;
worldMat._43 = 3;

This will move your object to the world coordinate (-5;-5;3).
Repeat the operation for every objects

Look at the DirectX sdk help for more info about matrix.

Good Luck

Share this post


Link to post
Share on other sites
before call the draw subset of the object try doing a a translation first

D3DXMatrixTranslation(&trans_matrix, xcoordinate, ycoordinate, zcoordinate);
hr = mesh->DrawSubset( i );
hope this helps

gud luck!

Share this post


Link to post
Share on other sites
Don''t forget that after creating a translation matrix:

D3DXMatrixTranslation(&trans_matrix, xcoordinate, ycoordinate, zcoordinate);

You have to actually set the transform:

SetTransform(D3DTS_WORLD, &trans_matrix);

before calling:

hr = mesh->DrawSubset( i );




Share this post


Link to post
Share on other sites
quote:

Your right, just set the last column of the matrix like this



Did u mean column or row?...
_41, _42, _43 is the 4th row of the matrix.

DX uses row major matrices, so setting the 4th row is right.

calling D3DXMatrixTranslation is best though.

For drawing/placing meshes at different places, there are different techniques...
First: Create a transformation matrix for each mesh, containing the correct translation and rotation values in it.
(then use one of these)
1. Then transform (multiply) all the vertices in the mesh using this matrix and use this vertices to render the mesh. This is not recommended though.
2. Save the common world transformation matrix. Multiply the transform (pre-multiply) matrix of the mesh with the world transform matrix, set the resultant matrix as the world matrix. Render the mesh. Repeat for each mesh everytime using the saved common world transform matrix as the world transform matrix.

3. Try using a matrix stack approach similar to the one in OpenGL. You can roll your own or use ID3DXMatrixStack. Either way you have to set the D3DTS_WORLDTRANSFORM for each mesh (if you don''t want to do step 1).

hope this helpz



Share this post


Link to post
Share on other sites
thanks for all the help, im tryin it out and it should work...

but for now enlighten me for a second, this ''translation matrix'' or ''world matrix''... is this the same kind of matrix i learned about in 8th grade math??? likee
[[1,4,3,3]
[2,7,1,2]
[3,0,0,1]]
...except 3D. Does multiplying and such work in a similar fashion? And whats it composed of, like the world matrix, I dont imagine it would have the coordinates of every point in 3D space for a largely sized area.

thanks for any explanations, just wanna try to understand Direct3D some more...

Share this post


Link to post
Share on other sites
i just figured out the matrixes myself, just ignore the math behidn the matrixes. when you have a thing like this:

void drawMesh(x, y, z, rx, ry, rz)
{
D3DXMatrixRotationX(&MatTemp, rx); // Pitch
D3DXMatrixMultiply(&rot_matrix, &rot_matrix, &MatTemp);
D3DXMatrixRotationY(&MatTemp, ry); // Yaw
D3DXMatrixMultiply(&rot_matrix, &rot_matrix, &MatTemp);
D3DXMatrixRotationZ(&MatTemp, rz); // Roll
D3DXMatrixMultiply(&rot_matrix, &rot_matrix, &MatTemp);

//Translate out cube in/out of the screen
D3DXMatrixTranslation(&trans_matrix, x, y, z);

//Build our final World Matrix
D3DXMatrixMultiply(&matWorld,&rot_matrix,&trans_matrix);

//Set our World Matrix
pD3DDv->SetTransform(D3DTS_WORLD,&matWorld );

//then call mesh render here, or CMesh::Render if your using that
}

Share this post


Link to post
Share on other sites
Interesting...

Ya, im starting to understand them I guess
Theres a great article explaining them and vectors on this site

Alternatively for that drawMesh function, you could simply use D3DXMatrixRotationYawPitchRoll and do all those steps in 1... but in case that you did that on purpose as an example, thanks

Share this post


Link to post
Share on other sites