Nested Clipping

I’m try create a windowing system using OpenGL. I have a series of nested frames that represent windows. Each has its own local transformation so that if you move the parent frame, all children frames move as well. Each frame also has a width and height. I would like to clip what is drawn in the child frames by each of the parent frames.

Is there an easy way to do this?

I was looking at clipping planes. I could use 4 clipping planes to clip to the child to the parent frame, but this would not clip a child to its grandparent frame.

I’m not sure how to proceed.

glScissor may help you. Or stencil buffer.

Maintain a stack of clipping rectangles and draw-offset points.


extern RECT ClipRect;
extern POINT DrawOffs;
bool xEnterClip(x,y,wid,hei){
   push(ClipRect);
   push(DrawOffs);
   x+=DrawOffs.x;
   y+=DrawOffs.y;
   DrawOffs.x=x;
   DrawOffs.y=y;
   RECT clip1 = {x,y,x+wid,y+hei};
   ClipRect = IntersectRect(ClipRect,clip1);
   SetScissorRect(ClipRect);
   if(Rectangle_Area(ClipRect)==0)return false;
   return true;
}
void xLeaveClip(){
   DrawOffs=pop();
   ClipRect=pop();
   SetScissorRect(ClipRect);
}

I personally find it better to not use scissors, just my own rectangle-clipping code - scissors weren’t present in some widely-used cards, and there are less than 200 quads per window to draw.

Also, you can use the z-buffer for different windows, that will overlap. (add a preliminary stage that only draws to z).