From e3890a6c1471e393ba39cc431e3d1f62de0280b8 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 18 Dec 2012 23:15:48 +0100 Subject: [PATCH] [DEBUG] some matrix and vector corections --- etk/math/Matrix4.cpp | 39 ++++++++++++++++++-- etk/math/Matrix4.h | 4 +- etk/math/Vector2D.h | 87 ++++++++++++++++++++++++++++++++++++-------- etk/math/Vector3D.h | 16 ++++---- 4 files changed, 117 insertions(+), 29 deletions(-) diff --git a/etk/math/Matrix4.cpp b/etk/math/Matrix4.cpp index b5c9b40..1407068 100644 --- a/etk/math/Matrix4.cpp +++ b/etk/math/Matrix4.cpp @@ -12,6 +12,11 @@ #include +void etk::Matrix4::Rotate(etk::Vector3D vect, float angleRad) +{ + etk::Matrix4 tmpMat = etk::matRotate(vect, angleRad); + *this *= tmpMat; +} void etk::Matrix4::Rotate(etk::Vector3D& vect, float angleRad) { @@ -21,13 +26,41 @@ void etk::Matrix4::Rotate(etk::Vector3D& vect, float angleRad) void etk::Matrix4::Translate(etk::Vector3D& vect) { - etk::Matrix4 tmpMat = etk::matScale(vect); + etk::Matrix4 tmpMat = etk::matTranslate(vect); *this *= tmpMat; } +etk::Matrix4 etk::matPerspective(float fovx, float aspect, float zNear, float zFar) +{ + etk::Matrix4 tmp; + for(int32_t iii=0; iii<4*4 ; iii++) { + tmp.m_mat[iii] = 0; + } + + float xmax = zNear * tanf(fovx * M_PI / 360.0); + float xmin = -xmax; + + float ymin = xmin / aspect; + float ymax = xmax / aspect; + + // 0 1 2 3 + // 4 5 6 7 + // 8 9 10 11 + // 12 13 14 15 + + tmp.m_mat[0] = (2.0 * zNear) / (xmax - xmin); + tmp.m_mat[5] = (2.0 * zNear) / (ymax - ymin); + tmp.m_mat[10] = -(zFar + zNear) / (zFar - zNear); + tmp.m_mat[2] = (xmax + xmin) / (xmax - xmin); + tmp.m_mat[6] = (ymax + ymin) / (ymax - ymin); + tmp.m_mat[14] = -1.0; + tmp.m_mat[11] = -(2.0 * zFar * zNear) / (zFar - zNear); + + return tmp; +} -etk::Matrix4 etk::matPerspective(float left, float right, float bottom, float top, float nearVal, float farVal) +etk::Matrix4 etk::matOrtho(float left, float right, float bottom, float top, float nearVal, float farVal) { etk::Matrix4 tmp; for(int32_t iii=0; iii<4*4 ; iii++) { @@ -40,8 +73,6 @@ etk::Matrix4 etk::matPerspective(float left, float right, float bottom, float to tmp.m_mat[7] = -1*(top + bottom) / (top - bottom); tmp.m_mat[11] = -1*(farVal + nearVal) / (farVal - nearVal); tmp.m_mat[15] = 1; - //TK_INFO("Perspective :"); - //etk::matrix::Display(tmp); return tmp; } diff --git a/etk/math/Matrix4.h b/etk/math/Matrix4.h index 37a5c31..30b6f4a 100644 --- a/etk/math/Matrix4.h +++ b/etk/math/Matrix4.h @@ -266,6 +266,7 @@ namespace etk * @param[in] angleRad angle to apply. */ void Rotate(etk::Vector3D& vect, float angleRad=0.0); + void Rotate(etk::Vector3D vect, float angleRad=0.0); /** * @brief Makes a translation of the matrix * @param[in] vect Translation to apply. @@ -290,7 +291,8 @@ namespace etk */ Matrix4 Invert(void); }; - Matrix4 matPerspective(float left, float right, float bottom, float top, float nearVal, float farVal); + Matrix4 matPerspective(float fovx, float aspect, float zNear, float zFar); + Matrix4 matOrtho(float left, float right, float bottom, float top, float nearVal, float farVal); Matrix4 matTranslate(etk::Vector3D vect); Matrix4 matScale(etk::Vector3D vect); Matrix4 matRotate(etk::Vector3D vect, float angleRad=0.0); diff --git a/etk/math/Vector2D.h b/etk/math/Vector2D.h index 297e71e..1748850 100644 --- a/etk/math/Vector2D.h +++ b/etk/math/Vector2D.h @@ -33,8 +33,13 @@ namespace etk * = assigment *****************************************************/ const Vector2D& operator= (const Vector2D& obj ) { - x = (T)obj.x; - y = (T)obj.y; + x = obj.x; + y = obj.y; + return *this; + } + const Vector2D& operator= (const T val ) { + x = val; + y = val; return *this; } /***************************************************** @@ -59,8 +64,13 @@ namespace etk * += operator *****************************************************/ const Vector2D& operator+= (const Vector2D& obj) { - x += (T)obj.x; - y += (T)obj.y; + x += obj.x; + y += obj.y; + return *this; + } + const Vector2D& operator+= (const T val) { + x += val; + y += val; return *this; } /***************************************************** @@ -68,16 +78,27 @@ namespace etk *****************************************************/ Vector2D operator+ (const Vector2D& obj) { Vector2D tmpp(x,y); - tmpp.x += (T)obj.x; - tmpp.y += (T)obj.y; + tmpp.x += obj.x; + tmpp.y += obj.y; + return tmpp; + } + Vector2D operator+ (const T val) { + Vector2D tmpp(x,y); + tmpp.x += val; + tmpp.y += val; return tmpp; } /***************************************************** * -= operator *****************************************************/ const Vector2D& operator-= (const Vector2D& obj) { - x -= (T)obj.x; - y -= (T)obj.y; + x -= obj.x; + y -= obj.y; + return *this; + } + const Vector2D& operator-= (const T val) { + x -= val; + y -= val; return *this; } /***************************************************** @@ -85,16 +106,33 @@ namespace etk *****************************************************/ Vector2D operator- (const Vector2D& obj) { Vector2D tmpp(x,y); - tmpp.x -= (T)obj.x; - tmpp.y -= (T)obj.y; + tmpp.x -= obj.x; + tmpp.y -= obj.y; + return tmpp; + } + Vector2D operator- (const T val) { + Vector2D tmpp(x,y); + tmpp.x -= val; + tmpp.y -= val; return tmpp; } /***************************************************** * /= operator *****************************************************/ const Vector2D& operator/= (const Vector2D& obj) { - x /= (T)obj.x; - y /= (T)obj.y; + if (obj.x!=0) { + x /= obj.x; + } + if (obj.y!=0) { + y /= obj.y; + } + return *this; + } + const Vector2D& operator/= (const T val) { + if (val != 0) { + x /= val; + y /= val; + } return *this; } /***************************************************** @@ -106,12 +144,23 @@ namespace etk tmpp.y /= (T)obj.y; return tmpp; } + Vector2D operator/ (const T val) { + Vector2D tmpp(x,y); + tmpp.x /= val; + tmpp.y /= val; + return tmpp; + } /***************************************************** * *= operator *****************************************************/ const Vector2D& operator*= (const Vector2D& obj) { - x *= (T)obj.x; - y *= (T)obj.y; + x *= obj.x; + y *= obj.y; + return *this; + } + const Vector2D& operator*= (const T val) { + x *= val; + y *= val; return *this; } /***************************************************** @@ -119,8 +168,14 @@ namespace etk *****************************************************/ Vector2D operator* (const Vector2D& obj) { Vector2D tmpp(x,y); - tmpp.x *= (T)obj.x; - tmpp.y *= (T)obj.y; + tmpp.x *= obj.x; + tmpp.y *= obj.y; + return tmpp; + } + Vector2D operator* (const T val) { + Vector2D tmpp(x,y); + tmpp.x *= val; + tmpp.y *= val; return tmpp; } /***************************************************** diff --git a/etk/math/Vector3D.h b/etk/math/Vector3D.h index c9210fe..761f149 100644 --- a/etk/math/Vector3D.h +++ b/etk/math/Vector3D.h @@ -66,7 +66,7 @@ namespace etk z += (T)obj.z; return *this; } - const Vector3D& operator+= (const float val) { + const Vector3D& operator+= (const T val) { x += val; y += val; z += val; @@ -82,7 +82,7 @@ namespace etk tmpp.z += (T)obj.z; return tmpp; } - Vector3D operator+ (const float val) { + Vector3D operator+ (const T val) { Vector3D tmpp(x,y,z); tmpp.x += val; tmpp.y += val; @@ -98,7 +98,7 @@ namespace etk z -= (T)obj.z; return *this; } - const Vector3D& operator-= (const float val) { + const Vector3D& operator-= (const T val) { x -= val; y -= val; z -= val; @@ -114,7 +114,7 @@ namespace etk tmpp.z -= (T)obj.z; return tmpp; } - Vector3D operator- (const float val) { + Vector3D operator- (const T val) { Vector3D tmpp(x,y,z); tmpp.x -= val; tmpp.y -= val; @@ -136,7 +136,7 @@ namespace etk } return *this; } - const Vector3D& operator/= (const float val) { + const Vector3D& operator/= (const T val) { if (val==0) { return *this; } @@ -161,7 +161,7 @@ namespace etk } return tmpp; } - Vector3D operator/ (const float val) { + Vector3D operator/ (const T val) { Vector3D tmpp(x,y,z); if (val==0) { return tmpp; @@ -180,7 +180,7 @@ namespace etk z *= (T)obj.z; return *this; } - const Vector3D& operator*= (const float val) { + const Vector3D& operator*= (const T val) { x *= val; y *= val; z *= val; @@ -196,7 +196,7 @@ namespace etk tmpp.z *= (T)obj.z; return tmpp; } - Vector3D operator* (const float val) { + Vector3D operator* (const T val) { Vector3D tmpp(x,y,z); tmpp.x *= val; tmpp.y *= val;