PDA

View Full Version : Supporting both DirectX and Opengl



technomage
02-12-2006, 11:02 AM
Hi guys and gals

I'm busy writing the basic framework for the InfinitEngine for the InfiniteSpace-online project.

Now I would like to be able to support both Opengl and DirectX. The current idea is that the correct dll is loaded a runt-ime based on which rendering engine you want to use. this works very well, and I have a very basic triangle appearing in both Direct and Opengl.

Opengl

http://www.infinitespace-online.net/images/screenshots/InfinitEngineOpengl.png


DirectX

http://www.infinitespace-online.net/images/screenshots/InfinitEngineDirectX.png


Now some of you will be able to spot the problem. Because DirectX used a Left Handed coordinate system, the triangle in the DirectX looks different. Also the triangle looks further away in the DirectX window, dispite having the same eye location and look at points.

Now I have read some documents about this problem , the solution M$ give is to revers the vertex order on every vertex you send to get the same results and to scale the view matrix by -1 in the z direction. This is the document I found

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/Coordinate_Systems.asp

Now there has to be a better way than this. I'd rather not have to put in extra processing to sort out my verticies for all calls to directX. And to be honest I haven't done any directX coding for about 6 years so I wouldn't know how to scale the view matrix by -1 in the z direction.

Has anyone tried to support both opengl and directx before? If so how did you resolve this problem?

Any ideas?

Dean

JernejL
02-12-2006, 11:34 AM
don't bother with this, just support one GFX library.

K4Z
02-12-2006, 12:55 PM
I haven't ever tried to support multiple rendering engines before :shock: (Hope that works out :wink:)

But having dealt with multiple co-ord systems, typically thats the easiest option, reversing the view matrix and vertex orders.

Which would you pick as your 'Main' renderer (Most strong at coding in)? OGL?
What about keeping the DX Left handed system, and flip it in OGL instead?

---

Instead of modifying the vertex order, would flipping the backface around work?

glEnable(GL_CULL_FACE);
glCullFace(GL_Front);

Or something to that effect. It's a littke hackish, but it's all visual in the end.

grudzio
02-12-2006, 01:08 PM
I think that it is easier to change OpenGL to work with D3D coordinate system. Use glFrontFace(GL_CW) to set clockwise polygon orientation as in D3D. This way you can use same vertex layout for both apis. To scale world space call glScalef(1.0,1.0,-1.0) before any view transformation.

You can also take a look at Irrlicht engine. It supports D3D, OpenGL and even software renderer.

But to be honest I agree with Defi.

WILL
02-12-2006, 05:51 PM
Hmm... well to be honest. If you had to slow one or the other down lets look at who you'd be slowing down if you choose one or the other.

DirectX: Some Windows users that prefer DirectX.

OpenGL: Some Windows users that prefer OpenGL. All Linux, MacOSX and BSD users.

Now this isn't really taking into account Vista, but then again who cares about that now. It's not even out yet and I doubt that MS will get a away with slowing down another API just to boost their own for very long. (Vista sales will suffer for it and there'll be more new Linux users in the world.)

JernejL
02-12-2006, 07:40 PM
To be clear on this, with opengl you are actually on better in vista, you will have full hardware acceleration as long as you'll have proper gfx drivers installed. on vista opengl runs in like 2 or 3 modes:

- DX wrapper, see: http://www.delphi3d.net/hardware/viewreport.php?report=1566
- opengl drivers from windows xp driver model (no aeroglass in windowed mode, but full speed)
- fully accelerated opengl driver from a graphics card vendor (nv & ati)

so yeah, opengl is working well on vista.

technomage
02-12-2006, 10:59 PM
I found out how to do it. Basically you need to trow away calls to gluPerspectrive and D3DXMatrixPerspectiveFovRH etc. and do your own Perspective calculations and set the matrix manually.

You also need to ensure that the vertex order mode is the same i.e CCW or CW.

I just need to find some good matrix classes now that support in built functions like Perspective and Orthographic, or find a reference on wikipedia.

WILL
02-12-2006, 11:19 PM
Thanks for correcting me Delfi. ;) I have no desire to jump on the Vista bandwagon so I'm a bit outta touch with the specifics.

However for matricies... what about those libraries that Daniel was talking about earlier in another thread? Those of any use to you?

JernejL
02-12-2006, 11:30 PM
Thanks for correcting me Delfi. ;) I have no desire to jump on the Vista bandwagon

yeah same here, but i would pay a full price of vista ultimate to have DX10 on my XP to play some cool games like alan wake & crysis...

WILL
02-12-2006, 11:55 PM
Well the new next gen games are tempting no?

So in supporting both OpenGL and DirectX which functions are unique to one and not the other?

technomage
03-12-2006, 05:22 PM
OK, managed to fix problem 1, which is the perspective matrix.

I had to basically throw away the helper libraries for both API's and write my own

The second problem (the triangle verticies being ina differnt order) is fixed as well. Turns out I forgot that the glColor call needs to be before the glVertex call in Opengl.

technomage
09-12-2006, 12:44 PM
Made some good progress with the engine over the last few days. I have implemented transforms and rotations and I now have a spinning cube and triangle. :) OK, it's not a great engine at the moment, but it works in DirectX and Opengl without any code changes to my test app.

So it can be done people. I'm going to try and get some texture and shader stuff done over the next week or so.

Customary screen shots for those that are interested.

Opengl

http://www.infinitespace-online.net/images/screenshots/InfinitEngineOpengl2.png


DirectX

http://www.infinitespace-online.net/images/screenshots/InfinitEngineDirectX2.png


BTW - I'm using SDL as the window manager and input manager at the moment, DirectX 9 works very nicely with SDL (as does opengl). I also found TinySDGL (http://www.kyb.tuebingen.mpg.de/bu/people/gf/software/) as well, which I might use for a software renderer . TinyGL might be a good thing for people working on mobile games with FPC as it implements a basic opengl layer (including vertex arrays) and does all the rendering in software. it's quite quick too. :D It's in C so you either need a dll or convert it to pascal. :cry:

savage
10-12-2006, 11:29 AM
NICE! From Oz.