PDA

View Full Version : any recommendations for a port from directx ~7?



virtualpaul
06-05-2011, 07:03 PM
Hi,

This is my first post. I wrote a lot of stuff in C/C++ ~10 years ago in directX ~7. I am wondering if there are easy ways to convert this to OpenGl?

A friend told me that now everything is done by the GPUs with another language. My original code was sending list of pixels, polygons, textures to the card using function calls.

mhagain
06-06-2011, 06:48 AM
What your friend is referring to is programmable shaders, and it's certainly not everything. There's still a good sized chunk of the pipeline that still uses the "old way".

If you were writing a new application I'd recommend that you use shaders from the outset; for porting an existing one you don't have to and you can still use the old-style draw calls and state changes.

Going D3D to OpenGL sounds interesting. I'm thinking that you'll find it easier if you use vertex arrays in OpenGL - most beginning tutorials will use immediate mode (glBegin/glEnd), but vertex arrays have a closer mapping to D3D draw calls. glDrawArrays is roughly equivalent to DrawPrimitive, and glDrawElements is roughly equivalent to DrawIndexedPrimitive.

For states, SetTextureStageState can be seen as a combination of glTexEnv and glTexParameter. In OpenGL some of the states (glTexParameter) are properties of the texture object itself, and so only need to be set once (when the texture is created) and will automatically apply when you use the texture. The GL_COMBINE stuff has a good equivalence to the other STSS states.

SetRenderState has no clear mapping; there's a collection of glEnable/glShadeModel/glCullFace/etc calls and there's no easy way of doing one-to-one conversion; you'll basically need to go through your code and handle each case individually.

OpenGL extensions will probably drive you barmy. I recommend using something like GLEW instead of trying to manage them yourself. On modern hardware you can assume an absolute minimum of OpenGL 1.4/1.5 support (and that's talking integrated Intels; a real GPU will be much better) which has rough equivalence to D3D7.

For matrixes you may be able to convert quite cleanly by managing the matrixes yourself and using glLoadMatrix instead of the matrix stacks. D3D has a separate view matrix, OpenGL doesn't so you might need to handle this. There are subtle (or not-so-subtle) differences in how each handles the texture matrix, with OpenGL being clearly superior here as it Just Works the same way as the other matrixes.

The right-handed/left-handed/row-major/column-major thing isn't that big a deal really; it's more important that you pick your convention and use it consistently.

So where to start? I'd definiely recommend that you don't attempt to port any of your older programs right away. You should probably write some simple demo programs based on tutorials first, just to get a feel for the API differences and how things work. As soon as you start getting comfortable with things, you can then look to porting maybe your simplest program and see how you get on.

Good luck!

virtualpaul
06-06-2011, 07:09 PM
Cool.

Thanks a lot for all your advices. Looks like you know what you are talking about... :)

I will use your post as my first tutorial!

I had read a bit about the OpenGL versions a few months ago when searching for a way to draw a GUI on mobile phones but the project was put aside.

I guess it must be tough to write a library that would support both the mobile phone stuff and the high-end PC graphics card too.