views:

1908

answers:

14
+4  Q: 

DirectX or OpenGL

If you were writing the next 3d graphics intensive application in C# (like a 3d modelling and animation software), which one would be a better choice?

If we consider C# as platform independent, then OpenGL seems tempting, but what about the performance, etc?

Since the used language is C#, the performance is pretty crucial to consider.

Edit: You can also consider SlimDX and TAO, OpenTK, csGL, etc too.

A: 

There is a managed API for DirectX, which gives you access to the DirectX API as "native" C# objects. That's a huge leg-up over OpenGL.

Joel Coehoorn
Managed DirectX is deprecated in favor of XNA, and XNA is a totally and completely different API.
Cody Brocious
The current alternative to MDX is SlimDX not XNA, but there are also managed wrappers for OpenGL, i.e. SharpGL
Neil N
A: 

I don't feel that OpenGL fits nearly as well into a pure OO environment as something like XNA does. That said, if you really care about cross-platform compatibility, it shouldn't matter what you backend to.

Design the business logic of your application to be independent from the rendering backend. You should be able to plug in an OpenGL rendering object then swap it out for an XNA renderer no problem. Not only does this increase your potential customer base (by enabling support for both), but makes your application's design far nicer.

Also a small note, DX shouldn't be used from .NET, as Managed DirectX has been deprecated; use XNA.

Cody Brocious
+12  A: 

Performance difference between Direct3D and OpenGL is near nil. The feature sets of the two do not map one to one, but they are close. The main pro for OpenGL is cross platform support.

Ronny Vindenes
Is this still valid? DX always seem to win out on my machine...
0xC0DEFACE
Ronny Vindenes
+5  A: 

OpenGL lagged in the past in relation to performance features, but things got fixed eventually. To give an example, consider bindable uniforms, where Direct3D had a faster mechanism before OpenGL acquired a similar one. Apart from supporting different feature-sets at times, there's no difference.

So unless you intend on working with the very newest GPU features, I'd advise you go with OpenGL. It's safe to say there aren't many areas where OpenGL lags in performance-related features.

By the way, C# and .NET are platform-independent only if you take certain precautions.

Eduard - Gabriel Munteanu
Thanks, what precautions do you mean?
Joan Venge
That is, using platform-independent features and code. Direct3D is an example of non-portable application of C#/.NET.
Eduard - Gabriel Munteanu
A: 

It depends what platform you intend to target.

If you only need to target windows use XNA

For cross-platform work, maybe someone else out there has done some work using mono with openGL - I'm assuming you are intending to make graphics software rather than a game, so having somekind of framework like winForms would be very helpful for all your UI controls.

JSmyth
+12  A: 

I'd recommend OpenGL for the following reasons :-

  1. Cross Platform - OpenGLES being of particular relevance these days for Mobile platforms
  2. The OpenGL shading language implementation is inherently superior to DirectX shaders.
  3. OpenGL is somewhat easier to learn as it's possible to set up basic renders with very few lines of code
  4. Philosophically OpenGL was designed as a general purpose rendering engine, whereas DirectX was always games orientated, so OpenGL would seem a better fit for your question.
  5. OpenGL is stable technology that has been around for some time and will continue to be so. DirectX is more dependent on the whim of Microsoft and could be deprecated in an instant if MS felt like it.

That said, the requirements of your system and your personal preferences could tip it either way as both approaches are solid implementations. On the downside OpenGL is very much a state machine and can be tricky to fit into OO, although it's certainly not impossible.

EDIT: Added to clarify my comment on the OpenGL shader model being inherently superior to DirectX. This is because DirectX shaders are compiled with the program at development time against a generic GPU model, whereas OpenGL shaders are held as source code and compiled by the OpenGL driver at run time. Consequently theoretically it's possible for the driver writer to take advantage of specific (or updated when running an old program) features of the GPU and create compiled code that can run faster than DirectX shaders. It's a small point, but potentially quite an important one.

Cruachan
1. OpenGL ES isn't important here, as there's no way to run .NET code on any popular OGLES platform. 2. With the technique model used in HLSL, I see nothing superior about GLSL -- mind backing that up? 3. OGL is much harder to get started with in the .NET space. Look at XNA. [...]
Cody Brocious
4. There's nothing about Direct3D that's games-oriented any more than OGL. Yes, DX has more to it, but there's no reason you have to use any of it. 5. Yes, MS could deprecate it all, but this applies to anything proprietary. Are you really suggesting MS is going to kill DX?
Cody Brocious
1. Not *yet* - and being able to port your knowledge is advantageous generally2. In OpenGL the GLSL compiler is part of the video driver, whereas in DirectX the HLSL compiler is part of DirectX. Theoretically performance is superior with GLSL
Cruachan
3. Maybe not directly in Net, but the availability of any number of toy GLUT based systems to test rendering techniques and ideas in *is* and advantage.4. True less of an issue than it was, but historically DirectX was a totally games orientated system which has since been deployed elsewhere...
Cruachan
...and OpenGL the reverse (Doom being it's breakthrough title into game rendering). Not a big issue this any longer I give you.5. I simply point to the **long** trail of discarded MS technologies. Wouldn't be the first time :-)
Cruachan
would you care to elaborate why GLSL is inherently superior to Cg (which, effectively, is the same as HLSL)?
roe
Because theoretically the later compilation of GLSL by the video-drive means it can make optimal use of the card's hardware, whereas HLSL being compiler earlier must use a standard machine model.
Cruachan
+1  A: 

If you don't like XNA, you can use SlimDX. Unlike XNA, SlimDX supports DirectX 10.

Michael Kelley
+1  A: 

With OpenGL you can use "DirectX 10 features" like geometry shaders on Windows XP and Linux. Using GLUT it is very simple to get a demo application up and running within minutes.

heeen
GLUT is a dinosaur and has a non-free license, GLFW is much better.
LiraNuna
+1  A: 

I've used both OpenGL and DirectX. I think the performance is pretty similar. I prefer the programming model of OpenGL -- especially its handling of transformations, and direct support of picking operations. I dislike the way MS continues to rewrap the same functionality every time it upgrades the OS, and I think OpenGL protects you from that.

However, both are quirky and you need to spend a good deal of time making sure that it interacts nicely with the hosting application framework, whether Windows or something else.

Jeff Kotula
+1  A: 

You will probably want to look into the IrrLicht engine, it has both a C++ and .Net API, and it entirely Graphics API agnostic (meaning you can use the same code to execute OpenGL or DirectX and the programmer wont even have to know which you are using)

You might also want to look into SlimDX, a very fast, lightweight, open source alternative to XNA

Neil N
+5  A: 

Performance in managed code, with respect to the graphics subsystem, is not bad. SlimDX pays a slightly penalty over completely native code on each call into DirectX, but it is by no means severe. The actual penalty depends on the call -- a call into DrawPrimitive will be vastly more expensive overall than a call to SetRenderState, so percentage-wise you end up losing a lot more on the SetRenderState calls. SlimDX incorporates a tuned math library that generally performs very well, although you have to be a bit careful with it. Profiling, even with a junker tool like NProf, highlights this stuff very quickly so it's not difficult to fix.

Overall, if we consider generic, completely optimal C++ and C# code doing rendering via D3D, the C# version is probably within 10-15% of the C++ version. That's hard to achieve though; consider how much time you're saving by working in C# which you can apply to higher level graphics optimizations that you probably simply wouldn't have time for if you had to build the entire thing in C++. And even if you managed to get that extra 10% in C++, it'd promptly shrink to 5% within a few months, when a new round of hardware tears through your application code faster than ever. I know what I'd pick -- which is why I wrote SlimDX to begin with.

OpenTK is subject to similar performance characteristics, with the caveat that their math library is rather slow in places. This is an implementation bug that I've discussed with them, and will hopefully be fixed before too long.

Promit
Hi, thanks for the reply. It's great that you wrote slimdx. Thanks for that.
Joan Venge
+2  A: 

DirectX is going to have better video driver support on Windows since it is what MSFT uses to certify cards. We've found OpenGL support is lacking, crashy or plain wrong for cheaper cards on Windows.

jeffamaphone
A: 

You may want to look at this: http://groups.google.com/group/microsoft.public.win32.programmer.directx.managed/browse_thread/thread/1fc097147796e15b

The Managed API isn't being supported now, so OpenGL may be your best bet, unless you want to go with XNA.

Depending on what other platforms you may want to support, such as mobile devices or XBox360 then that may help you decide which api to use.

James Black
+3  A: 

Here is my honest suggestion for you: Make use of both.

Advice

I would any day weigh which one has more tools available to finishing a program, and then for performances reason, I would make use of the other in order to ensure maximum performance on different systems.

Thoughts on the debate

Let us review: The performance of the program depends on many things, mainly your effort (the application's code) and the drivers on the given computer. Graphics APIs are a mean for your application to communicate to the GPU, and thus makes you incredibly dependent on how well a given driver is implemented for installed GPU.

My choice

Direct3D is sometimes faster on some graphics card than OpenGL, and that is because of graphics vendors and their drivers.

DirectX offers plenty of tools to speed up development. I realize it has a very steep learning curve to begin with, but allow me to remind, that you happen to be a programmer. I even dare to say a very good one.

Conclusion

Therefore you must be able to fight your way in and slowly developing your own framework that utilizes both APIs and thus make you capable of testing and implementing whatever program you have in mind.

Sincerely,

Mossa Nova Merhi

Mossa Nova