Skip to main content
GameDev.net gamedev.net
🔒 Locked

DirectX vs OpenGL ?

Started by TheCompBoy Aug 9, 2011 at 2:21 PM 39 replies 18.2k views
Original Post
TheCompBoy
TheCompBoy
Hello!
I read about DirectX and OpenGL and which one is the best to use for what and which one is bad at what..

All i found was that OpenGL is cross-platform and its easier to code with it.. But is this realy true or can some one please provide me with a better website where i can read about it ? Or something,

Thanks!
Hodgman
Hodgman
Previous threads here

GL works on Linux/Mac/Windows
GLES works on mobile devices and browsers
DX9 works on Windows XP/Vista/7
DX11 works on Windows Vista/7

GL has 4 main versions --
* GL1 is the basic stuff that the majority of tutorials will cover. This is old and deprecated, but can still be used as an introduction to graphics programming.
* GL2 is the equivalent to DX9. It adds shaders and other 'modern' features.
* GL3/GL4 are the modern versions, equivalent to DX10/11.

GLES has 2 main versions -- one for older mobiles, and one for newer mobiles with more modern features.

D3D has 3 main versions at the moment --
* DX9 is only used if you need to support Windows XP.
* DX10 has been superseded by DX11, and isn't required any more.
* DX11 is the current gold standard for graphics APIs, but only works on Vista/Win7.
TheCompBoy
TheCompBoy
Thank you for this great information!
Cornstalks
Cornstalks

I read about DirectX and OpenGL and which one is the best to use for what and which one is bad at what..

Actually, the real question is what you are good and bad at. They're both graphical APIs (well, D3D is, which is a part of DirectX), and you can do cool stuff in both of them. The only time it'll ever make a difference is if you have the knowledge and ability to take either API to its limits. Which, at your current state, you can't do... yet. So pick one and learn it. Don't be afraid of picking the wrong one. There isn't necessarily a "wrong" choice. And the best programmers will know both, so if you plan on continuing to progress you'll probably learn a bit of both anyway.


All i found was that OpenGL is cross-platform

Yes, it is.


its easier to code with it.

Not necessarily. I've heard plenty say DirectX is easier, and plenty say OpenGL is easier.


But is this realy true or can some one please provide me with a better website where i can read about it ? Or something,

If your programs are going to run on just Windows, my honest suggestion is putting two peices of paper on a dart board, one labeled "DirectX" and one labeled "OpenGL", and throwing darts until you hit one, and then just go with that one.

My one precaution is that there's a lot of outdated documentation and tutorials for OpenGL (i.e. NeHe's tutorials are waaaaaaay outdated). So make sure that whichever you go with, you stick with current and relevant documentation and tutorials.

[edit]

Blasted Hodgman, would you slow down for a minute!?! The last couple of threads I've replied to, there's been no one who's replied when I start to reply, but by the time I hit the "Post" button, you've already claimed the glory of first reply and managed to get the OP to reply as well! I give you the award of Uber Ninja.
yckx
yckx
OpenGL is a cross-platform graphics API. It only does graphics. It is easier to initialize than DirectX, but I don't think it's significantly easier or more difficult to use. I haven't looked at it in a while, so I'm not the guy to give you many specifics. Also, you'll need other libraries if you want sound or game-controller support.

DirectX is Windows-only, and has APIs for sound and input as well as graphics. It's a bit more complicated to set up, but not severely so. If you're targeting WinXP then you're limited to DirectX 9; if you're not targeting XP then look at DirectX 11. DirectX 9 still has a fixed-function pipeline, which makes it easier to get into than DirectX 11's shader-only model, but so much has changed between the two versions that learning DX 9 has limited benefits if you're more forward-looking.
TheCompBoy
TheCompBoy
Thanks, im sure i will have use for all of theese ideas!
ryan20fun
ryan20fun
just pick whichever one your more comfitible with.
from what ive read and checked out, there is no "real" difference between the two, other OpenGL does not care weather your running XP/Vista/Win7 it wont cut you off from features if your card supports it.
Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.
JackTheRapper
JackTheRapper
Choosing between DirectX or OpenGL is largely pragmatic. "Knowing" how to use a given API is purely semantics so don't get bogged down with "is x better than y", especially at this stage.
DEADC0DE
DEADC0DE
I (subjective opinion!) would definitively say yes, OpenGL is easier and better to read than DirectX.

Take a look at a DirectX "Hello World" and OpenGL "Hello World". You will be able to read the OpenGL-code at once, but with DirectX you will first think of "wth? What's LPWSTR? pd3dDevice? SRV? Why aren't there any self-documenting names?". (at least that's what I thought of my first DirectX tutorial)

And it's getting even worse if you what to find a very uncommon functionality in the msdn documentation AND understand it. - good luck!
Well, OpenGL hasn't a super-good documentation either, but most function-names (I had to use) are self-explaining.

If you want to use DirectX, Program on Windows or if you like Visual Studio then ok. If you don't care I would use OpenGL.
But take a look at a Hello World Example of both before you make a decision! ;)
arbitus
arbitus

I (subjective opinion!) would definitively say yes, OpenGL is easier and better to read than DirectX.

Take a look at a DirectX "Hello World" and OpenGL "Hello World". You will be able to read the OpenGL-code at once, but with DirectX you will first think of "wth? What's LPWSTR? pd3dDevice? SRV? Why aren't there any self-documenting names?". (at least that's what I thought of my first DirectX tutorial)

And it's getting even worse if you what to find a very uncommon functionality in the msdn documentation AND understand it. - good luck!
Well, OpenGL hasn't a super-good documentation either, but most function-names (I had to use) are self-explaining.

If you want to use DirectX, Program on Windows or if you like Visual Studio then ok. If you don't care I would use OpenGL.
But take a look at a Hello World Example of both before you make a decision! ;)


While you are thinking about that, keep in mind that DirectX is built using "simple" COM. Whether you believe that is a good thing or not, a capable Windows programmer should have a good understanding of the naming conventions found within the API.
DvDmanDT
DvDmanDT
My experience was that OpenGL (1.X though) was waaaay easier to get up and running and rendering a few triangles, but actually working with it was a pita compared to working with Direct3D (9.0c). It was mostly small things, such as math libraries, texture loading and font rendering IIRC. It was several years ago, but I can imagine some of it being the same due to cross platform and licensing issues.

Maybe someone with more (recent) OpenGL experience can clarify the following?
* Is there a math library in OpenGL for matrices/vectors/planes/etc?
* Can you load textures in png or jpeg formats without using external libraries such as DevIL?
* Is there any simple way to render a string in OpenGL? I seem to recall there being some function to draw a single char using a supplied bitmap font..?
Yann L
Yann L

But take a look at a Hello World Example of both before you make a decision! ;)

A Hello World example is about the worst possible way to judge an API or language.


My experience was that OpenGL (1.X though) was waaaay easier to get up and running and rendering a few triangles, but actually working with it was a pita compared to working with Direct3D (9.0c). It was mostly small things, such as math libraries, texture loading and font rendering IIRC. It was several years ago, but I can imagine some of it being the same due to cross platform and licensing issues.

It's neither of these. It's just that these things are out of the scope of a graphics API. They have never and will never be part of OpenGL. Note that D3D11 (and partially D3D10) went the same way, essentially removing all this bloated D3DX stuff. The only really useful remains were moved to XNAMath, which became largely API independent.


Maybe someone with more (recent) OpenGL experience can clarify the following?
* Is there a math library in OpenGL for matrices/vectors/planes/etc?
* Can you load textures in png or jpeg formats without using external libraries such as DevIL?
* Is there any simple way to render a string in OpenGL? I seem to recall there being some function to draw a single char using a supplied bitmap font..?

No, no and no. The first (lack of math library) may be debatable, but everything else should never be part of a graphics API. Keep in mind that the main focus of a competitive graphics API is not beginner friendliness, but flexibility, performance, scalability, standarization and ease of driver implementation for IHVs. If you are looking for a beginner friendly framework, then both D3D and OpenGL are the wrong choices. A third party graphics or game engine is a much better solution for such scenarios.
SimonForsman
SimonForsman

My experience was that OpenGL (1.X though) was waaaay easier to get up and running and rendering a few triangles, but actually working with it was a pita compared to working with Direct3D (9.0c). It was mostly small things, such as math libraries, texture loading and font rendering IIRC. It was several years ago, but I can imagine some of it being the same due to cross platform and licensing issues.

Maybe someone with more (recent) OpenGL experience can clarify the following?
* Is there a math library in OpenGL for matrices/vectors/planes/etc?
* Can you load textures in png or jpeg formats without using external libraries such as DevIL?
* Is there any simple way to render a string in OpenGL? I seem to recall there being some function to draw a single char using a supplied bitmap font..?


It has nothing to do with cross platform or licensing issues, OpenGL is an interface between your application and the graphics driver, the actual OpenGL implementation you run is written and provided by the hardware manufacturers(The same goes for Direct3D) and it makes little sense to complicate their job by adding a bunch of irrelevant stuff that they have to implement.

Direct3D is exactly the same, it doesn't support text rendering, png or jpeg loading or high level math either, all that is handled by D3DX (Which is a helper library included in the DirectX package (This is written by Microsoft and doesn't make things harder for the hardware manufacturers as they can ignore its existance)).

There are plenty of D3DX equivalents for OpenGL you can choose from, you can even use D3DX along with OpenGL if you're crazy enough just like you can use DirectSound with OpenGL. (Allthough D3DX is a bit more tightly coupled with D3D which makes it far more painful to use with another graphics API)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
DEADC0DE
DEADC0DE
A Hello World example is about the worst possible way to judge an API or language.


Well, everything I thought of the hello-world in DirectX/OpenGL becaue true (I hate the naming conventions in DX and have big problems with things that should be easy, but are not!).
OpenGL: never had really big problems with it. Ok, it's not easy and not everything goes well, but I never searched for 2 weeks to solve one problem. (in DX I have)

But as I said - subjective opinion. If someone likes DX I have no problem with that ;D


* Is there a math library in OpenGL for matrices/vectors/planes/etc?
* Can you load textures in png or jpeg formats without using external libraries such as DevIL?
* Is there any simple way to render a string in OpenGL? I seem to recall there being some function to draw a single char using a supplied bitmap font..?
[/quote]

I don't know about matrices/vectors and so on, but if you want to load a jpeg/render a string... I think you use additional libs in both, DirectX and OpenGL:
DirectX => Win32/DXUT/XNA?... (there is also D3DX11SaveTextureToFile)
OpenGL => glut/SDL/SDL_ttf/glui/glee/openml?...
Gl_Terminator
Gl_Terminator
Use directX i was an OpenGL fan but finally i gave up, directX has a lot more support, and complex stuff are handled more easy, and dont care openGL is cross-platform because wine runs DX very well on linux. read this link for more info http://vasilydev.blogspot.com/2011/08/dificulty-of-deploying-3d-applications.html
shdpl
shdpl

Use directX i was an OpenGL fan but finally i gave up, directX has a lot more support, and complex stuff are handled more easy,

Could you be more specific please?
BytePtr
BytePtr
When i first saw DirectX code and then OpenGL, i said: goodbye DirectX.
Im choosing OpenGL. And im glad that i did this. OpenGL is alot easier and cleaner. It makes many things so much easier.
And now im not even thinking about switching to DirectX.
21st Century Moose
21st Century Moose

When i first saw DirectX code and then OpenGL, i said: goodbye DirectX.
Im choosing OpenGL. And im glad that i did this. OpenGL is alot easier and cleaner. It makes many things so much easier.
And now im not even thinking about switching to DirectX.


On the other hand...

glEnableClientState (GL_VERTEX_ARRAY);
glVertexPointer (...);

glEnableClientState (GL_COLOR_ARRAY);
glColorPointer (...);

glClientActiveTexture (GL_TEXTURE0);
glEnableClientState (GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer (...);

glClientActiveTexture (GL_TEXTURE1);
glEnableClientState (GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer (...);

glClientActiveTexture (GL_TEXTURE1);
glEnableClientState (GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer (...);

glDrawRangeElements (...);

Versus...

device->SetFVF (...);
device->SetStreamSource (...);
device->SetIndices (...);
device->DrawIndexedPrimitive (...);

It does go both ways you know. And don't even get me started on "bind to modify"...
Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls. 
L. Spiro
L. Spiro
I am using both equally. I will mention a few things that I don’t see mentioned every time there is an OpenGL vs. Direct3D discussion.

OpenGL has a way of biting you in the ass because of its state-machine structure.
Example #1: Long ago while learning about both Direct3D and OpenGL, I had an engine that was running fine on both. Then one day when I disabled lighting, all of my models in the OpenGL version turned black instead of bright as they had always done before. I had no idea when no-light rendering stopped working so I started research on their lighting model.
I discovered that glColor is used when lighting is off.
I had glColor set to black to prevent it from modifying my sprites and other things it modifies.

Changing a state can have consequences where you least expect it until you really have a lot of experience with OpenGL.


Example #2: I was a beginner with OpenGL but not graphics programming or programming in general. So I knew to put in redundancy checks to prevent redundant state changes.
So if the texture ID in that slot is the same as the one being applied to that slot, early out and time saved.
Unfortunately in OpenGL this is not so simple as it is in Direct3D.
Since you have to bind textures both to create them and to use them, one day a bug appeared when an object was loaded in the middle of gameplay. Suddenly the wrong texture was on an object.
Of course it was a simply folly that was easy to fix, but it is a newbie trap for those just beginning in OpenGL who don’t know the full impact of every single function.


Example #3: And that makes multi-threading a real pain in OpenGL. To load two textures on different threads, you have to create thread-safe blocks (critical sections or mutexes) where you can be sure no other threads can call glBindTexture until you are done with the current texture. And that can be called during a render too so be sure to lock your critical section or mutex during the entire render too.
And glGetError is needed to tell if some functions fail and to get information about why they failed.
Have to create another critical section for that to make sure another thread does not clear the error while you are loading a texture. It goes on and on.
If you are beginning OpenGL, forget about multi-threaded resource creation.



Direct3D 9 has one major issue: Resource management. You will probably not be able to resize the window or toggle full-screen mode for a while if you are beginning Direct3D 9.
Direct3D 10 and Direct3D 11 are harder to grasp for a beginner, but people have been managing and they do not have this major issue, just as OpenGL does not.



Overall DirectX is less hassle-prone and easier to understand thanks to its object-oriented structure. Functions are more descriptive and terminology is more commonplace (see post by mhagain).
DirectX is also significantly faster than OpenGL on most Windows implementations (my new engine has equal logical overhead (same redundancy checks, same tick logic, same number of shader/texture swaps, etc.) but every scene in OpenGL is only 75% the speed of the same scene in Direct3D), but I doubt you will be able to use that difference as a beginner.


L. Spiro
I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid
21st Century Moose
21st Century Moose
My own opinion of the war between the two is that up to version 1.4, when compared to the equivalent D3D level, OpenGL was clearly superior. If that's all the functionality you need, and if you're happy to stick with immediate mode, then it's a no-brainer. You'll probably find OpenGL easier.

The vertex arrays API in OpenGL (the example I gave above) is a mess. If you're using vertex arrays then - when compared to a D3D8 or 9 level (the last versions that let you draw from system memory pointers) - D3D is easier and cleaner. In this case 3 of the function calls I gave above can be collapsed into one.

OpenGL 1.5 was where things got interesting. OpenGL's original implementation of VBOs was - to put it mildly - a crock. Yes, there was some sense in piggy-backing it on top of the vertex array API, but there are a whole heap of problems that arise from that and now we have the gl*Pointer calls that behave in two quite different manners depending on whether or not a VBO is bound. The lack of clear and unambiguous VBO data access semantics is not nice. As for which function to use: glMapBuffer? glBufferData? glBufferSubData? glMapBufferRange? WTF!

Of course that's improved with subsequent extensions, but extensions are both one of OpenGL's greatest strengths and one of the factors that can make you pull your hair out screaming. Want to run on as wide a range of hardware as possible? Prepare to enter the extension tango with multiple rendering backends. OpenGL 2.0 or higher shaders? GL_ARB_shader_objects? GL_ARB_vertex_program? These are the problems you have to deal with when you move on to making programs that people other than you will use. D3D by comparison has one clear API for each version. If you want to code a D3D9 app all of the D3D9 API is available.

OpenGL drivers are a horrible mess. There are no conformance tests yet available for the newer versions, so driver vendors are more or less free to interpret the spec whatever way they want. Driver quality can be poor (hello Intel, hello AMD). You can still get unexpectedly punted back to software emulation. Vendor-specific workarounds are still occasionally needed. Check this note from the AutoDesk people: http://archicad-talk.graphisoft.com/download.php?id=13495&sid=f6cb976ac04f7441b369bf3e58819086

OpenGL's OS portability is better, yes, but it's graphics hardware portability is actually significantly worse.

Plus D3D is faster:
DirectX is also significantly faster than OpenGL on most Windows implementations


I'll second that. I've consistently found the same, and so has this guy: http://aras-p.info/blog/2007/09/23/is-opengl-really-faster-than-d3d9/
Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls. 

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.