How to make an Direct3D program work in OpenGL context?

Started by
7 comments, last by 21st Century Moose 7 years, 6 months ago

What I mean is if the program was first written for a right-handed coordinate environment,

I then converted it into Direct3D, but the fruit of it is still using a right-handed system.

For example, when it starts thinking about a left turn, it is thinking about a negative radian,

however, in Direct3D context, a left turn is an increase in radian, which is the reverse,

But the point is I don't want to change the logic of the whole program from start to end,

It would be too error-prone.

I just want to say feed it a Direct3D Vector and let it run as if it is in a right-handed system.

Is it possible?

The major point is, I don't want to change it because it is already working.

Thanks

Jack

Advertisement
Both D3D and OpenGL use left handed coordinates, but a small tweak to your projection matrices let you work with right handed coordinates. Just use the same math/matrix code for both APIs (except for the fact that your GL projection matrices need to use a NDC with -1<z<1, and D3D projection matrices need to use a NDC with 0<z<1).

Dear Hodgman,

Thanks for responding.

Another problem following this is that I just want to change this part (this library),

At first blush, every other stuff is already working in the "default" environment which is Direct3D.

So what I do prefer is add another layer to this library, this will convert this into the parameter into OpenGL,

and I take back from it, it will be in Direct3D context again.

But I can't think of a way to make the program think this is a right turn


D3DXVECTOR3 crs = zs + R_min * dot(D3DXVECTOR3(cos(anglstart), sin(anglstart), 0), rotZ(PI / 2));

It is a right turn in the original program, but it is talking about a left-turn in Direct3D sense.

I don't mind changing all the PI / 2 to -PI / 2, but I was a little bit careless, everything would be screwed up.

You may say, okay, let's do a find-and-replace, but it is quite a complex library, there are a lot of convoluted logics going on

inside this library, Therefore It's not easy to change everything and hoping the last thing would behave as the original.

At least, I've already tried. :)

Thanks for helping.

Both D3D and OpenGL use left handed coordinates

Are you sure about this? My understanding was that OpenGL is right-handed (x-right, y-up, z-backwards) and DirectX is left-handed (x-right, y-up, z-forward). If you apply the right hand rule (e.g. https://en.wikipedia.org/wiki/Right-hand_rule) DirectX would not satisfy this. I wonder if there is a reference?

Both OpenGL and DirectX are left-handed in clip space.
You're probably thinking of world space and view space in the fixed function pipeline, which were right handed in OpenGL. Nowadays the FFP is dead and handedness in those spaces is up to the user.
AFAIK, GL/D3D both define NDC's axis as +x is right across the screen, +y is up the screen, and +z is into the screen.
Technically, z doesn't really have a direction, but the convention that most people use of less/less_equal depth testing means that lower coordinates are closer to the viewer.

Beyond that D3D puts the pixel origin at NDC x=-1,y=1, with +y going down... So D3D/GL viewport coords are flipped.

Everything else is down to the math library that you use (which includes any fixed function APIs). Traditionally GL view matrices were constructed such that the camera was looking down view-space -z, but that's a feature of your math library.

There's no reason you shouldn't use the same conventions in both APIs.

Thanks for clearing this up. This makes a lot of sense. I always looked at this from the definition of the projection matrix.

But I can't think of a way to make the program think this is a right turn

D3DXVECTOR3 crs = zs + R_min * dot(D3DXVECTOR3(cos(anglstart), sin(anglstart), 0), rotZ(PI / 2));
It is a right turn in the original program, but it is talking about a left-turn in Direct3D sense.
I don't mind changing all the PI / 2 to -PI / 2, but I was a little bit careless, everything would be screwed up.
You may say, okay, let's do a find-and-replace, but it is quite a complex library, there are a lot of convoluted logics going on
inside this library, Therefore It's not easy to change everything and hoping the last thing would behave as the original.

No don't do a find/replace across the whole project or anything like that. You only need a different projection matrix between GL and D3D. That's it.

Are you using the same math library on both APIs? e.g. is that D3DXVECTOR3 used on both D3D and GL?

Are you sure about this? My understanding was that OpenGL is right-handed (x-right, y-up, z-backwards) and DirectX is left-handed (x-right, y-up, z-forward). If you apply the right hand rule (e.g. https://en.wikipedia.org/wiki/Right-hand_rule) DirectX would not satisfy this. I wonder if there is a reference?


Neither are actually true.

Handedness is just a convention but both APIs are perfectly capable of supporting either left-handed or right-handed coordinates. For a reference, see for example the D3DXMatrixPerspectiveFovRH function.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement