0

How do I create a polygon in OpenGL 4.1 that has an inset border of n-pixels?

Presumably, since I have said the magic word "pixel", this means that I need to invoke a fragment shader of some form. (And, even though I said "pixel", this might need to be "pixel distance from edge" or some similar metric).

In addition, since I said the word "inset" (ie. I don't want any of the outline pixels to render outside the rendered polygon), I probably have invoked a stencil buffer. And may have invoked the idea of multiple passes.

However, I seem to be at a loss for how to put the pieces together to make the outline.

Here is what I mean in pictures (I chose rectangles simply because they are the easiest to define what an n-pixel outline is as well as decompose into the constituent triangles cleanly):

This is a blue rectangle with a 2 pixel wide blue border:

blue rectangle with 2 pixel wide blue border

This is a blue rectangle with a 3 pixel wide red border:

blue rectangle with 3 pixel wide red border

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    see [glLineWidth()](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glLineWidth.xhtml) .. the inward constraint can be done by adjusting coordinates of the rectangle/polygon ... similarly there is also `glPointSize()` if you want to have square points of desired size ... If you still want to go with shaders you need to implement perpendicular distance to line in Fragment and pass the geometry as texture ... then render single QUAD covering whole screen instead of rendering your geometry as Polygons – Spektre Jun 20 '19 at 10:37
  • I'm pretty sure this will not work. Attempting to draw a line as an outline with the same vertex coordinates to even something as simple as a triangle has quite a few known failure modes that are sufficiently well known that they are in OpenGL FAQs (things like pixels over/under rendering at corners, for example). Attempting to adjust *vertex* coordinates in order to get *pixel* adjustments is just adding fuel to the fire as the rendering has no hope of being constrained properly after roundoff errors. – Andrew Lentvorski Jun 21 '19 at 06:40
  • The common way of remedy that is to 1 render interior (filling) 2. render thick circumference (Lines) 3. render thick or sprited points. This method is not perfect I know but its fast. If you want to go for shaders that is doable, pixel perfect not too complicated but slow and has limitations like max texture size which limits geometry complexity. If you want geometry approach (generate circumference polygon) that one is uneasy see [draw outline for some connected lines](https://stackoverflow.com/a/22068534/2521214). I did not answer as you did not select which way you want to go yet... – Spektre Jun 21 '19 at 07:06

0 Answers0