OpenGL® Programming Guide: The Official Guide to Learning OpenGL®, Version 2.1, 6th Edition
Excerpt from Chapter 3: Viewing
Additional Clipping PlanesIn addition to the six clipping planes of the viewing volume (left, right, bottom, top, near, and far), you can define up to six additional clipping planes for further restriction of the viewing volume, as shown in Figure 3-22. This is useful for removing extraneous objects in a scene—for example, if you want to display a cutaway view of an object. Each plane is specified by the coefficients of its equation: Ax + By + Cz + D = 0. The clipping planes are automatically transformed appropriately by modeling and viewing transformations. The clipping volume becomes the intersection of the viewing volume and all half-spaces defined by the additional clipping planes. Remember that polygons that get clipped automatically have their edges reconstructed appropriately by OpenGL. Figure 3-22
You need to enable each additional clipping plane you define: glEnable(GL_CLIP_PLANEi); You can disable a plane with glDisable(GL_CLIP_PLANEi); All implementations of OpenGL must support at least six additional clipping planes, although some implementations may allow more. You can use glGetIntegerv() with GL_MAX_CLIP_PLANES to determine how many clipping planes are supported.
A Clipping Plane Code ExampleExample 3-5 renders a wireframe sphere with two clipping planes that slice away three-quarters of the original sphere, as shown in Figure 3-23. Figure 3-23 Example 3-5 Wireframe Sphere with Two Clipping Planes: clip.cvoid init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
void display(void)
{
GLdouble eqn[4] = {0.0, 1.0, 0.0, 0.0};
GLdouble eqn2[4] = {1.0, 0.0, 0.0, 0.0};
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPushMatrix();
glTranslatef(0.0, 0.0, -5.0);
/* clip lower half -- y < 0 */
glClipPlane(GL_CLIP_PLANE0, eqn);
glEnable(GL_CLIP_PLANE0);
/* clip left half -- x < 0 */
glClipPlane(GL_CLIP_PLANE1, eqn2);
glEnable(GL_CLIP_PLANE1);
glRotatef(90.0, 1.0, 0.0, 0.0);
glutWireSphere(1.0, 20, 16);
glPopMatrix();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Try This
|
|