summaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2017-01-02 21:38:20 +0100
committerRémi Verschelde <rverschelde@gmail.com>2017-01-02 21:52:26 +0100
commit3f3f5a5359973e95e94148676a9793d6f52468f3 (patch)
tree65adf17c3d3f8d3a83bec29f51142fe884e942d8 /core/math
parentdb46a344180d4eae1455e97e22bf84c9c304be7c (diff)
parent2820b2d82b2ed747011e37c543aefc6d4d4edee9 (diff)
Merge remote-tracking branch 'origin/gles3' into gles3-on-master
Various merge conflicts have been fixed manually and some mistakes might have been made - time will tell :)
Diffstat (limited to 'core/math')
-rw-r--r--core/math/camera_matrix.cpp33
-rw-r--r--core/math/camera_matrix.h5
-rw-r--r--core/math/math_funcs.h102
3 files changed, 138 insertions, 2 deletions
diff --git a/core/math/camera_matrix.cpp b/core/math/camera_matrix.cpp
index 1ed787160c..7910f5fafc 100644
--- a/core/math/camera_matrix.cpp
+++ b/core/math/camera_matrix.cpp
@@ -54,7 +54,7 @@ void CameraMatrix::set_zero() {
}
-Plane CameraMatrix::xform4(const Plane& p_vec4) {
+Plane CameraMatrix::xform4(const Plane& p_vec4) const {
Plane ret;
@@ -495,6 +495,28 @@ void CameraMatrix::set_light_bias() {
}
+void CameraMatrix::set_light_atlas_rect(const Rect2& p_rect) {
+
+ float *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.pos.x,
+ m[13]=p_rect.pos.y,
+ m[14]=0.0,
+ m[15]=1.0;
+}
+
CameraMatrix::operator String() const {
String str;
@@ -512,6 +534,15 @@ float CameraMatrix::get_aspect() const {
return w/h;
}
+int CameraMatrix::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);
+
+}
+
float CameraMatrix::get_fov() const {
const float * matrix = (const float*)this->matrix;
diff --git a/core/math/camera_matrix.h b/core/math/camera_matrix.h
index 246a5faba5..f9fb4837f1 100644
--- a/core/math/camera_matrix.h
+++ b/core/math/camera_matrix.h
@@ -30,6 +30,7 @@
#define CAMERA_MATRIX_H
#include "transform.h"
+#include "math_2d.h"
/**
@author Juan Linietsky <reduzio@gmail.com>
*/
@@ -53,6 +54,7 @@ struct CameraMatrix {
void set_identity();
void set_zero();
void set_light_bias();
+ void set_light_atlas_rect(const Rect2& p_rect);
void set_perspective(float p_fovy_degrees, float p_aspect, float p_z_near, float p_z_far,bool p_flip_fov=false);
void set_orthogonal(float p_left, float p_right, float p_bottom, float p_top, float p_znear, float p_zfar);
void set_orthogonal(float p_size, float p_aspect, float p_znear, float p_zfar,bool p_flip_fov=false);
@@ -78,13 +80,14 @@ struct CameraMatrix {
CameraMatrix operator*(const CameraMatrix& p_matrix) const;
- Plane xform4(const Plane& p_vec4);
+ Plane xform4(const Plane& p_vec4) const;
_FORCE_INLINE_ Vector3 xform(const Vector3& p_vec3) const;
operator String() const;
void scale_translate_to_fit(const AABB& p_aabb);
void make_scale(const Vector3 &p_scale);
+ int get_pixels_per_meter(int p_for_pixel_width) const;
operator Transform() const;
CameraMatrix();
diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h
index a5195e554b..ec0ed39471 100644
--- a/core/math/math_funcs.h
+++ b/core/math/math_funcs.h
@@ -175,6 +175,108 @@ public:
static double log(double x);
static double exp(double x);
+
+ static _FORCE_INLINE_ uint32_t halfbits_to_floatbits(uint16_t h)
+ {
+ uint16_t h_exp, h_sig;
+ uint32_t f_sgn, f_exp, f_sig;
+
+ h_exp = (h&0x7c00u);
+ f_sgn = ((uint32_t)h&0x8000u) << 16;
+ switch (h_exp) {
+ case 0x0000u: /* 0 or subnormal */
+ h_sig = (h&0x03ffu);
+ /* Signed zero */
+ if (h_sig == 0) {
+ return f_sgn;
+ }
+ /* Subnormal */
+ h_sig <<= 1;
+ while ((h_sig&0x0400u) == 0) {
+ h_sig <<= 1;
+ h_exp++;
+ }
+ f_exp = ((uint32_t)(127 - 15 - h_exp)) << 23;
+ f_sig = ((uint32_t)(h_sig&0x03ffu)) << 13;
+ return f_sgn + f_exp + f_sig;
+ case 0x7c00u: /* inf or NaN */
+ /* All-ones exponent and a copy of the significand */
+ return f_sgn + 0x7f800000u + (((uint32_t)(h&0x03ffu)) << 13);
+ default: /* normalized */
+ /* Just need to adjust the exponent and shift */
+ return f_sgn + (((uint32_t)(h&0x7fffu) + 0x1c000u) << 13);
+ }
+ }
+
+ static _FORCE_INLINE_ float halfptr_to_float(const uint16_t *h) {
+
+ union {
+ uint32_t u32;
+ float f32;
+ } u;
+
+ u.u32=halfbits_to_floatbits(*h);
+ return u.f32;
+ }
+
+ static _FORCE_INLINE_ uint16_t make_half_float(float f) {
+
+ union {
+ float fv;
+ uint32_t ui;
+ } ci;
+ ci.fv=f;
+
+ uint32_t x = ci.ui;
+ uint32_t sign = (unsigned short)(x >> 31);
+ uint32_t mantissa;
+ uint32_t exp;
+ uint16_t hf;
+
+ // get mantissa
+ mantissa = x & ((1 << 23) - 1);
+ // get exponent bits
+ exp = x & (0xFF << 23);
+ if (exp >= 0x47800000)
+ {
+ // check if the original single precision float number is a NaN
+ if (mantissa && (exp == (0xFF << 23)))
+ {
+ // we have a single precision NaN
+ mantissa = (1 << 23) - 1;
+ }
+ else
+ {
+ // 16-bit half-float representation stores number as Inf
+ mantissa = 0;
+ }
+ hf = (((uint16_t)sign) << 15) | (uint16_t)((0x1F << 10)) |
+ (uint16_t)(mantissa >> 13);
+ }
+ // check if exponent is <= -15
+ else if (exp <= 0x38000000)
+ {
+
+ /*// store a denorm half-float value or zero
+ exp = (0x38000000 - exp) >> 23;
+ mantissa >>= (14 + exp);
+
+ hf = (((uint16_t)sign) << 15) | (uint16_t)(mantissa);
+ */
+ hf=0; //denormals do not work for 3D, convert to zero
+ }
+ else
+ {
+ hf = (((uint16_t)sign) << 15) |
+ (uint16_t)((exp - 0x38000000) >> 13) |
+ (uint16_t)(mantissa >> 13);
+ }
+
+ return hf;
+ }
+
+
+
};