Matrices etc

hog

Senior Contributor
Joined
Mar 17, 2003
Messages
984
Location
UK
I can't seem to get me head round this problem...

I want to be able to load any x file and scale it and display it in at the origin.

I can't suss out how to do this? I have tried looking in the SDK help but it's as much use as a chocolate condom!

I know it has something to do with the transform.world matrix but can't suss it...

Can anyone explain in plain English how I go about this? :(
 
All you really need to know about matrices is that they're an array of values that allow you to perform all the transformations (scaling, translating, rotating, etc) you want in one place. Different components of the matrix correspond to your position, and directional vectors. I personally don't know the detailed math behind them.

Usually you start with an "identity" matrix, which holds default values, such as position at 0,0,0, and scale of 1, 1, 1. You then multiply this matrix with another matrix containing the next transformation.

A general example:

1) Set a matrix to identity
2) Multiply by a scaled matrix
3) Multiply by a translated matrix
4) Apply matrix to world matrix

In managed directx

Dim mat As Matrix
mat = Matrix.Identity
mat.Multiply(Matrix.Scale(2, 2, 2))
mat.Multiply(Matrix.Translate(0, 4, 5))
D3DDev.Transform.World = mat

Do this before rendering all objects. In your case, wanting the object at the origin, leave out the translate part.

If you get funky results, keep in mind that the order of multiplication counts. Translating then rotating gives different results than rotating then translating.
 
Thnx for the example and description, but alas still no joy :(

I have created a sphere in anim8or which has its centre right on the origin.

When I load it and render to my viewport the camera z value is -10 and I see a black screen.

If I move my cameras z value to 63 I start to see the wall of the sphere, at 176 I see the sphere completely.

No I presume that when the mesh is fisrt loaded my camera is actually inside the sphere?

What I want to be able to do is for my app to scale/move whatever so that it is displayed as it appears when I move the camera z value to 176.

Do you follow?

Any more help most gratefully recieved:)
 
halobear333, I have after much probing managed to get the results I'm after apart from one thing.

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

Visual Basic:
matMatrix.Identity
matMatrix.Multiply(Matrix.Scaling(0.05,0.05,0.05))

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.

Do you or does anyone know how I tell by what scale factor I should use on each matrix?
 
There is a thing in DirectX and all 3d Programs called UNIT, it is the eqivilant to a map scale. When you create your model it is usually exported in unit scale, so if you create all your models yourself, this shouldn't be a problem.

As Floating point numbers are used the math can cause slight variances in results depending on how higher range you have used in your floating point, so the NORMALIZE funtion is used to compute to the nearest UNIT.

As for the Math, 3D is basically an excercise in Math ... MATRIX, POINT, GEOMETRY and TRIGNOMETRY. So learning the math is very important. MSDN has an introduction to the theory of the math.

Microsoft 3D Introduction

A distance from the center point in a matrix is called the magnitude ( or length of a vector ). This is known as Pythagorean thoery. The short hand notation for magnitude of a vector ( how long is the distance from the center in 3d space ) is ||v||.

3D maffs is no trip to the beach, but is very nessasry if you plan to do anything worth anything.

:confused:
 

Attachments

Unfortunately, what you are asking would require some hardcore math to do nicely. You'd have to determine the dimensions of the object in screen(2D) coordinates, then find out what scale factor would make the object fit into the screen width and height.

For example, in the case of a sphere, you'd use its radius, and see what scale factor should be used for it to fit in screen space given the cameras position relative to it. Not easy.

I recommend waiting a while before tackling something like this, as it's very easy in 3D programming to get ahead of yourself and get discouraged :)

I struggled for a long time before I decided to take baby steps and create some simple classes (a camera class for example). Doing this gives a solid foundation to build upon.
 
TEAPOTS IN THE MIST

Here is a little helper to create a grid of teapots that I wrote in C#

first create a little class to contain an array of MESH teapots.

Code:
class myMeshes
{
	public Mesh theMesh;
	public int index;
}

then in the Initalisation ( InitializeDeviceObjects() )

Code:
int numberOfMesh = 25;

meshList = new myMeshes[numberOfMesh];
		
for( int i=0; i<numberOfMesh; i++ ) {
	meshList[i] = new myMeshes();
	meshList[i].index = i;
	meshList[i].theMesh = Mesh.Teapot(device);
}

then render the array of Mesh Objects

Code:
// Render the teapots
for( int iObject=0; iObject<meshList.Length; iObject++ )
{
	device.Transform.World = Microsoft.DirectX.Matrix.Translation( (float)((iObject%5 ) * 3 ), 0.0f, (float)((iObject/5 ) * 3.0f ) );
	meshList[iObject].theMesh.DrawSubset(0);
}

don't forget to declare the globals

Code:
private myMeshes[] meshList;

:)
 

Attachments

Mmm perhaps you are right....:(

I shall explain a little further tho...

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?

Perhaps the answer to the problem would be to supply a scaling control which the user adjusts to the get the display correct....but messy me thinks.

I get what you say about walk before running, but the app I have at the moment does so much that I never thought possible it seems like I'm so close yet so far if you get my drift:(
 
I have been told that the bounding box would give an idea of how big an object is.

Is this so? And if so how would I go about extracting this information from an x file?
 
The bit about the bounding box is true: it defines the maximum dimensions of the object. If you know an objects greatest dimension you could figure out what scale would allow it to fit in the window.

It's pretty easy to do:

For the "front" of the box, cycle through the vertices of the mesh and find the vertice with the greatest Z value. The "back" will be the lowest or most negative Z value. The top and bottom will be defined by the greatest and lowest Y values, and the left and right sides by the X values. C++ also has functions to create bounding boxes, though I'm not sure how to do it in managed directx (darn MS and their horrible documentation)

Find out which side is farthest from the center of the object and you have you have the largest dimension.

I just had a though about how to "cheat" fitting the object into the window: there should be a linear (y = mx + b) relationship between an objects largest extent and the scale needed to fit it into the screen. If you know the max dimension of two different objects and the scales needed for them, you should be able to plug those values into the equation and solve for m (b should be zero, I think). Then for any object, once you know its max dimension, you can calculate a scale (in theory).

Don't be discouraged about the idea of taking it slow. For example, it was very gratifying when I first managed to create a really solid camera class. It uses quaternions (4 dimensional vectors) for rotation and works in both 1st and 3rd person modes.

If you or anyone else wants, I'll polish and comment it and post the code. For me 3D rotations were the greatest hurdle, and the nice thing is that the quaternion code there can be used to rotate other, noncamera objects.

Sorry for rambling :-\
 
BOUNDING SPHERES

I've never used it, but this compiles in C# ... I only breifly looked into it for you but I think this should help unfreeze the works a bit.

Code:
C#

// Should be set be the CalculateBoungingSphere command
Microsoft.DirectX.Vector3 returnCenter;
Microsoft.DirectX.Direct3D.Frame myFrame = new Microsoft.DirectX.Direct3D.Frame();
// myFrame.MeshContainer = // your mesh here maybe ????? 
float boundingRadius = Microsoft.DirectX.Direct3D.Frame.CalculateBoundingSphere( myFrame, out returnCenter );

I would love to see the Camera example ... I had been using trignometry to change the camDes and alsorts ... I realise now that most people use an inverse matrix. But I've only been doing Direct X for a week now, so slap me.

:)
 
Here you go. I don't have a tidy little example program for it, but it should be very easy to integrate, as I made it as simple and independent as possible. There's also instructions at the top of D3D_Camera.vb.

This is in vb.net 2002 (I'm poor) so there's a small chance 2003 won't like it, but I doubt it.

If there's a problem I'll post a simple demo.
 

Attachments

Thanks chaps:)

I loaded your camera class and got it to work, basically just replacing the LookatLH . I will see what else it can do later:)

A rather messy fix I have to my problem at the moment is a combo box with scale factors which the user can select from to make the object appear correctly. Only trouble is if I set it to 0.00 the viewport is blank, if I set it to 1 some objects appear ok, others too small and others too big:(

This will not encourage the user to continue using my free app:(
 
Back
Top