How to adapt DirectX-style world/view/projection matrices to OpenGL?
Asked Answered
P

3

1

I have an application which uses DirectX, and hence a left-handed world-coordinate systen, a view which looks down positive z and a projection which projects into the unit cube but with z from 0..1.

I'm porting this application to OpenGL, and OpenGL has a different coordinate system etc. In particular, it's right-handed, the view looks down along negative z, and the projection is into the real unit cube.

What is the easiest way to adapt the DirectX matrices to OpenGL? The world-space change seems to be easy, just by flipping the x-axis, but I'm not entirely sure how to change the view/projection. I can modify all of the matrices individually before multiplying them together and sending them off to my shader. If possible, I would like to stick with the DirectX based coordinate system as far down the pipeline as possible (i.e. I don't want to change where I place my objects, how I compute my view frustum, etc. -- ideally, I'll only apply some modification to the matrices right before handing them over to my shaders.)

Promiscuous answered 12/9, 2010 at 16:4 Comment(1)
As long as you don't use the fixed function pipeline, there's no real difference. The left-/righthanded coordinate thing is nothing more than a convention, that the FFP and a few convenience functions follow.Eustazio
P
4

The answer is: There's no need to flip anything, only to tweak the depth range. OpenGL has -1..1, and DX has 0..1. You can use the DX matrices just as before, and only multiply a scale/bias matrix at the end on top of the projection matrix to scale the depth values from 0..1 to -1..1.

Promiscuous answered 1/1, 2011 at 10:21 Comment(0)
S
0

Not tested or anything, just out of the top of my head :

oglProj = transpose(dxProj)
glScalef (1., 1., -1.);  // invert z
glScalef(0.5, 0.5, 1); // from [-1,1] to [-0.5, 0.5]
glTranslatef(0.5, 0.5, 0) // from [-0.5, 0.5] to [0,1]

oglView = transpose(dxView)
glScalef (1., 1., -1.);  // invert z

oglModel = transpose(dwModel)
glScalef (1., 1., -1.);  // invert z

oglModelView = oglView * oglModel
Sperling answered 13/9, 2010 at 12:25 Comment(0)
S
-2

There is an age-old extension for OpenGL exactly for that: GL_ARB_TRANSPOSE_MATRIX. It transposes matrices on GPU and should be available on every video card.

Systematology answered 13/9, 2010 at 12:35 Comment(2)
This has like zero to do with the question: The problem is that OpenGL has a different chirality, and this cannot be solved with a transpose.Promiscuous
This solves other half of the problem - matrix layout difference.Systematology

© 2022 - 2024 — McMap. All rights reserved.