Cross out everything outside the polygon.

I have a map, for simplicity let it be just one texture. At the top of this map, I have a polygon that indicates the route the user should follow.

What I want is to draw everything outside the black polygon. Or, of course, just draw things inside the polygon.

To explain this, I took a picture. Blue lines define a polygon, each of which is a point in the polygon. Red with yellow lines is the part that I want to delete from the picture, and I leave the red color only with purple lines. The polygon starts at A and ends at B.

enter image description here

+1
source share
1

, , OpenGL. , . , , , . . , , , " ".

OpenGL , : . . , . , , , . / , .

:

  • , .
  • .

    glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    glEnable(GL_STENCIL_TEST);
    
  • , , / . , , . GL_INVERT op, , , / .

    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    glStencilFunc(GL_ALWAYS, 0, 1);
    glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT);
    glStencilMask(1);
    
  • , . (0.0, 0.0), , - . , . p1, p2,..., pN - , GL_TRIANGLE_FAN:

    (0.0f, 0.0f), p1, p2, ... , pN, p1
    

    , .

  • , , .

    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glStencilFunc(GL_EQUAL, 1, 1);
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
    
  • . , .

+8

All Articles