Hello stranger, welcome to yet another OpenGL4 tutorial of mine! In this one, we will do a follow-up to the previous tutorial and we will extend heightmap by loading it from the image file instead of random terrain, so that you have full control over your terrain! Moreover, we will add another layer of realism to our scene by implementing skybox - a textured box around the world, that depicts skies and far-reaching surroundings! This one won't be very long to be honest, so read carefully through and you will learn something new today .
In this tutorial, we are extending existing Heightmap class with a new static function called getHeightDataFromImage and a new constructor, that takes a filename and loads height data from image:
Let's examine that new functions getHeightDataFromImage. Let's have a look at the code first:
As you can see, it's pretty straightforward actually! All we have to do is to get the pointer to the raw data. If we can't load the image for whichever reason, we just return empty vector. Otherwise we simply iterate over all pixel using the raw pointer to data and we convert whichever byte value there is to float. Even if the image is saved as RGB or RGBA, I make assumption that this heightmap is really a grayscale image, thus it doesn't matter which color component I take out of it (grayscale has RGB components same), so I just take the first one (what pointer points to). At the end of the loop, the pointer is increased by number of bytes per pixel, which means we're jumping to the next pixel .
This function is then used in constructor to construct heightmap from a given filename as follows:
Relatively simple stuff - we just check if the vector has some data in it and then we proceed with load the same way, as we did with randomly generated data . Now let's discuss completely other thing - skybox.
For those of you that don't know what a skybox is - it's nothing but a big box that is around our camera, moves with us everywhere and has some textures of surroundings (skies, mountains, but also whatever alien worlds you might imagine ) mapped onto it. Actually, it does not even have to be big, just have some sufficient size (will explain later ). These textures are seamless, so you don't see any box edges or anything and the whole thing will look as one continuous sky. Let's have a look at single skybox, so that you have an idea what it looks like:
As you can see, skybox is just a textured cube and when we unfold its faces, we will get such a shape, as if we wanted to cut out a cube from the paper and then fold it. This idea is very simple, yet very powerful. We will create a separate class for skybox (static 3D mesh) to have everything we need in one place:
You might see that this class is inherited from Cube class, because skybox is really a box, a cube. I can just shortly explain what do these functions do. render of course renders the skybox with specific shader program centered around specific position (usually camera position). getSideFileName returns name of file for concrete side (front, back, left, right, top or bottom) and getTextureKey returns a key to store texture with for every side.
tryLoadTexture tries to load a texture for a given side. Why does it only try? Because some skyboxes you find around internet are sometimes missing some sides, usually bottom one, because this one is usually not visible (it's under terrain and you can't see it.). Unless there is a bug in game, like last time I played Doom (2016) and I just suddenly fell under the ground and died (just happened only once though). So it actually looks if there is a file with such name and if yes, it loads a texture. This way, we try to load all 6 sides . Let's now examine render function, this one requires some tricks.
All we have to do is to render all 6 sides separately, with different textures. But there are two most important things to notice. First one is a sampler that we use. I use trilinear filtering here, but important thing is that sampler clamps the texture to the edge! This is actually done in constructor of Skybox class, you can have a look at it, there is a method called setRepeat(false). Without using such a sampler, we would see the edges of the box:
With this trick, the sky is seamless, you don't notice any edges at all !
Second thing is to turn writing to depth mask off. Again, it's a very similar thing as with transparent objects. Basically we want the skybox to be our background and that's what we achieve by turning writing to depth buffer off! The skybox is rendered, but it's considered to be far away. Again, without using this, your skybox would block other renderings. Depending on its size, you would see as much world as skybox around you allows you to. I recommend to try it yourself too . I chose size 100.0f, because that is more than enough. Interestingly, any reasonable size works (even just 2.0f!), because important thing here is that box is far enough from the near clipping plane and not beyond far clipping plane. Again - try to play with this size value, set it to 1, 2, even 2000 and observe the results for yourself . But one hundred is like a reasonable value for me.
At the end of rendering, don't forget to turn depth mask on again, so that objects rendered later are normally depth compared . The way skybox is rendered is in renderScene and there is really nothing special - we render it around camera, so that skybox travels around us always. Moreover, we omit diffuse lightning calculations for skybox completely (we set the diffuse light to one that has no contribution and no factor) and only ambient gets applied. And that's all I have to say, it's really nothing less nothing more, pretty simple!
This is what has been achieved today...
... and I think it's pretty nice . Not only we have control over how our terrain looks now, but we also can render surroundings - skies and mountains with minimal effort! As always, I hope you have enjoyed this tutorial yet again and I hope you will also read my other tutorials!Download 10.19 MB (188 downloads)