[C++]Frontface culling/Render contours for toon shading etc.

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Ico
Posts: 289
Joined: Tue Aug 22, 2006 11:35 pm

[C++]Frontface culling/Render contours for toon shading etc.

Post by Ico »

I've seen more than one request for cell shading hints etc. Most end with someone suggesting to render the model in black with an increased scale. I wasn't happy with that until I found some nice step by step guides on how to do it "better". (It just looks really bad when your model has tons of polygons or "inside holes".)

Image

The trick is to cull front faces and draw the back faces using wireframe mode with lines thicker than 1. You'll end with the default render but with additional black (or colored lines) where front and back faces are connected with each other. The default irrlicht source allows you to cull backfaces or don't do it - I haven't seen any way to cull front faces (besides inverting normals). Did I tell you, I hate vector math? :P
If there is a different approach to do it, let me know!

The zip file includes a diff/patch file to apply to the Irrlicht source (revision 863) to allow front face culling. I didn't touch the software drivers for this - nor the DX8 source. Would be nice if someone with more DX/OGL knowledge could check my modifications - just grabbed them from 5 minute google sessions and they seem to work.

Materials get a new attribute - FrontfaceCulling. It works the same way as BackfaceCulling but it has the higher priority.
OpenGL: If you set both to true (nothing should be drawn), BackfaceCulling is ignored.
Direct3D: If you set both, everything should be culled (untested).
This is mainly because I wasn't sure what would be better and I did the Direct3D one first.

The zip file also includes a text file with a simple function to draw "back face wire frames" ( RenderBFWF() ) of specific scene nodes.

To get contours for a toon shader, draw your SceneNode as you always do and then call the RenderBFWF() function. The parameters allow you to modify the line thickness and their color.

Important: Line thickness won't work in DirectX mode. As far as I know that's a known Irrlicht limitation at the moment. Slap me, if I'm wrong. :)

For those looking for a toon shader: This isn't a complete implementation of a "toon shader". You still have to do the lighting/color manipulation. or just use smooth textures or whatever you want.

----------

If you want to modify or use my code or even print it out to test your new document shredder ... do whatever you want, but I'd like to know if it becomes anything really useful.

Download: FileFront.com
fmx

Post by fmx »

I've done cel-shaing before, and this is exactly the method I used - setting the Wireframe to a thicker black.
but it wasn't with Irrlicht though, just using OpenGL.

Looks great though.

Thanks for posting this up Ico!
:D


EDIT:
I can add an extra bit of advice:
If I remember correctly, I had some problems (with my original OpenGL version) where objects/characters that were far away from the camera still had lines as thick as the objects/characters who were closer to the camera
The scene looked very 'strange' IMO.

So what I did was, I set the thickness of the background Wireframe line to become thinner as it becomes further from the camera.
Its a subtle change, but the results may be more preferable.
koller202
Posts: 143
Joined: Tue May 08, 2007 4:53 am
Location: Thailand

Post by koller202 »

compile in 1.3.1
error in

Code: Select all

lmat.FrontfaceCulling=true;
and how to fix sourcecode use frontfaceculling.diff i don t know how to patch it

and result if i comment

Image


thank you ico
________
Entourage Dicussion
Last edited by koller202 on Thu Feb 17, 2011 1:16 am, edited 1 time in total.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

For those interested about the actual shading, here is a cel shader for HLSL and GLSL

Good luck.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Ico
Posts: 289
Joined: Tue Aug 22, 2006 11:35 pm

Post by Ico »

koller202 wrote:compile in 1.3.1
error in

Code: Select all

lmat.FrontfaceCulling=true;
and how to fix sourcecode use frontfaceculling.diff i don t know how to patch it
Under Linux the program to apply the patch is simply "patch" iirc (I'm only once a year under Linux or so ;) ). For Windows there are similar alternatives but nothing built in. I use "Tortoise SVN" for such tasks (context menu "apply patch") but you'll need a local SVN copy of Irrlicht for this to work.

You could also open the file in a text editor like Notepad and try to read the instructions. It's not that hard to figure out.
koller202
Posts: 143
Joined: Tue May 08, 2007 4:53 am
Location: Thailand

Post by koller202 »

thx ico
________
Fake Weed
Post Reply