# inverse a matrix in HLSL

This topic is 3511 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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:
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------

[maxvertexcount(3)]
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 on other sites
If the matrix is orthogonal, you can just transpose it. What are you using the matrix for?

##### 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 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 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.
http://en.wikipedia.org/wiki/Gram-Schmidt_process

This gamedev post discusses good ways to do a matrix inversion on a 3x3.
http://www.gamedev.net/community/forums/topic.asp?topic_id=464489

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633660
• Total Posts
3013221
×