Comparison of OpenGL and Direct3D
In this article I tried to expose the basic facts that should be known to both developers and end users.
Since the topic is very holistic, I tried to keep the tone as neutral as possible.
Bird's-eye view
Both APIs provide access to 3D graphics hardware acceleration features.
Direct3D is a proprietary development of Microsoft, created specifically for Windows. Currently used on Microsoft Xbox as well. Unavailable on other platforms (if you do not take into account the emulation of the API provided by Wine, as well as virtualization).
OpenGL is an open standard developed by the non-profit organization Khronos Group with the participation of the community. All major GPU manufacturers (nVidia, AMD, Intel), one way or another, influenced OpenGL. Unlike Direct3D, it is available on a very large number of platforms. In particular, OpenGL is the main API for interacting with the GPU on Linux and Mac OS.
API "External" Technical Differences
Direct3D is based on COM technology . COM is essentially the standard for binary representation of components. As you know, classes in pure C ++ cannot be used from other programming languages, since they do not have a standardized binary representation. In particular, each compiler uses its own method of decorating names . COM, however, allows you to work with an object-oriented concept from any language that supports it. COM is also a Windows-specific technology (it uses such Windows-specific things as the registry).
An application on Direct3D uses pointers to object interfaces. Work with an object is carried out by calling methods of its interface. For example, the interface of the so-called device (a device in Direct3D is the execution context for a specific window) has the name (examples for Direct3D 9) IDirect3DDevice9, for a texture object - IDirect3DTexture9, etc. The creation of objects occurs as calls to the methods of the IDirect3DDevice9 interface, for example, for texture it will be IDirect3DDevice9 :: CreateTexture.
There is a significant amount of change in Direct3D 10. Direct3D 10 is not backward compatible with Direct3D 9. I.e. to transfer the program to the new API, you will need to rewrite all the code related to rendering . More about Direct3D 10 below.
OpenGL uses the usual functions of the C language. There is a standardized ABI for them , which means that OpenGL can be used from any language that supports calling native library functions (i.e., practically speaking, from any generally).
OpenGL uses the so-called state machine (state machine). The result of calls to OpenGL functions depends on the internal state, and can change it. In OpenGL, to access a specific object (for example, a texture), you must first select it as the current function glBindTexture, and then you can already influence the object, for example, setting the contents of the texture is done by calling glTexImage2D.
The analog of the device concept in Direct3D is the context here. The OpenGL context is tied to a specific window, just like the device in Direct3D.
A common feature of the two APIs is that both do not provide anything outside of graphics. Namely, there are no functions for creating a window, for working with keyboard / mouse input, or for working with sound (here I do not touch on other parts of DirectX, such as DirectInput and DirectSound). Those. they are not high-level libraries.
In the most simplified form, we can say this: OpenGL and Direct3D allow you to draw triangles. And that’s it. The bottom line is that triangles can be drawn in very different ways (textures, lighting, transforms, etc.).
The most important difference
His name is extensions.
Direct3D is essentially fixed within the same major version. Any changes / additions occur only with the release of the next version.
In OpenGL, a really accessible API is defined by the GPU manufacturer. An OpenGL implementation allows you to define extensions to the core specification. An application can get a list of supported extensions at runtime, and check for the availability of those that it wants to use.
In fact, almost all the functionality of OpenGL is an extension. The development of OpenGL goes like this: a new feature appears, the manufacturer implements it in its driver and documents the available extension. Applications can use the new features right now, without waiting for inclusion in the official specification. If this extension is specific to a specific manufacturer, then in the name it carries its name (for example, like this: GL_NV_point_sprite, where NV means nVidia). If the extension is implemented by many vendors, then the name uses EXT (for example, GL_EXT_draw_range_elements).
Over time, if the extension is widely used, it is standardized in the ARB , and after that it contains the ARB in the name (for example, GL_ARB_vertex_buffer_object). Such an extension has an official status.
The most important extensions over time become part of the core specification. Each new version of OpenGL is essentially an old version + several new integrated extensions. However, new features continue to be available as extensions. Those. in fact, from the point of view of the program, it may not even matter what version of OpenGL. The main thing is what extensions are available. The OpenGL version is just a way to indicate which set of extensions is guaranteed to be supported.
What's New in Direct3D 10/11 and OpenGL 3.x
Microsoft has made a radical overhaul of the API in Direct3D 10. Now it has a more unified and modern look. Some obsolete things were thrown away, such as a fixed function rendering (without using shaders). Another transition was made to the new model of the driver. In particular, the Direct3D implementation can now have not only the kernel-space part, but also user-space. This saves time on switching user-space / kernel-space. However, due to the new driver model, Direct3D 10 and higher is not available on Windows XP. Given the still great popularity of Windows XP, this is pretty sad.
The OpenGL implementation was originally divided into user-space and kernel-space parts, so there was no such problem. Another difference is that so far no changes have been made to the OpenGL API that would not be backward compatible. Each innovation is an extension.
The functionality that appeared in Direct3D 10, for example, geometric shaders, is available in OpenGL on any platform through an extension, or, starting with OpenGL 3.2, as part of the main specification. It is worth emphasizing, this is important, the Direct3D 10/11 functionality is available in OpenGL on any platform, including Windows XP. Thus, many have the impression that Direct3D 10 is not available on Windows XP solely for political reasons, and not because of any real technical problems. However, I can not judge here, while maintaining a neutral tone, about whether there really were such problems when introducing a new model of video drivers.
Now about the innovations in OpenGL 3.x. Starting with OpenGL 3.0, the so-called deprecation model has appeared. Part of the old functionality related to fixed function rendering, as well as rendering based on glBegin / glEnd, and many other obsolete and irrelevant things, were declared deprecated, and were subsequently removed from the main OpenGL 3.1 specification. This allows you to keep the main specification in an up-to-date and modern form.
It would seem that this should break compatibility with older programs. Indeed, before, when the program created the OpenGL context, it simply got the version context as accessible as possible. It was OK, because new versions have always been add-ons over previous ones. To avoid compatibility issues, programs that want to get OpenGL 3.x context should use the new context creation method, which allows you to specify which version of OpenGL to get.
But, as follows from what has already been written earlier about extensions, and this is important, the functionality of OpenGL 3.x can be obtained through extensions without creating a context using the new method. Those. OpenGL 1.1 + extensions = OpenGL 3.2 ! Thus, full backward compatibility is maintained. For example, geometric shaders are an extensionGL_ARB_geometry_shader4 .
Common misconceptions
OpenGL lags behind Direct3D, and in general, judging by such sluggish changes in the specification, it’s probably completely dead.
Actually, the reason for this error is the ignorance about extensions. Generally speaking, OpenGL can and often is ahead of (!) Direct3D in terms of innovation, because a manufacturer can add an extension to OpenGL without waiting for anyone, while only Microsoft can make changes to Direct3D.
OpenGL is for professional graphics programs, and Direct3D is for games.
This fallacy has a historical reason. OpenGL was originally developed as a 3D graphics library that MAY, but DO NOT, MUST accelerate hardware. It also explains the presence of some features, such as rendering stereo images that games don't need. Direct3D was developed much later, immediately with the expectation of acceleration on the GPU. At the time of the appearance of many professional graphics packages, Direct3D simply did not exist.
Interesting nuances
Microsoft ships with Windows drivers without OpenGL support. OpenGL will render without acceleration, or emulated through DirectX. So, if you need OpenGL support for Windows, you need to install the driver from the manufacturer’s website. The reasons for this rejection of OpenGL are most likely purely political again.
So what to do, how to live?
Note: But this part is very subjective.
If you are a developer and decide which API to use, then consider the following:
OpenGL is a massive cross-platform platform, in particular, the availability of all new features on Windows XP, where Direct3D 10/11 does not exist and never will be.
Against OpenGL - drivers in Windows out of the box do not have OpenGL support, so you need to install them from the manufacturer’s website.
If you are a beginner in the development of 3D applications and would like to master this area, I would recommend doing this: first deal with Direct3D (the reason is simple - Microsoft provides a very high-quality SDK), and then deal with OpenGL (it will be very simple after Direct3D). Unfortunately, there is no such thing as an SDK for OpenGL. Therefore, mastering from scratch will be more difficult.
That's all. Good luck in the development!