glClipPlane对裁剪frustum是很管用的,可进一步定义视图体的范围。裁剪平面由方程Ax+By+Cz+D=0确定。所有满足[A B C D]M-1[Xe Ye Ze We]T>0的人眼坐标[Xe Ye Ze We]的点都位于该平面定义的半空间中,而该半空间以外的所有点都被裁剪掉——摘自Redbook。
void findPlane(GLfloat plane[4], vec3 p1, vec3 p2, vec3 p3)
{
/* Need 2 vectors to find cross product. */
vec3 vec1, vec2;
vec1= p2 - p1;
vec2 = p3 - p1;
/* find cross product to get A, B, and C of plane equation */
plane[A] = vec1.y * vec2.z - vec1.z * vec2.y;
plane[B] = -(vec1.x * vec2.z - vec1.z * vec2.x);
plane[C] = vec1.x * vec2.y - vec1.y * vec2.x;
plane[D] = -(plane[0]*p1.x + plane[1]*p1.y + plane[2]*p1.z);
/////////
for user clip planes,there are at least the following
options:
- Use the built in User Clip plane functionality already available in GL or GLES1.1.
This would be exactly what I need, but I've read that there are all sorts of
issues when trying to get this to work on both ATI & Nvidia hardware when
using GLSL.
- Perform the clipping using a fragment shader. I don't know much about this
technique (any information would be appreciated!), but I'm under the impression
that this is not very efficient since the pixel is rejected very late in the
rendering pipeline.
- Use oblique frustum culling and use the near plane to perform clipping. I've
already used this technique to perform clipping in a few cases. However, this
technique only allows one clip plane and cannot be turned on and off at will
since it manipulates the depth buffer. I currently need one clip plane to be
used against all primitives (oblique frustum is perfect for this) and a second
which can be turned on and off at will while still allowing culled objects to be
rendered correctly with the existing depth information.
- Perform clipping using alpha values in conjunction with the Alpha test. I
don't know where alpha testing is perform within the rendering pipeline, but if
it's earlier than fragment shader execution, it could be more efficient than
option 2. The application of alpha values for testing would use a 1d alpha
texture (one pixel opaque, one pixel transparent) which could be applied to
match the clip plane using texture matrix manipulation.