Jump to content
  • Advertisement
Sign in to follow this  
lucky6969b

OpenGL How to make an Direct3D program work in OpenGL context?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

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.

Edited by lucky6969b

Share this post


Link to post
Share on other sites

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?

Edited by Dirk Gregorius

Share this post


Link to post
Share on other sites
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. Edited by Mona2000

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!