#### Archived

This topic is now archived and is closed to further replies.

# Inverting an Orthographic Projection Matrix

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

## Recommended Posts

Is it possible? Do orthographic projection matrices intrinsically have a determinate of very close to zero (if not zero) or am i doing something wrong? Thanks for any help

##### Share on other sites

Yes, "scaling down" orthographic projections have determinants close to zero. You didn''t state the problem you were having though.

An orthographic projection that drops a dimension can''t be inverted.

##### Share on other sites
I am trying to create an orthographic scene view (editor) and ''unproject'' my mouse pos from screen space to object space.

It isnt the end of the world if it isnt possible, there are other less elegant ways i can go about it.

##### Share on other sites
Of course you can inverse it (Don't drop the z component, GL will do it for you when needed) ! It's basic AFFINE (LINEAR) PROJECTIVE maths.

The inverse is simple :

For the 3x3 submatrix, if there is no scaling it's the transposed matrix. If there is uniform scaling. Well ... s/(s*s) = 1/s, so just transpose and multiply by 1/(s*s). If there is non uniform scaling .. well google search "matrix inversion", won't loose my time for that. For the translation :

P' = M*P+T
P = M-1*(P'-T)

So T'=-M-1*T
such that
P = M-1*P' + T'

The main issue concerning floating point precision is not small numers byt themselves, it is substracting numbers that result in much smaller numbers which may give you an erratic Det. If you use uniform scaling then you don't have to waste processor time computing the Det (s3), you don't need it.

[edited by - Charles B on November 29, 2003 8:01:48 AM]

##### Share on other sites
I might have gotten something wrong but:
When I have a camera facing any directions in the world there is quite a few 3D-points that will end up on the same 2D screen coordinate.
Assuming screen is isomorph to R² and not with a limited number of pixels it´s exactly a line in 3D that ends up as being projected on a single screenpos. This is clearly not an injective function hence it´s not invertible (<=> Det = 0).

So your mousepos will be a representation for a complete line in 3D not for a single 3D-pos (you can still cast a ray in the direction of the line and see what you hit).

Charles B:
Either you just revolutionized math by inverting non-invertible functions or one of us is on the wrong track. Anyways for me your post was like randomly reading phrases in an LA-book and with no connection to the question.

##### Share on other sites
Atheist,
In the context of GL or D3D a matrix is 4*4 not 4*2. I hope you realized it lately.

Yeah I realized this point may have been unclear (for a noob like you) and I was ready to reedit my post. But it was obvious to me that LowCal has some way to retrieve the z component from the z-buffer or with ray casting on the scene.

Anyway I suggested it mentionning :
Don't drop the z component, GL will do it for you when needed
... at the rasterization level. Have you ever coded software engines ? Well I did, and well before Quake1, thanks for your advises.

This means you need the z component in entry (thus the z-column) of the inverse transfo (mouse x,y,z). And thus you need the z row of the orthographic matrix. Of course inversible requires same dimensions both sides. Pls don't insult my math level it's much higher than what you imagine So if you were really bright, and have a portion of my 150 IQ you would have caught it between the lines.

No connection to the question ? GRRR who do you think you are ? I started coding games 24 years ago and followed some very high level studies in maths. My answer is exactly what is required in common isometric games. Uniform scaling is a very useful situation because you can simply u-scale/rotate/translate and THEN apply a x,y,z scaling of the screen coordinates (this means concatenating matrices) but this can be done in a more efficient way. Did you imagine this before rushing to insults ?

Now I advise you to read my other technical posts before emitting such hasted judgements.

[edited by - Charles B on November 29, 2003 1:21:06 PM]

rofl.

##### Share on other sites
Yep ROFL. Another detail you certainly did not catch :

"determinate of very close to zero, if not zero".

If LCSD had dropped the z component in his matrix it would not have said close to zero but exactly zero. Because as you should know :

Det = ?*?*0 - ?*?*0 + ...

Makes exactly zero whenever you use floating point ( IEEE Standard 754) or fixed point. 0*x is always 0 exactly in any implementation.

So its seems only one did not get the context taking others as idiots, and it's you Atheist. Next time you'll do better ... don't worry.

[edited by - Charles B on November 29, 2003 2:04:52 PM]

##### Share on other sites
Thanks Charles B. So if i don''t scale the projection matrix (which i dont) the inverse of it is simply the transpose?

##### Share on other sites
Exactly. In linear algebra the inverse of a rotation is the transposed matrix. Then you certainly to remap the 2D x,y (and maybe z if you get it from the zbuffer) coordinates to pixel coordinates. I mean know the inverse factors.

Mouse(x,y,z) -> Camera (x,y,z)
-- Multiply by transposed + inv translation ->
3DPoint in world coords(x,y,z).

That''s pretty simple. I am sure you can find sample codes on the web even here in the forums or pages dedicated to 3D or isometric games. I am too lazy to write it here.

##### Share on other sites
Oh and BTW you don''t even need to actually transpose the matrix. Simply consider how the matrix multiplication is and it becomes dot products.

M = [X,Y,Z] in columns

Mt =[X,Y,Z] in rows

So transforming P''(Camera coords) into P (world coords) is just :
P''''=P''-T (T translation)
P = {x=X*P'''',y=Y*P'''',z=Z*P''''}

Now this really depends on how you get the z intersection with your scene. If it''s with the z-buffer then my solution works directly. If you use ray casting, Atheist was right. Define the ray equation in screen space (a Point(x,y,0) and the Zcamera vector for direction) then rotate Z (simplify), -translate and rotate (the point) as shown above. Raycast in your scene and that''s done.

##### Share on other sites
I do not see how its possible for the inverse of the proj matrix to simply be the transpose if its determinant is zero, or very close. I thought the pre-requisit for a transposed matrix to equal the inverse was for the determinate to be 1.

So where you mention simply transposing the 3x3 sub matrix above are you referring to the modelview matrix? i.e the 3x3 matrix would be the rotation and scaling section, i dont use scaling, and rotation matrices always have a determinate of 1, so a viable method for inversing that would be to transpose the 3x3 matrix and negate the translation section.

However that is not where my problem lies, my problem lies in inverting the projection matrix, which ive pretty much come to the conclusion is impossible, because i have google for 'inverse orthographic matrix' and several different variants on those keywords and still come up with nothing. As well as experimented with all of your suggestions above but to no avail.

On the plus side however i have now implemented alot more matrix functions than i had before

[edited by - LowCalorieSoftDrink on November 30, 2003 2:13:58 AM]

##### Share on other sites
Well maybe the best thing then is that you send a code sample. I have to know how you define your projection matrix and model view matrix precisely. I also have to know how you get the intersection with the ray/scene. Ray casting or zbuffer ?

But yes if you do not apply scaling and just rotate/translate the model the modelview inverse is the transposed matrix of course. Then if you don''t drop any component (z) in your projection matrix it is inversible (it''s only remapping 2D coodrinates and z). If you use a zbuffer you cant'' drop z. That''s what I supposed. One of the qui proquos is that in one sense glOrtho * gl ModelView is mathematically a projection. I know that under GL these matrices are defined separately. I am too accustommed to the inner logic of the graphic pilpeline because here your scene/object verts will directly ouput screen verts with the TOTAL projection matrix.

So I have to know the code sequence where you define the GL or D3D transfo world -> camera.

• ### Forum Statistics

• Total Topics
628730
• Total Posts
2984427

• 25
• 11
• 10
• 16
• 14