Archived

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

onnel

OpenGL Converting opengl to d3d coords

Recommended Posts

I''ve written a d3d conversion of an opengl md3 parser. The problem is, as I understand it, all opengl (and md3) coords are Lefthanded and d3d''s are right handed (or is it vice-versa?). What do I need to convert an opengl vertex into a d3d one? I know this has been discussed before, but the search engine was of no use. Sorry. Should be an easy one! Thanks for the help, Onnel

Share this post


Link to post
Share on other sites
I''m quite sure OpenGL can use left handed AND right handed coords. Just choose the one you want (I don''t remember how to do this, check your doc).

I suppose D3D8 can do this too.

Share this post


Link to post
Share on other sites
Simply negate the Z coordinate to transform between a left-handed and a right-handed coordinate system.
In a left handed coordinate system the positive Z axis points 'into' the screen, while in a right-handed coordinate system the positive Z axis 'comes out of' the screen.

The positive X axis always point to the right and the positive Y axis always point upwards.

Hold you right hand in front of your face, with your thumb (X axis) pointing to the right and your index finger (Y axis) pointing upwards, then point your middle finger (Z axis) towards your face. This is where the name 'right handed coordinate system' comes from.

Now hold your left hand in front of your face, similarly with your index finger (Y axis) pointing upwards and your thumb (X axis) pointing to the right, now your middle finger (Z axis) points away from your face. Hence the name 'left handed coordinate system'.

So, you see, there isn't really much difference between the two. IIRC OpenGL by default uses an RHS (Right handed system), while Direct3D uses an LHS. To make either one use the other system, simply scale your world matrix with (1, 1, -1) before applying your transformations and rotations etc...

[EDIT: I realised that I had given an incorrect scale vector]

Edited by - Dactylos on July 31, 2001 6:33:29 AM

Share this post


Link to post
Share on other sites
DAC,

Thanks for the good explanation! Does this even truly apply to the way coordinates are stored then, or does it only apply to transformations done on coords? If it doesn''t effect the way coords and stored, and front how I understand what you wrote, it doesn;t, then there is no problem loading the exact same vertex data into OPENGL or D3D.

Is this correct?

Are there any other issues that would cause problems loading index and vertex data which was originally stored for use with opengl into d3d? Winding issues (ordering of vertices CW vs. CCW), or anything else?

Thanks!
Onnel

Share this post


Link to post
Share on other sites
If you load a model created for an LHS into an application that uses an RHS, then the model will appear mirrored in the XY plane (the Z coordinate is in effect implicitly negated; so to speak ).

You should decide whether to use an RHS or an LHS in your engine and then load the vertices etc in the exact same way, but each frame you would automatically mirror the world matrix in the XY plane (in other words, scale by (1, 1, -1) as I mentioned above) if you are using the rendering API for which your chosen coordinate system isn't the default.

ex.

If you choose to always work with a right handed coordinate system (I'm assuming here that OpenGL uses a right handed system and Direct3D uses a left handed system. This might not be correct however, please look this up, or try it out. If I have it backwards, then simply do it 'the opposite way' from what I show here).

When rendering with the OpenGL API you don't have to do anything, because it uses a right handed system by default. so simply apply all the translations/rotations/etc and then render your model.

When rendering with the Direct3D API, you should apply a scale(1, 1, -1) to the world matrix (or the view matrix) every frame, before doing any transformations and/or rendering. This should ideally be done in your BeginFrame() function or equivalent (the one where you call d3d_device->BeginScene() etc...), so that afterward all translations and rotations and whatever the application using your engine performs are in effect performed in a right handed coordinate system.

If you don't apply this scale your models will appear mirrored (eg. an enemy wielding a sword in his right hand would appear correctly under one API, while the sword would appear in his left hand under the other API).

Of course there is also the possibility that I'm totally off and that both OpenGL and Direct3D use the same kind of coordinate system. If so, then just ignore what I've said (it's of course still good to know).

Umm.... Feels like I'm mostly ranting by now, so I'll stop...

[EDIT: I realised that I had given an incorrect scale vector]

Edited by - Dactylos on July 31, 2001 6:34:31 AM

Share this post


Link to post
Share on other sites
This may be a little of the topic,
but I think both OpenGL and D3D can use both RH and LH systems.

I use RH for both APIs. It''s just the question of projection matrix and face culling. To use RH for d3d call D3DXPerspectiveRH (instead of D3DXPrespectiveLH).

Share this post


Link to post
Share on other sites
That's a convenience function that performs what I have described above (in addition to setting up a perspective projection matrix). Though, I claimed that you should apply the scale to the world matrix, perhaps I was wrong, and the projection matrix is the one you should scale.

I was not aware such a function existed, since I mostly dabble in OpenGL. What I have described above is however the 'theory' behind using different coordinate systems in the two APIs. AFAIK there is no such convenience function in OpenGL and you must explicitly perform a mirror in the XY plane (if you want a nonstandard coord-system).

The culling mode can be changed in both APIs to use either CW or CCW.

Edited by - Dactylos on July 31, 2001 10:37:44 AM

Share this post


Link to post
Share on other sites

  • Forum Statistics

    • Total Topics
      627737
    • Total Posts
      2978873
  • Similar Content

    • By DelicateTreeFrog
      Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
      Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
      For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
      So, here's what the plan is so far as far as loading goes:
      Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
      Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
      Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
      The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
      So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
      With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!
    • By JJCDeveloper
      I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      -Questions
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
    • By ferreiradaselva
      Both functions are available since 3.0, and I'm currently using `glMapBuffer()`, which works fine.
      But, I was wondering if anyone has experienced advantage in using `glMapBufferRange()`, which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
      Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
  • Popular Now