Matrix Problem

Started by
27 comments, last by wasd 18 years, 6 months ago
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?
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.

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?


Does nobody have any ideas?

Surely, obtaining the worldMatrix of a mesh is a routine thing?
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

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

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.
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.
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.
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.
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();
}
}

This topic is closed to new replies.

Advertisement