Edge detection

Hi folks,

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.

Thanks much!

What do you mean by “overkill”? Didn’t you get the desired result?

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.

Here is a solution for the silhouette using the stencil buffer: http://www.flipcode.com/archives/Object_Outlining.shtml

For the edges within an object, I can think of two solutions (maybe there are others):

  1. Render the normals of the object into a framebuffer first and then do edge detection (with Sobel or similiar) on this framebuffer.

  2. 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=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.

Here is a solution for the silhouette using the stencil buffer: http://www.flipcode.com/archives/Object_Outlining.shtml

For the edges within an object, I can think of two solutions (maybe there are others):

  1. Render the normals of the object into a framebuffer first and then do edge detection (with Sobel or similiar) on this framebuffer.

  2. 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.

Best regards.

I don’t see why: you could generate the lines just after loading the mesh, since it is not view-dependent.

Well yes, but that would mean that I’d have to choose the lines manually, right? I cannot somehow generate them automatically?

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.