I would like to implement a shader that is capable of detecting edges and and make them black strokes like in cel shading. The thing is that not only the silhouette should be black, but also the edges that lie in the mesh. I want to achieve something like this.
Now I tried several approaches including Frei-Chen Edge detector but it seems as if this algorithm is a bit of an overkill.
I would be very thankful if somebody could give me some help with a bit of code on how to achieve this black edges effect.
Well, as far as I know now, Frei-Chen detector is made for 2d textures but not for 3d meshes. Getting the silhouette for rounded elements shouldn’t be too hard: You grab all vertices where the normal vector is perpendicular to the view vector. This would work for a sphere for example. But this wouldn’t work for meshes like cubes. Furthermore, I have no idea how to detect the “creases” of the cube.
Ah, I see. I think that you need two different approaches to render the silhouette and the edges of the object or at least I’m not aware of a solution which can handle both.
[QUOTE=Hermannicus;1282435]Ah, I see. I think that you need two different approaches to render the silhouette and the edges of the object or at least I’m not aware of a solution which can handle both.
For the edges within an object, I can think of two solutions (maybe there are others):
Render the normals of the object into a framebuffer first and then do edge detection (with Sobel or similiar) on this framebuffer.
Choose manually which edges you want to be drawn by creating a second index array with those edges and draw it with GL_LINES.
Hope this helps.[/QUOTE]
Thanks Hermannicus for your advice. I’ll try to implement the stencil buffer solution, although I fear that it will have some negative effect on the performance.
Concerning 1. on the edges within the object: I’m not sure how I can do that with the normals. 2. is not a possibility unfortunately since I want to load arbitrary meshes.
Your mesh is composed of triangles or quads, which are defined by vertices, and thus edges between those vertices. You could iterate over all polygons and deduce the lines to draw.
Thanks Spoops for your help! Your approach sounds good, but could you please explain in more detail how I could choose which edges I have to draw and which ones I have to skip? That would be awesome.