Hello all
I have subclassed CCNode to work as a clip box, any children outside the content size of this box are clipped.
This works great in iPad, iPad simulator and iPhone .... but NOT on the iPhone device, there only clip plane 0 is working, the other 3 do not.
here is my code, it's called in "visit" and "draw" to clip op children.
- (void)clipPlane:(BOOL)clip
{
if (clip)
{
// enable clipping
glEnable(GL_CLIP_PLANE0);
glEnable(GL_CLIP_PLANE1);
glEnable(GL_CLIP_PLANE2);
glEnable(GL_CLIP_PLANE3);
// clip
glClipPlanef(GL_CLIP_PLANE0, (GLfloat *)&cpL);
glClipPlanef(GL_CLIP_PLANE1, (GLfloat *)&cpR);
glClipPlanef(GL_CLIP_PLANE2, (GLfloat *)&cpT);
glClipPlanef(GL_CLIP_PLANE3, (GLfloat *)&cpB);
}
else
{
// disable clipping
glDisable(GL_CLIP_PLANE0);
glDisable(GL_CLIP_PLANE1);
glDisable(GL_CLIP_PLANE2);
glDisable(GL_CLIP_PLANE3);
}
}
And for those who are interested, this is how i calculate the clip planes, "prepareClipping" is called when the content size is updated.
- (GZEPlaneEQ)getClipPlaneForPoint1:(ccVertex3F)a point2:(ccVertex3F)b point3:(ccVertex3F)c
{
GZEPlaneEQ e;
e.a = a.y * (b.z - c.z) + b.y * (c.z - a.z) + c.y * (a.z - b.z);
e.b = a.z * (b.x - c.x) + b.z * (c.x - a.x) + c.z * (a.x - b.x);
e.c = a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y);
e.d = -(a.x * (b.y * c.z - c.y * b.z) + b.x * (c.y * a.z - a.y * c.z) + c.x * (a.y * b.z - b.y * a.z));
return e;
}
- (void)prepareClipping
{
// content size
CGSize size = [self contentSize];
// page
CGRect rectPG = CGRectMake(0.0f, 0.0f, size.width, size.height);
// points
ccVertex3F pBL = (ccVertex3F){ rectPG.origin.x, rectPG.origin.y, 0.0f };
ccVertex3F pTL = (ccVertex3F){ rectPG.origin.x, rectPG.origin.y + rectPG.size.height, 0.0f };
ccVertex3F pBR = (ccVertex3F){ rectPG.origin.x + rectPG.size.width, rectPG.origin.y, 0.0f };
ccVertex3F pTR = (ccVertex3F){ rectPG.origin.x + rectPG.size.width, rectPG.origin.y + rectPG.size.height, 0.0f };
ccVertex3F pLB = (ccVertex3F){ rectPG.origin.x, rectPG.origin.y, 100.0f };
ccVertex3F pRT = (ccVertex3F){ rectPG.origin.x + rectPG.size.width, rectPG.origin.y + rectPG.size.height, 100.0f };
// planes
cpL = [self getClipPlaneForPoint1:pTL point2:pLB point3:pBL];
cpR = [self getClipPlaneForPoint1:pTR point2:pBR point3:pRT];
cpT = [self getClipPlaneForPoint1:pTL point2:pTR point3:pRT];
cpB = [self getClipPlaneForPoint1:pBL point2:pLB point3:pBR];
}