Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormano-wii <germano.costa@ig.com.br>2019-03-22 16:32:57 +0300
committermano-wii <germano.costa@ig.com.br>2019-03-22 16:35:17 +0300
commit6c4b9dd9636a55037d4d5abbe3c905c7a8ce09fd (patch)
tree1518f6720328862e0f79bf5cc9e1741068f76f76 /source/blender/gpu
parentf6a6770f06ee8811493123cc8f66b0753c74a2f8 (diff)
Style: describe code using comments instead of preprocessing directives.
Diffstat (limited to 'source/blender/gpu')
-rw-r--r--source/blender/gpu/intern/gpu_matrix.c75
1 files changed, 39 insertions, 36 deletions
diff --git a/source/blender/gpu/intern/gpu_matrix.c b/source/blender/gpu/intern/gpu_matrix.c
index 5ed0f2ee406..9f0a5851390 100644
--- a/source/blender/gpu/intern/gpu_matrix.c
+++ b/source/blender/gpu/intern/gpu_matrix.c
@@ -451,6 +451,43 @@ void GPU_matrix_project(const float world[3], const float model[4][4], const flo
win[2] = (v[2] + 1) * 0.5f;
}
+/**
+ * The same result could be obtained as follows:
+ *
+ * \code{.c}
+ * float projinv[4][4];
+ * invert_m4_m4(projinv, projmat);
+ * co[0] = 2 * co[0] - 1;
+ * co[1] = 2 * co[1] - 1;
+ * co[2] = 2 * co[2] - 1;
+ * mul_project_m4_v3(projinv, co);
+ * \endcode
+ *
+ * But that solution loses much precision.
+ * Therefore, get the same result without inverting the matrix.
+ */
+static void gpu_mul_invert_projmat_m4_unmapped_v3(const float projmat[4][4], float co[3])
+{
+ float left, right, bottom, top, near, far;
+ bool is_persp = projmat[3][3] == 0.0f;
+
+ projmat_dimensions(
+ projmat, &left, &right, &bottom, &top, &near, &far);
+
+ co[0] = left + co[0] * (right - left);
+ co[1] = bottom + co[1] * (top - bottom);
+
+ if (is_persp) {
+ co[2] = far * near / (far + co[2] * (near - far));
+ co[0] *= co[2];
+ co[1] *= co[2];
+ }
+ else {
+ co[2] = near + co[2] * (far - near);
+ }
+ co[2] *= -1;
+}
+
bool GPU_matrix_unproject(const float win[3], const float model[4][4], const float proj[4][4], const int view[4], float world[3])
{
float in[3];
@@ -467,43 +504,9 @@ bool GPU_matrix_unproject(const float win[3], const float model[4][4], const flo
in[0] = (in[0] - view[0]) / view[2];
in[1] = (in[1] - view[1]) / view[3];
-#if 0
- float projinv[4][4];
-
- if (!invert_m4_m4(projinv, proj)) {
- zero_v3(world);
- return false;
- }
-
- /* Map to range -1 to +1 */
- in[0] = 2 * in[0] - 1;
- in[1] = 2 * in[1] - 1;
- in[2] = 2 * in[2] - 1;
-
- mul_project_m4_v3(projinv, in);
-#else
- float left, right, bottom, top, near, far;
- bool is_persp = proj[3][3] == 0.0f;
-
- projmat_dimensions(
- proj, &left, &right, &bottom, &top, &near, &far);
-
- in[0] = left + in[0] * (right - left);
- in[1] = bottom + in[1] * (top - bottom);
-
- if (is_persp) {
- in[2] = far * near / (far + in[2] * (near - far));
- in[0] *= in[2];
- in[1] *= in[2];
- }
- else {
- in[2] = near + in[2] * (far - near);
- }
-
- in[2] *= -1;
-#endif
-
+ gpu_mul_invert_projmat_m4_unmapped_v3(proj, in);
mul_v3_m4v3(world, viewinv, in);
+
return true;
}