summaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
Diffstat (limited to 'core/math')
-rw-r--r--core/math/basis.cpp12
-rw-r--r--core/math/basis.h3
-rw-r--r--core/math/camera_matrix.cpp58
-rw-r--r--core/math/camera_matrix.h19
-rw-r--r--core/math/vector3.cpp4
-rw-r--r--core/math/vector3.h10
-rw-r--r--core/math/vector3i.cpp55
-rw-r--r--core/math/vector3i.h272
8 files changed, 431 insertions, 2 deletions
diff --git a/core/math/basis.cpp b/core/math/basis.cpp
index ddf5f13d55..14079f811d 100644
--- a/core/math/basis.cpp
+++ b/core/math/basis.cpp
@@ -244,6 +244,18 @@ void Basis::scale_local(const Vector3 &p_scale) {
*this = scaled_local(p_scale);
}
+float Basis::get_uniform_scale() const {
+ return (elements[0].length() + elements[1].length() + elements[2].length()) / 3.0;
+}
+
+void Basis::make_scale_uniform() {
+ float l = (elements[0].length() + elements[1].length() + elements[2].length()) / 3.0;
+ for (int i = 0; i < 3; i++) {
+ elements[i].normalize();
+ elements[i] *= l;
+ }
+}
+
Basis Basis::scaled_local(const Vector3 &p_scale) const {
Basis b;
b.set_diagonal(p_scale);
diff --git a/core/math/basis.h b/core/math/basis.h
index 6c3a939d70..0261cf67c6 100644
--- a/core/math/basis.h
+++ b/core/math/basis.h
@@ -108,6 +108,9 @@ public:
void scale_local(const Vector3 &p_scale);
Basis scaled_local(const Vector3 &p_scale) const;
+ void make_scale_uniform();
+ float get_uniform_scale() const;
+
Vector3 get_scale() const;
Vector3 get_scale_abs() const;
Vector3 get_scale_local() const;
diff --git a/core/math/camera_matrix.cpp b/core/math/camera_matrix.cpp
index 380bae871a..c4981b954b 100644
--- a/core/math/camera_matrix.cpp
+++ b/core/math/camera_matrix.cpp
@@ -276,6 +276,36 @@ Vector2 CameraMatrix::get_viewport_half_extents() const {
return Vector2(res.x, res.y);
}
+void CameraMatrix::get_far_plane_size(real_t &r_width, real_t &r_height) const {
+
+ const real_t *matrix = (const real_t *)this->matrix;
+ ///////--- Far Plane ---///////
+ Plane far_plane = Plane(matrix[3] - matrix[2],
+ matrix[7] - matrix[6],
+ matrix[11] - matrix[10],
+ -matrix[15] + matrix[14]);
+ far_plane.normalize();
+
+ ///////--- Right Plane ---///////
+ Plane right_plane = Plane(matrix[3] - matrix[0],
+ matrix[7] - matrix[4],
+ matrix[11] - matrix[8],
+ -matrix[15] + matrix[12]);
+ right_plane.normalize();
+
+ Plane top_plane = Plane(matrix[3] - matrix[1],
+ matrix[7] - matrix[5],
+ matrix[11] - matrix[9],
+ -matrix[15] + matrix[13]);
+ top_plane.normalize();
+
+ Vector3 res;
+ far_plane.intersect_3(right_plane, top_plane, &res);
+
+ r_width = res.x;
+ r_height = res.y;
+}
+
bool CameraMatrix::get_endpoints(const Transform &p_transform, Vector3 *p_8points) const {
Vector<Plane> planes = get_projection_planes(Transform());
@@ -485,6 +515,12 @@ void CameraMatrix::invert() {
}
}
+void CameraMatrix::flip_y() {
+ for (int i = 0; i < 4; i++) {
+ matrix[1][i] = -matrix[1][i];
+ }
+}
+
CameraMatrix::CameraMatrix() {
set_identity();
@@ -506,6 +542,28 @@ CameraMatrix CameraMatrix::operator*(const CameraMatrix &p_matrix) const {
return new_matrix;
}
+void CameraMatrix::set_depth_correction(bool p_flip_y) {
+
+ real_t *m = &matrix[0][0];
+
+ m[0] = 1;
+ m[1] = 0.0;
+ m[2] = 0.0;
+ m[3] = 0.0;
+ m[4] = 0.0;
+ m[5] = p_flip_y ? -1 : 1;
+ m[6] = 0.0;
+ m[7] = 0.0;
+ m[8] = 0.0;
+ m[9] = 0.0;
+ m[10] = 0.5;
+ m[11] = 0.0;
+ m[12] = 0.0;
+ m[13] = 0.0;
+ m[14] = 0.5;
+ m[15] = 1.0;
+}
+
void CameraMatrix::set_light_bias() {
real_t *m = &matrix[0][0];
diff --git a/core/math/camera_matrix.h b/core/math/camera_matrix.h
index 2eed6d25d6..60f7d15974 100644
--- a/core/math/camera_matrix.h
+++ b/core/math/camera_matrix.h
@@ -50,6 +50,7 @@ struct CameraMatrix {
void set_identity();
void set_zero();
void set_light_bias();
+ void set_depth_correction(bool p_flip_y = true);
void set_light_atlas_rect(const Rect2 &p_rect);
void set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov = false);
void set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov, int p_eye, real_t p_intraocular_dist, real_t p_convergence_dist);
@@ -74,6 +75,7 @@ struct CameraMatrix {
bool get_endpoints(const Transform &p_transform, Vector3 *p_8points) const;
Vector2 get_viewport_half_extents() const;
+ void get_far_plane_size(real_t &r_width, real_t &r_height) const;
void invert();
CameraMatrix inverse() const;
@@ -90,6 +92,23 @@ struct CameraMatrix {
int get_pixels_per_meter(int p_for_pixel_width) const;
operator Transform() const;
+ void flip_y();
+
+ bool operator==(const CameraMatrix &p_cam) const {
+ for (uint32_t i = 0; i < 4; i++) {
+ for (uint32_t j = 0; j < 4; j++) {
+ if (matrix[i][j] != p_cam.matrix[i][j]) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ bool operator!=(const CameraMatrix &p_cam) const {
+ return !(*this == p_cam);
+ }
+
CameraMatrix();
CameraMatrix(const Transform &p_transform);
~CameraMatrix();
diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp
index 71ff79c0fc..353b2acd16 100644
--- a/core/math/vector3.cpp
+++ b/core/math/vector3.cpp
@@ -103,7 +103,7 @@ Vector3 Vector3::cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a,
Vector3 out;
out = 0.5 * ((p1 * 2.0) +
(-p0 + p2) * t +
- (2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 +
+ (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * t2 +
(-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3);
return out;
}
@@ -122,7 +122,7 @@ Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, c
Vector3 out;
out = 0.5 * ((p1 * 2.0) +
(-p0 + p2) * t +
- (2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 +
+ (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * t2 +
(-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3);
return out;
}
diff --git a/core/math/vector3.h b/core/math/vector3.h
index 4ad3017109..3bf8644af9 100644
--- a/core/math/vector3.h
+++ b/core/math/vector3.h
@@ -32,6 +32,7 @@
#define VECTOR3_H
#include "core/math/math_funcs.h"
+#include "core/math/vector3i.h"
#include "core/ustring.h"
class Basis;
@@ -147,6 +148,15 @@ struct Vector3 {
_FORCE_INLINE_ bool operator>=(const Vector3 &p_v) const;
operator String() const;
+ _FORCE_INLINE_ operator Vector3i() const {
+ return Vector3i(x, y, z);
+ }
+
+ _FORCE_INLINE_ Vector3(const Vector3i &p_ivec) {
+ x = p_ivec.x;
+ y = p_ivec.y;
+ z = p_ivec.z;
+ }
_FORCE_INLINE_ Vector3(real_t p_x, real_t p_y, real_t p_z) {
x = p_x;
diff --git a/core/math/vector3i.cpp b/core/math/vector3i.cpp
new file mode 100644
index 0000000000..8a4ddf03b9
--- /dev/null
+++ b/core/math/vector3i.cpp
@@ -0,0 +1,55 @@
+/*************************************************************************/
+/* vector3i.cpp */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#include "vector3i.h"
+
+void Vector3i::set_axis(int p_axis, int32_t p_value) {
+ ERR_FAIL_INDEX(p_axis, 3);
+ coord[p_axis] = p_value;
+}
+int32_t Vector3i::get_axis(int p_axis) const {
+
+ ERR_FAIL_INDEX_V(p_axis, 3, 0);
+ return operator[](p_axis);
+}
+
+int Vector3i::min_axis() const {
+
+ return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2);
+}
+int Vector3i::max_axis() const {
+
+ return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
+}
+
+Vector3i::operator String() const {
+
+ return (itos(x) + ", " + itos(y) + ", " + itos(z));
+}
diff --git a/core/math/vector3i.h b/core/math/vector3i.h
new file mode 100644
index 0000000000..6f9754d3b9
--- /dev/null
+++ b/core/math/vector3i.h
@@ -0,0 +1,272 @@
+/*************************************************************************/
+/* vector3i.h */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#ifndef VECTOR3I_H
+#define VECTOR3I_H
+
+#include "core/typedefs.h"
+#include "core/ustring.h"
+
+struct Vector3i {
+
+ enum Axis {
+ AXIS_X,
+ AXIS_Y,
+ AXIS_Z,
+ };
+
+ union {
+ struct {
+ int32_t x;
+ int32_t y;
+ int32_t z;
+ };
+
+ int32_t coord[3];
+ };
+
+ _FORCE_INLINE_ const int32_t &operator[](int p_axis) const {
+
+ return coord[p_axis];
+ }
+
+ _FORCE_INLINE_ int32_t &operator[](int p_axis) {
+
+ return coord[p_axis];
+ }
+
+ void set_axis(int p_axis, int32_t p_value);
+ int32_t get_axis(int p_axis) const;
+
+ int min_axis() const;
+ int max_axis() const;
+
+ _FORCE_INLINE_ void zero();
+
+ _FORCE_INLINE_ Vector3i abs() const;
+ _FORCE_INLINE_ Vector3i sign() const;
+
+ /* Operators */
+
+ _FORCE_INLINE_ Vector3i &operator+=(const Vector3i &p_v);
+ _FORCE_INLINE_ Vector3i operator+(const Vector3i &p_v) const;
+ _FORCE_INLINE_ Vector3i &operator-=(const Vector3i &p_v);
+ _FORCE_INLINE_ Vector3i operator-(const Vector3i &p_v) const;
+ _FORCE_INLINE_ Vector3i &operator*=(const Vector3i &p_v);
+ _FORCE_INLINE_ Vector3i operator*(const Vector3i &p_v) const;
+ _FORCE_INLINE_ Vector3i &operator/=(const Vector3i &p_v);
+ _FORCE_INLINE_ Vector3i operator/(const Vector3i &p_v) const;
+
+ _FORCE_INLINE_ Vector3i &operator*=(int32_t p_scalar);
+ _FORCE_INLINE_ Vector3i operator*(int32_t p_scalar) const;
+ _FORCE_INLINE_ Vector3i &operator/=(int32_t p_scalar);
+ _FORCE_INLINE_ Vector3i operator/(int32_t p_scalar) const;
+
+ _FORCE_INLINE_ Vector3i operator-() const;
+
+ _FORCE_INLINE_ bool operator==(const Vector3i &p_v) const;
+ _FORCE_INLINE_ bool operator!=(const Vector3i &p_v) const;
+ _FORCE_INLINE_ bool operator<(const Vector3i &p_v) const;
+ _FORCE_INLINE_ bool operator<=(const Vector3i &p_v) const;
+ _FORCE_INLINE_ bool operator>(const Vector3i &p_v) const;
+ _FORCE_INLINE_ bool operator>=(const Vector3i &p_v) const;
+
+ operator String() const;
+
+ _FORCE_INLINE_ Vector3i(int32_t p_x, int32_t p_y, int32_t p_z) {
+ x = p_x;
+ y = p_y;
+ z = p_z;
+ }
+ _FORCE_INLINE_ Vector3i() { x = y = z = 0; }
+};
+
+Vector3i Vector3i::abs() const {
+
+ return Vector3i(ABS(x), ABS(y), ABS(z));
+}
+
+Vector3i Vector3i::sign() const {
+
+ return Vector3i(SGN(x), SGN(y), SGN(z));
+}
+
+/* Operators */
+
+Vector3i &Vector3i::operator+=(const Vector3i &p_v) {
+
+ x += p_v.x;
+ y += p_v.y;
+ z += p_v.z;
+ return *this;
+}
+
+Vector3i Vector3i::operator+(const Vector3i &p_v) const {
+
+ return Vector3i(x + p_v.x, y + p_v.y, z + p_v.z);
+}
+
+Vector3i &Vector3i::operator-=(const Vector3i &p_v) {
+
+ x -= p_v.x;
+ y -= p_v.y;
+ z -= p_v.z;
+ return *this;
+}
+Vector3i Vector3i::operator-(const Vector3i &p_v) const {
+
+ return Vector3i(x - p_v.x, y - p_v.y, z - p_v.z);
+}
+
+Vector3i &Vector3i::operator*=(const Vector3i &p_v) {
+
+ x *= p_v.x;
+ y *= p_v.y;
+ z *= p_v.z;
+ return *this;
+}
+Vector3i Vector3i::operator*(const Vector3i &p_v) const {
+
+ return Vector3i(x * p_v.x, y * p_v.y, z * p_v.z);
+}
+
+Vector3i &Vector3i::operator/=(const Vector3i &p_v) {
+
+ x /= p_v.x;
+ y /= p_v.y;
+ z /= p_v.z;
+ return *this;
+}
+
+Vector3i Vector3i::operator/(const Vector3i &p_v) const {
+
+ return Vector3i(x / p_v.x, y / p_v.y, z / p_v.z);
+}
+
+Vector3i &Vector3i::operator*=(int32_t p_scalar) {
+
+ x *= p_scalar;
+ y *= p_scalar;
+ z *= p_scalar;
+ return *this;
+}
+
+_FORCE_INLINE_ Vector3i operator*(int32_t p_scalar, const Vector3i &p_vec) {
+
+ return p_vec * p_scalar;
+}
+
+Vector3i Vector3i::operator*(int32_t p_scalar) const {
+
+ return Vector3i(x * p_scalar, y * p_scalar, z * p_scalar);
+}
+
+Vector3i &Vector3i::operator/=(int32_t p_scalar) {
+
+ x /= p_scalar;
+ y /= p_scalar;
+ z /= p_scalar;
+ return *this;
+}
+
+Vector3i Vector3i::operator/(int32_t p_scalar) const {
+
+ return Vector3i(x / p_scalar, y / p_scalar, z / p_scalar);
+}
+
+Vector3i Vector3i::operator-() const {
+
+ return Vector3i(-x, -y, -z);
+}
+
+bool Vector3i::operator==(const Vector3i &p_v) const {
+
+ return (x == p_v.x && y == p_v.y && z == p_v.z);
+}
+
+bool Vector3i::operator!=(const Vector3i &p_v) const {
+
+ return (x != p_v.x || y != p_v.y || z != p_v.z);
+}
+
+bool Vector3i::operator<(const Vector3i &p_v) const {
+
+ if (x == p_v.x) {
+ if (y == p_v.y)
+ return z < p_v.z;
+ else
+ return y < p_v.y;
+ } else {
+ return x < p_v.x;
+ }
+}
+
+bool Vector3i::operator>(const Vector3i &p_v) const {
+
+ if (x == p_v.x) {
+ if (y == p_v.y)
+ return z > p_v.z;
+ else
+ return y > p_v.y;
+ } else {
+ return x > p_v.x;
+ }
+}
+
+bool Vector3i::operator<=(const Vector3i &p_v) const {
+
+ if (x == p_v.x) {
+ if (y == p_v.y)
+ return z <= p_v.z;
+ else
+ return y < p_v.y;
+ } else {
+ return x < p_v.x;
+ }
+}
+
+bool Vector3i::operator>=(const Vector3i &p_v) const {
+
+ if (x == p_v.x) {
+ if (y == p_v.y)
+ return z >= p_v.z;
+ else
+ return y > p_v.y;
+ } else {
+ return x > p_v.x;
+ }
+}
+
+void Vector3i::zero() {
+
+ x = y = z = 0;
+}
+
+#endif // VECTOR3I_H