PDA

View Full Version : Edge detection



enne87
05-08-2016, 04:35 AM
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 (https://wiki.blender.org/uploads/thumb/d/da/Manual_-_Bevel_Modifier_-_3_Beveled_Cubes.png/610px-Manual_-_Bevel_Modifier_-_3_Beveled_Cubes.png).

Now I tried several approaches including Frei-Chen Edge detector (http://rastergrid.com/blog/2011/01/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!

Hermannicus
05-09-2016, 05:34 AM
Now I tried several approaches including Frei-Chen Edge detector (http://rastergrid.com/blog/2011/01/frei-chen-edge-detector/) but it seems as if this algorithm is a bit of an overkill.


What do you mean by "overkill"? Didn't you get the desired result?

enne87
05-09-2016, 08:30 AM
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.

Hermannicus
05-09-2016, 11:03 PM
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.

enne87
05-10-2016, 11:57 AM
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.

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.

Spoops
05-11-2016, 09:54 AM
2. is not a possibility unfortunately since I want to load arbitrary meshes.

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

enne87
05-15-2016, 02:13 PM
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?

Spoops
05-15-2016, 06:46 PM
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.

enne87
05-16-2016, 07:19 AM
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.