Sign in to follow this  

Multiplying World-View-Projection in shader?

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

Hi,

I have a quick questions: How do I multiply the three matrices world, view and projection correctly in the shader? I've these matrices given as float 4x3 , and all I'm trying to do is to get a WorldViewProjection-Matrix in the HLSL-shader (I'm using instancing so no pre-shader-multplying is possible):

[code]float4x4 WorldViewProjection = mul(mul(World, View), Projection);[/code]

However I keep getting the error-message that a float 4x3 can't be converted into 4x4. Somehow logical, I'm multiplying some 4x3-matrices, how should that go into a 4x4. Well.. does anyone know how to do so? If I set any of the given matrices to 4x3 I can't set them anymore (they stay 0 for all values in this case according to PIX). Any ideas?

Share this post


Link to post
Share on other sites
Just use 4x4 matrices, I never used 4x3 matrices...
Projection matrix is a 4x4 matrix so it makes sense to use 4x4 matrices, also, a vertex position in homogeneous space is stored in a 4D vector.

This is how I do it:
[code]
float4 posWS = mul(float4(vIn.posL,1.0f), gWorld);
float4 posVS = mul(posWS, gView);
vOut.posH = mul(posVS, gProj);
[/code]

Share this post


Link to post
Share on other sites
Thanks, I already thought that the projection matrix is a 4x4 matrix as I read it in some tutorials, however I wondered why my shader would only accept a 4x3-projection matrix from the main program. I found out that there was an issue going on with the settings of the matrices: I'm using the directx9 effect pool, and in the effect file I asign my matrix too the projection matrix was set to 4x3, and in the one I am working on its 4x4. Now its working as I expected.

Share this post


Link to post
Share on other sites
Using a 4x3 won't really save you anything, since each row of the matrix will end up getting aligned to 16-byte boundaries which means it will take up the same space as a 4x4. However for future reference, you can always just convert a 4x3 to a 4x4 in your shader code and the shader compiler will do the right thing:

[code]
float4x4 world4x4 = float4x4(float4(World._11_12_13, 0.0f), float4(World._21_22_23, 0.0f),
float4(World._31_32_33, 0.0f), float4(World._41_42_43, 1.0f));
[/code]

Share this post


Link to post
Share on other sites

This topic is 2391 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this