I have been designing a object outlining method for my home-grown game engine. I thought the stencil buffer would do it , but I have having problems with the results. I have been trying to make outlines that are always visible through all surfaces and do not join together. The method I designed is like many I have seen on the web , where you render to the stencil buffer writing 1's in buffer , then scale the object up and only render a outline colour to where the stencil is 0. That approach does not work on anything but a edge case. Maybe a cube with no other objects in the world. I need to select multiple overlapping objects at the same time with differing depths etc. ( real world example. )
I designed a way to do this , and it goes like this.
1. ) I render all the non-outlined stuff first.
2. ) Then I cycle through a linked list of Objects to draw with an outline , and then draw them scaled up in the outline colour Instead of writing a 1 , I use the GL_INCR ( increment ). Here is a picture of what that looks like with three scaled up squares that form intersections. It's a little hard to see , but where they intersect is a sum of all the increments. ( The three's ).
11111111111111111111111111111
11111111111111111111111111111
11111111111111111111111111111
111111111122222222222222211111111111111
111111111122222222222222211111111111111
111111111122222222222222211111111111111
111111111122223333333333322222222111111
111111111122223333333333322222222111111
111111111122223333333333322222222111111
111111111122223333333333222222222111111
111111111122223333333333322222222111111
111111111122223333333333322222222111111
111111111122223333333333222222222111111
111111111111112222222222222222222111111
1111111111111111111
1111111111111111111
1111111111111111111
1111111111111111111
1111111111111111111
1111111111111111111
Then I cycle again through the same objects , but with normal scale , and I subtract 1 ( GL_DECR ) instead , and what should result is this…
11111111111111111111111111111
10000000000000000000000000001
10000000000000000000000000001
111111111121111111111111100000000000001
100000000010000000000000100000000000001
100000000010000000000000100000000000001
100000000010001111111111211111111000001
100000000010001000000000100000001000001
100000000010001000000000100000001000001
100000000010001000000000100000001000001
100000000010001000000000100000001000001
100000000011112111111111211111111111111
100000000000001000000000100000001
111111111111112111111111211111112
1000000000000000001
1000000000000000001
1000000000000000001
1000000000000000001
1000000000000000001
1111111111111111111
where all the outlines remain as non zero , I then want to use this mask to render the stencil outline to the screen with glStencilFunc( GL_GREATER , 0 , 0xFF ) ; Unfortunately , what actually happens , is that the renderer , OpenGL in my case , increments the bits , not only once , but for every fragment of the the same pixel location from objects behind it , other front facing polygons , etc. I have not been able to get around this behaviour , and am wondering if anyone knows how to stop all the other fragments from incrementing the same pixel location. If someone knows , then this is a far superior outlining algo than anything I have seen on the web.
Sorry about the second pic , I messed up typing in the 1 2 and 3's. Its a little different but should be good enuf to understand what is really going on there.