• Advertisement
Sign in to follow this  

Matrix Problem

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

I have a mesh for which I am writing a shader. The shader requires the worldMatrix of the mesh, however, my Matrix contains the camera.viewProjection. How do I obtain the worldMatrix of the Mesh without effecting the camera Matrix?

Share this post


Link to post
Share on other sites
Advertisement
What does the world matrix of the mesh have to do with the camera?
A world matrix transforms from the mesh's object/local space to world space, and is totally independent of the camera.

Share this post


Link to post
Share on other sites
Coder, sorry if I wrote that poorly.

In my app creating a Matrix will effect the output to the screen, my camera matrix.

All I want to know is how to obtain the world matrix of a loaded mesh, so that I can pass it to a HLSL shader?


Share this post


Link to post
Share on other sites
Quote:
Original post by MooMansun
Does nobody have any ideas?

A lot of people have given you quite a lot of ideas and comments on this topic already. Isn't this the second/third time you've tried this?

Quote:
Original post by MooMansun
Surely, obtaining the worldMatrix of a mesh is a routine thing?

USING the world matrix on a mesh (either via a shader of fixed-func) is very common. What you seem to be asking is almost the inverse of that. You don't typically get a world matrix FROM a mesh, instead you manipulate a mesh BY a world matrix.

hth
Jack

Share this post


Link to post
Share on other sites
What do you mean by 'obtain the world matrix of a loaded mesh'? A mesh by itself doesn't have a world matrix (other than identity) unless you supply one. In other words, you decide what the world matrix for the mesh is; it's not something you have to 'ask for'.

I don't use D3D, but my understanding is that the device stores the view and 'world' matrices separately. So perhaps you're asking how to query the device for the world matrix? That doesn't make much sense though, because presumably you're the one who set the world matrix in the first place. But maybe my lack of D3D knowledge is getting me into trouble here...

One last thought. Maybe you're moving your camera around, but leaving your mesh stationary. In that case, the mesh world matrix is the identity matrix. So you might try submitting that to the shader and seeing if it works.

Share this post


Link to post
Share on other sites
Thanks guys,

I have tried everything that everyone has suggested. Under D3D, you set the device.Transform.world to the worldMatrix and the device.Transform.View to the viewMatrix.

"You don't typically get a world matrix FROM a mesh, instead you manipulate a mesh BY a world matrix."

I do understand that the mesh is manipulated by the worldMatrix, however, how do I get the worldMatrix in the first place?

The calling of Matrix.Identity returns my cameraMatrix (viewMatrix), not the worldMatrix of the mesh.

Share this post


Link to post
Share on other sites
Quote:
I do understand that the mesh is manipulated by the worldMatrix, however, how do I get the worldMatrix in the first place?
It may be that we're not making our points very well. What both jj and I are trying to say is that you don't get the world matrix for a mesh, you make it. You are in control! You have the power! There is no 'world matrix' but what you create...

Seriously though, I'm guessing that the D3D world matrix defaults to identity. Or maybe you have to set it explicitly - I'm not sure. In any case, unless you apply some transform or another, the mesh matrix is identity.

Of course it's also possible that we're misunderstanding your question...
Quote:
The calling of Matrix.Identity returns my cameraMatrix (viewMatrix), not the worldMatrix of the mesh.
What do you mean? It may be that you already know this material, but perhaps some clarification is in order. An identity matrix is a matrix with all major diagonal elements set to 1, and all other elements set to 0. The 4x4 case looks like this:
[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]
It has the property that anything you multiply by it remains unchanged. In this sense it is the matrix equivalent of the scalar value 1. With regard to matrix transformation, it is usually used as the 'starting point' for a series of transformations, or to create the effect of no transformations at all.

Share this post


Link to post
Share on other sites
I tried to use Matrix.Identity as my worldMatrix, however, my shader then shades the mesh relative to the camera position, not the position of the mesh.

Share this post


Link to post
Share on other sites
Perhaps the problem is in the shader. I've included it here:

//--------------------------------------------------------------------------------------
// File: Directional_Light.fx
//
// A Directional Light HLSL file for the CustomUI sample.
// Implementing HWT&L.
//
// Copyright (c 2005) Moo Mansun. All rights reserved.
//--------------------------------------------------------------------------------------


//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
float4x4 worldMatrix; // World matrix for object
float4x4 worldViewProjection; // World * View * Projection matrix
texture sceneTexture;



//Sampler state for texture lookup
sampler g_samScene =
sampler_state
{
Texture = <sceneTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};


//Return variables for VertScene
struct VS_OUTPUT
{
float4 Pos : POSITION;
float4 Light : TEXCOORD1;
float3 Norm : TEXCOORD2;
float2 Tex : TEXCOORD0;
};


//Vertex Shader Routine
VS_OUTPUT VertScene( float4 Pos : POSITION, float2 Tex : TEXCOORD0, float3 Normal : Normal )
{
VS_OUTPUT Out = (VS_OUTPUT)0;
Out.Pos = mul( Pos, worldViewProjection );
float4 Lite = (0.20, -0.76, -0.62, 0.99); //static test output light vector
float4 Lite2 = normalize(Lite);
Out.Light = Lite2;
Out.Norm = normalize(mul(Normal, worldMatrix));
Out.Tex = Tex;

return Out;
}


//Pixel Shader Routine
float4 PixScene( float2 Tex : TEXCOORD0, float4 Light: TEXCOORD1, float3 Norm : TEXCOORD2 ) : COLOR
{
float4 tex = tex2D( g_samScene, Tex );
float4 ambient = (0.1, 0.1, 0.1, -0.2);
float4 diffuse = (0.99, 0.99, 0.99, 0.4);

return saturate(tex + ambient + diffuse * dot(Norm, Light)); // +specular;)
}


//--------------------------------------------------------------------------------------
// Techniques
//--------------------------------------------------------------------------------------
technique RenderScene
{
pass P0
{
VertexShader = compile vs_2_0 VertScene();
PixelShader = compile ps_2_0 PixScene();
}
}

Share this post


Link to post
Share on other sites
Hm. Wish I could help further, but I don't know that much about shaders. I will point out the following line:

Out.Pos = mul( Pos, worldViewProjection );

Which suggests to me that the shader is using the view/projection matrix in its computations. If so, that could explain why the shader effect is changing as the camera moves. It seems that if the above line accessed the identity matrix instead, the shader results would be the same regardless of camera position.

Then again, I haven't done much work with shaders, so you should probably ignore anything I have to say on that subject :)

Share this post


Link to post
Share on other sites
Are you setting the device.Transform.World and device.Transform.View matrices? These are only for the fixed function pipeline, whereas you are using the programmable pipeline (i.e. a shader). You need to explicitly calculate the world * view * projection matrix, then set the worldViewProjection matrix in the shader with SetValue().

Share this post


Link to post
Share on other sites
"Are you setting the device.Transform.World and device.Transform.View matrices? These are only for the fixed function pipeline, whereas you are using the programmable pipeline (i.e. a shader). You need to explicitly calculate the world * view * projection matrix, then set the worldViewProjection matrix in the shader with SetValue()."

No, I haven't set device.Transform.World, because I am not using fixed functions.

My problem is that I need to know how to calculate the worldMatrix for 'world * view * projection matrix'.

Share this post


Link to post
Share on other sites
Matrix Multiplication: (code from my engine)


// a * b = o
static void MatrixMultiply(Matrix* a, Matrix* b, Matrix* o) {
o->m11 = a->m11*b->m11 + a->m12*b->m21 + a->m13*b->m31 + a->m14*b->m41;
o->m12 = a->m11*b->m12 + a->m12*b->m22 + a->m13*b->m32 + a->m14*b->m42;
o->m13 = a->m11*b->m13 + a->m12*b->m23 + a->m13*b->m33 + a->m14*b->m43;
o->m14 = a->m11*b->m14 + a->m12*b->m24 + a->m13*b->m34 + a->m14*b->m44;

o->m21 = a->m21*b->m11 + a->m22*b->m21 + a->m23*b->m31 + a->m24*b->m41;
o->m22 = a->m21*b->m12 + a->m22*b->m22 + a->m23*b->m32 + a->m24*b->m42;
o->m23 = a->m21*b->m13 + a->m22*b->m23 + a->m23*b->m33 + a->m24*b->m43;
o->m24 = a->m21*b->m14 + a->m22*b->m24 + a->m23*b->m34 + a->m24*b->m44;

o->m31 = a->m31*b->m11 + a->m32*b->m21 + a->m33*b->m31 + a->m34*b->m41;
o->m32 = a->m31*b->m12 + a->m32*b->m22 + a->m33*b->m32 + a->m34*b->m42;
o->m33 = a->m31*b->m13 + a->m32*b->m23 + a->m33*b->m33 + a->m34*b->m43;
o->m34 = a->m31*b->m14 + a->m32*b->m24 + a->m33*b->m34 + a->m34*b->m44;

o->m41 = a->m41*b->m11 + a->m42*b->m21 + a->m43*b->m31 + a->m44*b->m41;
o->m42 = a->m41*b->m12 + a->m42*b->m22 + a->m43*b->m32 + a->m44*b->m42;
o->m43 = a->m41*b->m13 + a->m42*b->m23 + a->m43*b->m33 + a->m44*b->m43;
o->m44 = a->m41*b->m14 + a->m42*b->m24 + a->m43*b->m34 + a->m44*b->m44;
}





You should be able to figure it out to apply it to D3D matrices (since the mXY correspond directly to the D3D ones)

Need more explaining? Mathworld Article.

Share this post


Link to post
Share on other sites
The maths is not the problem, multiplying matrices is easy enough. If I have a mesh memory, how do I find/set the worldMatrix correctly for it?

Share this post


Link to post
Share on other sites
Tell me where in worldspace you want it rendered, and what orientation and I'll give you the matrix.

It'll cost you the life of your first born child though, should you fail to guess my name by her 8th birthday.

Share this post


Link to post
Share on other sites
"Tell me where in worldspace you want it rendered, and what orientation and I'll give you the matrix."

This is something I am not sure of. I have just loaded a mesh to act as a 'level' or 'map'. The connection between a mesh and a matrix is something I am lost on.

If I try to create a Matrix (Matrix m = ...), this affects my camera matrix (viewMatrix).

Share this post


Link to post
Share on other sites
Well, there's hardly a point to using a matrix transform if you don't need to transform anything (or if you don't know what transforms you want to apply). As a rule of thumb, if you don't know what to use it for you shouldn't be using it.

I suggest you go back and learn about how D3D handles matrices before you even think about attempting HLSL stuff.

Share this post


Link to post
Share on other sites
Its not that I need to apply a transform. I need the worldMatrix so that I can pass this value to the HLSL shader.

I have been searching, no luck.

Share this post


Link to post
Share on other sites
Since you're not applying a transform, the world matrix is defined as

[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]


(you owe me your first born)

Share this post


Link to post
Share on other sites
That is the identity matrix. This makes the shading relative to my camera position, not the position between the light and the mesh.

Share this post


Link to post
Share on other sites
Normally the world matrix is created by using a few basic transform matrices, such as the translation matrix, rotation matrix, and the scaling matrix. The translation matrix simply moves all the vertices alike, thus translating the object from one point to another without changing its direction or form.
http://www.programmersheaven.com/2/world-view

So the translation matrix, rotation matrix, and the scaling matrix multiplied together will provide the worldMatrix.

How exactly?

Share this post


Link to post
Share on other sites
They're multiplied together:

World = Identity;
World *= Scaling;
World *= Rotation;
World *= Translation;

Share this post


Link to post
Share on other sites
Something like this:

worldFix = Matrix.Identity
worldFix *= Matrix.Translation(-center);
worldFix *= Matrix.RotationY(Geometry.DegreeToRadian(360.0f));
worldFix *= Matrix.RotationX(Geometry.DegreeToRadian(360.0f));

So, what do I use for scale that will keep the matrix the same?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement