Clip Plane plays an important role in the graphics field. For example, in the simulation of the water surface, when rendering the refraction texture, we must cut the vertices above the water surface by cropping the Plane.
In the past era of Fixed rendering pipelines, the implementation of cropping planes was relatively simple, for example, in DirectX 9, you can first set the equation of the cropping plane in the world coordinate system (ax + by + cz + d = 0), and then call SetClipPlane (DWORD Index, CONST float * pPlane) this API function is enough.
Attached example program:
VPosition = D3DXVECTOR3 (0, 0, 0); // point on the plane
VNormal = D3DXVECTOR3 (0, 1, 0); // Normal Vector
D3DXPlaneFromPointNormal (& clipplane, & vPosition, & vNormal); // generate a cropping plane
M_pDevice ()-> SetClipPlane (0, (float *) clipplane );
However, in the current programmable pipeline (programmable pipeline), the cropping plane is processed in the cropping coordinate system rather than in the world coordinate system.
The solutions to this problem are:
1) mark the vertex to be cropped and cut it out in Pixel Shader.
2) use Near-Oblique Plane pruning (Oblique Near-Plane Clipping) to modify the projection matrix and place the cropped vertex outside the video clip, thus avoiding the rendering of the vertex.
3) modify the plane equation to convert it from the world coordinate system to the crop coordinate system.
In the above method, the first and second methods are not efficient: cropping in Pixel Shader does not reduce unnecessary vertex processing, and calculation of the near-oblique plane cropping matrix is cumbersome. Therefore, method 3 is the best choice.
To convert a plane from the world coordinate system to the crop coordinate system, you must find this transformation matrix.
Let the plane equation ax + by + cz + d = 0 and use a 4-dimensional vector to represent n (a, B, c, d ), set a point p :( x, y, z, 1) on the plane ). According to the definition of the plane equation, there are:
NTP= Ax + by + cz + d = 0
When matrix R is set, point P can be converted from the world coordinate system to the crop coordinate system. matrix Q allows Plane n to implement the same transformation. So there are:
P'=RPN'=QN
P 'and n' are the converted points and planes respectively.
N'TP'= 0 (QN) T (RP) = 0
NTQTRP= 0
If:QTR=I
So:
NTQTRP=NTIP=NTP= 0
So:
QT =
R-1
Q= (
R-1) T
In DirectX 3D, a point is converted from the world coordinate system to the crop coordinate system. The matrix used is the product of the observation matrix and the projection matrix, that is:
D3DXMATRIX TranMatrix = matView * matProj;
(TranMatrix is the transformation matrix. matView and matProj are the observation matrix and projection matrix respectively)
The complete code for transformation in D3D is attached:
D3DXPLANE tempPlane = clipplane;
D3DXPlaneNormalize (& tempPlane, & tempPlane );
D3DXMATRIX TranMatrix = matView * matProj;
D3DXMatrixInverse (& TranMatrix, NULL, & TranMatrix );
D3DXMatrixTranspose (& TranMatrix, & TranMatrix );
D3DXPlaneTransform (& tempPlane, & tempPlane, & TranMatrix );
References:
1. Back Face Culling Notes, Jordan Smith (University of California, Berkeley)
Http://www.cs.berkeley.edu /~ Ug/slide/pipeline/assignments/backfacecull.shtml
2. GameDev Forum
Http://www.gamedev.net/community/forums/topic.asp? Topic_id = 402381
3. Oblique Near-Plane Clipping with Orthographic Camera, Aras
Http://aras-p.info/texts/obliqueortho.html