OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
culling.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2014-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include "culling.hpp"
22 
24 {
25  mat4 inv_view_proj = mat_inverse(view_projection);
26 
27  // Get world-space coordinates for clip-space bounds.
28  vec4 lbn = inv_view_proj * vec4(-1, -1, -1, 1);
29  vec4 ltn = inv_view_proj * vec4(-1, 1, -1, 1);
30  vec4 lbf = inv_view_proj * vec4(-1, -1, 1, 1);
31  vec4 rbn = inv_view_proj * vec4( 1, -1, -1, 1);
32  vec4 rtn = inv_view_proj * vec4( 1, 1, -1, 1);
33  vec4 rbf = inv_view_proj * vec4( 1, -1, 1, 1);
34  vec4 rtf = inv_view_proj * vec4( 1, 1, 1, 1);
35 
36  vec3 lbn_pos = vec_project(lbn);
37  vec3 ltn_pos = vec_project(ltn);
38  vec3 lbf_pos = vec_project(lbf);
39  vec3 rbn_pos = vec_project(rbn);
40  vec3 rtn_pos = vec_project(rtn);
41  vec3 rbf_pos = vec_project(rbf);
42  vec3 rtf_pos = vec_project(rtf);
43 
44  // Get plane equations for all sides of frustum.
45  vec3 left_normal = vec_normalize(vec_cross(lbf_pos - lbn_pos, ltn_pos - lbn_pos));
46  vec3 right_normal = vec_normalize(vec_cross(rtn_pos - rbn_pos, rbf_pos - rbn_pos));
47  vec3 top_normal = vec_normalize(vec_cross(ltn_pos - rtn_pos, rtf_pos - rtn_pos));
48  vec3 bottom_normal = vec_normalize(vec_cross(rbf_pos - rbn_pos, lbn_pos - rbn_pos));
49  vec3 near_normal = vec_normalize(vec_cross(ltn_pos - lbn_pos, rbn_pos - lbn_pos));
50  vec3 far_normal = vec_normalize(vec_cross(rtf_pos - rbf_pos, lbf_pos - rbf_pos));
51 
52  planes[0] = vec4(left_normal, -vec_dot(left_normal, lbn_pos)); // Left
53  planes[1] = vec4(right_normal, -vec_dot(right_normal, rbn_pos)); // Right
54  planes[2] = vec4(near_normal, -vec_dot(near_normal, lbn_pos)); // Near
55  planes[3] = vec4(far_normal, -vec_dot(far_normal, lbf_pos)); // Far
56  planes[4] = vec4(top_normal, -vec_dot(top_normal, ltn_pos)); // Top
57  planes[5] = vec4(bottom_normal, -vec_dot(bottom_normal, lbn_pos)); // Bottom
58 }
59 
Definition: matrix.h:51
float vec_dot(const T &a, const T &b)
Definition: vector_math.h:289
vec3 vec_cross(const vec3 &a, const vec3 &b)
Definition: vector_math.h:280
Definition: matrix.h:75
Definition: matrix.h:104
mat4 mat_inverse(const mat4 &a)
Definition: vector_math.h:430
void compute_frustum_from_view_projection(vec4 *planes, const mat4 &view_projection)
Definition: culling.cpp:23
T vec_normalize(const T &vec)
Definition: vector_math.h:304
vec3 vec_project(const vec4 &vec)
Definition: vector_math.h:333