Add bindings for Vector4, Vector4i, Projection built-in types.
This commit is contained in:
@@ -37,6 +37,8 @@
|
||||
|
||||
#include <godot_cpp/godot.hpp>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace godot {
|
||||
|
||||
int CharString::length() const {
|
||||
@@ -186,6 +188,43 @@ void String::parse_utf16(const char16_t *from, int len) {
|
||||
internal::gdn_interface->string_new_with_utf16_chars_and_len(_native_ptr(), from, len);
|
||||
}
|
||||
|
||||
String String::num_real(double p_num, bool p_trailing) {
|
||||
if (p_num == (double)(int64_t)p_num) {
|
||||
if (p_trailing) {
|
||||
return num_int64((int64_t)p_num) + ".0";
|
||||
} else {
|
||||
return num_int64((int64_t)p_num);
|
||||
}
|
||||
}
|
||||
#ifdef REAL_T_IS_DOUBLE
|
||||
int decimals = 14;
|
||||
#else
|
||||
int decimals = 6;
|
||||
#endif
|
||||
// We want to align the digits to the above sane default, so we only
|
||||
// need to subtract log10 for numbers with a positive power of ten.
|
||||
if (p_num > 10) {
|
||||
decimals -= (int)floor(log10(p_num));
|
||||
}
|
||||
return num(p_num, decimals);
|
||||
}
|
||||
|
||||
String itos(int64_t p_val) {
|
||||
return String::num_int64(p_val);
|
||||
}
|
||||
|
||||
String uitos(uint64_t p_val) {
|
||||
return String::num_uint64(p_val);
|
||||
}
|
||||
|
||||
String rtos(double p_val) {
|
||||
return String::num(p_val);
|
||||
}
|
||||
|
||||
String rtoss(double p_val) {
|
||||
return String::num_scientific(p_val);
|
||||
}
|
||||
|
||||
CharString String::utf8() const {
|
||||
int size = internal::gdn_interface->string_to_utf8_chars(_native_ptr(), nullptr, 0);
|
||||
char *cstr = memnew_arr(char, size + 1);
|
||||
|
||||
934
src/variant/projection.cpp
Normal file
934
src/variant/projection.cpp
Normal file
@@ -0,0 +1,934 @@
|
||||
/*************************************************************************/
|
||||
/* projection.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2022 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 <godot_cpp/variant/projection.hpp>
|
||||
|
||||
#include <godot_cpp/variant/aabb.hpp>
|
||||
#include <godot_cpp/variant/plane.hpp>
|
||||
#include <godot_cpp/variant/rect2.hpp>
|
||||
#include <godot_cpp/variant/string.hpp>
|
||||
#include <godot_cpp/variant/transform3d.hpp>
|
||||
#include <godot_cpp/variant/variant.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
float Projection::determinant() const {
|
||||
return matrix[0][3] * matrix[1][2] * matrix[2][1] * matrix[3][0] - matrix[0][2] * matrix[1][3] * matrix[2][1] * matrix[3][0] -
|
||||
matrix[0][3] * matrix[1][1] * matrix[2][2] * matrix[3][0] + matrix[0][1] * matrix[1][3] * matrix[2][2] * matrix[3][0] +
|
||||
matrix[0][2] * matrix[1][1] * matrix[2][3] * matrix[3][0] - matrix[0][1] * matrix[1][2] * matrix[2][3] * matrix[3][0] -
|
||||
matrix[0][3] * matrix[1][2] * matrix[2][0] * matrix[3][1] + matrix[0][2] * matrix[1][3] * matrix[2][0] * matrix[3][1] +
|
||||
matrix[0][3] * matrix[1][0] * matrix[2][2] * matrix[3][1] - matrix[0][0] * matrix[1][3] * matrix[2][2] * matrix[3][1] -
|
||||
matrix[0][2] * matrix[1][0] * matrix[2][3] * matrix[3][1] + matrix[0][0] * matrix[1][2] * matrix[2][3] * matrix[3][1] +
|
||||
matrix[0][3] * matrix[1][1] * matrix[2][0] * matrix[3][2] - matrix[0][1] * matrix[1][3] * matrix[2][0] * matrix[3][2] -
|
||||
matrix[0][3] * matrix[1][0] * matrix[2][1] * matrix[3][2] + matrix[0][0] * matrix[1][3] * matrix[2][1] * matrix[3][2] +
|
||||
matrix[0][1] * matrix[1][0] * matrix[2][3] * matrix[3][2] - matrix[0][0] * matrix[1][1] * matrix[2][3] * matrix[3][2] -
|
||||
matrix[0][2] * matrix[1][1] * matrix[2][0] * matrix[3][3] + matrix[0][1] * matrix[1][2] * matrix[2][0] * matrix[3][3] +
|
||||
matrix[0][2] * matrix[1][0] * matrix[2][1] * matrix[3][3] - matrix[0][0] * matrix[1][2] * matrix[2][1] * matrix[3][3] -
|
||||
matrix[0][1] * matrix[1][0] * matrix[2][2] * matrix[3][3] + matrix[0][0] * matrix[1][1] * matrix[2][2] * matrix[3][3];
|
||||
}
|
||||
|
||||
void Projection::set_identity() {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
matrix[i][j] = (i == j) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Projection::set_zero() {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
matrix[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Plane Projection::xform4(const Plane &p_vec4) const {
|
||||
Plane ret;
|
||||
|
||||
ret.normal.x = matrix[0][0] * p_vec4.normal.x + matrix[1][0] * p_vec4.normal.y + matrix[2][0] * p_vec4.normal.z + matrix[3][0] * p_vec4.d;
|
||||
ret.normal.y = matrix[0][1] * p_vec4.normal.x + matrix[1][1] * p_vec4.normal.y + matrix[2][1] * p_vec4.normal.z + matrix[3][1] * p_vec4.d;
|
||||
ret.normal.z = matrix[0][2] * p_vec4.normal.x + matrix[1][2] * p_vec4.normal.y + matrix[2][2] * p_vec4.normal.z + matrix[3][2] * p_vec4.d;
|
||||
ret.d = matrix[0][3] * p_vec4.normal.x + matrix[1][3] * p_vec4.normal.y + matrix[2][3] * p_vec4.normal.z + matrix[3][3] * p_vec4.d;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Vector4 Projection::xform(const Vector4 &p_vec4) const {
|
||||
return Vector4(
|
||||
matrix[0][0] * p_vec4.x + matrix[1][0] * p_vec4.y + matrix[2][0] * p_vec4.z + matrix[3][0] * p_vec4.w,
|
||||
matrix[0][1] * p_vec4.x + matrix[1][1] * p_vec4.y + matrix[2][1] * p_vec4.z + matrix[3][1] * p_vec4.w,
|
||||
matrix[0][2] * p_vec4.x + matrix[1][2] * p_vec4.y + matrix[2][2] * p_vec4.z + matrix[3][2] * p_vec4.w,
|
||||
matrix[0][3] * p_vec4.x + matrix[1][3] * p_vec4.y + matrix[2][3] * p_vec4.z + matrix[3][3] * p_vec4.w);
|
||||
}
|
||||
Vector4 Projection::xform_inv(const Vector4 &p_vec4) const {
|
||||
return Vector4(
|
||||
matrix[0][0] * p_vec4.x + matrix[0][1] * p_vec4.y + matrix[0][2] * p_vec4.z + matrix[0][3] * p_vec4.w,
|
||||
matrix[1][0] * p_vec4.x + matrix[1][1] * p_vec4.y + matrix[1][2] * p_vec4.z + matrix[1][3] * p_vec4.w,
|
||||
matrix[2][0] * p_vec4.x + matrix[2][1] * p_vec4.y + matrix[2][2] * p_vec4.z + matrix[2][3] * p_vec4.w,
|
||||
matrix[3][0] * p_vec4.x + matrix[3][1] * p_vec4.y + matrix[3][2] * p_vec4.z + matrix[3][3] * p_vec4.w);
|
||||
}
|
||||
|
||||
void Projection::adjust_perspective_znear(real_t p_new_znear) {
|
||||
real_t zfar = get_z_far();
|
||||
real_t znear = p_new_znear;
|
||||
|
||||
real_t deltaZ = zfar - znear;
|
||||
matrix[2][2] = -(zfar + znear) / deltaZ;
|
||||
matrix[3][2] = -2 * znear * zfar / deltaZ;
|
||||
}
|
||||
|
||||
Projection Projection::create_depth_correction(bool p_flip_y) {
|
||||
Projection proj;
|
||||
proj.set_depth_correction(p_flip_y);
|
||||
return proj;
|
||||
}
|
||||
|
||||
Projection Projection::create_light_atlas_rect(const Rect2 &p_rect) {
|
||||
Projection proj;
|
||||
proj.set_light_atlas_rect(p_rect);
|
||||
return proj;
|
||||
}
|
||||
|
||||
Projection Projection::create_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov) {
|
||||
Projection proj;
|
||||
proj.set_perspective(p_fovy_degrees, p_aspect, p_z_near, p_z_far, p_flip_fov);
|
||||
return proj;
|
||||
}
|
||||
|
||||
Projection Projection::create_perspective_hmd(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) {
|
||||
Projection proj;
|
||||
proj.set_perspective(p_fovy_degrees, p_aspect, p_z_near, p_z_far, p_flip_fov, p_eye, p_intraocular_dist, p_convergence_dist);
|
||||
return proj;
|
||||
}
|
||||
|
||||
Projection Projection::create_for_hmd(int p_eye, real_t p_aspect, real_t p_intraocular_dist, real_t p_display_width, real_t p_display_to_lens, real_t p_oversample, real_t p_z_near, real_t p_z_far) {
|
||||
Projection proj;
|
||||
proj.set_for_hmd(p_eye, p_aspect, p_intraocular_dist, p_display_width, p_display_to_lens, p_oversample, p_z_near, p_z_far);
|
||||
return proj;
|
||||
}
|
||||
|
||||
Projection Projection::create_orthogonal(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_znear, real_t p_zfar) {
|
||||
Projection proj;
|
||||
proj.set_orthogonal(p_left, p_right, p_bottom, p_top, p_zfar, p_zfar);
|
||||
return proj;
|
||||
}
|
||||
|
||||
Projection Projection::create_orthogonal_aspect(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov) {
|
||||
Projection proj;
|
||||
proj.set_orthogonal(p_size, p_aspect, p_znear, p_zfar, p_flip_fov);
|
||||
return proj;
|
||||
}
|
||||
|
||||
Projection Projection::create_frustum(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_near, real_t p_far) {
|
||||
Projection proj;
|
||||
proj.set_frustum(p_left, p_right, p_bottom, p_top, p_near, p_far);
|
||||
return proj;
|
||||
}
|
||||
|
||||
Projection Projection::create_frustum_aspect(real_t p_size, real_t p_aspect, Vector2 p_offset, real_t p_near, real_t p_far, bool p_flip_fov) {
|
||||
Projection proj;
|
||||
proj.set_frustum(p_size, p_aspect, p_offset, p_near, p_far, p_flip_fov);
|
||||
return proj;
|
||||
}
|
||||
|
||||
Projection Projection::create_fit_aabb(const AABB &p_aabb) {
|
||||
Projection proj;
|
||||
proj.scale_translate_to_fit(p_aabb);
|
||||
return proj;
|
||||
}
|
||||
|
||||
Projection Projection::perspective_znear_adjusted(real_t p_new_znear) const {
|
||||
Projection proj = *this;
|
||||
proj.adjust_perspective_znear(p_new_znear);
|
||||
return proj;
|
||||
}
|
||||
|
||||
Plane Projection::get_projection_plane(Planes p_plane) const {
|
||||
const real_t *matrix = (const real_t *)this->matrix;
|
||||
|
||||
switch (p_plane) {
|
||||
case PLANE_NEAR: {
|
||||
Plane new_plane = Plane(matrix[3] + matrix[2],
|
||||
matrix[7] + matrix[6],
|
||||
matrix[11] + matrix[10],
|
||||
matrix[15] + matrix[14]);
|
||||
|
||||
new_plane.normal = -new_plane.normal;
|
||||
new_plane.normalize();
|
||||
return new_plane;
|
||||
} break;
|
||||
case PLANE_FAR: {
|
||||
Plane new_plane = Plane(matrix[3] - matrix[2],
|
||||
matrix[7] - matrix[6],
|
||||
matrix[11] - matrix[10],
|
||||
matrix[15] - matrix[14]);
|
||||
|
||||
new_plane.normal = -new_plane.normal;
|
||||
new_plane.normalize();
|
||||
return new_plane;
|
||||
} break;
|
||||
case PLANE_LEFT: {
|
||||
Plane new_plane = Plane(matrix[3] + matrix[0],
|
||||
matrix[7] + matrix[4],
|
||||
matrix[11] + matrix[8],
|
||||
matrix[15] + matrix[12]);
|
||||
|
||||
new_plane.normal = -new_plane.normal;
|
||||
new_plane.normalize();
|
||||
return new_plane;
|
||||
} break;
|
||||
case PLANE_TOP: {
|
||||
Plane new_plane = Plane(matrix[3] - matrix[1],
|
||||
matrix[7] - matrix[5],
|
||||
matrix[11] - matrix[9],
|
||||
matrix[15] - matrix[13]);
|
||||
|
||||
new_plane.normal = -new_plane.normal;
|
||||
new_plane.normalize();
|
||||
return new_plane;
|
||||
} break;
|
||||
case PLANE_RIGHT: {
|
||||
Plane new_plane = Plane(matrix[3] - matrix[0],
|
||||
matrix[7] - matrix[4],
|
||||
matrix[11] - matrix[8],
|
||||
matrix[15] - matrix[12]);
|
||||
|
||||
new_plane.normal = -new_plane.normal;
|
||||
new_plane.normalize();
|
||||
return new_plane;
|
||||
} break;
|
||||
case PLANE_BOTTOM: {
|
||||
Plane new_plane = Plane(matrix[3] + matrix[1],
|
||||
matrix[7] + matrix[5],
|
||||
matrix[11] + matrix[9],
|
||||
matrix[15] + matrix[13]);
|
||||
|
||||
new_plane.normal = -new_plane.normal;
|
||||
new_plane.normalize();
|
||||
return new_plane;
|
||||
} break;
|
||||
}
|
||||
|
||||
return Plane();
|
||||
}
|
||||
|
||||
Projection Projection::flipped_y() const {
|
||||
Projection proj = *this;
|
||||
proj.flip_y();
|
||||
return proj;
|
||||
}
|
||||
|
||||
Projection Projection ::jitter_offseted(const Vector2 &p_offset) const {
|
||||
Projection proj = *this;
|
||||
proj.add_jitter_offset(p_offset);
|
||||
return proj;
|
||||
}
|
||||
|
||||
void Projection::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) {
|
||||
if (p_flip_fov) {
|
||||
p_fovy_degrees = get_fovy(p_fovy_degrees, 1.0 / p_aspect);
|
||||
}
|
||||
|
||||
real_t sine, cotangent, deltaZ;
|
||||
real_t radians = Math::deg2rad(p_fovy_degrees / 2.0);
|
||||
|
||||
deltaZ = p_z_far - p_z_near;
|
||||
sine = Math::sin(radians);
|
||||
|
||||
if ((deltaZ == 0) || (sine == 0) || (p_aspect == 0)) {
|
||||
return;
|
||||
}
|
||||
cotangent = Math::cos(radians) / sine;
|
||||
|
||||
set_identity();
|
||||
|
||||
matrix[0][0] = cotangent / p_aspect;
|
||||
matrix[1][1] = cotangent;
|
||||
matrix[2][2] = -(p_z_far + p_z_near) / deltaZ;
|
||||
matrix[2][3] = -1;
|
||||
matrix[3][2] = -2 * p_z_near * p_z_far / deltaZ;
|
||||
matrix[3][3] = 0;
|
||||
}
|
||||
|
||||
void Projection::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) {
|
||||
if (p_flip_fov) {
|
||||
p_fovy_degrees = get_fovy(p_fovy_degrees, 1.0 / p_aspect);
|
||||
}
|
||||
|
||||
real_t left, right, modeltranslation, ymax, xmax, frustumshift;
|
||||
|
||||
ymax = p_z_near * tan(Math::deg2rad(p_fovy_degrees / 2.0));
|
||||
xmax = ymax * p_aspect;
|
||||
frustumshift = (p_intraocular_dist / 2.0) * p_z_near / p_convergence_dist;
|
||||
|
||||
switch (p_eye) {
|
||||
case 1: { // left eye
|
||||
left = -xmax + frustumshift;
|
||||
right = xmax + frustumshift;
|
||||
modeltranslation = p_intraocular_dist / 2.0;
|
||||
} break;
|
||||
case 2: { // right eye
|
||||
left = -xmax - frustumshift;
|
||||
right = xmax - frustumshift;
|
||||
modeltranslation = -p_intraocular_dist / 2.0;
|
||||
} break;
|
||||
default: { // mono, should give the same result as set_perspective(p_fovy_degrees,p_aspect,p_z_near,p_z_far,p_flip_fov)
|
||||
left = -xmax;
|
||||
right = xmax;
|
||||
modeltranslation = 0.0;
|
||||
} break;
|
||||
}
|
||||
|
||||
set_frustum(left, right, -ymax, ymax, p_z_near, p_z_far);
|
||||
|
||||
// translate matrix by (modeltranslation, 0.0, 0.0)
|
||||
Projection cm;
|
||||
cm.set_identity();
|
||||
cm.matrix[3][0] = modeltranslation;
|
||||
*this = *this * cm;
|
||||
}
|
||||
|
||||
void Projection::set_for_hmd(int p_eye, real_t p_aspect, real_t p_intraocular_dist, real_t p_display_width, real_t p_display_to_lens, real_t p_oversample, real_t p_z_near, real_t p_z_far) {
|
||||
// we first calculate our base frustum on our values without taking our lens magnification into account.
|
||||
real_t f1 = (p_intraocular_dist * 0.5) / p_display_to_lens;
|
||||
real_t f2 = ((p_display_width - p_intraocular_dist) * 0.5) / p_display_to_lens;
|
||||
real_t f3 = (p_display_width / 4.0) / p_display_to_lens;
|
||||
|
||||
// now we apply our oversample factor to increase our FOV. how much we oversample is always a balance we strike between performance and how much
|
||||
// we're willing to sacrifice in FOV.
|
||||
real_t add = ((f1 + f2) * (p_oversample - 1.0)) / 2.0;
|
||||
f1 += add;
|
||||
f2 += add;
|
||||
f3 *= p_oversample;
|
||||
|
||||
// always apply KEEP_WIDTH aspect ratio
|
||||
f3 /= p_aspect;
|
||||
|
||||
switch (p_eye) {
|
||||
case 1: { // left eye
|
||||
set_frustum(-f2 * p_z_near, f1 * p_z_near, -f3 * p_z_near, f3 * p_z_near, p_z_near, p_z_far);
|
||||
} break;
|
||||
case 2: { // right eye
|
||||
set_frustum(-f1 * p_z_near, f2 * p_z_near, -f3 * p_z_near, f3 * p_z_near, p_z_near, p_z_far);
|
||||
} break;
|
||||
default: { // mono, does not apply here!
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
void Projection::set_orthogonal(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_znear, real_t p_zfar) {
|
||||
set_identity();
|
||||
|
||||
matrix[0][0] = 2.0 / (p_right - p_left);
|
||||
matrix[3][0] = -((p_right + p_left) / (p_right - p_left));
|
||||
matrix[1][1] = 2.0 / (p_top - p_bottom);
|
||||
matrix[3][1] = -((p_top + p_bottom) / (p_top - p_bottom));
|
||||
matrix[2][2] = -2.0 / (p_zfar - p_znear);
|
||||
matrix[3][2] = -((p_zfar + p_znear) / (p_zfar - p_znear));
|
||||
matrix[3][3] = 1.0;
|
||||
}
|
||||
|
||||
void Projection::set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov) {
|
||||
if (!p_flip_fov) {
|
||||
p_size *= p_aspect;
|
||||
}
|
||||
|
||||
set_orthogonal(-p_size / 2, +p_size / 2, -p_size / p_aspect / 2, +p_size / p_aspect / 2, p_znear, p_zfar);
|
||||
}
|
||||
|
||||
void Projection::set_frustum(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_near, real_t p_far) {
|
||||
ERR_FAIL_COND(p_right <= p_left);
|
||||
ERR_FAIL_COND(p_top <= p_bottom);
|
||||
ERR_FAIL_COND(p_far <= p_near);
|
||||
|
||||
real_t *te = &matrix[0][0];
|
||||
real_t x = 2 * p_near / (p_right - p_left);
|
||||
real_t y = 2 * p_near / (p_top - p_bottom);
|
||||
|
||||
real_t a = (p_right + p_left) / (p_right - p_left);
|
||||
real_t b = (p_top + p_bottom) / (p_top - p_bottom);
|
||||
real_t c = -(p_far + p_near) / (p_far - p_near);
|
||||
real_t d = -2 * p_far * p_near / (p_far - p_near);
|
||||
|
||||
te[0] = x;
|
||||
te[1] = 0;
|
||||
te[2] = 0;
|
||||
te[3] = 0;
|
||||
te[4] = 0;
|
||||
te[5] = y;
|
||||
te[6] = 0;
|
||||
te[7] = 0;
|
||||
te[8] = a;
|
||||
te[9] = b;
|
||||
te[10] = c;
|
||||
te[11] = -1;
|
||||
te[12] = 0;
|
||||
te[13] = 0;
|
||||
te[14] = d;
|
||||
te[15] = 0;
|
||||
}
|
||||
|
||||
void Projection::set_frustum(real_t p_size, real_t p_aspect, Vector2 p_offset, real_t p_near, real_t p_far, bool p_flip_fov) {
|
||||
if (!p_flip_fov) {
|
||||
p_size *= p_aspect;
|
||||
}
|
||||
|
||||
set_frustum(-p_size / 2 + p_offset.x, +p_size / 2 + p_offset.x, -p_size / p_aspect / 2 + p_offset.y, +p_size / p_aspect / 2 + p_offset.y, p_near, p_far);
|
||||
}
|
||||
|
||||
real_t Projection::get_z_far() const {
|
||||
const real_t *matrix = (const real_t *)this->matrix;
|
||||
Plane new_plane = Plane(matrix[3] - matrix[2],
|
||||
matrix[7] - matrix[6],
|
||||
matrix[11] - matrix[10],
|
||||
matrix[15] - matrix[14]);
|
||||
|
||||
new_plane.normal = -new_plane.normal;
|
||||
new_plane.normalize();
|
||||
|
||||
return new_plane.d;
|
||||
}
|
||||
|
||||
real_t Projection::get_z_near() const {
|
||||
const real_t *matrix = (const real_t *)this->matrix;
|
||||
Plane new_plane = Plane(matrix[3] + matrix[2],
|
||||
matrix[7] + matrix[6],
|
||||
matrix[11] + matrix[10],
|
||||
-matrix[15] - matrix[14]);
|
||||
|
||||
new_plane.normalize();
|
||||
return new_plane.d;
|
||||
}
|
||||
|
||||
Vector2 Projection::get_viewport_half_extents() const {
|
||||
const real_t *matrix = (const real_t *)this->matrix;
|
||||
///////--- Near Plane ---///////
|
||||
Plane near_plane = Plane(matrix[3] + matrix[2],
|
||||
matrix[7] + matrix[6],
|
||||
matrix[11] + matrix[10],
|
||||
-matrix[15] - matrix[14]);
|
||||
near_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;
|
||||
near_plane.intersect_3(right_plane, top_plane, &res);
|
||||
|
||||
return Vector2(res.x, res.y);
|
||||
}
|
||||
|
||||
Vector2 Projection::get_far_plane_half_extents() 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);
|
||||
|
||||
return Vector2(res.x, res.y);
|
||||
}
|
||||
|
||||
bool Projection::get_endpoints(const Transform3D &p_transform, Vector3 *p_8points) const {
|
||||
Array planes = get_projection_planes(Transform3D());
|
||||
const Planes intersections[8][3] = {
|
||||
{ PLANE_FAR, PLANE_LEFT, PLANE_TOP },
|
||||
{ PLANE_FAR, PLANE_LEFT, PLANE_BOTTOM },
|
||||
{ PLANE_FAR, PLANE_RIGHT, PLANE_TOP },
|
||||
{ PLANE_FAR, PLANE_RIGHT, PLANE_BOTTOM },
|
||||
{ PLANE_NEAR, PLANE_LEFT, PLANE_TOP },
|
||||
{ PLANE_NEAR, PLANE_LEFT, PLANE_BOTTOM },
|
||||
{ PLANE_NEAR, PLANE_RIGHT, PLANE_TOP },
|
||||
{ PLANE_NEAR, PLANE_RIGHT, PLANE_BOTTOM },
|
||||
};
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
Vector3 point;
|
||||
bool res = planes[intersections[i][0]].operator Plane().intersect_3(planes[intersections[i][1]].operator Plane(), planes[intersections[i][2]].operator Plane(), &point);
|
||||
ERR_FAIL_COND_V(!res, false);
|
||||
p_8points[i] = p_transform.xform(point);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Array Projection::get_projection_planes(const Transform3D &p_transform) const {
|
||||
/** Fast Plane Extraction from combined modelview/projection matrices.
|
||||
* References:
|
||||
* https://web.archive.org/web/20011221205252/https://www.markmorley.com/opengl/frustumculling.html
|
||||
* https://web.archive.org/web/20061020020112/https://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf
|
||||
*/
|
||||
|
||||
Array planes;
|
||||
|
||||
const real_t *matrix = (const real_t *)this->matrix;
|
||||
|
||||
Plane new_plane;
|
||||
|
||||
///////--- Near Plane ---///////
|
||||
new_plane = Plane(matrix[3] + matrix[2],
|
||||
matrix[7] + matrix[6],
|
||||
matrix[11] + matrix[10],
|
||||
matrix[15] + matrix[14]);
|
||||
|
||||
new_plane.normal = -new_plane.normal;
|
||||
new_plane.normalize();
|
||||
|
||||
planes.push_back(p_transform.xform(new_plane));
|
||||
|
||||
///////--- Far Plane ---///////
|
||||
new_plane = Plane(matrix[3] - matrix[2],
|
||||
matrix[7] - matrix[6],
|
||||
matrix[11] - matrix[10],
|
||||
matrix[15] - matrix[14]);
|
||||
|
||||
new_plane.normal = -new_plane.normal;
|
||||
new_plane.normalize();
|
||||
|
||||
planes.push_back(p_transform.xform(new_plane));
|
||||
|
||||
///////--- Left Plane ---///////
|
||||
new_plane = Plane(matrix[3] + matrix[0],
|
||||
matrix[7] + matrix[4],
|
||||
matrix[11] + matrix[8],
|
||||
matrix[15] + matrix[12]);
|
||||
|
||||
new_plane.normal = -new_plane.normal;
|
||||
new_plane.normalize();
|
||||
|
||||
planes.push_back(p_transform.xform(new_plane));
|
||||
|
||||
///////--- Top Plane ---///////
|
||||
new_plane = Plane(matrix[3] - matrix[1],
|
||||
matrix[7] - matrix[5],
|
||||
matrix[11] - matrix[9],
|
||||
matrix[15] - matrix[13]);
|
||||
|
||||
new_plane.normal = -new_plane.normal;
|
||||
new_plane.normalize();
|
||||
|
||||
planes.push_back(p_transform.xform(new_plane));
|
||||
|
||||
///////--- Right Plane ---///////
|
||||
new_plane = Plane(matrix[3] - matrix[0],
|
||||
matrix[7] - matrix[4],
|
||||
matrix[11] - matrix[8],
|
||||
matrix[15] - matrix[12]);
|
||||
|
||||
new_plane.normal = -new_plane.normal;
|
||||
new_plane.normalize();
|
||||
|
||||
planes.push_back(p_transform.xform(new_plane));
|
||||
|
||||
///////--- Bottom Plane ---///////
|
||||
new_plane = Plane(matrix[3] + matrix[1],
|
||||
matrix[7] + matrix[5],
|
||||
matrix[11] + matrix[9],
|
||||
matrix[15] + matrix[13]);
|
||||
|
||||
new_plane.normal = -new_plane.normal;
|
||||
new_plane.normalize();
|
||||
|
||||
planes.push_back(p_transform.xform(new_plane));
|
||||
|
||||
return planes;
|
||||
}
|
||||
|
||||
Projection Projection::inverse() const {
|
||||
Projection cm = *this;
|
||||
cm.invert();
|
||||
return cm;
|
||||
}
|
||||
|
||||
void Projection::invert() {
|
||||
int i, j, k;
|
||||
int pvt_i[4], pvt_j[4]; /* Locations of pivot matrix */
|
||||
real_t pvt_val; /* Value of current pivot element */
|
||||
real_t hold; /* Temporary storage */
|
||||
real_t determinant = 1.0f;
|
||||
for (k = 0; k < 4; k++) {
|
||||
/** Locate k'th pivot element **/
|
||||
pvt_val = matrix[k][k]; /** Initialize for search **/
|
||||
pvt_i[k] = k;
|
||||
pvt_j[k] = k;
|
||||
for (i = k; i < 4; i++) {
|
||||
for (j = k; j < 4; j++) {
|
||||
if (Math::abs(matrix[i][j]) > Math::abs(pvt_val)) {
|
||||
pvt_i[k] = i;
|
||||
pvt_j[k] = j;
|
||||
pvt_val = matrix[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Product of pivots, gives determinant when finished **/
|
||||
determinant *= pvt_val;
|
||||
if (Math::is_zero_approx(determinant)) {
|
||||
return; /** Matrix is singular (zero determinant). **/
|
||||
}
|
||||
|
||||
/** "Interchange" elements (with sign change stuff) **/
|
||||
i = pvt_i[k];
|
||||
if (i != k) { /** If elements are different **/
|
||||
for (j = 0; j < 4; j++) {
|
||||
hold = -matrix[k][j];
|
||||
matrix[k][j] = matrix[i][j];
|
||||
matrix[i][j] = hold;
|
||||
}
|
||||
}
|
||||
|
||||
/** "Interchange" columns **/
|
||||
j = pvt_j[k];
|
||||
if (j != k) { /** If columns are different **/
|
||||
for (i = 0; i < 4; i++) {
|
||||
hold = -matrix[i][k];
|
||||
matrix[i][k] = matrix[i][j];
|
||||
matrix[i][j] = hold;
|
||||
}
|
||||
}
|
||||
|
||||
/** Divide column by minus pivot value **/
|
||||
for (i = 0; i < 4; i++) {
|
||||
if (i != k) {
|
||||
matrix[i][k] /= (-pvt_val);
|
||||
}
|
||||
}
|
||||
|
||||
/** Reduce the matrix **/
|
||||
for (i = 0; i < 4; i++) {
|
||||
hold = matrix[i][k];
|
||||
for (j = 0; j < 4; j++) {
|
||||
if (i != k && j != k) {
|
||||
matrix[i][j] += hold * matrix[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Divide row by pivot **/
|
||||
for (j = 0; j < 4; j++) {
|
||||
if (j != k) {
|
||||
matrix[k][j] /= pvt_val;
|
||||
}
|
||||
}
|
||||
|
||||
/** Replace pivot by reciprocal (at last we can touch it). **/
|
||||
matrix[k][k] = 1.0 / pvt_val;
|
||||
}
|
||||
|
||||
/* That was most of the work, one final pass of row/column interchange */
|
||||
/* to finish */
|
||||
for (k = 4 - 2; k >= 0; k--) { /* Don't need to work with 1 by 1 corner*/
|
||||
i = pvt_j[k]; /* Rows to swap correspond to pivot COLUMN */
|
||||
if (i != k) { /* If elements are different */
|
||||
for (j = 0; j < 4; j++) {
|
||||
hold = matrix[k][j];
|
||||
matrix[k][j] = -matrix[i][j];
|
||||
matrix[i][j] = hold;
|
||||
}
|
||||
}
|
||||
|
||||
j = pvt_i[k]; /* Columns to swap correspond to pivot ROW */
|
||||
if (j != k) { /* If columns are different */
|
||||
for (i = 0; i < 4; i++) {
|
||||
hold = matrix[i][k];
|
||||
matrix[i][k] = -matrix[i][j];
|
||||
matrix[i][j] = hold;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Projection::flip_y() {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
matrix[1][i] = -matrix[1][i];
|
||||
}
|
||||
}
|
||||
|
||||
Projection::Projection() {
|
||||
set_identity();
|
||||
}
|
||||
|
||||
Projection Projection::operator*(const Projection &p_matrix) const {
|
||||
Projection new_matrix;
|
||||
|
||||
for (int j = 0; j < 4; j++) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
real_t ab = 0;
|
||||
for (int k = 0; k < 4; k++) {
|
||||
ab += matrix[k][i] * p_matrix.matrix[j][k];
|
||||
}
|
||||
new_matrix.matrix[j][i] = ab;
|
||||
}
|
||||
}
|
||||
|
||||
return new_matrix;
|
||||
}
|
||||
|
||||
void Projection::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 Projection::set_light_bias() {
|
||||
real_t *m = &matrix[0][0];
|
||||
|
||||
m[0] = 0.5;
|
||||
m[1] = 0.0;
|
||||
m[2] = 0.0;
|
||||
m[3] = 0.0;
|
||||
m[4] = 0.0;
|
||||
m[5] = 0.5;
|
||||
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.5;
|
||||
m[13] = 0.5;
|
||||
m[14] = 0.5;
|
||||
m[15] = 1.0;
|
||||
}
|
||||
|
||||
void Projection::set_light_atlas_rect(const Rect2 &p_rect) {
|
||||
real_t *m = &matrix[0][0];
|
||||
|
||||
m[0] = p_rect.size.width;
|
||||
m[1] = 0.0;
|
||||
m[2] = 0.0;
|
||||
m[3] = 0.0;
|
||||
m[4] = 0.0;
|
||||
m[5] = p_rect.size.height;
|
||||
m[6] = 0.0;
|
||||
m[7] = 0.0;
|
||||
m[8] = 0.0;
|
||||
m[9] = 0.0;
|
||||
m[10] = 1.0;
|
||||
m[11] = 0.0;
|
||||
m[12] = p_rect.position.x;
|
||||
m[13] = p_rect.position.y;
|
||||
m[14] = 0.0;
|
||||
m[15] = 1.0;
|
||||
}
|
||||
|
||||
Projection::operator String() const {
|
||||
String str;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
str = str + String((j > 0) ? ", " : "\n") + rtos(matrix[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
real_t Projection::get_aspect() const {
|
||||
Vector2 vp_he = get_viewport_half_extents();
|
||||
return vp_he.x / vp_he.y;
|
||||
}
|
||||
|
||||
int Projection::get_pixels_per_meter(int p_for_pixel_width) const {
|
||||
Vector3 result = xform(Vector3(1, 0, -1));
|
||||
|
||||
return int((result.x * 0.5 + 0.5) * p_for_pixel_width);
|
||||
}
|
||||
|
||||
bool Projection::is_orthogonal() const {
|
||||
return matrix[3][3] == 1.0;
|
||||
}
|
||||
|
||||
real_t Projection::get_fov() const {
|
||||
const real_t *matrix = (const real_t *)this->matrix;
|
||||
|
||||
Plane right_plane = Plane(matrix[3] - matrix[0],
|
||||
matrix[7] - matrix[4],
|
||||
matrix[11] - matrix[8],
|
||||
-matrix[15] + matrix[12]);
|
||||
right_plane.normalize();
|
||||
|
||||
if ((matrix[8] == 0) && (matrix[9] == 0)) {
|
||||
return Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x))) * 2.0;
|
||||
} else {
|
||||
// our frustum is asymmetrical need to calculate the left planes angle separately..
|
||||
Plane left_plane = Plane(matrix[3] + matrix[0],
|
||||
matrix[7] + matrix[4],
|
||||
matrix[11] + matrix[8],
|
||||
matrix[15] + matrix[12]);
|
||||
left_plane.normalize();
|
||||
|
||||
return Math::rad2deg(Math::acos(Math::abs(left_plane.normal.x))) + Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x)));
|
||||
}
|
||||
}
|
||||
|
||||
float Projection::get_lod_multiplier() const {
|
||||
if (is_orthogonal()) {
|
||||
return get_viewport_half_extents().x;
|
||||
} else {
|
||||
float zn = get_z_near();
|
||||
float width = get_viewport_half_extents().x * 2.0;
|
||||
return 1.0 / (zn / width);
|
||||
}
|
||||
|
||||
// usage is lod_size / (lod_distance * multiplier) < threshold
|
||||
}
|
||||
void Projection::make_scale(const Vector3 &p_scale) {
|
||||
set_identity();
|
||||
matrix[0][0] = p_scale.x;
|
||||
matrix[1][1] = p_scale.y;
|
||||
matrix[2][2] = p_scale.z;
|
||||
}
|
||||
|
||||
void Projection::scale_translate_to_fit(const AABB &p_aabb) {
|
||||
Vector3 min = p_aabb.position;
|
||||
Vector3 max = p_aabb.position + p_aabb.size;
|
||||
|
||||
matrix[0][0] = 2 / (max.x - min.x);
|
||||
matrix[1][0] = 0;
|
||||
matrix[2][0] = 0;
|
||||
matrix[3][0] = -(max.x + min.x) / (max.x - min.x);
|
||||
|
||||
matrix[0][1] = 0;
|
||||
matrix[1][1] = 2 / (max.y - min.y);
|
||||
matrix[2][1] = 0;
|
||||
matrix[3][1] = -(max.y + min.y) / (max.y - min.y);
|
||||
|
||||
matrix[0][2] = 0;
|
||||
matrix[1][2] = 0;
|
||||
matrix[2][2] = 2 / (max.z - min.z);
|
||||
matrix[3][2] = -(max.z + min.z) / (max.z - min.z);
|
||||
|
||||
matrix[0][3] = 0;
|
||||
matrix[1][3] = 0;
|
||||
matrix[2][3] = 0;
|
||||
matrix[3][3] = 1;
|
||||
}
|
||||
|
||||
void Projection::add_jitter_offset(const Vector2 &p_offset) {
|
||||
matrix[3][0] += p_offset.x;
|
||||
matrix[3][1] += p_offset.y;
|
||||
}
|
||||
|
||||
Projection::operator Transform3D() const {
|
||||
Transform3D tr;
|
||||
const real_t *m = &matrix[0][0];
|
||||
|
||||
tr.basis.elements[0][0] = m[0];
|
||||
tr.basis.elements[1][0] = m[1];
|
||||
tr.basis.elements[2][0] = m[2];
|
||||
|
||||
tr.basis.elements[0][1] = m[4];
|
||||
tr.basis.elements[1][1] = m[5];
|
||||
tr.basis.elements[2][1] = m[6];
|
||||
|
||||
tr.basis.elements[0][2] = m[8];
|
||||
tr.basis.elements[1][2] = m[9];
|
||||
tr.basis.elements[2][2] = m[10];
|
||||
|
||||
tr.origin.x = m[12];
|
||||
tr.origin.y = m[13];
|
||||
tr.origin.z = m[14];
|
||||
|
||||
return tr;
|
||||
}
|
||||
Projection::Projection(const Vector4 &p_x, const Vector4 &p_y, const Vector4 &p_z, const Vector4 &p_w) {
|
||||
matrix[0] = p_x;
|
||||
matrix[1] = p_y;
|
||||
matrix[2] = p_z;
|
||||
matrix[3] = p_w;
|
||||
}
|
||||
Projection::Projection(const Transform3D &p_transform) {
|
||||
const Transform3D &tr = p_transform;
|
||||
real_t *m = &matrix[0][0];
|
||||
|
||||
m[0] = tr.basis.elements[0][0];
|
||||
m[1] = tr.basis.elements[1][0];
|
||||
m[2] = tr.basis.elements[2][0];
|
||||
m[3] = 0.0;
|
||||
m[4] = tr.basis.elements[0][1];
|
||||
m[5] = tr.basis.elements[1][1];
|
||||
m[6] = tr.basis.elements[2][1];
|
||||
m[7] = 0.0;
|
||||
m[8] = tr.basis.elements[0][2];
|
||||
m[9] = tr.basis.elements[1][2];
|
||||
m[10] = tr.basis.elements[2][2];
|
||||
m[11] = 0.0;
|
||||
m[12] = tr.origin.x;
|
||||
m[13] = tr.origin.y;
|
||||
m[14] = tr.origin.z;
|
||||
m[15] = 1.0;
|
||||
}
|
||||
|
||||
Projection::~Projection() {
|
||||
}
|
||||
|
||||
} // namespace godot
|
||||
@@ -134,6 +134,14 @@ Variant::Variant(const Transform2D &v) {
|
||||
from_type_constructor[TRANSFORM2D](_native_ptr(), v._native_ptr());
|
||||
}
|
||||
|
||||
Variant::Variant(const Vector4 &v) {
|
||||
from_type_constructor[VECTOR4](_native_ptr(), v._native_ptr());
|
||||
}
|
||||
|
||||
Variant::Variant(const Vector4i &v) {
|
||||
from_type_constructor[VECTOR4I](_native_ptr(), v._native_ptr());
|
||||
}
|
||||
|
||||
Variant::Variant(const Plane &v) {
|
||||
from_type_constructor[PLANE](_native_ptr(), v._native_ptr());
|
||||
}
|
||||
@@ -154,6 +162,10 @@ Variant::Variant(const Transform3D &v) {
|
||||
from_type_constructor[TRANSFORM3D](_native_ptr(), v._native_ptr());
|
||||
}
|
||||
|
||||
Variant::Variant(const Projection &v) {
|
||||
from_type_constructor[PROJECTION](_native_ptr(), v._native_ptr());
|
||||
}
|
||||
|
||||
Variant::Variant(const Color &v) {
|
||||
from_type_constructor[COLOR](_native_ptr(), v._native_ptr());
|
||||
}
|
||||
@@ -317,6 +329,18 @@ Variant::operator Transform2D() const {
|
||||
return result;
|
||||
}
|
||||
|
||||
Variant::operator Vector4() const {
|
||||
Vector4 result;
|
||||
to_type_constructor[VECTOR4](result._native_ptr(), _native_ptr());
|
||||
return result;
|
||||
}
|
||||
|
||||
Variant::operator Vector4i() const {
|
||||
Vector4i result;
|
||||
to_type_constructor[VECTOR4I](result._native_ptr(), _native_ptr());
|
||||
return result;
|
||||
}
|
||||
|
||||
Variant::operator Plane() const {
|
||||
Plane result;
|
||||
to_type_constructor[PLANE](result._native_ptr(), _native_ptr());
|
||||
@@ -347,6 +371,12 @@ Variant::operator Transform3D() const {
|
||||
return result;
|
||||
}
|
||||
|
||||
Variant::operator Projection() const {
|
||||
Projection result;
|
||||
to_type_constructor[PROJECTION](result._native_ptr(), _native_ptr());
|
||||
return result;
|
||||
}
|
||||
|
||||
Variant::operator Color() const {
|
||||
Color result;
|
||||
to_type_constructor[COLOR](result._native_ptr(), _native_ptr());
|
||||
@@ -703,11 +733,14 @@ void Variant::clear() {
|
||||
false, // VECTOR3,
|
||||
false, // VECTOR3I,
|
||||
true, // TRANSFORM2D,
|
||||
false, // VECTOR4,
|
||||
false, // VECTOR4I,
|
||||
false, // PLANE,
|
||||
false, // QUATERNION,
|
||||
true, // AABB,
|
||||
true, // BASIS,
|
||||
true, // TRANSFORM,
|
||||
true, // TRANSFORM3D,
|
||||
true, // PROJECTION,
|
||||
|
||||
// misc types
|
||||
false, // COLOR,
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
|
||||
#include <godot_cpp/variant/vector2.hpp>
|
||||
|
||||
#include <godot_cpp/core/error_macros.hpp>
|
||||
#include <godot_cpp/variant/string.hpp>
|
||||
#include <godot_cpp/variant/vector2i.hpp>
|
||||
|
||||
@@ -40,6 +39,10 @@ real_t Vector2::angle() const {
|
||||
return Math::atan2(y, x);
|
||||
}
|
||||
|
||||
Vector2 Vector2::from_angle(const real_t p_angle) {
|
||||
return Vector2(Math::cos(p_angle), Math::sin(p_angle));
|
||||
}
|
||||
|
||||
real_t Vector2::length() const {
|
||||
return Math::sqrt(x * x + y * y);
|
||||
}
|
||||
@@ -65,7 +68,7 @@ Vector2 Vector2::normalized() const {
|
||||
|
||||
bool Vector2::is_normalized() const {
|
||||
// use length_squared() instead of length() to avoid sqrt(), makes it more stringent.
|
||||
return Math::is_equal_approx(length_squared(), 1.0, UNIT_EPSILON);
|
||||
return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON);
|
||||
}
|
||||
|
||||
real_t Vector2::distance_to(const Vector2 &p_vector2) const {
|
||||
@@ -81,7 +84,7 @@ real_t Vector2::angle_to(const Vector2 &p_vector2) const {
|
||||
}
|
||||
|
||||
real_t Vector2::angle_to_point(const Vector2 &p_vector2) const {
|
||||
return Math::atan2(y - p_vector2.y, x - p_vector2.x);
|
||||
return (p_vector2 - *this).angle();
|
||||
}
|
||||
|
||||
real_t Vector2::dot(const Vector2 &p_other) const {
|
||||
@@ -93,7 +96,7 @@ real_t Vector2::cross(const Vector2 &p_other) const {
|
||||
}
|
||||
|
||||
Vector2 Vector2::sign() const {
|
||||
return Vector2(Math::sign(x), Math::sign(y));
|
||||
return Vector2(SIGN(x), SIGN(y));
|
||||
}
|
||||
|
||||
Vector2 Vector2::floor() const {
|
||||
@@ -108,7 +111,7 @@ Vector2 Vector2::round() const {
|
||||
return Vector2(Math::round(x), Math::round(y));
|
||||
}
|
||||
|
||||
Vector2 Vector2::rotated(real_t p_by) const {
|
||||
Vector2 Vector2::rotated(const real_t p_by) const {
|
||||
real_t sine = Math::sin(p_by);
|
||||
real_t cosi = Math::cos(p_by);
|
||||
return Vector2(
|
||||
@@ -128,14 +131,20 @@ Vector2 Vector2::project(const Vector2 &p_to) const {
|
||||
return p_to * (dot(p_to) / p_to.length_squared());
|
||||
}
|
||||
|
||||
Vector2 Vector2::clamp(const Vector2 &p_min, const Vector2 &p_max) const {
|
||||
return Vector2(
|
||||
CLAMP(x, p_min.x, p_max.x),
|
||||
CLAMP(y, p_min.y, p_max.y));
|
||||
}
|
||||
|
||||
Vector2 Vector2::snapped(const Vector2 &p_step) const {
|
||||
return Vector2(
|
||||
Math::snapped(x, p_step.x),
|
||||
Math::snapped(y, p_step.y));
|
||||
}
|
||||
|
||||
Vector2 Vector2::clamped(real_t p_len) const {
|
||||
real_t l = length();
|
||||
Vector2 Vector2::limit_length(const real_t p_len) const {
|
||||
const real_t l = length();
|
||||
Vector2 v = *this;
|
||||
if (l > 0 && p_len < l) {
|
||||
v /= l;
|
||||
@@ -145,35 +154,17 @@ Vector2 Vector2::clamped(real_t p_len) const {
|
||||
return v;
|
||||
}
|
||||
|
||||
Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const {
|
||||
Vector2 p0 = p_pre_a;
|
||||
Vector2 p1 = *this;
|
||||
Vector2 p2 = p_b;
|
||||
Vector2 p3 = p_post_b;
|
||||
|
||||
real_t t = p_weight;
|
||||
real_t t2 = t * t;
|
||||
real_t t3 = t2 * t;
|
||||
|
||||
Vector2 out;
|
||||
out = 0.5 * ((p1 * 2.0) +
|
||||
(-p0 + p2) * t +
|
||||
(2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 +
|
||||
(-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3);
|
||||
return out;
|
||||
}
|
||||
|
||||
Vector2 Vector2::move_toward(const Vector2 &p_to, const real_t p_delta) const {
|
||||
Vector2 v = *this;
|
||||
Vector2 vd = p_to - v;
|
||||
real_t len = vd.length();
|
||||
return len <= p_delta || len < CMP_EPSILON ? p_to : v + vd / len * p_delta;
|
||||
return len <= p_delta || len < (real_t)CMP_EPSILON ? p_to : v + vd / len * p_delta;
|
||||
}
|
||||
|
||||
// slide returns the component of the vector along the given plane, specified by its normal vector.
|
||||
Vector2 Vector2::slide(const Vector2 &p_normal) const {
|
||||
#ifdef MATH_CHECKS
|
||||
ERR_FAIL_COND_V(!p_normal.is_normalized(), Vector2());
|
||||
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 must be normalized.");
|
||||
#endif
|
||||
return *this - p_normal * this->dot(p_normal);
|
||||
}
|
||||
@@ -184,9 +175,9 @@ Vector2 Vector2::bounce(const Vector2 &p_normal) const {
|
||||
|
||||
Vector2 Vector2::reflect(const Vector2 &p_normal) const {
|
||||
#ifdef MATH_CHECKS
|
||||
ERR_FAIL_COND_V(!p_normal.is_normalized(), Vector2());
|
||||
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 must be normalized.");
|
||||
#endif
|
||||
return 2.0 * p_normal * this->dot(p_normal) - *this;
|
||||
return 2.0f * p_normal * this->dot(p_normal) - *this;
|
||||
}
|
||||
|
||||
bool Vector2::is_equal_approx(const Vector2 &p_v) const {
|
||||
@@ -194,7 +185,7 @@ bool Vector2::is_equal_approx(const Vector2 &p_v) const {
|
||||
}
|
||||
|
||||
Vector2::operator String() const {
|
||||
return String::num(x, 5) + ", " + String::num(y, 5);
|
||||
return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ")";
|
||||
}
|
||||
|
||||
Vector2::operator Vector2i() const {
|
||||
|
||||
@@ -30,12 +30,25 @@
|
||||
|
||||
#include <godot_cpp/variant/vector2i.hpp>
|
||||
|
||||
#include <godot_cpp/core/error_macros.hpp>
|
||||
#include <godot_cpp/variant/string.hpp>
|
||||
#include <godot_cpp/variant/vector2.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
Vector2i Vector2i::clamp(const Vector2i &p_min, const Vector2i &p_max) const {
|
||||
return Vector2i(
|
||||
CLAMP(x, p_min.x, p_max.x),
|
||||
CLAMP(y, p_min.y, p_max.y));
|
||||
}
|
||||
|
||||
int64_t Vector2i::length_squared() const {
|
||||
return x * (int64_t)x + y * (int64_t)y;
|
||||
}
|
||||
|
||||
double Vector2i::length() const {
|
||||
return Math::sqrt((double)length_squared());
|
||||
}
|
||||
|
||||
Vector2i Vector2i::operator+(const Vector2i &p_v) const {
|
||||
return Vector2i(x + p_v.x, y + p_v.y);
|
||||
}
|
||||
@@ -106,11 +119,11 @@ bool Vector2i::operator!=(const Vector2i &p_vec2) const {
|
||||
}
|
||||
|
||||
Vector2i::operator String() const {
|
||||
return String::num(x, 0) + ", " + String::num(y, 0);
|
||||
return "(" + itos(x) + ", " + itos(y) + ")";
|
||||
}
|
||||
|
||||
Vector2i::operator Vector2() const {
|
||||
return Vector2((real_t)x, (real_t)y);
|
||||
return Vector2((int32_t)x, (int32_t)y);
|
||||
}
|
||||
|
||||
} // namespace godot
|
||||
|
||||
@@ -30,97 +30,109 @@
|
||||
|
||||
#include <godot_cpp/variant/vector3.hpp>
|
||||
|
||||
#include <godot_cpp/core/error_macros.hpp>
|
||||
#include <godot_cpp/variant/basis.hpp>
|
||||
#include <godot_cpp/variant/string.hpp>
|
||||
#include <godot_cpp/variant/vector2.hpp>
|
||||
#include <godot_cpp/variant/vector3i.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
void Vector3::rotate(const Vector3 &p_axis, real_t p_phi) {
|
||||
*this = Basis(p_axis, p_phi).xform(*this);
|
||||
void Vector3::rotate(const Vector3 &p_axis, const real_t p_angle) {
|
||||
*this = Basis(p_axis, p_angle).xform(*this);
|
||||
}
|
||||
|
||||
Vector3 Vector3::rotated(const Vector3 &p_axis, real_t p_phi) const {
|
||||
Vector3 Vector3::rotated(const Vector3 &p_axis, const real_t p_angle) const {
|
||||
Vector3 r = *this;
|
||||
r.rotate(p_axis, p_phi);
|
||||
r.rotate(p_axis, p_angle);
|
||||
return r;
|
||||
}
|
||||
|
||||
void Vector3::set_axis(int p_axis, real_t p_value) {
|
||||
void Vector3::set_axis(const int p_axis, const real_t p_value) {
|
||||
ERR_FAIL_INDEX(p_axis, 3);
|
||||
coord[p_axis] = p_value;
|
||||
}
|
||||
|
||||
real_t Vector3::get_axis(int p_axis) const {
|
||||
real_t Vector3::get_axis(const int p_axis) const {
|
||||
ERR_FAIL_INDEX_V(p_axis, 3, 0);
|
||||
return operator[](p_axis);
|
||||
}
|
||||
|
||||
int Vector3::min_axis() const {
|
||||
return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2);
|
||||
Vector3 Vector3::clamp(const Vector3 &p_min, const Vector3 &p_max) const {
|
||||
return Vector3(
|
||||
CLAMP(x, p_min.x, p_max.x),
|
||||
CLAMP(y, p_min.y, p_max.y),
|
||||
CLAMP(z, p_min.z, p_max.z));
|
||||
}
|
||||
|
||||
int Vector3::max_axis() const {
|
||||
return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
|
||||
}
|
||||
|
||||
void Vector3::snap(Vector3 p_step) {
|
||||
void Vector3::snap(const Vector3 p_step) {
|
||||
x = Math::snapped(x, p_step.x);
|
||||
y = Math::snapped(y, p_step.y);
|
||||
z = Math::snapped(z, p_step.z);
|
||||
}
|
||||
|
||||
Vector3 Vector3::snapped(Vector3 p_step) const {
|
||||
Vector3 Vector3::snapped(const Vector3 p_step) const {
|
||||
Vector3 v = *this;
|
||||
v.snap(p_step);
|
||||
return v;
|
||||
}
|
||||
|
||||
Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const {
|
||||
Vector3 p0 = p_pre_a;
|
||||
Vector3 p1 = *this;
|
||||
Vector3 p2 = p_b;
|
||||
Vector3 p3 = p_post_b;
|
||||
Vector3 Vector3::limit_length(const real_t p_len) const {
|
||||
const real_t l = length();
|
||||
Vector3 v = *this;
|
||||
if (l > 0 && p_len < l) {
|
||||
v /= l;
|
||||
v *= p_len;
|
||||
}
|
||||
|
||||
real_t t = p_weight;
|
||||
real_t t2 = t * t;
|
||||
real_t t3 = t2 * t;
|
||||
|
||||
Vector3 out;
|
||||
out = 0.5 * ((p1 * 2.0) +
|
||||
(-p0 + p2) * t +
|
||||
(2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * t2 +
|
||||
(-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3);
|
||||
return out;
|
||||
return v;
|
||||
}
|
||||
|
||||
Vector3 Vector3::move_toward(const Vector3 &p_to, const real_t p_delta) const {
|
||||
Vector3 v = *this;
|
||||
Vector3 vd = p_to - v;
|
||||
real_t len = vd.length();
|
||||
return len <= p_delta || len < CMP_EPSILON ? p_to : v + vd / len * p_delta;
|
||||
return len <= p_delta || len < (real_t)CMP_EPSILON ? p_to : v + vd / len * p_delta;
|
||||
}
|
||||
|
||||
Basis Vector3::outer(const Vector3 &p_b) const {
|
||||
Vector3 row0(x * p_b.x, x * p_b.y, x * p_b.z);
|
||||
Vector3 row1(y * p_b.x, y * p_b.y, y * p_b.z);
|
||||
Vector3 row2(z * p_b.x, z * p_b.y, z * p_b.z);
|
||||
Vector2 Vector3::octahedron_encode() const {
|
||||
Vector3 n = *this;
|
||||
n /= Math::abs(n.x) + Math::abs(n.y) + Math::abs(n.z);
|
||||
Vector2 o;
|
||||
if (n.z >= 0.0f) {
|
||||
o.x = n.x;
|
||||
o.y = n.y;
|
||||
} else {
|
||||
o.x = (1.0f - Math::abs(n.y)) * (n.x >= 0.0f ? 1.0f : -1.0f);
|
||||
o.y = (1.0f - Math::abs(n.x)) * (n.y >= 0.0f ? 1.0f : -1.0f);
|
||||
}
|
||||
o.x = o.x * 0.5f + 0.5f;
|
||||
o.y = o.y * 0.5f + 0.5f;
|
||||
return o;
|
||||
}
|
||||
|
||||
Vector3 Vector3::octahedron_decode(const Vector2 &p_oct) {
|
||||
Vector2 f(p_oct.x * 2.0f - 1.0f, p_oct.y * 2.0f - 1.0f);
|
||||
Vector3 n(f.x, f.y, 1.0f - Math::abs(f.x) - Math::abs(f.y));
|
||||
float t = CLAMP(-n.z, 0.0f, 1.0f);
|
||||
n.x += n.x >= 0 ? -t : t;
|
||||
n.y += n.y >= 0 ? -t : t;
|
||||
return n.normalized();
|
||||
}
|
||||
|
||||
Basis Vector3::outer(const Vector3 &p_with) const {
|
||||
Vector3 row0(x * p_with.x, x * p_with.y, x * p_with.z);
|
||||
Vector3 row1(y * p_with.x, y * p_with.y, y * p_with.z);
|
||||
Vector3 row2(z * p_with.x, z * p_with.y, z * p_with.z);
|
||||
|
||||
return Basis(row0, row1, row2);
|
||||
}
|
||||
|
||||
Basis Vector3::to_diagonal_matrix() const {
|
||||
return Basis(x, 0, 0,
|
||||
0, y, 0,
|
||||
0, 0, z);
|
||||
}
|
||||
|
||||
bool Vector3::is_equal_approx(const Vector3 &p_v) const {
|
||||
return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y) && Math::is_equal_approx(z, p_v.z);
|
||||
}
|
||||
|
||||
Vector3::operator String() const {
|
||||
return (String::num(x, 5) + ", " + String::num(y, 5) + ", " + String::num(z, 5));
|
||||
return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ")";
|
||||
}
|
||||
|
||||
Vector3::operator Vector3i() const {
|
||||
|
||||
@@ -30,36 +30,42 @@
|
||||
|
||||
#include <godot_cpp/variant/vector3i.hpp>
|
||||
|
||||
#include <godot_cpp/core/error_macros.hpp>
|
||||
#include <godot_cpp/variant/string.hpp>
|
||||
#include <godot_cpp/variant/vector3.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
void Vector3i::set_axis(int p_axis, int32_t p_value) {
|
||||
void Vector3i::set_axis(const int p_axis, const int32_t p_value) {
|
||||
ERR_FAIL_INDEX(p_axis, 3);
|
||||
coord[p_axis] = p_value;
|
||||
}
|
||||
|
||||
int32_t Vector3i::get_axis(int p_axis) const {
|
||||
int32_t Vector3i::get_axis(const 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);
|
||||
Vector3i::Axis Vector3i::min_axis_index() const {
|
||||
return x < y ? (x < z ? Vector3i::AXIS_X : Vector3i::AXIS_Z) : (y < z ? Vector3i::AXIS_Y : Vector3i::AXIS_Z);
|
||||
}
|
||||
|
||||
int Vector3i::max_axis() const {
|
||||
return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
|
||||
Vector3i::Axis Vector3i::max_axis_index() const {
|
||||
return x < y ? (y < z ? Vector3i::AXIS_Z : Vector3i::AXIS_Y) : (x < z ? Vector3i::AXIS_Z : Vector3i::AXIS_X);
|
||||
}
|
||||
|
||||
Vector3i Vector3i::clamp(const Vector3i &p_min, const Vector3i &p_max) const {
|
||||
return Vector3i(
|
||||
CLAMP(x, p_min.x, p_max.x),
|
||||
CLAMP(y, p_min.y, p_max.y),
|
||||
CLAMP(z, p_min.z, p_max.z));
|
||||
}
|
||||
|
||||
Vector3i::operator String() const {
|
||||
return (String::num(x, 0) + ", " + String::num(y, 0) + ", " + String::num(z, 5));
|
||||
return "(" + itos(x) + ", " + itos(y) + ", " + itos(z) + ")";
|
||||
}
|
||||
|
||||
Vector3i::operator Vector3() const {
|
||||
return Vector3((real_t)x, (real_t)y, (real_t)z);
|
||||
return Vector3(x, y, z);
|
||||
}
|
||||
|
||||
} // namespace godot
|
||||
|
||||
106
src/variant/vector4.cpp
Normal file
106
src/variant/vector4.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
/*************************************************************************/
|
||||
/* vector4.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2022 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 <godot_cpp/variant/vector4.hpp>
|
||||
|
||||
#include <godot_cpp/variant/basis.hpp>
|
||||
#include <godot_cpp/variant/vector4i.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
bool Vector4::is_equal_approx(const Vector4 &p_vec4) const {
|
||||
return Math::is_equal_approx(x, p_vec4.x) && Math::is_equal_approx(y, p_vec4.y) && Math::is_equal_approx(z, p_vec4.z) && Math::is_equal_approx(w, p_vec4.w);
|
||||
}
|
||||
|
||||
real_t Vector4::length() const {
|
||||
return Math::sqrt(length_squared());
|
||||
}
|
||||
|
||||
void Vector4::normalize() {
|
||||
*this /= length();
|
||||
}
|
||||
|
||||
Vector4 Vector4::normalized() const {
|
||||
return *this / length();
|
||||
}
|
||||
|
||||
bool Vector4::is_normalized() const {
|
||||
return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); // use less epsilon
|
||||
}
|
||||
|
||||
Vector4 Vector4::abs() const {
|
||||
return Vector4(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w));
|
||||
}
|
||||
|
||||
Vector4 Vector4::sign() const {
|
||||
return Vector4(Math::sign(x), Math::sign(y), Math::sign(z), Math::sign(w));
|
||||
}
|
||||
|
||||
Vector4 Vector4::inverse() const {
|
||||
return Vector4(1.0f / x, 1.0f / y, 1.0f / z, 1.0f / w);
|
||||
}
|
||||
|
||||
Vector4::Axis Vector4::min_axis_index() const {
|
||||
uint32_t min_index = 0;
|
||||
real_t min_value = x;
|
||||
for (uint32_t i = 1; i < 4; i++) {
|
||||
if (operator[](i) < min_value) {
|
||||
min_index = i;
|
||||
min_value = operator[](i);
|
||||
}
|
||||
}
|
||||
return Vector4::Axis(min_index);
|
||||
}
|
||||
|
||||
Vector4::Axis Vector4::max_axis_index() const {
|
||||
uint32_t max_index = 0;
|
||||
real_t max_value = x;
|
||||
for (uint32_t i = 1; i < 4; i++) {
|
||||
if (operator[](i) > max_value) {
|
||||
max_index = i;
|
||||
max_value = operator[](i);
|
||||
}
|
||||
}
|
||||
return Vector4::Axis(max_index);
|
||||
}
|
||||
|
||||
Vector4 Vector4::clamp(const Vector4 &p_min, const Vector4 &p_max) const {
|
||||
return Vector4(
|
||||
CLAMP(x, p_min.x, p_max.x),
|
||||
CLAMP(y, p_min.y, p_max.y),
|
||||
CLAMP(z, p_min.z, p_max.z),
|
||||
CLAMP(w, p_min.w, p_max.w));
|
||||
}
|
||||
|
||||
Vector4::operator String() const {
|
||||
return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ", " + String::num_real(w, false) + ")";
|
||||
}
|
||||
|
||||
} // namespace godot
|
||||
95
src/variant/vector4i.cpp
Normal file
95
src/variant/vector4i.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
/*************************************************************************/
|
||||
/* vector4i.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2022 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 <godot_cpp/variant/vector4i.hpp>
|
||||
|
||||
#include <godot_cpp/variant/string.hpp>
|
||||
#include <godot_cpp/variant/vector4.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
void Vector4i::set_axis(const int p_axis, const int32_t p_value) {
|
||||
ERR_FAIL_INDEX(p_axis, 4);
|
||||
coord[p_axis] = p_value;
|
||||
}
|
||||
|
||||
int32_t Vector4i::get_axis(const int p_axis) const {
|
||||
ERR_FAIL_INDEX_V(p_axis, 4, 0);
|
||||
return operator[](p_axis);
|
||||
}
|
||||
|
||||
Vector4i::Axis Vector4i::min_axis_index() const {
|
||||
uint32_t min_index = 0;
|
||||
int32_t min_value = x;
|
||||
for (uint32_t i = 1; i < 4; i++) {
|
||||
if (operator[](i) < min_value) {
|
||||
min_index = i;
|
||||
min_value = operator[](i);
|
||||
}
|
||||
}
|
||||
return Vector4i::Axis(min_index);
|
||||
}
|
||||
|
||||
Vector4i::Axis Vector4i::max_axis_index() const {
|
||||
uint32_t max_index = 0;
|
||||
int32_t max_value = x;
|
||||
for (uint32_t i = 1; i < 4; i++) {
|
||||
if (operator[](i) > max_value) {
|
||||
max_index = i;
|
||||
max_value = operator[](i);
|
||||
}
|
||||
}
|
||||
return Vector4i::Axis(max_index);
|
||||
}
|
||||
|
||||
Vector4i Vector4i::clamp(const Vector4i &p_min, const Vector4i &p_max) const {
|
||||
return Vector4i(
|
||||
CLAMP(x, p_min.x, p_max.x),
|
||||
CLAMP(y, p_min.y, p_max.y),
|
||||
CLAMP(z, p_min.z, p_max.z),
|
||||
CLAMP(w, p_min.w, p_max.w));
|
||||
}
|
||||
|
||||
Vector4i::operator String() const {
|
||||
return "(" + itos(x) + ", " + itos(y) + ", " + itos(z) + ", " + itos(w) + ")";
|
||||
}
|
||||
|
||||
Vector4i::operator Vector4() const {
|
||||
return Vector4(x, y, z, w);
|
||||
}
|
||||
|
||||
Vector4i::Vector4i(const Vector4 &p_vec4) {
|
||||
x = p_vec4.x;
|
||||
y = p_vec4.y;
|
||||
z = p_vec4.z;
|
||||
w = p_vec4.w;
|
||||
}
|
||||
|
||||
} // namespace godot
|
||||
Reference in New Issue
Block a user