-1

Here is a task that every GIS application can do: given some polygons, fill each polygon with a chosen color. Like this: image

What is the best way of doing this repeatedly in Opengl? That is, the polygons do not change, and I want to vary the data for coloring to produce difference renderings.

Redrawing polygons for each rendering is the most straightforward solution, but it seems to be a waste, since the geometries do not change at all.

Or is it better to create a stencil for each polygon, and stencil print the entire map? If there are too many polygons, will doing hundreds or thousands of rendering passes create a problem?

Spektre
  • 49,595
  • 11
  • 110
  • 380

2 Answers2

0

For each vertex of a polygon, map a certain color.That means when you send the data to the shaders, with each call the vertex array object sends 2 parameters: a vector which is needed in the vertex shader and a vector which will be used as the fragment color.That is the simplest way.

For example think of a triangle drawn in opengl . if you send its vertices to the vertex shader and set a color in the fragment shader everytime when a vertex enters the shader pipeline it will be positioned accordingly and on the screen set with the given color from the fragment shader.

The technique which I poorly explained ( sry I am not the best at explanations) , is used in the colored triangle example in which colors interpolate.Red maped to a corner , Green maped to another , and Blue to the last. If you set it so the red color maps to every corner you get your colored triangle.That is the basic principle.Oh and you draw the minimum count of triangles and you need one pair of shaders . Note : a polygon is made out of N triangles and you need to map the same color to every vertex of each triangle drawn in that polygon.

Robert
  • 49
  • 1
  • 4
0

I think a bigger issue will be that OpenGL doesn't support polygons or vector drawing in general, but there are libraries for this. You'll have to use an existing solution for vector drawing, or failing that, you'll have to convert from your GIS data (usually a list of points for a polygon) to triangles. This is likely the biggest obstacle.

The fact that the geometry doesn't change isn't really an issue, you would generally store geometry into one or more buffers, then create logic to only draw what is visible inside your view point area, perhaps even go as far to only generate the geometry for the visible area.

See also this question and it's answers.

Rendering Vector Graphics in OpenGL?

Community
  • 1
  • 1
finlaybob
  • 697
  • 10
  • 30