Search code examples
openglpyopengl

OpenGL: Keep drawing inside a region on a plane in 3D space


Step 1: I use glVertex() to draw a shape, say a rectangle, in 3D space.

Step 2: I then draw things inside this rectangle - for example with glutStrokeCharacter() and with glVertex() again.

I need to keep what are drawn in step 2 inside the rectangle drawn in step 1, clipping away anything that goes outside the boundary. Can someone give some hint on how to do this?


Solution

    1. Render bounding geometry into stencil buffer.
    2. Enable stencil test
    3. Render regular geometry into color buffer; the stencil test will prevent fragments outside the bounding geometry from writing to the color buffer.