Sign in to follow this  

About moving a 'mesh'

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

hi everybody, I'm a new user in that wonderful site and forum, special thanks for all who wanted to devolope programming by DX Library. I've one question and will be so glad if I found the answer. I'm using DirectX 8.0 in Visual Basic 6.0 and I found a big trouble after puting a 'mesh' in the scene, and I still amazing that there's no fuctions or classes can define the position of mesh, I mean 'x,y,z'. I'm going mad cause I'm searchin' more and more for that problem but ... all my tries lost in vain ... My friend told you sould make anew 'matrix' and put the 'mesh' in it, then you can choose the place or position of that new matrix, rotation and scaling and ... But I misunderstood him, he must try to explain it to me, and in fact that's imposible cause can you tell me what can I do if I wanna create or putting 1000 mesh in the scene, that mean creating 1000 matrix .... oh that's drive me crazy of course ... I hope I can find the answer with someone in that active and energetic forum I'm still bigenner, and sorry for my bad English cause it's not dialect Yours Wolf Attack

Share this post


Link to post
Share on other sites
Whenever you draw some triangles in D3D, it uses a matrix to decide where in 3D space they go. Direct3DDevice8 has a method called SetTransform, if you pass D3DTS_WORLD (or similar I forget the exact name) then the matrix you pass is used for world transforms. If you create a translation matrix (D3DXMatrixTranslation) then setting this as the world matrix will move every vertex by the trasnlation values you set. Then your mesh gets drawn where you want.

Share this post


Link to post
Share on other sites
When you draw in direct3d, the mesh object has no position of its own. You must move the position of the world to the correct place where you want to draw the mesh and then draw it. This has to be repeated for every frame.


You can think of the world transform as being kind of like a marker that is defined within 3d space and tells dx where to draw the next mesh.

The world transform starts off equals to the identity matrix which is = to (0,0,0).

You may multiply different matrices together to achieve multiple transforms, for instance you may Multiply a rotation matrix by a translation matrix to first rotate and then move the position of this 'marker'.Once you have moved the draw position 'marker' you may drawn your mesh.
After that mesh has been positioned and drawn.

Lets say that you wanted to draw the same mesh in a different position/rotation. You would set the world transform back to the identity matrix to move the location that you are going to draw back to the origin (0,0,0). You may now use another single or multiple combined matrices to move the draw location once more, then you may call the mesh drawsubset method (or equivelent in dx8).

Important( The same mesh object can be used over and over again to draw copies of the mesh in world space, YOU DONT NEED Multiple instances of the same mesh class if you want to draw several copies of the same mesh, you may re-use the original).

I have done my best to try to explain this as well as i can, if your still stuck dont hesitate to instant messsage me, I know how frustrating learning the basics is!

Hope this helps

[Edited by - Bitem2k on August 2, 2005 7:50:21 AM]

Share this post


Link to post
Share on other sites
If its any consolation, think of it like this. The Object is always rendered in the MIDDLE of the screen. So what you need to do is 'move' the screen to where you want the object rendered. Then move the screen back

Now Dont ingrain this too deeply into your head. Because technically, you move the world to the camera, then render the object, then move the world back to where you want it.

Might just help you a little bit to wrap your head around it.

Share this post


Link to post
Share on other sites
Well, i guess you also could lock the vertex buffer and manipulate all the vertices. But i can't recommend that.
The others are right, use matrices.
Before you render a mesh, calculate its rotation, scale and position.
For example:

float x; //x position of your model
float y; //y position of your model
float scale; //scale of your model
float rot; //rotation of your... well you get the idea

D3DXMATRIX matTranslation;
D3DXMATRIX matScaling;
D3DXMATRIX matRotate;
D3DXMATRIX matTransform;

D3DXMatrixScaling (&matScaling, scale,scale,scale); //alternatively you could also set up three seperate scale values; scaleX, scaleY, scaleZ
D3DXMatrixTranslation (&matTranslation, x, y, z);
matTransform = matScaling * matTranslation;
D3DXMatrixRotationZ (&matRotate, rot);
matTransform *= matRotate;

m_pD3dDev->SetTransform(D3DTS_WORLD, &matTransform);
// now you're ready to set textures and draw primitives


Share this post


Link to post
Share on other sites
hi everybody,

Again in a second massage I've written, thanks for all parts who participated in that beginner massage ...

If we join the previous at all, we can take that result:
"Sorry I'll write the following code in VB 6.0 till turing and updating my point to C++"


Dim Mesh As D3DXMesh
Dim MeshMaterials() As D3DMATERIAL8
Dim MeshTextures() As Direct3DTexture8
Dim nMaterials As Long

Dim mtrlBuffer As D3DXBuffer
Set Mesh = D3DX.LoadMeshFromX(App.Path & "\mesh1.x", D3DXMESH_MANAGED, D3DDevice, Nothing, mtrlBuffer, nMaterials)

ReDim MeshMaterials(nMaterials) As D3DMATERIAL8
ReDim MeshTextures(nMaterials) As Direct3DTexture8

For I = 0 To nMaterials - 1
D3DX.BufferGetMaterial mtrlBuffer, I, MeshMaterials(I)
MeshMaterials(I).Ambient = MeshMaterials(I).Diffuse
TextureFile = D3DX.BufferGetTextureName(mtrlBuffer, I)
If TextureFile <> "" Then
Set MeshTextures(I) = D3DX.CreateTextureFromFileEx(D3DDevice, App.Path & "\" & TextureFile, 128, 128, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR, 0, ByVal 0, ByVal 0)
End If
Next I


and in a bigen&end scene, we write


D3DDevice.BeginScene
...
For I = 0 To nMaterials - 1
D3DDevice.SetMaterial MeshMaterials(I)
D3DDevice.SetTexture 0, MeshTextures(I)

Mesh.DrawSubset I
Next I
...
D3DDevice.EndScene


the above is just the simple way to puting a mesh object in the scene by "LoadMeshFromX"
and this code will include the materials and textures automatically if it put at the same folder which contains the x file ...

now I've also a vertex buffer, it just a sample face, and the above mesh which called 'mesh1.x' what can I do to rotate the mesh object without rotating the sample face ?

that's my trouble ...

and thanks very much for your continuous aids

yours

Share this post


Link to post
Share on other sites
Please men, I need your help for that simply question
just wanna know how can I move the mesh object in the scene

if it would provided with a simple code I'll be so glad cause I'm still a little begenner programmer in DX

I'm using DirectX 8.0 in Visual Basic 6.0

thanks

Share this post


Link to post
Share on other sites
I'm having the exact same problem. I was about to post a topic regarding it, until I saw this one.
I think I understand the theory of how to solve it, but not practically. When/If I do find the solution, I'll post it up straight away.

Share this post


Link to post
Share on other sites
I found it, I found it, finally I found it
Every mesh object should put on a separated matrix
follow this chart :


Dim matWorld1 As D3DMATRIX
Dim matWorld2 As D3DMATRIX
Dim matWorld3 As D3DMATRIX

Private Sub Form_Load()
D3DXMatrixIdentity matWorld1
D3DXMatrixIdentity matWorld2
D3DXMatrixIdentity matWorld3

Do
D3DDevice.Clear ......

D3DDevice.BeginScene
......
D3DDevice.SetTransform D3DTS_WORLD, matWorld1
'// every code will be included in 'matworld1' till the next 'SetTranseForm'
For I = 0 To nMaterials - 1
D3DDevice.SetMaterial MeshMaterials(I)
D3DDevice.SetTexture 0, MeshTextures(I)
Mesh1.DrawSubset I '// order to draw mesh1 from x file
Next I

D3DDevice.SetTransform D3DTS_WORLD, matWorld2
'// every code will be included in 'matworld2' till the next 'SetTranseForm'
For D = 0 To nMaterials - 1
D3DDevice.SetMaterial MeshMaterials(D)
D3DDevice.SetTexture 0, MeshTextures(D)
Mesh2.DrawSubset I '// order to draw mesh2 from x file
Next D

D3DDevice.SetTransform D3DTS_WORLD, matWorld3
'// every code will be included in 'matworld3' till the next 'SetTranseForm'
.......
D3DDevice.EndScene

D3DDevice.Present ......
DoEvents
Loop
End Sub


and now if want to move your object, you can use DirectInput or a 'KeyCode' switchs :
the next code will move 'mesh1' which put in 'matWorld1' before :


Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyQ Then
Dim e As Single
e = e + 1
D3DXMatrixTranslation matWorld1, e, e, e
D3DDevice.SetTransform D3DTS_WORLD, matWorld1
End If
End Sub


thanks for all
and now I should breathe again

Share this post


Link to post
Share on other sites
Oh, thank you soo much! It works perfectly.
Instead of having matWorld1, and Mesh1, I set them as arrays, like: matWorld(99) and Mesh(99), and use a simple For loop after D3DDevice.BeginScene, if you get what I mean. Just makes things a tad neater.
Anyway, thanks again. :D

Share this post


Link to post
Share on other sites

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