5
votes

I have created a regular grid which originates from a 2D image, i.e. each pixels has a vertex. There are two triangles per four pixels so that I have a triangle in the top right and in the bottom left. I use vertex and index buffers for that.

Current state

Now I dynamically remove triangles / faces at the border of two different kinds of vertices (according to my application) because else there would be distortions. I wrote a geometry shader which takes a triangle and outputs the triangle or nothing (see first picture). The shader recognizes if a triangle is "faulty" (has orange edges) and omits it.

Now this works fine, but I may lose some details because of my vertex geometry. I can add complementary triangles to the mesh (see second picture, new triangles with dashed orange line).

Goal state

How do I accomplish this in OpenGL?

My first idea is to create one quad instead of two triangles, check for the four possible triangles cases and create those triangles dynamically in the geometry shader. But this might be slow; GL_QUADs are deprecated and alternatives might be slow too. What do you have in mind?

1
Is there a particular reason why you need to be able to generate all this geometry on the fly, or is it also possible to generate everything once in advance, and only update it when necessary. Because if you're not updating these triangles every single frame, it's better to use the latter approach instead instead of a geometry shader.Bartvbl
I'm processing real-time video, which means different image data every frame.Close Call
I would still say geometry shaders may be your best bet here. You create a buffer with your vertex grid, then use the geometry shader to change the order in which vertices are emitted. If you decide not to draw a triangle, simply reverse the order of its indices, and use backface culling to have it become invisible. This will still allow you to add your complementary triangles.Bartvbl
@Bartvbl I am not sure if I can follow you. I assume that I process one triangle at once in the geometry shader, which means that I may omit (no need for reversing the index order) it or keep it. I cannot access the missing 4th vertex to generate the complementary triangle, as in my understanding.Close Call
A geometry shader executes on a single triangle at a time. The input to the function is a struct like the one shown here: khronos.org/opengl/wiki/Geometry_Shader#Inputs. You can decide yourself in which order to emit these vertices. Or you can change their coordinates such that the triangles become of the "complementary" kind. Finally, you can invert the order in which the vertices are emitted to cause them to be culled through backface culling (I seem to remember the geometry shader can do this as a built-in feature, but I can's find it at the moment)Bartvbl

1 Answers

4
votes

Here's my idea:

Put the whole grid in a buffer/texture.
Build four triangles for each four pixels. They cross each other, yes.
In the geometry shader you can tell if a triangle is "faulty" because it connects two wrong regions. Or, sampling form the texture, because the crossing triangle is valid, so this new one can be discarded.

EDIT: Another approach

Use the texture. Draw instanced with GL_POINTS. With some order and the help of the instanceID the shader knows where the point is.
For this point test the four possible triangles. If you instance top to down and left to right, only a point to the right and the two below are used for the four triangles. And you avoid repeating tests.
Emit only those you choose.