# Calculating WorldView

## Recommended Posts

MooMansun    100
Hi, I need to calculate the WorldView/World Matrix to output to a HLSL shader. I am using a First person viewmatrix as the camera. Use this as your example equation: effect.SetValue("worldViewProjection", WorldMatrix * viewMatrix * camera.ProjectionMatrix); How is it done?

##### Share on other sites
MooMansun    100
Will Matrix.Identity return the worldMatrix?

##### Share on other sites
jollyjeffers    1570
Quote:
 Original post by MooMansunWill Matrix.Identity return the worldMatrix?

No. The "Identity Matrix" is a special property of any mathematically defined matrix where row==column=1.0 [smile]

Quote:
 effect.SetValue("worldViewProjection", WorldMatrix * viewMatrix * camera.ProjectionMatrix);

The above fragment is, as planely read, the world view projection matrix. To be what you describe in your post you'd have to drop the camera.ProjectionMatrix part.

I'm not entirely sure what you're asking though.

You've got a "world view" matrix - that is the World*View matrix? and you want to set this to an HLSL shader? if so, just set the results of World*View to whatever the appropriate constant name is in your HLSL script..??

hth
Jack

##### Share on other sites
MooMansun    100
I just want to calculate the 'WorldMatrix' so that I can insert the result into the command.

"effect.SetValue("worldViewProjection", WorldMatrix * viewMatrix * camera.ProjectionMatrix);"

If I have a mesh in memory, how do I calculate its worldMatrix?

##### Share on other sites
MooMansun    100
OK, I have found that Matrix.Inverse(viewMatrix) will provide the world matrix. I now have the problem were my controls, linked to the viewmatrix, are messed up.

I know that Matrix.Inverse(viewMatrix) is manipulating the matrix directly and causing the problem.

How do I create a matrix that controls the inverse without screwin up the viewMatrix?

##### Share on other sites
jlgosse    130
Although I haven't really started game development yet, I have some advice for you.

Read up on your linear algebra and specifically mathematics involving matrices and vectors. It will be SO much easier on you, I hear.

##### Share on other sites
MooMansun    100
I understand the math, that's a pinch. I'm having a problem creating a second matrix to control the World.

The viewMatrices inverse is the world matrix. I need to control both, not one or the other. When I call Matrix.Invert(viewMatrix), it does not return a new Matrix for the worldMatrix, it inverts the viewMatrix.

Now when I try control the viewMatrix, I am controlling what should be the worldMatrix.

I know in other languages I could copy the memory, but I'm using c# at the moment.

##### Share on other sites
MooMansun    100
If I were to put it C++ terms. The viewMatrix is a pointer to the Matrix held in memory. When I try to create a Matrix which is a pointer to worldMatrix (inverted viewMatrix), I only end up manipulating the viewMatrix.

Do I need to implement a MatrixStack?
Will this allow me to control both the viewMatrix and the worldMatrix?

##### Share on other sites
You've got a few things confused. The inverse of the view matrix is NOT equal to the world matrix. The world matrix is used to position your models throughout the scene. Each object you render will have its own world matrix. The view matrix describes the position and orientation of the camera. This matrix will stay the same for the whole frame.

Again, the world matrix is used to position your models inside the scene. In mathematical terms, the world matrix transforms vertices from model-space to world space. Each object in your scene will have its own world matrix so that you can position it where ever you want when rendering.

neneboricua

##### Share on other sites
MooMansun    100
It was near 4 or 5am when I wrote that, not thinking straight.

I posted this elsewhere:

Actually, I just need to be able to calculate the inverse of the current viewMatrix.

Btw, device.Transform.World resulted in the same (D3DERR_INVALIDCALL)

Found this at Microsoft -

Computation of Inverse World Matrix
The world matrix could be represented as:

Mw = Mwr * Mwt, where Mwr is the rotation part and Mwtb is the translation part.

http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnardir3d/html/d3dlight3.asp

I think I have seen this formula before...

##### Share on other sites
MooMansun    100
Great, I'm back were I started. How can I calculate the current worldMatrix without effecting the matrix in memory.

I'm using pure hardware VP and get commands are not an option. Device.Transform.World fails, passing the value to a temp store only gives me an additional pointer....

Lang: c#

Help anyone???

##### Share on other sites
The link you posted is to a document that is over 7 years old. There have been over a dozen SDK releases since then! The document is severely out of date.

Quote:
 From http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnardir3d/html/d3dlight3.aspComputation of Inverse World MatrixThe world matrix could be represented as:Mw = Mwr * Mwt, where Mwr is the rotation part and Mwtb is the translation part.

Mwr is a matrix with just the rotation part of the world matrix, and Mwt is a matrix with just the translation part of the world matrix. In no way does this imply that you can get the world matrix by taking the inverse of the view matrix.

Like someone else has already said; you need to have a basic understanding of linear algebra to do this.

Applications should not need to call GetTransform... or whatever, to get the world matrix of an object. Each object in your scene should have its own world matrix. The world matrix is used to transform geometry to different parts of the scene.

neneboricua

##### Share on other sites
thezbuffer    706
Quote:

Not just here - he's at it in the newsgroups too:
Moo's newsgroup argument

##### Share on other sites
MooMansun    100
ZBuffer,

The answers provided have not provided a solution to my problem.

They either fail or they just effect the Matrix in memory.

I need to obtain the worldView Matrix, however, under pure hw vp any matrix operation effects the Matrix in memory.

Is there a way around this?

##### Share on other sites
MooMansun    100
Here, I have found someone else who says the worldMatrix is the inverse of the viewMatrix, their name is WitchLord (Moderator)...

Quote:
"I also advice you to take a look at the following matrix structures for the worldmatrix and viewmatrix:

World Matrix
View Matrix

See the similarities? The view matrix is in fact the inverse of the world matrix."

http://www.gamedev.net/community/forums/topic.asp?topic_id=15526&forum_id=10&gforum_id=

##### Share on other sites
MooMansun    100
Think of it a different way, since it is just a Matrix, is there any Math operation which would return the inverted values?

Something like Math.Matrix.Invert(Matrix) would be handy...

##### Share on other sites
Ok, that's it. I give up. I won't keep this going if the OP isn't willing to take advice. Good luck in all your efforts.

neneboricua

##### Share on other sites
MooMansun    100
neneboricua:
"Applications should not need to call GetTransform... or whatever, to get the world matrix of an object. Each object in your scene should have its own world matrix. The world matrix is used to transform geometry to different parts of the scene."

This I know, however, you need to calculate the world matrix in order to have one. We were attempting to use GetTransform to obtain it.

So, before we end up flaming each other, I will keep it simple and ask one question at a time.

1. How do you calculate the worldMatrix of a loaded mesh?

##### Share on other sites
jollyjeffers    1570
Quote:
 Original post by MooMansunyou need to calculate the world matrix in order to have one. We were attempting to use GetTransform to obtain it.

This sounds like an infinite catch-22 loop to me [smile]

Provided you're not running a pure device, then calling GetTransform() where you haven't already set one should return a default. Off the top of my head, I think you'd just end up with the identity matrix.

Quote:
 Original post by MooMansun1. How do you calculate the worldMatrix of a loaded mesh?

Build it using whatever information is available.

Some mesh formats will export, from the modeller, a set of matrices (bone/skeletal meshes are a good example of this) that you can read from the specific file format. If there isn't any information in the file you're loading, then you might want to look at the "level" data - there could be position/rotation/scale information in the level indicating where the current mesh is.

Failing those, Just assume an identity world matrix for the mesh..

An example I have kicking around stores 3 vectors with a mesh - rotation, scaling and translation. I have a copy of these 3 vectors with every instance of the mesh that is loaded. If I choose to move that mesh around at runtime then I alter one (or more) of those 3 vectors.

When it comes to rendering, calls to D3DXMatrixIdentity(), D3DXMatrixMultiply(), D3DXMatrixScaling(), D3DXMatrixTranslation(), D3DXMatrixRotation*() are used to create the world matrix for that mesh. I then SetTransform() this matrix (or apply it to the vshader constant table) and render...

hth
Jack

##### Share on other sites
MooMansun    100
Thanks guys,

It is a bit like the chicken and the egg. I am sure it can be done. I am running a pure device, which means any matrix operation effects the Matrix in memory.

Just for tonight, I am abandoning this DX9c thing for 'Team America', I need a good laugh and I'm sure something will come to me...

Again, thanks for all your help, chat later...