Exploring the lighter side of graphics

In this part we will be doing a bit of simple geometry instancing with a minecraft twist, basically what we are going to do is first generate a volumetric landscape of sorts, then render it using cubes, but not the usual way, we are going to do it as a particle system, which is i guess a little bit more than usual as it involves other stuff than strictly openGL, and since there are a few things i want to try out in this field you will be seeing more of it.
This tutorial assumes that you have all the stuff needed for part 1 and in addition texturing available.
So first we need our volumetric function
[stextbox id=”grey”]

[/stextbox]
The way this works is simple, first it uses the noise3 function, which is just plain and simple perlin noise which you can find just by googling “perlin noise”, here i combine several octaves to create a more interesting landscape, In the game Minecraft they use interpolation as well to achieve smoother hills and flatter areas along with a host of other interesting/secret methods to make it nicer, though the basic principle still applies.
The line tmp=-((y-2)/2); produces a density gradient that makes it less dense from y=2 and up and more dense under it, or simply put it creates land instead of floating islands.
Finally the function returns if there is land at a the coordinates or not.

Next up is a function that hides unseen cubes, essentially it just checks if there are cubes all around it, if so it deletes it, it’s a way for us to render less with the same visual result.
[stextbox id=”grey”]

[/stextbox]
the final function we need to add is just a function that scans trough a block of cubes and then feeds the resulting particle array in a VBO, you can download and check the code on exactly how this is done but it’s just a bunch of loops basically.

Next we need to move on to the shaders, i wont show either the fragment or vertex shader as their simple enough, and really the only addition is texturing for the fragment shader so i am only going to show the geometry shader, and really it’s the one you came from
[stextbox id=”grey”]

[/stextbox]
the first bit defines all the inputs and outputs, nothing to exiting but note how it’s easy it is to define your geometry inputs and outputs, here we only use tri-strips and frankly it’s really the only thing you would need as you can make quads triangles and strips with it at the same time, interestingly enough it’s also the only thing you can make along with points and line-strips, so if you need to output individual triangles, quads or lines then just call EndPrimitive for each segment and it just works.
[stextbox id=”grey”]

[/stextbox]
this is the first face of six, again download and check the code to see the whole thing, because we are doing some texturing we need to transfer texture coordinates, we also need to do the matrix transformation here instead of the vertex shader as before, the way all this works is that all out variables defined previously gets the last values set for it feed to the rasteriser, not totally unlike the now deprecated glBegin/glEnd paradigm.
The result is this if all is done correctly, now remember all these cubes are just particles, but if you’re really clever you could use this method to do simple instancing of simple debris, dust, leaves by just rendering a simple particle system and using the geometry shader to enhance that.
Koch curves and other fun stuff are next.