Placing Meshes

Started by
6 comments, last by phatslug 21 years, 8 months ago
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.
Advertisement
"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
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!
if you see me running, try and catch me!!
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 );




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



There is no problem so complicated that it cannot be complicated further.
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...
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
}
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

This topic is closed to new replies.

Advertisement