Hey guys, first of all awesome new look for the website... very trendy!
My projects been dead for a while yet again lol... But I've restarted it for the last time in the form of a graphics engine instead of a game engine that I'll work on bit by bit through the holidays and through uni while trying to do a video at least every month or so. The project name for now is GroundUpEngine reborn, and I'm using the OpenGL SuperBible 4th Edition to refresh my memory so I can write the engine better than ever.
Anywhore... I have a problem with alpha blending trees to be specific, in the screenshot below you can see that in Blender the modelling software I'm using renders the tree and its leafs fine but in the engine their depth appears incorrect relative to the camera.
I've tried and failed so far:
Disabling the depth buffer when drawing the tree
Modelling the tree with less geometry
Last edited by GroundUpEngine on Fri Jul 13, 2012 4:48 pm, edited 1 time in total.
You need to draw solid objects before blended objects. Think of rendering as real painting. You draw something solid, then draw a "thin" object on top of it.
The tree branch and leafs are now separate models, and the leafs are drawn in front of the tree branch..
depth testing disabled:
depth testing enabled, hmm I thought disabling depth testing helped alpha blending... it seems the branch vertices are causing the depth buffer to freak out:
Generally if you don't have many other passes that need to happen you'll separate opaque geometry from transparent geometry.
1) (Optional)Render opaque geometry w/ color writes disabled (z-write enabled, z-test enabled)
2) Render your opaque geometry front to back (improves fast z-reject) w/ z-test enabled, (z-write disabled if you did step 1)
3) Do whatever other rendering things you might need to do
4) Set your blend state, in this case your blend functions look pretty typical.
5) Render transparent geometry back to front (z-test should be on...opaque geometry can occlude transparent geom. However, z-writes should be off)
What is likely happening is that you have to realize you fully control the process here. You CAN disable and enable both z-reads and z-writes independently. If you have z-test enabled, you also have z-write enabled (by default) so that as you render transparent faces you're testing against the z-buffer and writing to it if they pass. This is not correct for transparent faces. You simply want the test for opaque faces.
I'm so rusty with OpenGL it's terrible, thanks for the advice I understand the depth buffer a bit better now. A better output was produced by these changes but the tree trunk is still sticking out through the leaves, I have two separate OBJ meshes rendered in this order: tree trunk -> leaves.
p.s. My instinct now is be hacky and shorten the tree trunk so it rests just underneath the leaves, but these days I want to better myself and make clean operational applications
Last edited by GroundUpEngine on Fri Jul 13, 2012 4:40 pm, edited 3 times in total.
superLED wrote:I can't help, but I have a little question: What are you using to load in the models?
No prob, I used my own simple OBJ mesh parser. I can give you a rough idea of the code being used but there's a few things you need to know first though:
C/C++ basics e.g. loops, STL strings, STL vectors, file IO, etc.
OpenGL basics (OpenGL 1 in this case) e.g. building gemometry, drawing with gemometric primitives, etc.
Below is an example of a simple triangle with no shading or textures, first the modelling software (blender in this case) would export the OBJ mesh to a file e.g. 'triangle.obj':
bbguimaraes wrote:You need to draw solid objects before blended objects. Think of rendering as real painting. You draw something solid, then draw a "thin" object on top of it.
This, pretty much. In fact there's something call the Painter's Algorithm that solves this exact problem. The API's own depth sort works well enough without transparent objects, but adding transparent objects in your scene can drive you mad :P
The tree trunk is showing through the leaves because it's actually correct with the depth sort, now it's time to make a better looking tree Make the "leaves" part of the OBJ branches, actually, even if they're just as a texture. Draw a transparent texture with branches and paste tiny versions of those leaves over it, and the tree will already look a lot better.
bbguimaraes wrote:You need to draw solid objects before blended objects. Think of rendering as real painting. You draw something solid, then draw a "thin" object on top of it.
This, pretty much. In fact there's something call the Painter's Algorithm that solves this exact problem. The API's own depth sort works well enough without transparent objects, but adding transparent objects in your scene can drive you mad :P
The tree trunk is showing through the leaves because it's actually correct with the depth sort, now it's time to make a better looking tree Make the "leaves" part of the OBJ branches, actually, even if they're just as a texture. Draw a transparent texture with branches and paste tiny versions of those leaves over it, and the tree will already look a lot better.
That solves the problem indeed, but then I figured to optimize the model and since you can't see the branches inside the leafs (even when the branches are part of the leafs model), so I took the geometry out all together giving the same affect! I'll keep that idea of a better tree model in mind though