From 780bb88a7a5b30eaf8a62b999a30ac7bb4153ebf Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Sat, 3 Jan 2015 12:05:16 +0100 Subject: Refactor 'fit in camera view' code, and expose it to RNA. This changes BKE's fitting code to use `BKE_camera_params_compute_viewplane` instead of `BKE_camera_view_frame`. This allows that code to work with orthographic projection too. Also, two funcs were added to rna's Object, to resp. get the projection matrix of that object (mostly useful for cameras and lamps objects), and return position this object should be to see all (to fit) a given set of points. Reviewers: campbellbarton Reviewed By: campbellbarton Differential Revision: https://developer.blender.org/D961 --- source/blender/blenlib/BLI_math_geom.h | 3 ++ source/blender/blenlib/intern/math_geom.c | 53 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/BLI_math_geom.h b/source/blender/blenlib/BLI_math_geom.h index 95ab3185d1f..ee99f860a97 100644 --- a/source/blender/blenlib/BLI_math_geom.h +++ b/source/blender/blenlib/BLI_math_geom.h @@ -265,6 +265,9 @@ void orthographic_m4(float mat[4][4], const float left, const float right, void window_translate_m4(float winmat[4][4], float perspmat[4][4], const float x, const float y); +void planes_from_projmat(float mat[4][4], float left[4], float right[4], float top[4], float bottom[4], + float front[4], float back[4]); + int box_clip_bounds_m4(float boundbox[2][3], const float bounds[4], float winmat[4][4]); void box_minmax_bounds_m4(float min[3], float max[3], diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index a360148182d..59d9cf85dec 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -3104,6 +3104,59 @@ void window_translate_m4(float winmat[4][4], float perspmat[4][4], const float x } } +/** + * Frustum planes extraction from a projection matrix (homogeneous 4d vector representations of planes). + * + * plane parameters can be NULL if you do not need them. + */ +void planes_from_projmat(float mat[4][4], float left[4], float right[4], float top[4], float bottom[4], + float near[4], float far[4]) +{ + /* References: + * + * https://fgiesen.wordpress.com/2012/08/31/frustum-planes-from-the-projection-matrix/ + * http://www8.cs.umu.se/kurser/5DV051/HT12/lab/plane_extraction.pdf + */ + + int i; + + if (left) { + for (i = 4; i--; ) { + left[i] = mat[i][3] + mat[i][0]; + } + } + + if (right) { + for (i = 4; i--; ) { + right[i] = mat[i][3] - mat[i][0]; + } + } + + if (bottom) { + for (i = 4; i--; ) { + bottom[i] = mat[i][3] + mat[i][1]; + } + } + + if (top) { + for (i = 4; i--; ) { + top[i] = mat[i][3] - mat[i][1]; + } + } + + if (near) { + for (i = 4; i--; ) { + near[i] = mat[i][3] + mat[i][2]; + } + } + + if (far) { + for (i = 4; i--; ) { + far[i] = mat[i][3] - mat[i][2]; + } + } +} + static void i_multmatrix(float icand[4][4], float Vm[4][4]) { int row, col; -- cgit v1.2.3