Merry Christmas and I welcome you to my 9th tutorial of my OpenGL4 Tutorial Series! This one is a simpler one, we will only learn about how to render stuff in 2D using orthographic projection matrix and how to render 2D elements over the scene nicely (just like you have HUDs in games showing your hitpoints, ammo etc...). We will really briefly touch blending, although there will be a separate tutorial for it, but we can improve the quality of our 2D rendered elements using it. The last thing we will learn here is, what is fragment discard. Off to learning!
In orthographic projection, you simply take your object and you project it onto a drawing surface from one view (let's say from front) using lines that are perpendicular to the drawing surface. If you don't understand, let's just take a simple picture:
Can you see this now? We look from one direction and draw lines from every point of the object to the rendering surface and that's it! In our case, rendering surface is our 2D screen and we're looking at the objects from front. Moreover, orthographic projection will be even simpler, because we won't even project 3D objects, but only 2D objects. Our projection matrix will correspond to our window, so that if we render something at [100, 100], it will indeed appear on position [100px, 100px] in the window coordinates!
In class OpenGLWindow, I have created new variable called _orthoMatrix, which we recalculate everytime the window is resized. In order to calculate orthographic projection matrix, we will simply use glm function and provided window dimensions:
And that's it! We now have orthographic projection matrix, which we can use for 2D rendering! In this tutorial, I have decided to render Christmas tree in the bottom-left corner of the window and snowflake in the bottom-right corner of the window. This will be our HUD ! In order to render it, we will have to prepare some things though. Let's go through the initialization code:
As you can see, there is a new shader program especially for rendering 2D objects called ortho2DProgram. Furthermore I have created a separate sampler for 2D objects. The reason is, that we don't need mipmapping here, because for now I plan to display 2D object as big as it was originally. Even if I had to resize it, then there might be some performance penalty, but I really think that amount of rendered 2D stuff is neglibible in comparison to 3D stuff, so in the end, it doesn't even matter (hope you understood that Linkin Park reference ). And I have also created new static geometry consisting of glm::vec2 vertices, anchored in the lower-left vertex:
One question might arise in your head though - why not center this, just as we did by the 3D cube? The reason is simple - it's easier to position objects in game HUD using lower left point rather than with center point. Another advantage now is, that these points are exactly same as texture coordinates, that we will use.
As mentioned, there is a dedicated shader program for rendering 2D objects, consisting of vertex and fragment shader. The following code snippet represents vertex shader:
It is actually very similar to the 3D rendering, one significant difference is, that we receive 2D vertices as position instead of 3D vertices. This means, that if we perform matrix multiplication, we have to extend our vec2 to vec4 using 0.0 for Z coordinate and 1.0 for W coordinate. We won't do anything with texture coordinate, just pass it further to the fragment shader .
Fragment shader also deserves some explanation:
There is not that much stuff going on, we just take the texel from texture and in the end, we output texel multiplied with uniform color. But there is one new thing we will learn today - fragment discarding.
Because our christmas tree and snowflake textures are PNG images with transparency, we can use this information to simply not render invisible points! It's those points, that have alpha component set to zero. So if there comes a pixel to fragment shader containing zero alpha, we will simply discard it, that means, it won't be rendered at all, like if it has never been there in the first place! This is super cool, because we can create 2D transparent objects this way .
Let's have a look at the rendering code in the renderScene() function:
There are several things that we have to go through. First of all, we start rendering 2D stuff by disabling depth test. When depth test is disabled, we don't care about what is in the depth buffer, we will simply render always, so we overwrite what's on the screen already. Then, there is this new thing blending enabled, if the corresponding boolean variable is enabled. This is something brand new - blending is mixing of colors. In OpenGL, one usually mixes what is on the screen already with what we are about to render. At the moment, we don't need to understand blending, it will be covered in another tutorial in detail (or have a look at my older Blending tutorial for now). But by pressing F4 key, you can observe the visual quality of 2D elements and see the difference for yourself with blending enabled / disabled . And the last thing we should set up is turning depth mask on / off. If it is disabled with glDepthMask(0), we are saying, that we don't even want to write to depth buffer (why would we - 2D things are last things to render so we absolutely don't care about changing depth buffer now).
After that, we will simply render two textures - Christmas tree texture and snowflake texture. The only important thing here to mention is the scale matrix - in order to render those images (textures) with their original size, we have to scale our quad of size 1 to the desired size and that is the original pixel image size. Moreover, to render snowflake in the bottom-right corner, we need to make a translation too. I translate it to the very right of the window and the offset it back with minus christmasTree.getWidth(). This way it's exactly where we want it to be .
After we're done rendering, we just have to disable blending, in case it was enabled, re-enable depth test and re-enable writing to depth mask. With F4 key, you can turn this blending on and off and see the visual difference for yourself. As I have mentioned it will be explained in detail in later tutorials (or have a look at my older Blending tutorial for now). But long story short - instead of just displaying those edgy pixels, it mixes them with what already is on the screen, removing that jaggy edges. The fragment discarding is actually not necessary with blending enabled, but it's a very small optimization, because it removes invisible pixels, that would make no difference to the final render, thus saving some computational time .
This is what has been achieved today:
I think it is a really nice thing to know and now you should be able to render some nice gaming panels . But because it's Christmas time (my favorite time of the year), "Christmasy" scene was simply a must. I really hope you have enjoyed this rather short tutorial and see you in the next one! Until then, enjoy your Christmas holiday!Download 3.75 MB (549 downloads)