Dynamically create additional regular mesh triangles in OpenGL

I created a regular grid that arises from a 2D image, that is, each pixel has a vertex. There are two four pixel triangles, so I have a triangle in the upper right and lower left. I use vertex and index buffers for this.

Current state

Now I dynamically delete triangles / faces on the border of two different types of vertices (according to my application), because otherwise there will be distortions. I wrote a geometric shader that takes a triangle and draws a triangle or nothing (see the first image). The shader recognizes if the triangle is “faulty” (has orange edges) and lowers it.

Now this works great, but I may lose some details due to my vertex geometry. I can add additional triangles to the grid (see. Second image, new triangles with a dashed orange line).

Goal Status

How to do it in OpenGL?

My first idea is to create one square instead of two triangles, check four possible cases of triangles and create these triangles dynamically in a geometric shader. But it can be slow; GL_QUAD is deprecated, and alternatives may be slow. What do you have in mind?

+5
source share
1 answer

Here is my idea:

Put the whole grid in a buffer / texture.
Build four triangles for every four pixels. Yes, they cross each other.
In the geometric shader, you can determine if the triangle is “faulty” because it connects two irregular areas. Or, the selection forms a texture because the intersection triangle is valid, so this new one can be discarded.

EDIT : A Different Approach

Use texture. Draw with GL_POINTS. With some order and instanceID, the shader knows where the point is.
To do this, check the four possible triangles. If you use a vertex down and left to right, for four triangles, only one point to the right and two to the bottom are used. And you avoid repeating the tests.
Release only those you choose.

+4
source

Source: https://habr.com/ru/post/1264568/


All Articles