[Bf-blender-cvs] [b49290be14d] blender2.8: Partial fix for T54491: Use a more accurate solution to calculate the frustum boundbox

Germano noreply at git.blender.org
Mon Apr 2 17:21:21 CEST 2018


Commit: b49290be14d985d4cc60b0d110b8b8ad4d543ba0
Author: Germano
Date:   Mon Apr 2 12:21:09 2018 -0300
Branches: blender2.8
https://developer.blender.org/rBb49290be14d985d4cc60b0d110b8b8ad4d543ba0

Partial fix for T54491: Use a more accurate solution to calculate the frustum boundbox

===================================================================

M	source/blender/draw/intern/draw_manager_exec.c

===================================================================

diff --git a/source/blender/draw/intern/draw_manager_exec.c b/source/blender/draw/intern/draw_manager_exec.c
index 03db49bb4a7..07df8c28aeb 100644
--- a/source/blender/draw/intern/draw_manager_exec.c
+++ b/source/blender/draw/intern/draw_manager_exec.c
@@ -384,6 +384,103 @@ void DRW_state_clip_planes_reset(void)
 /** \name Clipping (DRW_clipping)
  * \{ */
 
+/* Extract the 8 corners (world space).
+ * Although less accurate, this solution can be simplified as follows:
+ *
+ * BKE_boundbox_init_from_minmax(&bbox, (const float[3]){-1.0f, -1.0f, -1.0f}, (const float[3]){1.0f, 1.0f, 1.0f});
+ * for (int i = 0; i < 8; i++) {mul_project_m4_v3(viewprojinv, bbox.vec[i]);}
+ */
+static void draw_frustum_boundbox_calc(const float (*projmat)[4], const float (*viewinv)[4], BoundBox *r_bbox)
+{
+	float screenvecs[3][3], loc[3], near, far, w_half, h_half;
+	bool is_persp = projmat[3][3] == 0.0f;
+	copy_m3_m4(screenvecs, viewinv);
+	copy_v3_v3(loc, viewinv[3]);
+
+	/* get the values of the minimum and maximum
+	 * clipping planes distances and the height and
+	  *width of the near plane divided by half. */
+	if (is_persp) {
+		near = projmat[3][2] / (projmat[2][2] - 1.0f);
+		far = projmat[3][2] / (projmat[2][2] + 1.0f);
+		w_half = near / projmat[0][0];
+		h_half = near / projmat[1][1];
+	}
+	else {
+		near = (projmat[3][2] + 1.0f) / projmat[2][2];
+		far = (projmat[3][2] - 1.0f) / projmat[2][2];
+		w_half = 1.0f / projmat[0][0];
+		h_half = 1.0f / projmat[1][1];
+	}
+
+	/* With vectors aligned to the screen, reconstruct
+	 * the near plane from the dimensions obtained earlier. */
+	float mid[3], hor[3], ver[3];
+	mul_v3_v3fl(hor, screenvecs[0], w_half);
+	mul_v3_v3fl(ver, screenvecs[1], h_half);
+	madd_v3_v3v3fl(mid, loc, screenvecs[2], -near);
+
+	/* The case below is for non-symmetric frustum. */
+	if (is_persp) {
+		madd_v3_v3fl(mid, hor, projmat[2][0]);
+		madd_v3_v3fl(mid, ver, projmat[2][1]);
+	}
+	else {
+		madd_v3_v3fl(mid, hor, projmat[3][0]);
+		madd_v3_v3fl(mid, ver, projmat[3][1]);
+	}
+
+	r_bbox->vec[0][0] = mid[0] - ver[0] - hor[0];
+	r_bbox->vec[0][1] = mid[1] - ver[1] - hor[1];
+	r_bbox->vec[0][2] = mid[2] - ver[2] - hor[2];
+
+	r_bbox->vec[3][0] = mid[0] + ver[0] - hor[0];
+	r_bbox->vec[3][1] = mid[1] + ver[1] - hor[1];
+	r_bbox->vec[3][2] = mid[2] + ver[2] - hor[2];
+
+	r_bbox->vec[7][0] = mid[0] + ver[0] + hor[0];
+	r_bbox->vec[7][1] = mid[1] + ver[1] + hor[1];
+	r_bbox->vec[7][2] = mid[2] + ver[2] + hor[2];
+
+	r_bbox->vec[4][0] = mid[0] - ver[0] + hor[0];
+	r_bbox->vec[4][1] = mid[1] - ver[1] + hor[1];
+	r_bbox->vec[4][2] = mid[2] - ver[2] + hor[2];
+
+	/* Get the coordinates of the far plane. */
+	if (is_persp) {
+		float sca_far = far / near;
+		mid[0] = mid[0] + (mid[0] - loc[0]) * sca_far;
+		mid[1] = mid[1] + (mid[1] - loc[1]) * sca_far;
+		mid[2] = mid[2] + (mid[2] - loc[2]) * sca_far;
+
+		mul_v3_fl(hor, sca_far);
+		mul_v3_fl(ver, sca_far);
+	}
+	else {
+		madd_v3_v3v3fl(mid, loc, screenvecs[2], -far);
+
+		/* Non-symmetric frustum. */
+		madd_v3_v3fl(mid, hor, projmat[3][0]);
+		madd_v3_v3fl(mid, ver, projmat[3][1]);
+	}
+
+	r_bbox->vec[1][0] = mid[0] - ver[0] - hor[0];
+	r_bbox->vec[1][1] = mid[1] - ver[1] - hor[1];
+	r_bbox->vec[1][2] = mid[2] - ver[2] - hor[2];
+
+	r_bbox->vec[2][0] = mid[0] + ver[0] - hor[0];
+	r_bbox->vec[2][1] = mid[1] + ver[1] - hor[1];
+	r_bbox->vec[2][2] = mid[2] + ver[2] - hor[2];
+
+	r_bbox->vec[6][0] = mid[0] + ver[0] + hor[0];
+	r_bbox->vec[6][1] = mid[1] + ver[1] + hor[1];
+	r_bbox->vec[6][2] = mid[2] + ver[2] + hor[2];
+
+	r_bbox->vec[5][0] = mid[0] - ver[0] + hor[0];
+	r_bbox->vec[5][1] = mid[1] - ver[1] + hor[1];
+	r_bbox->vec[5][2] = mid[2] - ver[2] + hor[2];
+}
+
 static void draw_clipping_setup_from_view(void)
 {
 	if (DST.clipping.updated)
@@ -396,14 +493,7 @@ static void draw_clipping_setup_from_view(void)
 
 	/* Extract Clipping Planes */
 	BoundBox bbox;
-	BKE_boundbox_init_from_minmax(&bbox, (const float[3]){-1.0f, -1.0f, -1.0f}, (const float[3]){1.0f, 1.0f, 1.0f});
-
-	/* Extract the 8 corners (world space). */
-	for (int i = 0; i < 8; i++) {
-		/* Use separate matrix mul for more precision. */
-		mul_project_m4_v3(projinv, bbox.vec[i]);
-		mul_m4_v3(viewinv, bbox.vec[i]);
-	}
+	draw_frustum_boundbox_calc(projmat, viewinv, &bbox);
 
 	/* Compute clip planes using the world space frustum corners. */
 	for (int p = 0; p < 6; p++) {



More information about the Bf-blender-cvs mailing list