Detailed Implementation of viewing frustum in DirectX

Source: Internet
Author: User
Copyright:Original Works are allowed to be reproduced. During reprinting, be sure to mark them as hyperlinks.ArticleOriginal source, author information, and this statement. Otherwise, legal liability will be held. Http://xiaoxiang.blog.51cto.com/88051/26928
Most of this article is translated from the article Fast Extraction of viewing frustum planes from the world-view-projection matrix co-authored by Gil gribb and Klaus Hartmann, if you are interested, you can search and view the original text. The implementation process under DirectX and OpenGL is clear. Here we only talk about DirectX. Here we will introduce Algorithm You can calculate the six sides of viewing frustum directly from the world, observation, and projection matrix. It is fast and accurate, and allows us to quickly determine frustum planes in camera space, world space, or object space. First, we start with the projection matrix (Project), that is, assuming that the world matrix and the observation matrix (View) are all units of the matrix. This means that the camera is located at the origin point in the world coordinate system and is facing the positive direction of the Z axis. Defines a vertex v (x y z w = 1) and a 4*4 projection matrix m = m (I, j ), then, we use this matrix m to convert vertex v. the converted vertex is V '= (x 'y' Z' W'), which can be written as follows: After conversion, viewing frustum is actually a box parallel to the axis. If vertex v is in this box, the vertex v before conversion is in the viewing frustum before conversion. In direct3d, if the following inequalities are true, then V' is in this box.
-W' <x' <W'-W' <y' <w'0 <Z' <W': The following conclusions can be obtained, we need to test whether x' is in the left half of the space. Based on the above table, we can determine whether-W' <x' is true. With the information we mentioned, you can write the inequality as follows:-(V * col4) <(V * col1) that is: 0 <(V * col4) + (V * col1) Get the final form: 0 <v * (col1 + col4) writes here, in fact, it is already equivalent to describing the plane equation of the Left cropping surface of viewing frustum before conversion: x * (m14 + m11) + y * (M24 + m21) + z * (m34 + m31) + W * (m44 + M41) = 0 when w = 1, we can simply take the following form: x * (m14 + m11) + y * (M24 + m21) + z * (m34 + m31) + (m44 + M41) = 0 this gives a basic plane equation: AX + by + cz + D = 0 where, A = (m14 + m11), B = (M24 + M21), c = (m34 + m31), D = (m44 + M41) OK, right click here to crop the surface on the left. Repeat the preceding steps to derive several other cropping surfaces. For details, see the following table: the final plane equations are all ununitized (the normal vector of the plane is not the unit vector), and the normal vector points to the interior of the space. That is to say, if we want to judge that V is inside the space, all the six surfaces must satisfy the requirements of AX + by + cz + D> 0. So far, we have assumed the world matrix (World) and the observation matrix (View) are all unitized matrices. However, this algorithm does not want to be restricted by such conditions, but can be used under any conditions. In fact, this is not complicated, and it is incredibly simple. If you think about it, you will understand it immediately, so we will not explain it in detail. The following three conclusions are given: 1. if the matrix m is equal to the projection matrix P (M = P), the cropping surface provided by the algorithm is in the camera space (CAMERA space) 2. if M is equal to the combination of observation matrix V and projection matrix P (M = V * P), the cropping surface provided by the algorithm is in the World Space (World Space) 3. if the matrix m is equal to the world matrix W, observe the combination of matrix V and projection matrix P (M = W * V * P ), the cropping surface provided by the algorithm is in the object space. So far, all the theoretical knowledge has been completed. The specific implementation is given below.Code : ============================== Frustum. h ==============================## ifndef _ frustumh __
# DEFINE _ frustumh _ # include <d3dx9. h> class frustum
{
Public: frustum ();
~ Frustum (); // call this every time the camera moves to update the frustum
Void calculatefrustum (d3dxmatrix viewmatrix, d3dxmatrix projectmatrix); // This takes a 3D point and returns true if it's inside of the frustum
Bool pointinfrustum (d3dxvector3 point); Private:
// This holds the B C and D values for each side of our frustum.
D3dxplane frustumplane [6];
}; # Endif // _ frustumh ======================== frustum. CPP =========================# include "frustum. h"
# Include <d3dx9math. h> Enum frustumside {right, left, bottom, top, front, back}; frustum: frustum ()
{
} Frustum ::~ Frustum ()
{
} // Calculate frustum \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ *
/////
//// This extracts our frustum from the projection and view matrix.
/////
/// // Calculate frustum \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ * Void frustum: calculatefrustum (d3dxmatrix viewmatrix, d3dxmatrix projectmatrix)
{
D3dxmatrix combomatrix;
D3dxmatrixmultiply (& combomatrix, & viewmatrix, & projectmatrix); // right clipping plane
Frustumplane [right]. A = combomatrix. _ 14-combomatrix. _ 11;
Frustumplane [right]. B = combomatrix. _ 24-combomatrix. _ 21;
Frustumplane [right]. c = combomatrix. _ 34-combomatrix. _ 31;
Frustumplane [right]. d = combomatrix. _ 44-combomatrix. _ 41;

// Normalize
D3dxplanenormalize (& frustumplane [right], & frustumplane [right]); // left clipping plane
Frustumplane [left]. A = combomatrix. _ 14 + combomatrix. _ 11;
Frustumplane [left]. B = combomatrix. _ 24 + combomatrix. _ 21;
Frustumplane [left]. c = combomatrix. _ 34 + combomatrix. _ 31;
Frustumplane [left]. d = combomatrix. _ 44 + combomatrix. _ 41; // Normalize
D3dxplanenormalize (& frustumplane [left], & frustumplane [left]); // bottom clipping plane
Frustumplane [bottom]. A = combomatrix. _ 14 + combomatrix. _ 12;
Frustumplane [bottom]. B = combomatrix. _ 24 + combomatrix. _ 22;
Frustumplane [bottom]. c = combomatrix. _ 34 + combomatrix. _ 32;
Frustumplane [bottom]. d = combomatrix. _ 44 + combomatrix. _ 42; // Normalize
D3dxplanenormalize (& frustumplane [bottom], & frustumplane [bottom]); // top clipping plane
Frustumplane [Top]. A = combomatrix. _ 14-combomatrix. _ 12;
Frustumplane [Top]. B = combomatrix. _ 24-combomatrix. _ 22;
Frustumplane [Top]. c = combomatrix. _ 34-combomatrix. _ 32;
Frustumplane [Top]. d = combomatrix. _ 44-combomatrix. _ 42; // Normalize
D3dxplanenormalize (& frustumplane [Top], & frustumplane [Top]); // near clipping plane
Frustumplane [Front]. A = combomatrix. _ 14 + combomatrix. _ 13;
Frustumplane [Front]. B = combomatrix. _ 24 + combomatrix. _ 23;
Frustumplane [Front]. c = combomatrix. _ 34 + combomatrix. _ 33;
Frustumplane [Front]. d = combomatrix. _ 44 + combomatrix. _ 43; // Normalize
D3dxplanenormalize (& frustumplane [Front], & frustumplane [Front]); // far clipping plane
Frustumplane [Back]. A = combomatrix. _ 14-combomatrix. _ 13;
Frustumplane [Back]. B = combomatrix. _ 24-combomatrix. _ 23;
Frustumplane [Back]. c = combomatrix. _ 34-combomatrix. _ 33;
Frustumplane [Back]. d = combomatrix. _ 44-combomatrix. _ 43; // Normalize
D3dxplanenormalize (& frustumplane [Back], & frustumplane [Back]);
} // Point in frustum \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ *
/////
//// This determines if a point is inside of the frustum
/////
//// // Point in frustum \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ * Bool frustum: pointinfrustum (d3dxvector3 point)
{
For (INT I = 0; I <6; I ++)
{
Float x = d3dxplanedotcoord (& frustumplane [I], & Point );
If (x <0)
Return false;
}
// The point was inside of the frustum (in front of all the sides of the frustum)
Return true;
}

This article is from the "Xiao Xiang" blog, please be sure to keep this source http://xiaoxiang.blog.51cto.com/88051/26928

Contact Us

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.