Very often there are various misconceptions about these two
APIs .
I tried to present in this article the basic facts that should be known both to developers and end users.
Since the topic is very holivar, I tried to stick to the most neutral tone.
A bird's eye view
Both APIs provide access to 3D hardware acceleration features.
')
Direct3D - proprietary development of Microsoft, created specifically for Windows. Currently used on the Microsoft Xbox as well. Not available on other platforms (unless you take into account the emulation 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), anyway, 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.
"External" technical differences API
Direct3D is based on
COM technology. COM is essentially a standard 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 also allows you to work with an object-oriented concept from any language that supports it. COM is also a Windows-specific technology (uses such Windows-specific things as the registry).
The application on Direct3D uses pointers to object interfaces. Working with an object is done by calling the methods of its interface. For example, the interface of the so-called device (device in Direct3D is the execution context for a particular window), has the name (examples for Direct3D 9) IDirect3DDevice9, for the texture object - IDirect3DTexture9, etc. Creating objects occurs as calls to methods of the IDirect3DDevice9 interface, for example, for a texture, it will be IDirect3DDevice9 :: CreateTexture.
There are a significant number of changes in Direct3D 10. Direct3D 10 is not backward compatible with Direct3D 9. That is, To transfer the program to the new API, you will need to rewrite all the code related to
rendering . Read more about Direct3D 10 below.
OpenGL uses the usual C functions. For them, there is a standardized
ABI , which means that OpenGL can be used from any language that supports calling functions of the native libraries (that is, practically speaking, from any at all).
OpenGL uses the so-called state machine (state machine). The result of OpenGL function calls 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 influence the object, for example, setting the contents of the texture by calling glTexImage2D.
The analogy 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 beyond the limits of working with graphics. Namely, there are no functions for creating a window, or for working with keyboard / mouse input, or for working with sound (here I don’t touch on other parts of DirectX, such as DirectInput and DirectSound). Those. they are not high level libraries.
In the most simplified form, you can say this: OpenGL and Direct3D allow you to draw triangles. And that's all. The bottom line is that the 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 a single major version. Any changes / additions occur only with the release of the next version.
In OpenGL, the real-world API is determined by the GPU manufacturer. The implementation of OpenGL allows you to define extensions to the main specification. An application can get a list of supported extensions at runtime, and check for accessibility 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 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 EXT is used in the name (for example, GL_EXT_draw_range_elements).
Over time, if an extension is widely used, it is standardized in
ARB , and then contains an ARB in the name (for example, GL_ARB_vertex_buffer_object). Such an extension has official status.
The most important extensions over time become part of the main specification. Each new version of OpenGL is essentially an old version + several new integrated extensions. At the same time, new features continue to be available as extensions. Those. in fact, from the point of view of the program, it may be all the same what version of OpenGL. The main thing - what extensions are available. An OpenGL version is simply 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 made a radical rework API in Direct3D 10. Now it has a more unified and modern look. Some obsolete things were thrown, such as fixed function rendering (without using shaders). Another transition was made to the new model of the driver. In particular, the implementation of Direct3D can now have not only the kernel-space part, but also the user-space. This saves time for 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 implementation of OpenGL was initially 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, which would not be backward compatible. Every 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 that this is important, the
functionality of Direct3D 10/11 is available in OpenGL on any platform, including Windows XP . Thus, many got the impression that Direct3D 10 is not available on Windows XP solely for political reasons, and not because of some real technical problems. However, I can not judge here, maintaining a neutral tone, about whether there were really 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 appeared. The 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 current and modern form.
It would seem that this should break the compatibility with old programs. After all, before, when the program created the OpenGL context, it simply received the context of the version as accessible as possible. It was OK, because new versions have always been add-ons over previous ones. To avoid incompatibility, programs that want to get the OpenGL 3.x context should use the new context creation method, which allows you to specify which version of OpenGL you need to get.
But, as follows from what has already been written about extensions before, and this is important, OpenGL 3.x functionality can be obtained through extensions without creating a context with a new method. Those.
OpenGL 1.1 + extensions = OpenGL 3.2 ! Thus, full backward compatibility is maintained. For example, geometric shaders are an extension of
GL_ARB_geometry_shader4 .
Common Misconceptions
OpenGL lags behind Direct3D, and in general, judging by such sluggish changes in the specification, probably already completely dead.Actually, the reason for such a delusion is ignorance about extensions. Generally speaking, OpenGL can and often
outstrips (!) Direct3D in terms of innovation, since the 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 misconception has a historical reason. OpenGL was originally developed as a 3D graphics library that CAN, but DO NOT have to be accelerated by hardware. It also explains the presence of some features, such as rendering stereo images that are not needed by games. Direct3D was developed much later, immediately with an acceleration calculation on the GPU. At the time of the advent of many professional graphics packages, Direct3D simply did not exist.
Interesting nuances
Microsoft ships Windows drivers with no OpenGL support. OpenGL will render without acceleration, or be emulated via DirectX. So, if you need support for OpenGL under Windows, you need to install the driver from the manufacturer’s website. The reasons for this rejection of OpenGL, most likely, are again purely political.
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:
Behind OpenGL - massive cross-platform, in particular, the availability of all new features on Windows XP, where Direct3D 10/11 is not, and never will be.
Against OpenGL - the 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 new to developing 3D applications, and you want to master this area, then 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 easy after Direct3D). Unfortunately, there is no such thing as an SDK for OpenGL. Therefore, it will be more difficult to master from scratch.
That's all. Successes in the development!