# OpenGL Learning 09_ Clipping plane Clipplane

Source: Internet
Author: User

In addition to the 6 clipping planes of the viewing body (left, right, bottom, top, near, and far), additional clipping planes up to 6 can be specified, with further restrictions imposed on the view body.

Each plane is specified by its equation ax + by + Cz + d= 0 factor. The clipping plane performs the appropriate transformations automatically based on the model and the View matrix. The final cropping area will be the intersection of all the half-spaces defined by the scene body and other clipping planes. Remember that OpenGL automatically reconstructs the edges of partially cropped polygons.

Defines a clipping plane. The equation parameter points to a plane equation ax + by + Cz + D = 0 of 4 coefficients. All visual coordinates (XE ye ze We) that meet (A B C D) M-1 (Xe ye ze We) t≥0 are located in the half space defined by this plane, where M is the current model view matrix when calling Glclipplane (). All points that are not in this half-space will be cropped off. The plane parameter is Gl_clip_planei, where I is an integer that indicates which valid clipping plane needs to be defined. The value of I is between 0 and the maximum other clipping plane number minus 1.
We need to enable each of the defined clipping planes:

You can also disable a clipping plane with this function:
All OpenGL implementations must support at least 6 other clipping planes, and some implementations may allow more than 6 other clipping planes. You can call the Glgetintegerv () function with the Gl_max_clip_planes parameter to query the maximum number of other clipping planes supported by the OpenGL implementation that you use.

The clipping performed by calling the Glclipplane () function is done in visual coordinates, rather than in clipping coordinates. If the projection matrix is a singular matrix (that is, the three-dimensional coordinates are flattened to the true projection matrix of the two-dimensional coordinates), this difference is very large. When cropping in visual coordinates, even if the projection matrix is a singular matrix, the cropping is still carried out in three-dimensional space.

The following example cuts a sphere with two clipping planes.

`main.cpp//opengl_10_clipplane////Created by Apple on 15/1/19.//Copyright (c) 2015 CC. All rights reserved.//#include <iostream> #include <glut/glut.h>/** * Initialize Operation */void init () {//Set clear screen color GLCL    Earcolor (0.0f, 0.0f, 0.0f, 0.0f); Sets the shading mode, the fill color is consistent with the vertex glshademodel (Gl_flat);}    /** * Display Drawing effect */void display () {//Cleanup color buffer glclear (gl_color_buffer_bit);    Set Paint color glcolor3f (1.0, 1.0, 1.0);    Copy the current matrix and save it to the top of the stack Glpushmatrix ();        Move 5 Unit length Gltranslatef (0.0f, 0.0f, -5.0f) in negative direction along the z axis;    Define clipping plane Array, Ax + by + Cz + D = 0 gldouble Equn[4] = {0.0f, 1.0f, 0.0f, 0.0f};    Gldouble Equn2[4] = {1.0f, 0.0f, 0.0f, 0.0f};    Set clipping plane, y>0 glclipplane (GL_CLIP_PLANE0, Equn);        Glenable (GL_CLIP_PLANE0);    Set clipping plane, x>0 glclipplane (Gl_clip_plane1, equn2);        Glenable (GL_CLIP_PLANE1);    Rotate 90 degrees along the x-axis Glrotatef (90.0f, 1.0f, 0.0f, 0.0f);        Renders a sphere, the sphere radius, the number of lines (meridians) of a circular line with a diameter distribution on the z axis, and the number of bars around the z axis (the dimension line) Glutwiresphere (2.0f, 20.0f, 16.0f); Pop-up stack top momentArray Glpopmatrix (); Force completion of Drawing instructions Glflush ();} /** * Resize Window * * @param width Width * @param height */void reshape (int width, int height) {//Set viewport rectangle area, by default, viewport is set    Glviewport (0, 0, (Glsizei) width, (glsizei) height) for the entire pixel rectangle that occupies the open window;    The following matrix operation is defined as the projection matrix operation Glmatrixmode (gl_projection); is equal to the previous matrix transformation caused the change of the stack top matrix back to bits, set as the unit matrix!    is equal to the effect of the previous matrix transformation.        Glloadidentity (); Create a matrix that represents a symmetric perspective tiling truncated body//Set the eye open angle, the aspect ratio of the scene, the distance from the near section, the distance from the far section gluperspective (60.0f, (glfloat) Width/(glfloat) height, 1.0f        , 20.0f);    The matrix operation is then defined as the Model View matrix Operation Glmatrixmode (Gl_modelview);    }int Main (int argc, const char * argv[]) {//Initialize GLUT library Glutinit (&AMP;ARGC, (char**) argv); Set the single-buffered, RGB-pixel-formatted window glutinitdisplaymode (Glut_single |    GLUT_RGB);    Set window Size Glutinitwindowsize (500, 500);    Set window coordinates glutinitwindowposition (100, 100);        Create Window Glutcreatewindow ("Clipplane");        Initialization Operation Init ();    Set the display callback method Glutdisplayfunc (display);        Glutreshapefunc (reshape); PaintedThe thread starts looping glutmainloop (); return 0;}`

OpenGL Learning 09_ Clipping plane Clipplane

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.