OpenGL contour and creases render

386 views Asked by At

I want to render the outlines + creases with opengl but it's not going as I wish. Can someone, please help me to solve this problem? So far the outline it's rendered like this.

How it's rendered now

At this moment what I'm doing is: Frist I do this:

GL.Enable(EnableCap.PolygonOffsetFill);
GL.PolygonOffset(0, -1f);
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
GL.DepthFunc(DepthFunction.Lequal);
GL.DrawArrays(PrimitiveType.Triangles, 0, verticesList.Count);
GL.Disable(EnableCap.PolygonOffsetFill);

Then I render again like this:

GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
GL.LineWidth(5f);
GL.DepthFunc(DepthFunction.Less);
GL.DrawArrays(PrimitiveType.Triangles, 0, verticesList.Count);

I want to render those green lines as well:

How I want to render it

The wireframe over the filled shape looks like this:

How the wireframe looks

Thanks in advance!

2

There are 2 answers

0
Rabbid76 On

You cannot achieve this with GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);. The PolygonMode.Line mode does not render the outlines of the mesh, but the outlines of the primitives (triangles). There is no feature that will give you what you want right away. If you want to draw these lines on a solid mesh, you need to disable the depth test and create a separate geometry with Line primitives. You can use the same vertex coordinates for this geometry, but you must draw them in a different order. OpenGL is just a low-level API implemented by the graphics driver that gives you access to the GPU, but it's not a render, game, or CAD engine. You have to implement the engine and you can do it with OpenGL.

0
L Chougrani On

@Rabbid76 is right. there are many ways to achieve it though : Outlines : The outline can be renderer via another pass in OpenGL (you'll find many tutorial on this) Creases What you call "crease" is also known as "sharp edges" and you may need to render them on different mesh. It is quite easy to go about it if you have access to your mesh topology (meaning the connectivity between your different faces/vertices/edges). For that you could either :

  • Create another mesh with RenderMode.Line and populate it with all edges for which the adjacent face normals dotproduct exceed a certain threshold.
  • Add another "in array" to your shader as a vec3[] (if you render only trianlge), then choose an ordering for your x,y,z coordinates according to the faces vertices/edge order and update this vec3[] with either 1 (if sharp edge) or 0 (if not sharp edge) and then do some coloring according to that in your fragment shader. (I never tested this second method, it would seem a little overkill, but you'll end up with only one mesh)

this would be a result for the first implementation :

  • red: outline
  • green: sharp edge (30° angle threshold) enter image description here