Skip to main content
GameDev.net gamedev.net
🔒 Locked

matrices

Started by Hog Jan 8, 2004 at 10:59 AM 13 replies 1.3k views
Original Post
Hog
Hog
I am trying to load any mesh into a Visual Basic .NET application that will scale and postion it to a certain size and at the origin. Point is I just can''t seem to suss how to do it? Can anyone point me in the right direction using instructions for a numbskull?
(Visual Basic .NET) www.elpuerco.co.uk
neneboricua19
neneboricua19
I''ve never played with VB but the steps involved in this are the same for VB and C++. Most models are created so that they stand at the origin of model space. So if you want the model to stay at your scenes origin, all you have to do is scale the model, you don''t have to translate it.

Here''s a code sample that scales a model and translates it to some specific location in your 3D world. If you don''t want to translate it, you can just use the scaling matrix directly as your world matrix:

D3DXMATRIX matScale, matTrans, matWorld;
D3DXMatrixScaling( &matScale, xScale, yScale, zScale );
D3DXMatrixTranslation( &matTrans, xPos, yPos, zPos );
D3DXMatrixMultiply( &matWorld, &matScale, &matTrans );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

// Draw your model.
m_pModel->Render();


Hope this helps,
neneboricua
Hog
Hog
OK Thanks, this is what I have so far;

Dim matMatrix As Matrix

matMatrix = Matrix.Identity
matMatrix.Multiply(Matrix.Scaling(0.05, 0.05, 0.05))
m_obj3DDevice.Transform.World = matMatrix

I have a sphere which has it''s exact centre in space at 0,0,0
Before doing the above I had my camara z at -10. The screen was blank, I presume my camera is inside the sphere? If I move z to 176 I see the perfect sphere.

Now if I use the code above it appears perfect to start with. But as soon as I try to rotate by x,y,z etc it jumps back to full size again and I have to move the camera back to 176 to see it.

Any problem is that if I open another mesh it appears tiny cos of the scaling code. When I rotate this time it reverts to proper size?

How do I get to be able to open any mesh and have it appear at a descent size in the centre of my viewport?

Help please....
(Visual Basic .NET) www.elpuerco.co.uk
Hog
Hog

The problem is solved at the moment as far as the sphere is concerned by the following;

[vb]
matMatrix.Identity
matMatrix.Multiply(Matrix.Scaling(0.05,0.05,0.05))
[/vb]

This allows the sphere to be displayed filling the viewport nicely.

I then load the aeroplane mesh from the SDK media folder and find that the scaling factor is too much so the aeroplane appear tiny.

Does anyone know how I tell by what scale factor I should use on each matrix?
(Visual Basic .NET) www.elpuerco.co.uk
neneboricua19
neneboricua19
It''s not easy to code an app so that it knows automatically how to scale arbitrary geometry. In a commercial app, developers work (at least semi)closely with the artists. Near the beginning of a project, it is decided what scale to use for all the geometry. So normally the developers won''t even have to scale the geometry at all inside the engine; the artists will do it during model creation.

In your case, you''ll just have to experiment with the models you''re using and see what kind of scaling looks best for your world.

neneboricua
Hog
Hog
But what of Microsoft''s Mesh Viewer? This opens any mesh and has it scaled correctly in the centre of the viewport?
(Visual Basic .NET) www.elpuerco.co.uk
neneboricua19
neneboricua19
I didn''t say it wasn''t possible, I just said it wasn''t easy. If you want to, you can look at the source code for MView. It''s available from Microsoft''s DX download page. It''s in the extras component of DX.

Keep in mind that Mesh View only has to scale one model. It just has to figure out how to make a given model take up most of the viewport. This isn''t all that bad since it really doesn''t matter if the mesh is scaled to be 1000 times larger or maybe 1000 times smaller than the original. As long as this one mesh takes up most of the view screen, then it''s ok.

What you''re talking about is a little different. You want to make your code scale all your meshes so that your entire world is modeled on the same unit scale.

Here''s an example. Say you have a building model. The artist that made that model used units so that the building is 100 units high. The artist here was thinking that 1 unit = 1 meter. But what if you have another model of a baseball bat? The artist on this bat was working under the assumption that 1 unit = 1cm. So the bat comes out to be 100cm long. For a baseball bat to be 100cm seems reasonable. If you render both of these models directly, the building will come out to be the same size as the baseball bat. But how is your code to distinguish between these two models so that both models are the appropriate size relative to each other?

Since not all artists use the same units when they create their models, and unless you know what kind of units the artist used to develop the model, you have to play with each model individually to see how it fits best with the rest of your world.

neneboricua
Supernat02
Supernat02
You said as soon as you rotate the object, it goes back. It sounds like you''re doing something like this:

Dim matMatrix As Matrix

matMatrix = Matrix.Identity
matMatrix.Multiply(Matrix.Scaling(0.05, 0.05, 0.05))
m_obj3DDevice.Transform.World = matMatrix;

matMatrix = Matrix.Identity
matMatrix.Multiply(Matrix.Rotation(1, 0, 0))
m_obj3DDevice.Transform.World = matMatrix;

I don''t know what the VB code is for rotating, but I''m just saying a simple rotation 1 radian about the x axis in this case.

This won''t work, because the world transform is changed to just a rotation, disregarding the scaling. You have to multiply the matrices together for scaling and rotation. First you rotate it, then scale it, or vice versa if it''s about the origin, then set the world matrix.

Dim matMatrix As Matrix

matMatrix = Matrix.Identity
matMatrix.Multiply(Matrix.Rotation(1, 0, 0));
matMatrix.Multiply(Matrix.Scaling(0.05, 0.05, 0.05))
m_obj3DDevice.Transform.World = matMatrix;

If you are using texturing, without lighting, you should be able to see the inside of your sphere if you are in it.

So, one thing that concerns me is that you have to move to a far z INTO the screen before you can see the sphere. This could mean that your sphere''s model coords are out past z=176, assuming your camera is facing into the positive z axis. If your camera is facing into the negative z axis, this could mean that you just have your back turned on the sphere at z=-12. My best guess though is that your model''s origin is just way out in the positive z direction.

Chris

*Life is like the rock in a sling-shot. Pick a good target, take good aim, and get there as soon as possible!*
Chris ByersMicrosoft DirectX MVP - 2005
Hog
Hog
OK chaps your input is appreciated

I shall explain a little further...

I am writing a VB.NET app which allows the user to open any x file to display the content in a viewport. Basically I''m trying to mimic Mesh View but with some added thrills, all as an execise in learning how DX9 works.

From where I see it I want to be able to open any mesh as mesh view does and have it scaled centred into the viewport. I have managed to sort out the rotation by x,y,z etc but just need to know how to find out what scaling factor a given mesh requires.

My app works perfect with a sphere I created, but when I open meshes from the SDK to test they appear tiny. If I hard code the scale factor to another value they appear perfect and the app works as intended.

Is there something in the mesh file that meshview reads to know what scale factor to use?

Thnx for your input
(Visual Basic .NET) www.elpuerco.co.uk
Hog
Hog
To clarify...

I am only thinking of the single model meshes at the moment. I had not considered meshes that contain multiple models. My app will state that these will not view correctly.

I just want to open up any single modele mesh and display centre viewport scaled to a reasonable size just as mesh view does
(Visual Basic .NET) www.elpuerco.co.uk
Nik02
Nik02
From the mesh''s bounding box (or bounding sphere) dimensions, you can approximate how "big" the mesh is.

I believe MView uses this technique; haven''t had the need to look at the source myself, though.

-Nik
Niko Suni
Hog
Hog
OK so any idea how I find out the bounng box dimensions? Is there some method/property of the x file which details this?

Thnx
(Visual Basic .NET) www.elpuerco.co.uk
Nik02
Nik02
See Geometry class'' public methods ComputeBoundingBox and ComputeBoundingSphere.

C++ equivalents are D3DXComputeBoundingBox and D3DXComputeBoundingSphere.

-Nik
Niko Suni
Hog
Hog
Thnx for this

I had looked for the ComputeBoundingBox method in the Managed DirectX help which gives the following:

----

Note: This documentation is preliminary and is subject to change.
Computes a coordinate-axis-oriented bounding box.

Definition

Visual Basic Public Shared Sub ComputeBoundingBox( _
ByVal pointsFvf As GraphicsStream, _
ByVal numVertices As Integer, _
ByVal vertexFormat As VertexFormats, _
ByRef min As Vector3, _
ByRef max As Vector3 _
)

Parameters

pointsFvf Microsoft.DirectX.Direct3D.GraphicsStream.
numVertices System.Int32.
vertexFormat Microsoft.DirectX.Direct3D.VertexFormats.
min Microsoft.DirectX.Vector3.
max Microsoft.DirectX.Vector3.

----

And that''s it! This is of liitle help to me and I guess others as it doesn''t exactly explain what the hell ya supposed to do?

Why is the SDK help so vague?

Does anyone know a better source of help?

I find myself turning to you guys increasingly as each time I turn to the SDK help it proves fruitless.

Dudes....I so want to learn
(Visual Basic .NET) www.elpuerco.co.uk
Nik02
Nik02
-You need to lock the mesh's vertex buffer to get access to a GraphicsStream interface, representing the raw geometry data.
-You can ask the mesh for it's format to get a proper value to the VertexFormat parameter.
-The NumVertices should equal to the actual number of vertices in the mesh; again you can ask the mesh directly for this.
-The min and max vectors, as they are passed by reference, will be filled with min and max "corners" of the box after the computation returns. The values are simply defined as the minimum and maximum position of the vertices.
-The vector (max-min) of the returned bounding box values is the "size" of the mesh. Note that this is extremely naíve way to compute the geometry's size, but in most cases, it is more than enough effective.
-I'll leave the rest for an excercise to the reader

-Nik

EDIT: Though feel free to ask more info if needed.

[edited by - Nik02 on January 10, 2004 3:10:18 PM]
Niko Suni

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.