Sign in to follow this  
16bit_port

[D3D10] translation matrix in HLSL

Recommended Posts

I'm currently sending a triangle to the geometry shader which subdivides and spits out smaller ones. That works fine, but I would also like to translate this input triangle along the z-axis and behind an object before I do the subdivision.

I originally had something like this :

cbuffer cbPerObject
{
row_major float4x4 ViewProjectionMatrix;
}

...

void Main_GS( ... )
{
float4 v0 = mul( float4(input[0].Position, 1.f), ViewProjectionMatrix );
float4 v1 = mul( float4(input[1].Position, 1.f), ViewProjectionMatrix );
float4 v2 = mul( float4(input[2].Position, 1.f), ViewProjectionMatrix );

//subdivide
...
}





I've tried something like this :

void Main_GS( ... )
{
float4x4 TranslateBack = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 2,
0, 0, 0, 1 };
float4x4 WVP_Matrix = TranslateBack * ViewProjectionMatrix;

float4 v0 = mul( float4(input[0].Position, 1.f), WVP_Matrix );
float4 v1 = mul( float4(input[1].Position, 1.f), WVP_Matrix );
float4 v2 = mul( float4(input[2].Position, 1.f), WVP_Matrix );

//subdivide
...
}





However it looks like I'm getting a scaling effect (it got smaller, but STILL in front of the other object). I'll post the PIX results in a bit but right now I just want to know if I'm doing something fundamentally incorrect.

Note: if I do the translation, concatenate that to the ViewProjectionMatrix in my C++ program and do it that way, it works just fine (it ends up behind the other object). However, I would like to do the translation in shader. Thanks.

[Edited by - 16bit_port on December 3, 2010 12:11:04 PM]

Share this post


Link to post
Share on other sites
I tried changing it to :

float4x4 TranslateBack = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 2, 1 };




but the triangle doesn't even show up anymore. So I ran it through PIX and surprisingly I'm getting a homogeneous coordinate of zero!


It's supposed to look like :


I've even tried adding row_major :

row_major TranslateBack = ...

and

row_major New_WVP_Matrix = ...

but to no avail.

Share this post


Link to post
Share on other sites
You mentioned having done the translation on the CPU side and that it worked out fine. Can you post the entire matrix WVP that you used when it worked?

Are you using XNA math? By default those matrices are row_major when uploaded.

Also, it looks like you are doing the translation after applying the projection matrix which would end up with wrong z values as they won't falling between [0,1]. The matrix you have had a translation of 2 along the z-axis which is probably the causes the behavior you're seeing. Did you mean to do the translation before applying the projection matrix but after applying the view matrix?

Share this post


Link to post
Share on other sites
I think that you should declare:
<code>
//
// Global variable
//
matrix WVPMatrix;

main(vector position : POSITION){//the position of the triangle (not multiplied,just a input)

vector finalpos= mul(position, WVPMatrix);
return finalpos;
}
</code>
that's what I've learned,and also you should set the WVPMatrix in your main program.

Share this post


Link to post
Share on other sites
I changed the code in my original post to reflect what I really meant (changed cbPerObject::WVP_Matrix to cbPerObject::ViewProjectionMatrix). What I wanted to do was create a world/translation matrix and concatenate that to the ViewProjectionMatrix from the CPU side and get the WorldViewProjection matrix.

Anyway, I figured out what the problem was.
You can't do matrix concatenation with the * operator. You have to use the intrinsic function mul().


void Main_GS( ... )
{
float4x4 TranslateBack = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 2, 1 };
float4x4 WVP_Matrix = mul(TranslateBack, ViewProjectionMatrix);

float4 v0 = mul( float4(input[0].Position, 1.f), WVP_Matrix );
float4 v1 = mul( float4(input[1].Position, 1.f), WVP_Matrix );
float4 v2 = mul( float4(input[2].Position, 1.f), WVP_Matrix );

//subdivide
...
}

Share this post


Link to post
Share on other sites

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