Sign in to follow this  

inverse a matrix in HLSL

Recommended Posts

Believe it or not Google didn't show me much for this. How do I inverse a 2x2 matrix in HLSL? How about a 4x4? I found the math, but don't know the HLSL code. I am trying to do this. I ended up with this:
// Geometry Shader

void main(triangle GS_INPUT input[3] : SV_POSITION, inout TriangleStream<PS_INPUT> stream ) 
   // Calculate tangent frame
   float3 q1  = input[1].position - input[0].position;
   float3 q2  = input[2].position - input[0].position;

   float2x2 s;
   s[0] = float2(input[1].texCoord.x - input[0].texCoord.x, input[1].texCoord.y - input[0].texCoord.y);
   s[1] = float2(input[2].texCoord.x - input[0].texCoord.x, input[2].texCoord.y - input[0].texCoord.y);

// ...stumped

Share this post

Link to post
Share on other sites
I am on day 4 of trying to do per pixel lighting.
I made a series of posts a few days ago. The result seemed to be that

I need to calculate a tangent frame in a geometry shader.

I beleve I need the tangent frame in order to get my view direction and light direction in the pixel shader.

There was also talk of using a normal map, but I am not sure if I can do that anymore, because software exports normal maps in 3dsmax space instead of directx space.

I am just confused out of my mind, so if nothing else, I want a code snippet that can get the tangent frame in the geometry shader. Then I planned on trying to decide if I actually need it or not :P I am sure I will need it sometimes if not now.

Share this post

Link to post
Share on other sites
Ok, I've just got too many posts and too many questions.
Let's start over.

I'll make a new post going from the light equation backwards and nothing more until questions are resolved. Let's toss normal mapping and every confusing factor out the window. Maybe people can understand my problems easier when it is more clear what I need, instead of going from the vertex shader foward when they don't understand what I need.

[Edited by - brekehan on April 30, 2009 6:43:35 PM]

Share this post

Link to post
Share on other sites
I think the part your looking at is probably this:

"It is safe to assume, however, that the three vectors will at least be close to orthogonal, so using the Gram-Schmidt algorithm to orthogonalize them should not cause any unacceptable distortions. Using this process, new (still unnormalized) tangent vectors T′ and B′ are given by..."

I would think then that you could use this approach and once your matrix is orthogonal, you could then use the transpose instead of an inverse. Does anyone else agree?

Wikipidia has suedo code that looks pretty simple for doing the Gram-Schmidt algorithm.

This gamedev post discusses good ways to do a matrix inversion on a 3x3.

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