views:

120

answers:

1

I want to clip some closed path area from texture image in OpenGL ES and iPhone SDK.

I found that it can be done with planes and glClipPlanef function something like this:

glClipPlanef(GL_CLIP_PLANE0, v);
glEnable(GL_CLIP_PLANE0);   
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);  
glDisable(GL_CLIP_PLANE0);

where v is a const GLfloat array of coordinates.

I can not figure out how to set these coords to clip the closed path area?

A: 

I understand how to use glClipPlanef:

const GLfloat v[] = {
    A, B, C, -D
};

// bind to clip plane
glClipPlanef(GL_CLIP_PLANE0, v);
// enabled it
glEnable(GL_CLIP_PLANE0);   
// some draw logic
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);  
// clip area
glDisable(GL_CLIP_PLANE0);

where A, B, C, D - parameters of plane equation.

If we have three points with coords (x1,y1,z1), (x2,y2,z2), (x3,y3,z3) then

A = y1 * (z2 - z3) + y2 (z3 - z1) + y3 (z1 - z2) 
B = z1 * (x2 - x3) + z2 (x3 - x1) + z3 (x1 - x2) 
C = x1 * (y2 - y3) + x2 (y3 - y1) + x3 (y1 - y2) 
-D = x1 * (y2*z3 - y3*z2) + x2 * (y3*z1 - y1*z3) + x3 * (y1*z2 - y2*z1)

If you have 2D texture you'll need to define points as follows: (x1, y1, 0), (x2, y2, 0 and (x3, y3, 1). This will work.

Hope this will help somebody in future.

sashaeve