Recall back to when we first implemented three dimensional graphics in our game engine in week 9's post. One of the challenges we hypothesized was that of depth. As in, who is in front of whom? Well, we're fortunate that for those opaque meshes, the GPU is in charge of who is in front of what based on their world position and just overwrites that pixel with the pixel that is in the front. But what about transparent objects?
Because an object is transparent, we must know what is behind it. This order is very important, and we'll keep in mind the logic of drawing from front to back. Or perhaps more simply, we must first "draw" the pixels that are behind the transparent objects before we draw the transparent objects with their effect. This is crucial as the opaque object's must be on screen in order for the alpha blending to occur properly within the transparent meshes.
Previously in both our application and rendering threads, we had a vector that held the Meshes that would be rendered on screen. But how do we impose order within these structures? We'll have to sort them based on their Z position. We could have approached this by adding the transparent meshes to the same structures that held the rest of the meshes, but for simplicity, we'll just go ahead and make a separate structure that is sorted that only holds transparent meshes. We'll just make sure that these are sorted correctly based on their position's Z value, and that they are the ones drawn last.
Specifically, we do the following:
In the screenshots above you can see two cube meshes that use our transparent fragment shader, placed in front of one another to be able to see their transparencies with an opaque mesh in the background.
The Camera: [Key : Action]
[W : Move Forwards]
[A : Move to the Left]
[S : Move to the Right]
[D: Move Backwards]
[Space: Move UP]
[Left / Right Ctrl: Move DOWN]
The Brain: [Key : Action]
[Arrow UP : Move Forwards]
[Arrow LEFT: Move to the Left]
[Arrow RIGHT : Move to the Right]
[Arrow DOWN: Move Backwards]
[Page UP: Move UP]
[Page DOWN: Move DOWN]
You can try out these games via the below links. The only difference is that Direct3D will be used in the x64 version, with OpenGL in the other. They have been built and verified to work on Windows.