📜 ⬆️ ⬇️

Oblique frustum. Inside the oblique visibility pyramid

The following material is probably familiar, or even well known, to programmers who had experience with OpenGL, meanwhile, I found it appropriate to recall the oblique frustum model, partially observing (and sharing) the interest of Habr's readers in OpenGL issues and in general three-dimensional modeling, partly because of disagreement with the position of some developers like “... in order to use it, it is not at all necessary to understand how the projection matrix works,” partly out of respect and gratitude for Eric Lengel | Eric Lengyel , whose inventive thought has enriched the techniques of working in the OpenGL environment.

If there are mirror images in the scenes you are modeling and you have forgotten or not heard about “oblique frustum”, then it is possible that this article will not be useless for you.

In spite of the fact that I, following Erik Lengel, were more adhered to in presenting the material of OpenGL presentations, all the subsequent reasoning is easily extended to any other three-dimensional modeling systems.

Reflections
')

Reflections in 3D scenes


In the case where reflective surfaces are present in 3D scenes, the usual practice of obtaining reflections is to render the scene using an auxiliary view camera, which itself is a reflection of the main view camera by the plane of the reflecting surface.

arbitrary camera


Fig . 1. The top camera is the main camera through which the scene containing the reflective surface is rendered. The bottom camera is an auxiliary camera designed for rendering reflection. The X axis is directed to the observer for both the pyramids of visibility ( in the views adopted for OpenGL ), and it is obvious that the coordinate system for the camera that renders the reflection is left-handed .

When rendering a reflection with an auxiliary camera (a reflection camera), it may turn out that some of the objects located between the observer and the reflecting surface end up in the resulting render, forming unwanted artifacts in the image. In order to cut off the undesirable part of the reflected scene from the final render, the programmer restricts the part of the volume of the three-dimensional scene observed from the reflection chamber to an imaginary surface, using various techniques to interrupt the rendering of the scene beyond this surface.

You can use the “discard” instruction for a fragmentary shader or similar, specific for individual rendering implementations in the general ideology of 3D modeling (for example, kill in AGALMiniAssembler), however, if we want a universal solution that works equally well on any processor , then pay attention to the technique proposed by Eric Lengel.

The idea of ​​Eric Lengela was to modify the projection matrix in such a way that the near view of the visibility pyramid becomes just a sort of cutting plane that separates the volume of the main scene that is undesirable in the final render.

For further, it is appropriate to recall the elementary representations of analytic geometry .

Plane in space


For convenience of presentation, we will adhere to the following notation:

\ begin {center} \ begin {array} {| rlc} \ textbf {for points and vectors:} & amp; \ vec {A}, \ vec {P} \\ \ multicolumn {2} {| l} {\ textit {distinguishing them} \ mathit {w} \ textit {-coordinate in a uniform representation,}}} \\ \ multicolumn { 2} {| l} {\ textit {the coordinate record is enclosed in angle brackets} \ langle ,,, \ rangle} \\ [0.3em] \ textbf {for matrices:} & amp; \ mathbf {M}, \ mathbf {P} \\ \ textbf {transposed matrix:} & amp; \ mathbf {M} ^ \ mathrm {T} \\ \ textbf {inverse matrix:} & amp; \ mathbf {M} ^ {- 1} \\ \ textbf {character} x: & amp; sgn (x) = \ begin {cases} 1, & amp; \ quad \ text {if} x & gt; 0 \\ 0, & amp; \ quad \ text {if} x = 0 \\ -1, & amp; \ quad \ text {if} x & lt; 0 \ end {cases} \\ \ textbf {vector length:} & amp; \ | \ vec {P} \ | \\ \ textbf {scalar product of vectors:} & amp; \ vec {Q} \ cdot \ vec {P} \\ \ end {array} \ end {center}

Plane


For any spatial 3D point \ vec {P} and vectors \ vec {N} , set of 3D points \ vec {Q} other than \ vec {P} satisfying the equation \ vec {N} \ cdot (\ vec {Q} - \ vec {P}) defines a plane, with a point \ vec {P} is one of the points belonging to this plane, and the vector \ vec {N} is its normal vector.

Plane

Fig . 2. The plane is completely determined by the point belonging to it. \ vec {P} and normal vector \ vec {N} .

The plane equation is often written with the following expression:

Ax + By + Cz + D = 0,

Where \ mathit {A, B} and \ mathit {C} there is \ mathit {x, y, z} normal vector components \ vec {N} , and D = - \ vec {N} \ cdot \ vec {P} . Value | D | / \ | \ vec {N} \ | equal to the distance to the plane from the origin ( remember that the components of the normal vector divided by its length are the direction cosines of the unit vector normal to the plane ).

In the case of a normalized normal vector, the expression

d = \ vec {N} \ cdot \ vec {Q} + D,

can be used to find the distance from the plane to an arbitrary point \ vec {Q} . If a d = 0 , \ vec {Q} lies in the plane. If d & gt; 0 , point \ vec {Q} is located on the positive side of the plane, i.e. from the side of the normal plane vector, with d & lt; 0 , point \ vec {Q} is located away from the plane, in the opposite direction to the normal vector of the plane \ vec {N} .
It is convenient to record the plane by a four-dimensional vector. In short, the equation of the plane is written as: \ langle \ vec {N}, D \ rangle . Obviously, for an arbitrary point \ vec {Q} having in uniform 4-dimensional coordinates \ mathit {w} -coordinate equal to 1, Expression (2) can be rewritten as d = \ vec {L} \ cdot \ vec {Q} where \ vec {L} = \ langle \ vec {N}, D \ rangle and point \ vec {Q} lies in the plane if \ vec {L} \ cdot \ vec {Q} = 0 .

Plane transform


To understand the features of the spatial transformation of the plane, it will take some attention to transform the normal vector . In the spatial transformation of a polygonal model, the vectors are tangent to the surface of the polygons and the vectors do not behave normally. The tangent vector can often be represented as the difference between two transformed vertices, i.e. between two naturally transformed points, and, as a result, the nature of the transformed vector coincides with our expectations. But, in the general case of a spatial transformation, the matrix \ mathbf {M} which is not orthogonal, direct application of the transformation matrix to the normal vector will result in this vector no longer being normal — perpendicular to the surface of the polygon.

Since the vector is tangent \ vec {T} and the vector is normal \ vec {N} belonging to the same polygon must remain perpendicular for the scalar product of transformed vectors \ vec {T} {^ '} and \ vec {N} {^ '} the same condition must be met as for the source vectors: \ vec {T} {^ '} \ cdot \ vec {N} {^'} = 0 . Perform a few simple algebraic operations to clarify the nature of a normal vector:

if a \ mathbf {M} - 3x3 matrix of space transformation (for the case of tangent and normal vectors, spatial displacements are insignificant), and \ vec {T} {^ '} = \ mathbf {M} \ vec {T} , then we set ourselves the goal of finding the transformation matrix \ mathbf {G} for \ vec {N} such that it is performed

\ vec {N} {^ '} \ cdot \ vec {T} {^'} = (\ mathbf {G} \ vec {N}) \ cdot (\ mathbf {M} \ vec {T}) = 0,

remember that the multiplication of vectors can be written as follows (completely equivalent):

(\ mathbf {G} \ vec {N}) \ cdot (\ mathbf {M} \ vec {T}) = (\ mathbf {G} \ vec {N}) ^ \ mathrm {T} (\ mathbf {M } \ vec {T}) = \ vec {N} ^ \ mathrm {T} \ mathbf {G} ^ \ mathrm {T} \ mathbf {M} \ vec {T},

Insofar as \ vec {N} ^ \ mathrm {T} \ vec {T} = 0 , expression \ vec {N} ^ \ mathrm {T} \ mathbf {G} ^ \ mathrm {T} \ mathbf {M} \ vec {T} = 0 performed if \ mathbf {G} ^ \ mathrm {T} \ mathbf {M} = \ mathbf {I} where \ mathbf {I} - unit matrix. Which implies that \ mathbf {G} = (\ mathbf {M} ^ {- 1}) ^ \ mathrm {T} . A vector that is transformed in a similar way (by means of a transposed inverse transformation matrix) is a covariant vector, while a vector that transforms like a tangent vector is a contravariant vector.

However, the plane in homogeneous coordinates, unlike the normal vector, has a nonzero \ mathit {w} coordinate, and should further investigate its behavior in 4x4-transformations.

The distance to the plane from the origin, after applying the spatial transformation, taking into account the peculiarities of the transformation of the normal vector, for a point lying in this plane \ vec {P} , through the familiar scalar product:

\ begin {array} {l} D {^ '} = - ((\ mathbf {M} {^ {- 1}}) ^ \ mathrm {T} \ vec {N}) \ cdot (\ mathbf {M} \ vec {P} + \ vec {T}) \\ \ qquad {} = - ((\ mathbf {M} ^ {- 1}) ^ \ mathrm {T} \ vec {N}) ^ \ mathrm {T } \ mathbf {M} \ vec {P} - ((\ mathbf {M} ^ {- 1}) ^ \ mathrm {T} \ vec {N}) ^ \ mathrm {T} \ vec {T} \\ \ qquad {} = - \ vec {N} ^ \ mathrm {T} \ mathbf {M} ^ {- 1} \ mathbf {M} \ vec {P} - \ vec {N} ^ \ mathrm {T} \ mathbf {M} ^ {- 1} \ vec {T} \\ \ qquad {} = D - \ vec {N} \ cdot \ mathbf {M} ^ {- 1} \ vec {T}, \ end {array }

We used the transformation matrix in these calculations. \ mathbf {F} added to the operations of rotation, scaling and skew shift operation:

\ mathbf {F} = \ left [\: \; \ begin {matrix} \ quad {} & amp; \ quad {} & amp; \ vline & amp; \ quad {} \\ \ quad {} & amp; \ mathbf {M} \ quad {} & amp; \ vline & amp; \ vec {T} \\ \ quad {} & amp; \ quad {} & amp; \ vline & amp; \ quad {} \\ \ hline \ quad {} & amp; 0 \ quad {} & amp; \ vline & amp; 1 \ end {matrix} \: \; \ right] = \ left [\: \; \ begin {matrix} M_ {11} & amp; M_ {12} & amp; M_ {13} & amp; \ vline \, & amp; T_x \\ M_ {21} & amp; M_ {22} & amp; M_ {23} & amp; \ vline \, & amp; T_y \\ M_ {31} & amp; M_ {32} & amp; M_ {33} & amp; \ vline \, & amp; T_z \ hline 0 & amp; 0 & amp; 0 & amp; \ vline \ , & amp; 1 \\ \ end {matrix} \: \; \ right],

Inverse transformation matrix to matrix \ mathbf {F} It is looked for by the usual matrix inversion algorithm:

\ mathbf {F} ^ {- 1} = \ left [\: \; \ begin {matrix} \ quad {} & amp; \ quad {} & amp; \ vline & amp; \ quad {} \\ \ quad {} & amp; \ mathbf {M} ^ {- 1} \ quad {} & amp; \ vline & amp; - \ mathbf {M} ^ {- 1} \ vec {T} \\ \ quad {} & amp; \ quad {} & amp; \ vline & amp; \ quad {} \\ \ hline \ quad {} & amp; ; 0 \ quad {} & amp; \ vline & amp; 1 \ end {matrix} \: \; \ right] = \ left [\: \; \ begin {matrix} M_ {11} ^ {- 1} & amp; M_ {12} ^ {- 1} & amp; M_ {13} ^ {- 1} & amp; \ vline \, & amp; \ \ mathbf {M} ^ {- 1} T_x \\ M_ {21} ^ {- 1} & amp; M_ {22} ^ {- 1} & amp; M_ {23} ^ {- 1} & amp; \ vline \, & amp; \ mathbf {M} ^ {- 1} T_y \\ M_ {31} ^ {- 1} & amp; M_ {32} ^ {- 1} & amp; M_ {33} ^ {- 1} & amp; \ vline \, & amp; - \ mathbf {M} ^ {- 1} T_z \\ \ hline 0 & amp; 0 & amp; 0 & amp; \ vline \, & amp; 1 \\ \ end {matrix} \: \; \ right],

Transpose the inverse matrix:

{\ left (\ mathbf {F} ^ {- 1} \ right) ^ \ mathrm {T}} = \ left [\: \; \ begin {matrix} \ quad {} & amp; \ quad {} & amp; \ vline & amp; \ quad {} \\ \ quad {} & amp; {\ left (\ mathbf {M} ^ {- 1} \ right) {} ^ \ mathrm {T}} \ quad {} & amp; \ vline & amp; 0 \\ \ quad {} & amp; \ quad {} & amp; \ vline & amp; \ quad {} \\ \ hline \ quad {} & amp; - \ mathbf {M} ^ {- 1} \ vec {T} \ quad {} & amp; \ vline & &; 1 \ end {matrix} \: \; \ right],

Can see that D - \ vec {N} \ cdot \ mathbf {M} ^ {- 1} \ vec {T} from Expression (5) is the result of multiplying the fourth row of the transposed inverse transformation matrix by a four-dimensional vector in a uniform coordinate record \ langle N_x, N_y, N_z, D \ rangle i.e. for the plane \ vec {C} = \ langle \ vec {N}, D \ rangle , its image with spatial transformation, described by 4x4 transformation matrix \ mathbf {F} expressed:

\ vec {C} {^ '} = {\ left (\ mathbf {F} ^ {- 1} \ right) {} ^ \ mathrm {T}} \ vec {C}.

For further, it is essential that the plane obeys the covariant character of the spatial transformation.

Perspective Projection


Perspective projection is used to create a sense of depth for the observer on the projection plane , the perspective transformation matrix should map the visibility pyramid space to the normalized visibility cube space. The visibility pyramid can usually be expressed in terms of top, bottom, left, right, far, near or fovy, aspect, near, far, some OpenGL implementations have among their tools work tools for right-handed and left-handed coordinate systems. The differences and the order of multiplication of the matrix by the vector in each of the systems should be clear to any programmer.
frustum


Fig . 3. The truncated pyramid of visibility (frustum) in computer graphics systems cuts the field of view, for the purposes of subsequent rendering, from the sides and along the projection axes. The pyramid of visibility, in the space of the camera view, is located in the right-hand coordinate system so that the top of the pyramid lies in the center of the coordinate system, and the direction of the view from the camera is opposite to the axis z , the foreground is at a distance n along the negative axis direction z , distant plan - at a distance f along the negative axis direction z .

In general, the visibility pyramid does not need to have the shape of a regular truncated pyramid, it can be asymmetric, so the model with top, bottom, left, right, far, near is more suitable for illustrating the features of oblique frustum ("oblique visibility pyramid"). "Compressed space" cube visibility , closed in a volume bounded by planes x = \ pm 1, y = \ pm 1, z = \ pm 1 , for uniformity with English terminology, we will hereinafter be called the clip space .
To organize the cutting off of parts of objects in the original visibility pyramid, we need to modify the perspective projection matrix used in our model. Matrix OpenGL programmers can find such matrix parameters on the main OpenGL documentation website, Flash programmers (AS3) will most likely turn to the PerspectiveMatrix3D class, Direct3D programmers have their own sources , writing for android will find everything you need in the android.opengl.Matrix class, and t .d It is not excluded that someone, having understood the basic idea, will prefer to expand his own class of perspective transformation with additional functionality.

The point from the visibility pyramid space of the view camera is mapped into the clip space of the canonical cube, for example, with the following 4x4-matrix transformation (we use the perspective transformation matrix generated by glFrustum () - the OpenGL function):

\ vec {P} {^ '} = \ mathbf {M} _ {frustum} \ vec {P} = \ begin {bmatrix} \ frac {2n} {rl} & amp; 0 & amp; \ frac {r + l} {rl} & amp; 0 \\ [0.3em] 0 & amp; \ frac {2n} {t-b} & amp; \ frac {t + b} {t-b} & amp; 0 \\ [0.3em] 0 & amp; 0 & amp; - \ frac {f + n} {f-n} & amp; - \ frac {2nf} {f-n} \\ [0.3em] 0 & amp; 0 & amp; -1 & amp; 0 \ end {bmatrix} \! \! \ Begin {bmatrix} P_x \\ P_y \\ P_z \\ 1 \ end {bmatrix}.

With this conversion, \ mathit {w} - coordinate of the transformed point in the homogeneous space of the clip has a sign opposite to the sign \ mathit {z} coordinates of a point in the space of the camera view.

The features of the distortion of the space by the standard transformation matrix are visible from Fig.4:
\ mathit {z} -coordinate from the space of the pyramid of visibility is reflected in the range [-1, 1] NDC, and the infinite range behind the far plan of the pyramid of visibility from the view camera is compressed into a finite gap \ left [1, \ tfrac {f + n} {f-n} \ right] inside the NDC; the finite distance from the camera to the middle plane along the Z axis extends to an infinite gap ] {- \ infty}, -1] NDC; and the points along the Z axis, which are up to the camera, are reflected in the range \ left [\ tfrac {f + n} {f-n}, \ infty \ right [ .

Normalized Device Coordinates


Fig . 4. Reflection \ mathit {z} - coordinates of a point from the camera space of the view to the space of normalized device coordinates (NDC - normalized device coordinates ).

Replacing the foreground of the pyramid of visibility by the cut-off plane, we must preserve the main features of the perspective transformation matrix, \ mathit {z} -coordinate point lying on the modified near plane, in normalized coordinates of the device (NDC) should remain equal to -1. All further observations are universal for any reversible projection matrices, and the use of the projection matrix from Expression (10) serves only to illustrate the general process of modifying the transformation matrix.

If a \ vec {C} {^ '} is one of the planes bounding the clip space, and at the same time the transformation matrix \ mathbf {M} is a projection matrix from the camera space to the clip space, it’s not difficult to map this plane \ vec {C} {^ '} into the camera space from the clip space by means of the transposed matrix \ mathbf {M} ^ \ mathrm {T} , which obviously follows from Expression (9):

\ vec {C} = \ left [\ left (\ mathbf {M} ^ {- 1} \ right) {} ^ {- 1} \ right] {} ^ \ mathrm {T} \ vec {C} {^ '} = \ mathbf {M} ^ \ mathrm {T} \ vec {C} {^'}

Modifying the near plan of the pyramid of visibility


First, extract from an arbitrary projection matrix \ mathbf {M} four-dimensional vectors corresponding to the six clipping planes of the visibility pyramid. Eric Lengel proceeded from the fact that the planes in the clip space are always the same: the normal of any plane is parallel to one of the main coordinate axes.

Figure 5 shows the elements “ x - z » Three -dimensional slice four-dimensional homogeneous space of the clip. Inside this slice \ mathit {w} -coordinate of any point is equal to 1, thus, and \ mathit {w} -coordinate of each plane is equal to 1, and, of course, one of x -, y -, or z -coordinates are equal to ± 1, which is reflected in Table 1. To understand Table 1, you need to take another careful look at Expression (11): the sum of some two columns of the matrix \ mathbf {M} ^ \ mathrm {T} nothing more than the sum of the corresponding two rows of the matrix \ mathbf {M} .

slice of homogeneous clip space


Fig . 5. Normal vectors for left, right, near and far planes bounding the homogeneous cubic space of a clip. Normal vectors for the upper and lower planes of the clip space are directed from and to the observer.

Tab . 1. The relationship between the coordinates of the clip space and the space of the truncated visibility pyramid of the view camera. Projection matrix \ mathbf {M} translates the camera space of the view into the space of the clip, and the designation \ mathbf {M} _i presents i -th row of the matrix \ mathbf {M} .
\ begin {center} \ begin {array} {rcc} \ hline \ textbf {Frustum plane} & amp; \ textbf {Clip-space} & amp; \ textbf {Camera-space} \\ \ hline \ text {Near} \ qquad {} & amp; \ langle 0, 0, 1,1 \ rangle & amp; \ vec {M} _4 + \ vec {M} _3 \\ \ text {Far} \ qquad {} & amp; \ langle 0, 0, -1,1 \ rangle & amp; \ vec {M} _4 - \ vec {M} _3 \\ \ text {Left} \ qquad {} & amp; \ langle 1, 0, 0.1 \ rangle & amp; \ vec {M} _4 + \ vec {M} _1 \\ \ text {Right} \ qquad {} & amp; \ langle -1, 0, 0.1 \ rangle & amp; \ vec {M} _4 - \ vec {M} _1 \\ \ text {Bottom} \ qquad {} & amp; \ langle 0, 1, 0,1 \ rangle & amp; \ vec {M} _4 + \ vec {M} _2 \\ \ text {Top} \ qquad {} & amp; \ langle 0, -1, 0,1 \ rangle & amp; \ vec {M} _4 - \ vec {M} _2 \\ \ hline \ end {array} \ end {center}

Let be \ vec {C} = \ langle C_x, C_y, C_z, C_w \ rangle - some plane shown in Fig. 6, in the coordinate space of the camera of the form, by means of which we intend to limit our geometry. The camera is located on the negative side of the plane (from the side opposite to the direction of the plane vector), therefore C_w & lt; 0 . It is with this plane that we intend to replace the near plan of the pyramid of visibility, therefore, in accordance with the ratios of Table 1, for \ vec {C} must be performed:

\ vec {C} = \ vec {M} _4 + \ vec {M} _3.

We cannot modify the fourth row of the matrix of the perspective projection, since it is used to reflect negative z - coordinates in w - coordinate and necessary for the further correct operation of the graphic conveyor. However, with the second component of the right side of Expression (12), we can do more freely:

\ vec {M} _3 {^ '} = \ vec {C} - \ vec {M} _4.

cut clip space

Fig . 6. Replacing the near plane of the pyramid of visibility by the plane \ vec {C} .

Since, according to Table 1, the third row of the projection matrix is ​​included in the expression for the distant plan of the visibility pyramid, it is obvious that its modification should be taken into account for the distant plan :

\begin{array}{|c|}\hline
\;\\
\vec{F}=\vec{M}_4-\vec{M}_3^'}\;\\[0.3em]   
\qquad {}=2\vec{M}_4-\vec{C}\;\\[1em]
\hline \end{array}\:.

And this result is a noticeable problem for a perspective projection: since \ vec {M} _4 = \ langle0, 0, -1, 0 \ rangle , then the background and the foreground of the pyramid of visibility cease to be parallel, in the case of non-zero values ​​for C_x and C_y . Moreover, the shape of a truncated pyramid becomes extremely undesirable in the subsequent rendering: consider some point \ vec {P} = \ langle x, y, 0, w \ rangle for which is performed \ vec {C} \ cdot \ vec {P} = 0 and this entails equality to zero and \ vec {F} \ cdot \ vec {P} , from which we must conclude that our new near and far plans will intersect in a manner similar to that shown in Fig . 7 (a)

The projection of the depth of the point, which previously reached a maximum in the background, and which we need for the process of graphic rasterization, is no longer a projection along the axis z rather, it becomes a value depending on the position between the near and far plans. The dependence of the projection depth on the direction inside the visibility pyramid will seriously affect the correctness of the depth buffer values. However, this undesirable effect can be reduced to a level acceptable for the task of rasterization, reducing the angle between near and far plans to the minimum possible. Like any plane, a plane \ vec {C} it is possible to scale, and this property of it is most welcome in our case. Scaling plane \ vec {C} will affect the orientation of the far plan \ vec {F} , so we only need to adjust the scaling factor so as to minimize the angle between \ vec {C} and \ vec {F} without prejudice to the contents of the scene inside the visibility pyramid as shown in Fig . 7 (b).
modified far plane

Fig . 7. (a) The intersection of the amended in accordance with Expression (14) of the far plan \ vec {F} with modified foreground \ vec {C} at " x - y "-Planes. (b) Scaling the Middle Plan \ vec {C} by parameter \ alpha , introduced by Expression (17) changes the angle between the near and far plan to the minimum possible, without damaging the initial type of truncation. The shaded area refers to the amount of space that is not truncated.

Let be \ vec {C} {^ '} = \ left (\ mathbf {M} ^ {- 1} \ right) {} ^ \ mathrm {T} \ vec {C} is a projection of the new middle plane in the clip space ( \ mathbf {M} - the original projection matrix). Angle \ vec {Q} ^ ' inside the visibility pyramid, lying opposite the plane \ vec {C} will have the following coordinates:

\ vec {Q} {^ '} = \ langle sgn (C {^'} _x), sgn (C {^ '} _y), 1, 1 \ rangle.

For most perspective projections, component signs C {^ '} _x and C {^ '} _y the transformed planes coincide with the signs of the corresponding components C _x and C _y that allows us to use the signs of the coordinate decomposition of the original plane.
Having components of a converted angle \ vec {Q} {^ '} , we can already calculate the components of the original angle \ vec {Q} lying opposite the plane \ vec {C} , as \ vec {Q} = \ mathbf {M} {^ {- 1}} \ vec {Q} {^ '} . In the normal visibility pyramid, point \ vec {Q} at the apex of the angle formed by the intersection of two side planes and the far plane, lying opposite the plane \ vec {C} that is farthest from the plane \ vec {C} point.

To our distant plan contained a point \ vec {Q} condition must be met \ vec {F} \ cdot \ vec {Q} = 0 Let's add Expression (14) scaling the plane \ vec {C} a factor \ alpha

\ vec {F} = 2 \ vec {M} _4- \ alpha \ vec {C}

and we find from the condition \ vec {F} \ cdot \ vec {Q} = 0 scaling factor:

\ alpha = \ frac {2 \ vec {M} _4 \ cdot \ vec {Q}} {\ vec {C} \ cdot \ vec {Q}.

Replacement \ vec {C} on \ alpha \ vec {C} in Expression (13)

\ vec {M} _3 {^ '} = \ alpha \ vec {C} - \ vec {M} _4

and allow us to optimally orient the distant plan of the pyramid of visibility, as shown in Fig . 7 (b) (this replacement technique works correctly and for the visibility pyramid, the far plane of which is removed to infinity, - the case of an infinite projection matrix , - for this it is enough to require that the far plane be parallel to one of the two generators opposite to the plane \ vec {C} angle of faces).

Practical use of the observations made above


All theoretical studies that have been done earlier extend to any reversible projection matrices, but since, as an example, the standard for OpenGL matrix in Expression (10) has already been used, it is logical to continue the chain of examples with it.

The inverse matrix to it will look like this:

\ mathbf {M} {^ {- 1}} = \ begin {bmatrix} \ frac {r-l} {2n} & amp; 0 & amp; 0 & amp; \ frac {r + l} {2n} \\ [0.3em] 0 & amp; \ frac {tb} {2n} & amp; 0 & amp; \ frac {t + b} {2n} \\ [0.3em ] 0 & amp; 0 & amp; 0 & amp; -1 \\ [0.3em] 0 & amp; 0 & amp; \\ frac {f-n} {2nf} & amp; \ frac {f + n} {2nf} \ end {bmatrix}

Get the value for the third row of the modified projection matrix, as suggested by Expression (18) with \ alpha from Expression (17):

\ vec {M} _3 {^ '} = {\ frac {2 \ vec {M} _4 \ cdot \ vec {Q}} {\ vec {C} \ cdot \ vec {Q}} \ vec {C} - \ vec {M} _4

Insofar as \ vec {M} _4 = \ langle 0, 0, -1, 0 \ rangle then this expression can be written as

\ vec {M} _3 {^ '} = {\ frac {-2Q_z} {\ vec {C} \ cdot \ vec {Q}} \ vec {C} + \ langle 0, 0, 1, 0 \ rangle

Multiplying the inverse matrix from Expression (19) by \ vec {Q} {^ '} from Expression (15), we get \ vec {Q} :

\ vec {Q} = \ begin {bmatrix} sgn (C_x) \ frac {rl} {2n} + \ frac {r + l} {2n} \\ [0.5em] sgn (C_x) \ frac {tb} { 2n} + \ frac {t + b} {2n} \\ [0.5em] -1 \\ [0.3em] 1 / f \ end {bmatrix}

To ensure the correctness of the developed method of modifying the projection matrix, consider the special case of the location of the cut-off plane \ vec {C} perpendicular to the axis z i.e. parallel to the normal near plan of the pyramid of visibility, - in the coordinate record such a plane will look like \ vec {C} = \ langle 0, 0, -1, -d \ rangle where d - some positive distance. It is natural to expect that in the new projection matrix for the visibility pyramid, whose near-plan is removed at a distance d from the camera, the far plan will remain in its previous position.

Scalar product \ vec {C} \ cdot \ vec {Q} for such a plane will be equal 1-d / f , and Expression (21) to calculate the third row of the modified projection matrix will result in

\ begin {array} {l} \ vec {M} _3 {^ '} = {\ frac {2} {1-d / f}} \ langle 0, 0, -1, -d \ rangle + \ langle 0, 0, 1, 0 \ rangle \\ [0.3em] \ qquad {} = \ langle 0, 0, - \ frac {f + d} {fd}, - \ frac {2fd} {fd} \ rangle \ end { array}

- result, coincided with the expectations: with d = n the third row of the modified matrix coincides with the third row of the projection matrix from Expression (10).

As already assumed above, one should expect that the process of rasterization will not be as usual as in the case of the unmodified visibility pyramid. The full range of depth buffer values ​​will not be achieved along different directions within the visibility pyramid due to a change in the pyramid geometry. Take a vector of arbitrary direction \ vec {V} = \ langle V_x, V_y, V_z, 0 \ rangle in the space of the camera of the kind for which V_z & lt; 0 and examine normalized z -coordinate point \ langle 0, 0, 1, 0 \ rangle + s \ vec {V} located inside the visibility pyramid:

\ begin {array} {l} z (s) = {\ cfrac {(\ mathbf {M} {^ '} \ langle sV_x, sV_y, sV_z, 1 \ rangle) _z} {(\ mathbf {M} {^ '} \ langle sV_x, sV_y, sV_z, 1 \ rangle) _w}} \\ [0.8em] \ qquad {} = {\ cfrac {(\ alpha \ vec {C} - \ vec {M} _4) \ cdot \ langle sV_x, sV_y, sV_z, 1 \ rangle} {\ vec {M} _4 \ cdot \ langle sV_x, sV_y, sV_z, 1 \ rangle}} \ end {array},

Where \ alpha -scaling factor introduced by Expression (17). For \ vec {M} _4 = \ langle 0, 0, -1, 0 \ rangle . Expression (24) becomes

\ begin {array} {l} z (s) = \ cfrac {\ alpha sC_xV_x + \ alpha sC_yV_y + \ alpha sC_zV_z + sV_z + \ alpha C_w} {- sV_z} \\ [0.8em] \ qquad {} = \ cfrac {\ alpha s (\ vec {C} \ cdot \ vec {V}) + sV_z + \ alpha C_w} {- sV_z} \ end {array}.

We assume that the scalar product \ vec {C} \ cdot \ vec {V} \ geq 0 because otherwise the point \ langle 0, 0, 1, 0 \ rangle + s \ vec {V} would lie outside the pyramid of visibility. Consider the situation when s tends to infinity:

\ lim_ {s \ to \ infty} z (s) = - \ cfrac {\ alpha (\ vec {C} \ cdot \ vec {V}) + V_z} {V_z},

the resulting expression indicates the maximum attainable value of the normalized z coordinates in the direction \ vec {V} .
Investigate the direction \ vec {V} = \ langle 0, 0, -1, 0 \ rangle along the direct view from the camera position: the limit value indicated by Expression (26) is less than one if the condition is met \ alpha C_z & gt; -2 . In this case, z -coordinate far plan \ vec {F} , given by Expression (16), is less than zero, and the far plan is not a plane limiting the scope of the pyramid of visibility. Since the far plan may not be reachable along the direction \ vec {V} , the range of normalized values ​​for the depth buffer may be significantly narrower than in the case of the ordinary visibility pyramid.

A good practice for a programmer would be, before approving the chosen spatial model for further work, to investigate the behavior of the normalized coordinate inside the modified ( oblique | oblique frustum) visibility pyramid. Having detected problem areas in time, he can correct either the position of the camera or change the inclination angle of the section plane so as to simplify the work of the depth buffer, bringing it as close as possible to the normal mode. The cost of such an action will not be particularly significant, but the result will calm the perfectionist.
Continuing to exploit the standard projection matrix from Expression (10), it is easy to proceed to the next example of its use for studying the values ​​of normalized z Coordinates inside a modified visibility pyramid:

Let us assume that the plane, which cuts off geometry that we do not like, and with which we substitute the near plan of the visibility pyramid, is represented in guide cosines as \ vec {C} = \ langle C_x, C_y, -C_z, -d \ rangle and, in our particular case, C_x, C_y, C_z have positive values ​​(the plane lies opposite the upper right corner of the pyramid), while C_z - angle between negative axis direction z and the normal vector of our plane. Consider changing the normalized z Coordinates along the negative axis direction z depending on the angle between the normal vector of the plane of the near plane and the negative direction of the axis z , and the distance from the camera to the near plan .

Scalar product \ vec {C} \ cdot \ vec {Q} for this case, taking into account Expression (22) will give the following result:

\ vec {C} \ cdot \ vec {Q} = \ cfrac {rfC_x + tfC_y-n (d-fC_z)} {nf},

and the third row of the transformed projection matrix will take the form

\ begin {array} {l} \ vec {M} _3 {^ '} = {\ cfrac {2nf \ langle C_x, C_y, -C_z, -d \ rangle} {rfC_x + tfC_y-n (d-fC_z)} + \ langle 0, 0, 1, 0 \ rangle} \ end {array}.

Consider the behavior of the normalized coordinate. z in the direction of the front view from the camera for points \ vec {P} = \ langle 0, 0, P_z, 1 \ rangle from a range of values P_z \ in [-n, -f] : normalized z Coordinate for this case will be

z = {\ frac {P_z ^ '} {P_w ^'}} = - \ cfrac {(rfC_x + tfC_y-n (d + fC_z)) P_z-2nfd} {(rfC_x + tfC_y-n (d-fC_z)) P_z} \; .

The last expression is quite suitable for the purposes of numerical research. An example of such a study can be seen in Fig. 8. The range of normalized values ​​intended for the depth buffer narrows greatly with increasing angle between the normal of the plane and the negative direction of the axis. z , also significantly degrade the accuracy of the depth buffer can move the near plan in the direction from the camera to the far plan (it is known that too close to the camera near the plan also adversely affects the values ​​of the normalized coordinates).
modified far plane

Fig . 8. Narrowing the range of normalized z -coordinates in the direction of the projection \ vec {V} = \ langle 0, 0, -1, 0 \ rangle , depending on the angle between the normal vector of the near (section) plane and the negative direction of the axis z , and the distance from the camera to the near (secant) plane. Values ​​close to 1 correspond to the situation when the spatial point under study is located near the modified far plan , while the near plan is far enough from the far plan and its normal vector is slightly deviated from the negative axis direction. z . With an increase in the angle and movement of the near plan in the direction from the camera, the range of normalized values z -coordinates are narrowed down to values ​​unsuitable for the operation of most depth buffers .

Conclusion


The programmer in the process of developing an application often has to find a compromise between speed and realistic rendering. The technique outlined in this article allows not only to achieve maximum performance on the widest range of devices, but also helps to determine the situation in which it is desirable to edit the scene for its most favorable display.

Combining the processes of modifying the pyramid of visibility and testing the operation of the depth buffer for the modified pyramid (or taking into account the observations made above) is the key to the high quality of the final result of the hard work of the programmer.


Literature


[one] Eric Lengyel, Oblique View Frustum Depth Projection and Clipping . Journal of Game Development, Vol. 1, No. 2 (March 2005), pp. 5–16.

[2] Eric Lengyel, Mathematics for 3D Game Programming and Computer Graphics . Charles River Media, 2002, p. 103

Source: https://habr.com/ru/post/338434/


All Articles