# inverse transformation of the world matrix

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

## Recommended Posts

hello,

In a vertex shader program, I've got world transformation matrix and a point. I think that if I take inverse transformation matrix of the world matrix and multiply it by the point, I transform point from world space to object space.

psudecode:

Matrix worldm; // I only have these two variables

float4 point;

Matrix invWorl = inverseTransform(worldm);

float4 objectSpacePoint = invWorld * point; // I want this

How can I do this in hlsl ?

##### Share on other sites

Why not pass the inverse of the world matrix as a variable to your shader?

And normally you would be multiplying the point by the world, view, and projection matrices to transform from object space to world space.  Is that what you mean to do?

EDIT: If you still intend to invert the matrix, it would get very slow to invert the matrix for every vertex, every frame.  The best way to go about this is to pass the preinverted matrix to the shader once.

Edited by Matthew Meeks

##### Share on other sites

isn't there a way to find inverse transformation matrix of the world matrix in the hlsl shader ? passing it as a parameter is a solution but not my first choice :)

##### Share on other sites
isn't there a way to find inverse transformation matrix of the world matrix in the hlsl shader ? passing it as a parameter is a solution but not my first choice

It is usually possible to do this in a shader, but then you will be re-calculating the inverse for every vertex that gets processed.  If you calculate it once on the CPU, and pass it as a constant parameter, then you are much better off.  In addition, you would likely only be able to calculate the inverse within a shader if you know how the transformation was created - i.e. if you know the rotation amounts, the translation amount, and the (uniform) scaling amount.  Even so, it isn't a good idea to recalculate it in the shader program!

You could try transpose. http://msdn.microsoft.com/en-us/library/windows/desktop/bb509701(v=vs.85).aspx

This will give the inverse of any orthogonal matrix.

Typically the transformation has some translation component, so that probably wouldn't work for him.  @afraidofdark: can you describe what type of transform you are using?

##### Share on other sites

I can only support the recommendations to do it once on the CPU then pass it as a constant.  Inverting a matrix can potentially be an expensive operation and is definitely not one that you want to be running once per-vertex.

Your local friendly matrix library should contain a routine for inverting any 4x4 matrix, look at D3DXMatrixInverse or XMMatrixInverse, for example.

##### Share on other sites

I solve this by passing inverse matrix as a constant parameter. It works fine :)