OpenGL and Shaders › Graphics Resources

Graphics Resources

OpenGL and Shaders


OpenGL

If this is your first time reading computer graphics then terms like OpenGL and Shaders might be new to you. To explain it, I would use the following diagram:

Rendering Pipeline
Rendering Pipeline (Reference: Mozilla)

The above image is an image of the rendering pipeline. Rendering refers to the process of producing a 2D image from a 3D scene description. So, whenever we display any scene on our computer screen, the scene undergoes above steps in background before we see the final pixels on our screen. So, to control the different parts of this pipeline we use APIs (application programming interfaces) like OpenGL and to control what happens in the arrows (Refer above image), we use something called as “shaders”.

Officially, OpenGL is a family of cross-platform computer graphics APIs that are used to interact with the GPU for rendering. The main advantage of OpenGL is that it is cross-platform (runs on most Operating Systems like Linux, Windows, Mac OSX) and is implemented in most Nvidia and AMD GPUs. There are several versions of the API, and there are implementations, or “bindings” for several different programming languages like C++, Java, Python etc (For the full list refer to this link). Versions of OpenGL for embedded systems such as mobile phones are known as OpenGL ES and versions for use on Web pages are called WebGL. Most CS courses on computer graphics expect students to learn OpenGL or some variant of it as a part of the course.

Besides OpenGL, there are other APIs for rendering like DirectX and Vulkan. DirectX, developed by Microsoft is also supported by almost all Nvidia and AMD GPUs but it isn’t cross-platform and runs only on Windows OS. Vulkan is another API developed by Khronos Group (the same group that maintains OpenGL). It is more recent (the first version of Vulkan was released in 2016) and the main motivation behind Vulkan was to give programmers and game designers more low-level access to hardware/GPU to boost performance and efficiency. This of course comes at a cost of significant up-front work (i.e. more coding and programming effort) on the developer’s part. As per Khronos the Vulkan API, which will complement (and in some cases replace) OpenGL and OpenGL ES.

The main advantage of using these APIs is that a user gets to control almost each and every part of the rendering pipeline and perform necessary optimizations. In the long run, it is always useful to learn atleast one of these APIs (hence most CS courses on computer graphics expect students to learn OpenGL or some variant of it as a part of the course). But, a new user can always use other tools like three.js (a wrapper around WebGL granting high level access), Unity3D (game engine) etc to significantly reduce programming effort. For example to create a Facebook 3D post of rotating earth you literally require 0 lines of code if you use threejs:

In comparison to this, if you are using C++ version of OpenGL, just to create a rotating Earth, you will need to include a few libraries, link everything, place the camera, light source, sphere and assign texture. Though this might take a lot more time, it gives you more control.

Hence, in the subsequent subsections I list various resources for OpenGL. Since knowing the tools are also important, my next post is about these tools.

Learning OpenGL

Books

In my first post, I had listed some books for learning computer graphics. Usually, some of those books also cover OpenGL. Realtime Rendering has an awesome and updated graphics page with most recent books on Computer Graphics and programming. I would recommend going to their page if you are looking to buy a book on some part of graphics.

One of the most popular books on OpenGL is OpenGL SuperBible.

Videos

Websites

Some of the websites with tutorials on OpenGL (Reference: Awesome page of OpenGL)

OpenGL Libraries

Knowing what libraries to use is also important. It can help save the time spent exploring random libraries with limited support. Some of the widely used OpenGL libraries are (Reference: Awesome page of OpenGL):

OpenGL Profile Loaders

If you have been using OpenGL, you might have come across terms like OpenGL core or OpenGL extensions etc and it might be confusing when to use say GLEW or say gl3w and when not to use. The answer depends on your Operating System. Core OpenGL functions refer to features which were introduces as a part of the OpenGL specification i.e. (a specific version of OpenGL) whereas extensions are functions that provide extended functionality which the core of OpenGL does not provide.

So, OpenGL functions (core or extension) must be loaded at runtime, dynamically, whenever the function in question is not part of the platform’s original OpenGL ABI (application binary interface):

So, this leads to the following rules:

The above comments were part of a stackoverflow post. For more details please refer it.

A list of profile loaders are (Reference: Awesome page of OpenGL):

OpenGL Documentation

For documentation and API information, the place to go to is the official page on OpenGL by Khronos Group. It contains various useful links like cheat sheets, wiki etc.


Shaders

As I had explained in the first section shader helps you control what happens in the arrows. By definition, a shader is a user-defined program designed to run on some stage of a graphics processor. Its purpose is to execute one of the programmable stages of the rendering pipeline. The word “Shader” comes from the word “Shading” which is a technique for producing appropriate levels of color, light etc in an image.

OpenGL shaders are written in OpenGL Shading Langugage. There are also other shading languages which are used by different tools like CG, Metal, DirectX etc. But, the difference is usually just a change in syntax. The general concepts of shader programming is the same across shaders. So, if you know how to program in say GLSL, it would be easy to convert it to a CG shader.

In OpenGL shading language there are 5 types of shaders:

Compute Shaders are a bit different in the sense that they are programs that run on the graphics card outside of the normal rendering pipeline. They can be used for massively parallel GPGPU algorithms, or to accelerate parts of game rendering. Usually, for starters it is best to learn vertex and fragment shaders.

There is a myth among programmers that shaders are painful to write. This is both true and false. Usually, what makes shaders painful is the fact shaders are difficult to debug and shader programming is different from conventional programming. It is creative and parallel, but once you get a hang of it, it can be a lot of fun and it is cool to see the actual effects of your program. What I mean is that, though it can be tedious, it can be really satisfying to see the final effects. For example:

The two examples above are fragment shaders from shadertoy (more details in the next subsection). It may be possible that when you pressed the run button, the page may have lagged or got stuck. This is because these shaders try to run on your web browser by utilizing your PC’s graphic cards. So, if you don’t have GPU and have a weak integrated graphics card, the browser might have crashed.

Learning Shader Programming

Books

Like I had mentioned before, realtime rendering’s book page is really good and contains a nice collection of computer graphics related to books. So, if you are looking to learn a specific shader language, you should look up corresponding book on that page.

Websites and Tutorials

If you would like to learn fragment shaders, there are a lot of websites with tutorials on it like The Book on Shaders. The really cool part of a fragment shader is that, with just one quad (rectangle), you can do really cool stuff.

If you are new to shaders, I would highly recommend going through the following video by Pol Jeremias:

I had attended a similar session at Siggraph Asia 2017. It is a really nice tutorial on fragment shaders and assumes that you have zero background knowledge.


comments powered by Disqus