Cartoon shading


I have a trouble with my cartoon-style shading algorithm…I just draw the object as it is and then I add a black contour line with the following steps:


…the problem is: IF the object is transparent I will see all the the wireframe lines of the back polygons, and not only the contour lines…it is obviuos, but how can I avoid this in an efficient way?

draw your transparent object with stencil writes enabled - no testing, just replacing when depthtest ist passed, and then draw your outlines with stencil test enabled.

for more details about stencil, just take a look at the tutorials & faqs on

(btw. you can accomplish the same effect with alphatesting)

That’s the right solution!

Just to know…I haven’t understood how you would solve this problem with ALPHA_TEST…immagine you have to render in cartoon-style a transparent cube:
1- fist, you draw the transparent cube.
2- you enable front-face-culling
3- you set glPolygonMode(GL_BACK, GL_LINE)
4- …and now ??? …

if you enable alhpa-testing you will discard everything!..

He didn’t mention Alpha Testing, he did mention Stencil Testing.

Though, I would rather recommend simple Depth Testing if you can.
But if your transparent object does not write the depth buffer, well… use the stencil buffer

i meant alphaBLENDING(you can use the alphabuffer as a mask as well). but this would only work with opaque objects. so it’s not a solution for your problem…