From 8b00d67febf133e89f6a0bfabc41feed555dc4a9 Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 12 Jan 2019 21:48:33 +0800 Subject: =?UTF-8?q?*=E5=8E=BB=E6=8E=89=E6=96=87=E4=BB=B6=E5=89=8D=E7=BC=80?= =?UTF-8?q?je=5F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/math/bbox.h | 30 ++++++ src/libjin/math/bezier_curve.cpp | 0 src/libjin/math/bezier_curve.h | 17 ++++ src/libjin/math/constant.h | 10 ++ src/libjin/math/ellipse.cpp | 0 src/libjin/math/ellipse.h | 0 src/libjin/math/je_bbox.h | 30 ------ src/libjin/math/je_bezier_curve.cpp | 0 src/libjin/math/je_bezier_curve.h | 17 ---- src/libjin/math/je_constant.h | 10 -- src/libjin/math/je_ellipse.cpp | 0 src/libjin/math/je_ellipse.h | 0 src/libjin/math/je_math.h | 115 --------------------- src/libjin/math/je_matrix.cpp | 195 ------------------------------------ src/libjin/math/je_matrix.h | 160 ----------------------------- src/libjin/math/je_percentage.h | 36 ------- src/libjin/math/je_quad.h | 31 ------ src/libjin/math/je_random.cpp | 54 ---------- src/libjin/math/je_random.h | 33 ------ src/libjin/math/je_ranged_value.cpp | 71 ------------- src/libjin/math/je_ranged_value.h | 57 ----------- src/libjin/math/je_transform.cpp | 104 ------------------- src/libjin/math/je_transform.h | 50 --------- src/libjin/math/je_vector2.hpp | 82 --------------- src/libjin/math/je_vector3.hpp | 51 ---------- src/libjin/math/je_vector4.hpp | 57 ----------- src/libjin/math/math.h | 115 +++++++++++++++++++++ src/libjin/math/matrix.cpp | 195 ++++++++++++++++++++++++++++++++++++ src/libjin/math/matrix.h | 160 +++++++++++++++++++++++++++++ src/libjin/math/percentage.h | 36 +++++++ src/libjin/math/quad.h | 31 ++++++ src/libjin/math/random.cpp | 54 ++++++++++ src/libjin/math/random.h | 33 ++++++ src/libjin/math/ranged_value.cpp | 71 +++++++++++++ src/libjin/math/ranged_value.h | 57 +++++++++++ src/libjin/math/transform.cpp | 104 +++++++++++++++++++ src/libjin/math/transform.h | 50 +++++++++ src/libjin/math/vector2.hpp | 82 +++++++++++++++ src/libjin/math/vector3.hpp | 51 ++++++++++ src/libjin/math/vector4.hpp | 57 +++++++++++ 40 files changed, 1153 insertions(+), 1153 deletions(-) create mode 100644 src/libjin/math/bbox.h create mode 100644 src/libjin/math/bezier_curve.cpp create mode 100644 src/libjin/math/bezier_curve.h create mode 100644 src/libjin/math/constant.h create mode 100644 src/libjin/math/ellipse.cpp create mode 100644 src/libjin/math/ellipse.h delete mode 100644 src/libjin/math/je_bbox.h delete mode 100644 src/libjin/math/je_bezier_curve.cpp delete mode 100644 src/libjin/math/je_bezier_curve.h delete mode 100644 src/libjin/math/je_constant.h delete mode 100644 src/libjin/math/je_ellipse.cpp delete mode 100644 src/libjin/math/je_ellipse.h delete mode 100644 src/libjin/math/je_math.h delete mode 100644 src/libjin/math/je_matrix.cpp delete mode 100644 src/libjin/math/je_matrix.h delete mode 100644 src/libjin/math/je_percentage.h delete mode 100644 src/libjin/math/je_quad.h delete mode 100644 src/libjin/math/je_random.cpp delete mode 100644 src/libjin/math/je_random.h delete mode 100644 src/libjin/math/je_ranged_value.cpp delete mode 100644 src/libjin/math/je_ranged_value.h delete mode 100644 src/libjin/math/je_transform.cpp delete mode 100644 src/libjin/math/je_transform.h delete mode 100644 src/libjin/math/je_vector2.hpp delete mode 100644 src/libjin/math/je_vector3.hpp delete mode 100644 src/libjin/math/je_vector4.hpp create mode 100644 src/libjin/math/math.h create mode 100644 src/libjin/math/matrix.cpp create mode 100644 src/libjin/math/matrix.h create mode 100644 src/libjin/math/percentage.h create mode 100644 src/libjin/math/quad.h create mode 100644 src/libjin/math/random.cpp create mode 100644 src/libjin/math/random.h create mode 100644 src/libjin/math/ranged_value.cpp create mode 100644 src/libjin/math/ranged_value.h create mode 100644 src/libjin/math/transform.cpp create mode 100644 src/libjin/math/transform.h create mode 100644 src/libjin/math/vector2.hpp create mode 100644 src/libjin/math/vector3.hpp create mode 100644 src/libjin/math/vector4.hpp (limited to 'src/libjin/math') diff --git a/src/libjin/math/bbox.h b/src/libjin/math/bbox.h new file mode 100644 index 0000000..ce88080 --- /dev/null +++ b/src/libjin/math/bbox.h @@ -0,0 +1,30 @@ +#ifndef __JE_BBOX_H__ +#define __JE_BBOX_H__ + +namespace JinEngine +{ + namespace Math + { + + /// + /// + /// + struct BBox + { + BBox() + : l(0), r(0), t(0), b(0) + { + } + + BBox(float _l, float _r, float _t, float _b) + : l(+l), r(_r), t(_t), b(_b) + { + } + + float l, r, t, b; + }; + + } // namespace Math +} // namespace JinEngine + +#endif // __JE_BBOX_H__ \ No newline at end of file diff --git a/src/libjin/math/bezier_curve.cpp b/src/libjin/math/bezier_curve.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/math/bezier_curve.h b/src/libjin/math/bezier_curve.h new file mode 100644 index 0000000..6925baf --- /dev/null +++ b/src/libjin/math/bezier_curve.h @@ -0,0 +1,17 @@ +#ifndef __JIN_BEZIER_CURVE_H__ +#define __JIN_BEZIER_CURVE_H__ + +namespace JinEngine +{ + namespace Math + { + + class BezierCurve + { + + }; + + } // namespace Math +} // namespace JinEngine + +#endif \ No newline at end of file diff --git a/src/libjin/math/constant.h b/src/libjin/math/constant.h new file mode 100644 index 0000000..45577e7 --- /dev/null +++ b/src/libjin/math/constant.h @@ -0,0 +1,10 @@ +#ifndef __JE_MATH_CONSTANT_H__ +#define __JE_MATH_CONSTANT_H__ + +#define PI 3.1415926f + +// int16 范围 +#define INT16_RANGE_LEFT -32768 +#define INT16_RANGE_RIGHT 32767 + +#endif \ No newline at end of file diff --git a/src/libjin/math/ellipse.cpp b/src/libjin/math/ellipse.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/math/ellipse.h b/src/libjin/math/ellipse.h new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/math/je_bbox.h b/src/libjin/math/je_bbox.h deleted file mode 100644 index ce88080..0000000 --- a/src/libjin/math/je_bbox.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef __JE_BBOX_H__ -#define __JE_BBOX_H__ - -namespace JinEngine -{ - namespace Math - { - - /// - /// - /// - struct BBox - { - BBox() - : l(0), r(0), t(0), b(0) - { - } - - BBox(float _l, float _r, float _t, float _b) - : l(+l), r(_r), t(_t), b(_b) - { - } - - float l, r, t, b; - }; - - } // namespace Math -} // namespace JinEngine - -#endif // __JE_BBOX_H__ \ No newline at end of file diff --git a/src/libjin/math/je_bezier_curve.cpp b/src/libjin/math/je_bezier_curve.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/libjin/math/je_bezier_curve.h b/src/libjin/math/je_bezier_curve.h deleted file mode 100644 index 6925baf..0000000 --- a/src/libjin/math/je_bezier_curve.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef __JIN_BEZIER_CURVE_H__ -#define __JIN_BEZIER_CURVE_H__ - -namespace JinEngine -{ - namespace Math - { - - class BezierCurve - { - - }; - - } // namespace Math -} // namespace JinEngine - -#endif \ No newline at end of file diff --git a/src/libjin/math/je_constant.h b/src/libjin/math/je_constant.h deleted file mode 100644 index 45577e7..0000000 --- a/src/libjin/math/je_constant.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef __JE_MATH_CONSTANT_H__ -#define __JE_MATH_CONSTANT_H__ - -#define PI 3.1415926f - -// int16 范围 -#define INT16_RANGE_LEFT -32768 -#define INT16_RANGE_RIGHT 32767 - -#endif \ No newline at end of file diff --git a/src/libjin/math/je_ellipse.cpp b/src/libjin/math/je_ellipse.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/libjin/math/je_ellipse.h b/src/libjin/math/je_ellipse.h deleted file mode 100644 index e69de29..0000000 diff --git a/src/libjin/math/je_math.h b/src/libjin/math/je_math.h deleted file mode 100644 index 4036696..0000000 --- a/src/libjin/math/je_math.h +++ /dev/null @@ -1,115 +0,0 @@ -#ifndef __JE_UTILS_MATH_H__ -#define __JE_UTILS_MATH_H__ - -#include "je_constant.h" -#include "je_matrix.h" -#include "je_quad.h" - -namespace JinEngine -{ - namespace Math - { - - #ifdef min - #undef min - #endif // min - #ifdef max - #undef max - #endif // max - - template - inline T min(T a, T b) - { - return a < b ? a : b; - } - - template - inline T max(T a, T b) - { - return a > b ? a : b; - } - - template - inline T clamp(T a, T mi, T ma) - { - return min(max(a, mi), ma); - } - - template - inline bool within(T a, T mi, T ma) - { - return a >= mi && a <= ma; - } - - template - inline bool without(T a, T mi, T ma) - { - return a < mi || a > ma; - } - - template - inline T lowerBound(T a, T lower) - { - return a < lower ? lower : a; - } - - template - inline T upperBound(T a, T upper) - { - return a > upper ? upper : a; - } - - inline float lerp(float a, float b, float f) - { - f = clamp(f, 0, 1); - return a + f * (b - a); - } - - template - inline T abs(T a) - { - return a > 0 ? a : -a; - } - - template - inline T reverse(T a) - { - return -a; - } - - template - inline T lerp(T a, T b, float t) - { - return a + t * (b - a); - } - - template - inline T slerp(T start, T end, float percent) - { - // Dot product - the cosine of the angle between 2 vectors. - float dot = start * end; - // Clamp it to be in the range of Acos() - // This may be unnecessary, but floating point - // precision can be a fickle mistress. - dot = clamp(dot, -1.0f, 1.0f); - // Acos(dot) returns the angle between start and end, - // And multiplying that by percent returns the angle between - // start and the final result. - float theta = Mathf.Acos(dot)*percent; - Vector3 RelativeVec = end - start * dot; - RelativeVec.Normalize(); - // Orthonormal basis - // The final result. - return ((start*Mathf.Cos(theta)) + (RelativeVec*Mathf.Sin(theta))); - } - - template - inline T nlerp(T a, T b, float t) - { - - } - - } // namespace Math -} // namespace JinEngine - -#endif // __JE_UTILS_MATH_H__ \ No newline at end of file diff --git a/src/libjin/math/je_matrix.cpp b/src/libjin/math/je_matrix.cpp deleted file mode 100644 index ad7d1e5..0000000 --- a/src/libjin/math/je_matrix.cpp +++ /dev/null @@ -1,195 +0,0 @@ -#include "../graphics/je_vertex.h" - -#include "je_matrix.h" - -#include // memcpy -#include - -namespace JinEngine -{ - namespace Math - { - - const Matrix Matrix::Identity; - - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - - Matrix::Matrix() - { - setIdentity(); - } - - Matrix::~Matrix() - { - } - - void Matrix::setOrtho(float l, float r, float b, float t, float n, float f) - { - setIdentity(); - float w = r - l; - float h = t - b; - float z = f - n; - e[0] = 2 / w; - e[5] = 2 / h; - e[10] = -2 / z; - e[12] = -(r + l) / w; - e[13] = -(t + b) / h; - e[14] = -(f + n) / z; - e[15] = 1; - } - - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - - Matrix Matrix::operator * (const Matrix & m) const - { - Matrix t; - - t.e[0] = (e[0] * m.e[0]) + (e[4] * m.e[1]) + (e[8] * m.e[2]) + (e[12] * m.e[3]); - t.e[4] = (e[0] * m.e[4]) + (e[4] * m.e[5]) + (e[8] * m.e[6]) + (e[12] * m.e[7]); - t.e[8] = (e[0] * m.e[8]) + (e[4] * m.e[9]) + (e[8] * m.e[10]) + (e[12] * m.e[11]); - t.e[12] = (e[0] * m.e[12]) + (e[4] * m.e[13]) + (e[8] * m.e[14]) + (e[12] * m.e[15]); - - t.e[1] = (e[1] * m.e[0]) + (e[5] * m.e[1]) + (e[9] * m.e[2]) + (e[13] * m.e[3]); - t.e[5] = (e[1] * m.e[4]) + (e[5] * m.e[5]) + (e[9] * m.e[6]) + (e[13] * m.e[7]); - t.e[9] = (e[1] * m.e[8]) + (e[5] * m.e[9]) + (e[9] * m.e[10]) + (e[13] * m.e[11]); - t.e[13] = (e[1] * m.e[12]) + (e[5] * m.e[13]) + (e[9] * m.e[14]) + (e[13] * m.e[15]); - - t.e[2] = (e[2] * m.e[0]) + (e[6] * m.e[1]) + (e[10] * m.e[2]) + (e[14] * m.e[3]); - t.e[6] = (e[2] * m.e[4]) + (e[6] * m.e[5]) + (e[10] * m.e[6]) + (e[14] * m.e[7]); - t.e[10] = (e[2] * m.e[8]) + (e[6] * m.e[9]) + (e[10] * m.e[10]) + (e[14] * m.e[11]); - t.e[14] = (e[2] * m.e[12]) + (e[6] * m.e[13]) + (e[10] * m.e[14]) + (e[14] * m.e[15]); - - t.e[3] = (e[3] * m.e[0]) + (e[7] * m.e[1]) + (e[11] * m.e[2]) + (e[15] * m.e[3]); - t.e[7] = (e[3] * m.e[4]) + (e[7] * m.e[5]) + (e[11] * m.e[6]) + (e[15] * m.e[7]); - t.e[11] = (e[3] * m.e[8]) + (e[7] * m.e[9]) + (e[11] * m.e[10]) + (e[15] * m.e[11]); - t.e[15] = (e[3] * m.e[12]) + (e[7] * m.e[13]) + (e[11] * m.e[14]) + (e[15] * m.e[15]); - - return t; - } - - void Matrix::operator *= (const Matrix & m) - { - Matrix t = (*this) * m; - memcpy((void*)this->e, (void*)t.e, sizeof(float) * 16); - } - - const float * Matrix::getElements() const - { - return e; - } - - void Matrix::setIdentity() - { - memset(e, 0, sizeof(float) * 16); - e[0] = e[5] = e[10] = e[15] = 1; - } - - void Matrix::setTranslation(float x, float y) - { - setIdentity(); - e[12] = x; - e[13] = y; - } - - void Matrix::setRotation(float rad) - { - setIdentity(); - float c = cos(rad), s = sin(rad); - e[0] = c; e[4] = -s; - e[1] = s; e[5] = c; - } - - void Matrix::setScale(float sx, float sy) - { - setIdentity(); - e[0] = sx; - e[5] = sy; - } - - void Matrix::setShear(float kx, float ky) - { - setIdentity(); - e[1] = ky; - e[4] = kx; - } - - void Matrix::setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy) - { - memset(e, 0, sizeof(float) * 16); // zero out matrix - float c = cos(angle), s = sin(angle); - // matrix multiplication carried out on paper: - // |1 x| |c -s | |sx | |1 -ox| - // | 1 y| |s c | | sy | | 1 -oy| - // | 1 | | 1 | | 1 | | 1 | - // | 1| | 1| | 1| | 1 | - // move rotate scale origin - e[10] = e[15] = 1.0f; - e[0] = c * sx ; // = a - e[1] = s * sx ; // = b - e[4] = - s * sy; // = c - e[5] = c * sy; // = d - e[12] = x - ox * e[0] - oy * e[4]; - e[13] = y - ox * e[1] - oy * e[5]; - } - - void Matrix::translate(float x, float y) - { - Matrix t; - t.setTranslation(x, y); - this->operator *=(t); - } - - void Matrix::rotate(float rad) - { - Matrix t; - t.setRotation(rad); - this->operator *=(t); - } - - void Matrix::scale(float sx, float sy) - { - Matrix t; - t.setScale(sx, sy); - this->operator *=(t); - } - - void Matrix::shear(float kx, float ky) - { - Matrix t; - t.setShear(kx, ky); - this->operator *=(t); - } - - // | x | - // | y | - // | 0 | - // | 1 | - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - - void Matrix::transform(Graphics::Vertex* dst, const Graphics::Vertex* src, int size) const - { - for (int i = 0; i(v, 0, 1); - } - - Percentage(const Percentage& p) - { - value = p.value; - } - - void operator = (const Percentage& p) - { - value = p.value; - } - - float value; - - }; - - } -} - -#endif \ No newline at end of file diff --git a/src/libjin/math/je_quad.h b/src/libjin/math/je_quad.h deleted file mode 100644 index 5bda125..0000000 --- a/src/libjin/math/je_quad.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef __JE_QUAD_H__ -#define __JE_QUAD_H__ - -namespace JinEngine -{ - namespace Math - { - - /// - /// - /// - struct Quad - { - Quad() - : x(0), y(0), w(0), h(0) - { - } - - Quad(float _x, float _y, float _w, float _h) - : x(_x), y(_y), w(_w), h(_h) - { - } - - float x, y, w, h; - - }; - - } // namespace Math -} // namespace JinEngine - -#endif // __JE_QUAD_H__ \ No newline at end of file diff --git a/src/libjin/math/je_random.cpp b/src/libjin/math/je_random.cpp deleted file mode 100644 index b9313d2..0000000 --- a/src/libjin/math/je_random.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include "je_math.h" -#include "je_random.h" - -namespace JinEngine -{ - namespace Math - { - - RandomGenerator::RandomGenerator(uint32 seed) - : mSeed(seed) - { - } - - uint32 RandomGenerator::rand() - { - unsigned int next = mSeed; - uint32 result; - - next *= 1103515245; - next += 12345; - result = (unsigned int)(next / 65536) % 2048; - - next *= 1103515245; - next += 12345; - result <<= 10; - result ^= (unsigned int)(next / 65536) % 1024; - - next *= 1103515245; - next += 12345; - result <<= 10; - result ^= (unsigned int)(next / 65536) % 1024; - - mSeed = next; - - return result; - } - - uint32 RandomGenerator::rand(uint32 min, uint32 max) - { - uint32 n = rand(); - return n % (max - min + 1) + min; - } - - float RandomGenerator::randf(float min, float max, int ac) - { - float floor = 0, ceil = max + reverse(min); - uint32 a = pow(10.f, ac); - uint32 n = rand(floor*a, ceil*a); - return (float)n / a + min; - } - - } -} \ No newline at end of file diff --git a/src/libjin/math/je_random.h b/src/libjin/math/je_random.h deleted file mode 100644 index 0f53155..0000000 --- a/src/libjin/math/je_random.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef __JE_RANDOM_H__ -#define __JE_RANDOM_H__ - -#include "../common/je_types.h" - -namespace JinEngine -{ - namespace Math - { - - /// - /// A random number generator. - /// - class RandomGenerator - { - public: - RandomGenerator(uint32 seed); - - uint32 rand(); - - uint32 rand(uint32 min, uint32 max); - - float randf(float min, float max, int ac); - - private: - uint32 mSeed; - - }; - - } // namespace Math -} // namespace JinEngine - -#endif \ No newline at end of file diff --git a/src/libjin/math/je_ranged_value.cpp b/src/libjin/math/je_ranged_value.cpp deleted file mode 100644 index b28e72b..0000000 --- a/src/libjin/math/je_ranged_value.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include "je_ranged_value.h" - -namespace JinEngine -{ - namespace Math - { - - RangedValue::RangedValue() - : mCount(0) - { - } - - RangedValue::RangedValue(float* points, uint n) - : mCount(n) - { - for (uint i = 0; i < n; ++i) - { - float x = points[2*i]; - float y = points[2*i + 1]; - mXAxis.push_back(x); - mYAxis.push_back(y); - } - } - - void RangedValue::addPoint(float x, float y) - { - mXAxis.push_back(x); - mYAxis.push_back(y); - ++mCount; - } - - void RangedValue::removePoint(uint i) - { - mXAxis.erase(mXAxis.begin() + i); - mYAxis.erase(mYAxis.begin() + i); - --mCount; - } - - void RangedValue::insertPoint(uint i, float x, float y) - { - mXAxis.insert(mXAxis.begin() + i, x); - mYAxis.insert(mYAxis.begin() + i, y); - ++mCount; - } - - float RangedValue::getValue(float x) - { - int endIndex = -1; - int n = mCount; - for (int i = 1; i < n; i++) { - float t = mXAxis[i]; - if (t > x) { - endIndex = i; - break; - } - } - if (endIndex == -1) return mYAxis[n - 1]; - int startIndex = endIndex - 1; - float startValue = mYAxis[startIndex]; - float startX = mXAxis[startIndex]; - return startValue + (mYAxis[endIndex] - startValue) * ((x - startX) / (mXAxis[endIndex] - startX)); - } - - void RangedValue::clear() - { - mXAxis.clear(); - mYAxis.clear(); - } - - } -} \ No newline at end of file diff --git a/src/libjin/math/je_ranged_value.h b/src/libjin/math/je_ranged_value.h deleted file mode 100644 index 4749e91..0000000 --- a/src/libjin/math/je_ranged_value.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef __JE_RANGED_VALUE_H__ -#define __JE_RANGED_VALUE_H__ - -#include "../common/je_types.h" - -#include - -namespace JinEngine -{ - namespace Math - { - /* - * y ^ - * |-------- _ - * | \ / \ - * | --- \ - * | \ - * | \ - * +----------------------> x - */ - class RangedValue - { - public: - RangedValue(); - - /// - /// Points of ranged value. - /// - /// @param points Points. - /// @param n Number of points. - /// - RangedValue(float* points, uint n); - - virtual ~RangedValue() {} - - virtual void addPoint(float x, float y); - - virtual void removePoint(uint i); - - virtual void insertPoint(uint i, float x, float y); - - virtual float getValue(float x); - - virtual void clear(); - - private: - std::vector mXAxis; - std::vector mYAxis; - - uint mCount; - - }; - - } -} - -#endif \ No newline at end of file diff --git a/src/libjin/math/je_transform.cpp b/src/libjin/math/je_transform.cpp deleted file mode 100644 index 95ca14d..0000000 --- a/src/libjin/math/je_transform.cpp +++ /dev/null @@ -1,104 +0,0 @@ -#include "je_transform.h" - -namespace JinEngine -{ - namespace Math - { - - Transform::Transform() - : mScale(1, 1) - , mPosition(0, 0) - , mOrigin(0, 0) - , mRotation(0) - { - } - - Transform::Transform(float x, float y, float sx, float sy, float r, float ox, float oy) - { - set(x, y, sx, sy, r, ox, oy); - } - - void Transform::set(float x, float y, float sx, float sy, float r, float ox, float oy) - { - setPosition(x, y); - setScale(sx, sy); - setRotation(r); - setOrigin(ox, oy); - } - - void Transform::setScale(float sx, float sy) - { - mScale.set(sx, sy); - } - - Vector2 Transform::getScale() const - { - return mScale; - } - - void Transform::scale(float sx, float sy) - { - mScale.x() *= sx; - mScale.y() *= sy; - } - - void Transform::setPosition(const Vector2& p) - { - setPosition(p.x(), p.y()); - } - - void Transform::setPosition(float x, float y) - { - mPosition.set(x, y); - } - - Vector2 Transform::getPosition() const - { - return mPosition; - } - - void Transform::move(float x, float y) - { - mPosition.x() += x; - mPosition.y() += y; - } - - void Transform::move(const Vector2& v) - { - move(v.x(), v.y()); - } - - void Transform::setOrigin(float x, float y) - { - mOrigin.set(x, y); - } - - Vector2 Transform::getOrigin() const - { - return mOrigin; - } - - void Transform::setRotation(float r) - { - mRotation = r; - } - - float Transform::getRotation() const - { - return mRotation; - } - - void Transform::rotate(float r) - { - mRotation += r; - } - - Matrix Transform::getMatrix() const - { - Matrix m; - m.setTransformation(mPosition.x(), mPosition.y(), mRotation, mScale.x(), mScale.y(), mOrigin.x(), mOrigin.y()); - return m; - } - - } -} \ No newline at end of file diff --git a/src/libjin/math/je_transform.h b/src/libjin/math/je_transform.h deleted file mode 100644 index 35a1a13..0000000 --- a/src/libjin/math/je_transform.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef __JE_TRANSFORM_H__ -#define __JE_TRANSFORM_H__ - -#include "je_matrix.h" -#include "je_vector2.hpp" - -namespace JinEngine -{ - namespace Math - { - - class Transform - { - public: - Transform(); - Transform(float x, float y, float sx, float sy, float r, float ox, float oy); - - void set(float x, float y, float sx, float sy, float r, float ox, float oy); - - void setScale(float sx, float sy); - Vector2 getScale() const; - void scale(float sx, float sy); - - void setPosition(float x, float y); - void setPosition(const Vector2& p); - Vector2 getPosition() const; - void move(float x, float y); - void move(const Vector2& v); - - void setOrigin(float x, float y); - Vector2 getOrigin() const; - - void setRotation(float r); - float getRotation() const; - void rotate(float r); - - Matrix getMatrix() const; - - private: - Vector2 mPosition; - Vector2 mOrigin; - Vector2 mScale; - float mRotation; - - }; - - } -} - -#endif \ No newline at end of file diff --git a/src/libjin/math/je_vector2.hpp b/src/libjin/math/je_vector2.hpp deleted file mode 100644 index 6115a63..0000000 --- a/src/libjin/math/je_vector2.hpp +++ /dev/null @@ -1,82 +0,0 @@ -#ifndef __JE_VECTOR_H__ -#define __JE_VECTOR_H__ - -namespace JinEngine -{ - namespace Math - { - - template - class Vector2 - { - public: - Vector2() - { - data[0] = data[1] = 0; - } - Vector2(T _x, T _y) - { - data[0] = _x; - data[1] = _y; - } - Vector2(const Vector2& v) - { - data[0] = v.data[0]; - data[1] = v.data[1]; - } - - void operator = (const Vector2& v) - { - data[0] = v.data[0]; - data[1] = v.data[1]; - } - - Vector2 operator * (float n) - { - return Vector2(data[0]*n, data[1]*n); - } - - void operator +=(const Vector2 v) - { - data[0] += v.data[0]; - data[1] += v.data[1]; - } - - Vector2 operator +(const Vector2& v) - { - return Vector2(data[0] + v.data[0], data[1] + v.data[1]); - } - - void set(T _x, T _y) - { - data[0] = _x; - data[1] = _y; - } - - bool isZero() - { - return data[0] == 0 && data[1] == 0; - } - - #define _aliases(A, B) \ - inline T& A() { return data[0]; }\ - inline T& B() { return data[1]; }\ - inline T A() const { return data[0]; }\ - inline T B() const { return data[1]; } - - _aliases(x, y); - _aliases(w, h); - _aliases(u, v); - _aliases(colum, row); - - #undef _aliases - - private: - T data[2]; - - }; - - } // namespace Math -} // namespace JinEngine - -#endif \ No newline at end of file diff --git a/src/libjin/math/je_vector3.hpp b/src/libjin/math/je_vector3.hpp deleted file mode 100644 index 3c800b6..0000000 --- a/src/libjin/math/je_vector3.hpp +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef __JE_VECTOR3_H__ -#define __JE_VECTOR3_H__ - -namespace JinEngine -{ - namespace Math - { - - template - class Vector3 - { - public: - Vector3() - { - data[0] = data[1] = data[2] = 0; - } - Vector3(T _x, T _y, T _z) - { - data[0] = _x; - data[1] = _y; - data[2] = _z; - } - Vector3(const Vector3& v) - { - data[0] = v.data[0]; - data[1] = v.data[1]; - data[2] = v.data[2]; - } - - #define _aliases(A, B, C) \ - inline T& A() { return data[0]; }\ - inline T& B() { return data[1]; }\ - inline T& C() { return data[2]; }\ - inline T A() const { return data[0]; }\ - inline T B() const { return data[1]; }\ - inline T C() const { return data[2]; } - - _aliases(x, y, z); - _aliases(r, g, b); - - #undef _aliases - - private: - T data[3]; - - }; - - } // namespace Math -} // namespace JinEngine - -#endif \ No newline at end of file diff --git a/src/libjin/math/je_vector4.hpp b/src/libjin/math/je_vector4.hpp deleted file mode 100644 index b544126..0000000 --- a/src/libjin/math/je_vector4.hpp +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef __JE_VECTOR4_H__ -#define __JE_VECTOR4_H__ - -namespace JinEngine -{ - namespace Math - { - - template - class Vector4 - { - public: - Vector4() - { - data[0] = data[1] = data[2] = data[3] = 0; - } - Vector4(T _x, T _y, T _z, T _t) - { - data[0] = _x; - data[1] = _y; - data[2] = _z; - data[3] = _t; - } - Vector4(const Vector4& v) - { - data[0] = v.data[0]; - data[1] = v.data[1]; - data[2] = v.data[2]; - data[3] = v.data[3]; - } - - #define _aliases(A, B, C, D) \ - inline T& A() { return data[0]; } \ - inline T& B() { return data[1]; } \ - inline T& C() { return data[2]; } \ - inline T& D() { return data[3]; } \ - inline T A() const { return data[0]; } \ - inline T B() const { return data[1]; } \ - inline T C() const { return data[2]; } \ - inline T D() const { return data[3]; } - - _aliases(x, y, z, t); - _aliases(x1, y1, w, h); - _aliases(r, g, b, a); - _aliases(left, right, top, bottom); - - #undef _aliases - - private: - T data[4]; - - }; - - } // namespace Math -} // namespace JinEngine - -#endif \ No newline at end of file diff --git a/src/libjin/math/math.h b/src/libjin/math/math.h new file mode 100644 index 0000000..79ed4c8 --- /dev/null +++ b/src/libjin/math/math.h @@ -0,0 +1,115 @@ +#ifndef __JE_UTILS_MATH_H__ +#define __JE_UTILS_MATH_H__ + +#include "constant.h" +#include "matrix.h" +#include "quad.h" + +namespace JinEngine +{ + namespace Math + { + + #ifdef min + #undef min + #endif // min + #ifdef max + #undef max + #endif // max + + template + inline T min(T a, T b) + { + return a < b ? a : b; + } + + template + inline T max(T a, T b) + { + return a > b ? a : b; + } + + template + inline T clamp(T a, T mi, T ma) + { + return min(max(a, mi), ma); + } + + template + inline bool within(T a, T mi, T ma) + { + return a >= mi && a <= ma; + } + + template + inline bool without(T a, T mi, T ma) + { + return a < mi || a > ma; + } + + template + inline T lowerBound(T a, T lower) + { + return a < lower ? lower : a; + } + + template + inline T upperBound(T a, T upper) + { + return a > upper ? upper : a; + } + + inline float lerp(float a, float b, float f) + { + f = clamp(f, 0, 1); + return a + f * (b - a); + } + + template + inline T abs(T a) + { + return a > 0 ? a : -a; + } + + template + inline T reverse(T a) + { + return -a; + } + + template + inline T lerp(T a, T b, float t) + { + return a + t * (b - a); + } + + template + inline T slerp(T start, T end, float percent) + { + // Dot product - the cosine of the angle between 2 vectors. + float dot = start * end; + // Clamp it to be in the range of Acos() + // This may be unnecessary, but floating point + // precision can be a fickle mistress. + dot = clamp(dot, -1.0f, 1.0f); + // Acos(dot) returns the angle between start and end, + // And multiplying that by percent returns the angle between + // start and the final result. + float theta = Mathf.Acos(dot)*percent; + Vector3 RelativeVec = end - start * dot; + RelativeVec.Normalize(); + // Orthonormal basis + // The final result. + return ((start*Mathf.Cos(theta)) + (RelativeVec*Mathf.Sin(theta))); + } + + template + inline T nlerp(T a, T b, float t) + { + + } + + } // namespace Math +} // namespace JinEngine + +#endif // __JE_UTILS_MATH_H__ \ No newline at end of file diff --git a/src/libjin/math/matrix.cpp b/src/libjin/math/matrix.cpp new file mode 100644 index 0000000..d9324df --- /dev/null +++ b/src/libjin/math/matrix.cpp @@ -0,0 +1,195 @@ +#include "../graphics/vertex.h" + +#include "matrix.h" + +#include // memcpy +#include + +namespace JinEngine +{ + namespace Math + { + + const Matrix Matrix::Identity; + + // | e0 e4 e8 e12 | + // | e1 e5 e9 e13 | + // | e2 e6 e10 e14 | + // | e3 e7 e11 e15 | + + Matrix::Matrix() + { + setIdentity(); + } + + Matrix::~Matrix() + { + } + + void Matrix::setOrtho(float l, float r, float b, float t, float n, float f) + { + setIdentity(); + float w = r - l; + float h = t - b; + float z = f - n; + e[0] = 2 / w; + e[5] = 2 / h; + e[10] = -2 / z; + e[12] = -(r + l) / w; + e[13] = -(t + b) / h; + e[14] = -(f + n) / z; + e[15] = 1; + } + + // | e0 e4 e8 e12 | + // | e1 e5 e9 e13 | + // | e2 e6 e10 e14 | + // | e3 e7 e11 e15 | + // | e0 e4 e8 e12 | + // | e1 e5 e9 e13 | + // | e2 e6 e10 e14 | + // | e3 e7 e11 e15 | + + Matrix Matrix::operator * (const Matrix & m) const + { + Matrix t; + + t.e[0] = (e[0] * m.e[0]) + (e[4] * m.e[1]) + (e[8] * m.e[2]) + (e[12] * m.e[3]); + t.e[4] = (e[0] * m.e[4]) + (e[4] * m.e[5]) + (e[8] * m.e[6]) + (e[12] * m.e[7]); + t.e[8] = (e[0] * m.e[8]) + (e[4] * m.e[9]) + (e[8] * m.e[10]) + (e[12] * m.e[11]); + t.e[12] = (e[0] * m.e[12]) + (e[4] * m.e[13]) + (e[8] * m.e[14]) + (e[12] * m.e[15]); + + t.e[1] = (e[1] * m.e[0]) + (e[5] * m.e[1]) + (e[9] * m.e[2]) + (e[13] * m.e[3]); + t.e[5] = (e[1] * m.e[4]) + (e[5] * m.e[5]) + (e[9] * m.e[6]) + (e[13] * m.e[7]); + t.e[9] = (e[1] * m.e[8]) + (e[5] * m.e[9]) + (e[9] * m.e[10]) + (e[13] * m.e[11]); + t.e[13] = (e[1] * m.e[12]) + (e[5] * m.e[13]) + (e[9] * m.e[14]) + (e[13] * m.e[15]); + + t.e[2] = (e[2] * m.e[0]) + (e[6] * m.e[1]) + (e[10] * m.e[2]) + (e[14] * m.e[3]); + t.e[6] = (e[2] * m.e[4]) + (e[6] * m.e[5]) + (e[10] * m.e[6]) + (e[14] * m.e[7]); + t.e[10] = (e[2] * m.e[8]) + (e[6] * m.e[9]) + (e[10] * m.e[10]) + (e[14] * m.e[11]); + t.e[14] = (e[2] * m.e[12]) + (e[6] * m.e[13]) + (e[10] * m.e[14]) + (e[14] * m.e[15]); + + t.e[3] = (e[3] * m.e[0]) + (e[7] * m.e[1]) + (e[11] * m.e[2]) + (e[15] * m.e[3]); + t.e[7] = (e[3] * m.e[4]) + (e[7] * m.e[5]) + (e[11] * m.e[6]) + (e[15] * m.e[7]); + t.e[11] = (e[3] * m.e[8]) + (e[7] * m.e[9]) + (e[11] * m.e[10]) + (e[15] * m.e[11]); + t.e[15] = (e[3] * m.e[12]) + (e[7] * m.e[13]) + (e[11] * m.e[14]) + (e[15] * m.e[15]); + + return t; + } + + void Matrix::operator *= (const Matrix & m) + { + Matrix t = (*this) * m; + memcpy((void*)this->e, (void*)t.e, sizeof(float) * 16); + } + + const float * Matrix::getElements() const + { + return e; + } + + void Matrix::setIdentity() + { + memset(e, 0, sizeof(float) * 16); + e[0] = e[5] = e[10] = e[15] = 1; + } + + void Matrix::setTranslation(float x, float y) + { + setIdentity(); + e[12] = x; + e[13] = y; + } + + void Matrix::setRotation(float rad) + { + setIdentity(); + float c = cos(rad), s = sin(rad); + e[0] = c; e[4] = -s; + e[1] = s; e[5] = c; + } + + void Matrix::setScale(float sx, float sy) + { + setIdentity(); + e[0] = sx; + e[5] = sy; + } + + void Matrix::setShear(float kx, float ky) + { + setIdentity(); + e[1] = ky; + e[4] = kx; + } + + void Matrix::setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy) + { + memset(e, 0, sizeof(float) * 16); // zero out matrix + float c = cos(angle), s = sin(angle); + // matrix multiplication carried out on paper: + // |1 x| |c -s | |sx | |1 -ox| + // | 1 y| |s c | | sy | | 1 -oy| + // | 1 | | 1 | | 1 | | 1 | + // | 1| | 1| | 1| | 1 | + // move rotate scale origin + e[10] = e[15] = 1.0f; + e[0] = c * sx ; // = a + e[1] = s * sx ; // = b + e[4] = - s * sy; // = c + e[5] = c * sy; // = d + e[12] = x - ox * e[0] - oy * e[4]; + e[13] = y - ox * e[1] - oy * e[5]; + } + + void Matrix::translate(float x, float y) + { + Matrix t; + t.setTranslation(x, y); + this->operator *=(t); + } + + void Matrix::rotate(float rad) + { + Matrix t; + t.setRotation(rad); + this->operator *=(t); + } + + void Matrix::scale(float sx, float sy) + { + Matrix t; + t.setScale(sx, sy); + this->operator *=(t); + } + + void Matrix::shear(float kx, float ky) + { + Matrix t; + t.setShear(kx, ky); + this->operator *=(t); + } + + // | x | + // | y | + // | 0 | + // | 1 | + // | e0 e4 e8 e12 | + // | e1 e5 e9 e13 | + // | e2 e6 e10 e14 | + // | e3 e7 e11 e15 | + + void Matrix::transform(Graphics::Vertex* dst, const Graphics::Vertex* src, int size) const + { + for (int i = 0; i(v, 0, 1); + } + + Percentage(const Percentage& p) + { + value = p.value; + } + + void operator = (const Percentage& p) + { + value = p.value; + } + + float value; + + }; + + } +} + +#endif \ No newline at end of file diff --git a/src/libjin/math/quad.h b/src/libjin/math/quad.h new file mode 100644 index 0000000..5bda125 --- /dev/null +++ b/src/libjin/math/quad.h @@ -0,0 +1,31 @@ +#ifndef __JE_QUAD_H__ +#define __JE_QUAD_H__ + +namespace JinEngine +{ + namespace Math + { + + /// + /// + /// + struct Quad + { + Quad() + : x(0), y(0), w(0), h(0) + { + } + + Quad(float _x, float _y, float _w, float _h) + : x(_x), y(_y), w(_w), h(_h) + { + } + + float x, y, w, h; + + }; + + } // namespace Math +} // namespace JinEngine + +#endif // __JE_QUAD_H__ \ No newline at end of file diff --git a/src/libjin/math/random.cpp b/src/libjin/math/random.cpp new file mode 100644 index 0000000..5f3b28c --- /dev/null +++ b/src/libjin/math/random.cpp @@ -0,0 +1,54 @@ +#include +#include "math.h" +#include "random.h" + +namespace JinEngine +{ + namespace Math + { + + RandomGenerator::RandomGenerator(uint32 seed) + : mSeed(seed) + { + } + + uint32 RandomGenerator::rand() + { + unsigned int next = mSeed; + uint32 result; + + next *= 1103515245; + next += 12345; + result = (unsigned int)(next / 65536) % 2048; + + next *= 1103515245; + next += 12345; + result <<= 10; + result ^= (unsigned int)(next / 65536) % 1024; + + next *= 1103515245; + next += 12345; + result <<= 10; + result ^= (unsigned int)(next / 65536) % 1024; + + mSeed = next; + + return result; + } + + uint32 RandomGenerator::rand(uint32 min, uint32 max) + { + uint32 n = rand(); + return n % (max - min + 1) + min; + } + + float RandomGenerator::randf(float min, float max, int ac) + { + float floor = 0, ceil = max + reverse(min); + uint32 a = pow(10.f, ac); + uint32 n = rand(floor*a, ceil*a); + return (float)n / a + min; + } + + } +} \ No newline at end of file diff --git a/src/libjin/math/random.h b/src/libjin/math/random.h new file mode 100644 index 0000000..1eb22c4 --- /dev/null +++ b/src/libjin/math/random.h @@ -0,0 +1,33 @@ +#ifndef __JE_RANDOM_H__ +#define __JE_RANDOM_H__ + +#include "../common/types.h" + +namespace JinEngine +{ + namespace Math + { + + /// + /// A random number generator. + /// + class RandomGenerator + { + public: + RandomGenerator(uint32 seed); + + uint32 rand(); + + uint32 rand(uint32 min, uint32 max); + + float randf(float min, float max, int ac); + + private: + uint32 mSeed; + + }; + + } // namespace Math +} // namespace JinEngine + +#endif \ No newline at end of file diff --git a/src/libjin/math/ranged_value.cpp b/src/libjin/math/ranged_value.cpp new file mode 100644 index 0000000..1a14467 --- /dev/null +++ b/src/libjin/math/ranged_value.cpp @@ -0,0 +1,71 @@ +#include "ranged_value.h" + +namespace JinEngine +{ + namespace Math + { + + RangedValue::RangedValue() + : mCount(0) + { + } + + RangedValue::RangedValue(float* points, uint n) + : mCount(n) + { + for (uint i = 0; i < n; ++i) + { + float x = points[2*i]; + float y = points[2*i + 1]; + mXAxis.push_back(x); + mYAxis.push_back(y); + } + } + + void RangedValue::addPoint(float x, float y) + { + mXAxis.push_back(x); + mYAxis.push_back(y); + ++mCount; + } + + void RangedValue::removePoint(uint i) + { + mXAxis.erase(mXAxis.begin() + i); + mYAxis.erase(mYAxis.begin() + i); + --mCount; + } + + void RangedValue::insertPoint(uint i, float x, float y) + { + mXAxis.insert(mXAxis.begin() + i, x); + mYAxis.insert(mYAxis.begin() + i, y); + ++mCount; + } + + float RangedValue::getValue(float x) + { + int endIndex = -1; + int n = mCount; + for (int i = 1; i < n; i++) { + float t = mXAxis[i]; + if (t > x) { + endIndex = i; + break; + } + } + if (endIndex == -1) return mYAxis[n - 1]; + int startIndex = endIndex - 1; + float startValue = mYAxis[startIndex]; + float startX = mXAxis[startIndex]; + return startValue + (mYAxis[endIndex] - startValue) * ((x - startX) / (mXAxis[endIndex] - startX)); + } + + void RangedValue::clear() + { + mXAxis.clear(); + mYAxis.clear(); + } + + } +} \ No newline at end of file diff --git a/src/libjin/math/ranged_value.h b/src/libjin/math/ranged_value.h new file mode 100644 index 0000000..5a4c35d --- /dev/null +++ b/src/libjin/math/ranged_value.h @@ -0,0 +1,57 @@ +#ifndef __JE_RANGED_VALUE_H__ +#define __JE_RANGED_VALUE_H__ + +#include "../common/types.h" + +#include + +namespace JinEngine +{ + namespace Math + { + /* + * y ^ + * |-------- _ + * | \ / \ + * | --- \ + * | \ + * | \ + * +----------------------> x + */ + class RangedValue + { + public: + RangedValue(); + + /// + /// Points of ranged value. + /// + /// @param points Points. + /// @param n Number of points. + /// + RangedValue(float* points, uint n); + + virtual ~RangedValue() {} + + virtual void addPoint(float x, float y); + + virtual void removePoint(uint i); + + virtual void insertPoint(uint i, float x, float y); + + virtual float getValue(float x); + + virtual void clear(); + + private: + std::vector mXAxis; + std::vector mYAxis; + + uint mCount; + + }; + + } +} + +#endif \ No newline at end of file diff --git a/src/libjin/math/transform.cpp b/src/libjin/math/transform.cpp new file mode 100644 index 0000000..84c8327 --- /dev/null +++ b/src/libjin/math/transform.cpp @@ -0,0 +1,104 @@ +#include "transform.h" + +namespace JinEngine +{ + namespace Math + { + + Transform::Transform() + : mScale(1, 1) + , mPosition(0, 0) + , mOrigin(0, 0) + , mRotation(0) + { + } + + Transform::Transform(float x, float y, float sx, float sy, float r, float ox, float oy) + { + set(x, y, sx, sy, r, ox, oy); + } + + void Transform::set(float x, float y, float sx, float sy, float r, float ox, float oy) + { + setPosition(x, y); + setScale(sx, sy); + setRotation(r); + setOrigin(ox, oy); + } + + void Transform::setScale(float sx, float sy) + { + mScale.set(sx, sy); + } + + Vector2 Transform::getScale() const + { + return mScale; + } + + void Transform::scale(float sx, float sy) + { + mScale.x() *= sx; + mScale.y() *= sy; + } + + void Transform::setPosition(const Vector2& p) + { + setPosition(p.x(), p.y()); + } + + void Transform::setPosition(float x, float y) + { + mPosition.set(x, y); + } + + Vector2 Transform::getPosition() const + { + return mPosition; + } + + void Transform::move(float x, float y) + { + mPosition.x() += x; + mPosition.y() += y; + } + + void Transform::move(const Vector2& v) + { + move(v.x(), v.y()); + } + + void Transform::setOrigin(float x, float y) + { + mOrigin.set(x, y); + } + + Vector2 Transform::getOrigin() const + { + return mOrigin; + } + + void Transform::setRotation(float r) + { + mRotation = r; + } + + float Transform::getRotation() const + { + return mRotation; + } + + void Transform::rotate(float r) + { + mRotation += r; + } + + Matrix Transform::getMatrix() const + { + Matrix m; + m.setTransformation(mPosition.x(), mPosition.y(), mRotation, mScale.x(), mScale.y(), mOrigin.x(), mOrigin.y()); + return m; + } + + } +} \ No newline at end of file diff --git a/src/libjin/math/transform.h b/src/libjin/math/transform.h new file mode 100644 index 0000000..3f3c199 --- /dev/null +++ b/src/libjin/math/transform.h @@ -0,0 +1,50 @@ +#ifndef __JE_TRANSFORM_H__ +#define __JE_TRANSFORM_H__ + +#include "matrix.h" +#include "vector2.hpp" + +namespace JinEngine +{ + namespace Math + { + + class Transform + { + public: + Transform(); + Transform(float x, float y, float sx, float sy, float r, float ox, float oy); + + void set(float x, float y, float sx, float sy, float r, float ox, float oy); + + void setScale(float sx, float sy); + Vector2 getScale() const; + void scale(float sx, float sy); + + void setPosition(float x, float y); + void setPosition(const Vector2& p); + Vector2 getPosition() const; + void move(float x, float y); + void move(const Vector2& v); + + void setOrigin(float x, float y); + Vector2 getOrigin() const; + + void setRotation(float r); + float getRotation() const; + void rotate(float r); + + Matrix getMatrix() const; + + private: + Vector2 mPosition; + Vector2 mOrigin; + Vector2 mScale; + float mRotation; + + }; + + } +} + +#endif \ No newline at end of file diff --git a/src/libjin/math/vector2.hpp b/src/libjin/math/vector2.hpp new file mode 100644 index 0000000..6115a63 --- /dev/null +++ b/src/libjin/math/vector2.hpp @@ -0,0 +1,82 @@ +#ifndef __JE_VECTOR_H__ +#define __JE_VECTOR_H__ + +namespace JinEngine +{ + namespace Math + { + + template + class Vector2 + { + public: + Vector2() + { + data[0] = data[1] = 0; + } + Vector2(T _x, T _y) + { + data[0] = _x; + data[1] = _y; + } + Vector2(const Vector2& v) + { + data[0] = v.data[0]; + data[1] = v.data[1]; + } + + void operator = (const Vector2& v) + { + data[0] = v.data[0]; + data[1] = v.data[1]; + } + + Vector2 operator * (float n) + { + return Vector2(data[0]*n, data[1]*n); + } + + void operator +=(const Vector2 v) + { + data[0] += v.data[0]; + data[1] += v.data[1]; + } + + Vector2 operator +(const Vector2& v) + { + return Vector2(data[0] + v.data[0], data[1] + v.data[1]); + } + + void set(T _x, T _y) + { + data[0] = _x; + data[1] = _y; + } + + bool isZero() + { + return data[0] == 0 && data[1] == 0; + } + + #define _aliases(A, B) \ + inline T& A() { return data[0]; }\ + inline T& B() { return data[1]; }\ + inline T A() const { return data[0]; }\ + inline T B() const { return data[1]; } + + _aliases(x, y); + _aliases(w, h); + _aliases(u, v); + _aliases(colum, row); + + #undef _aliases + + private: + T data[2]; + + }; + + } // namespace Math +} // namespace JinEngine + +#endif \ No newline at end of file diff --git a/src/libjin/math/vector3.hpp b/src/libjin/math/vector3.hpp new file mode 100644 index 0000000..3c800b6 --- /dev/null +++ b/src/libjin/math/vector3.hpp @@ -0,0 +1,51 @@ +#ifndef __JE_VECTOR3_H__ +#define __JE_VECTOR3_H__ + +namespace JinEngine +{ + namespace Math + { + + template + class Vector3 + { + public: + Vector3() + { + data[0] = data[1] = data[2] = 0; + } + Vector3(T _x, T _y, T _z) + { + data[0] = _x; + data[1] = _y; + data[2] = _z; + } + Vector3(const Vector3& v) + { + data[0] = v.data[0]; + data[1] = v.data[1]; + data[2] = v.data[2]; + } + + #define _aliases(A, B, C) \ + inline T& A() { return data[0]; }\ + inline T& B() { return data[1]; }\ + inline T& C() { return data[2]; }\ + inline T A() const { return data[0]; }\ + inline T B() const { return data[1]; }\ + inline T C() const { return data[2]; } + + _aliases(x, y, z); + _aliases(r, g, b); + + #undef _aliases + + private: + T data[3]; + + }; + + } // namespace Math +} // namespace JinEngine + +#endif \ No newline at end of file diff --git a/src/libjin/math/vector4.hpp b/src/libjin/math/vector4.hpp new file mode 100644 index 0000000..b544126 --- /dev/null +++ b/src/libjin/math/vector4.hpp @@ -0,0 +1,57 @@ +#ifndef __JE_VECTOR4_H__ +#define __JE_VECTOR4_H__ + +namespace JinEngine +{ + namespace Math + { + + template + class Vector4 + { + public: + Vector4() + { + data[0] = data[1] = data[2] = data[3] = 0; + } + Vector4(T _x, T _y, T _z, T _t) + { + data[0] = _x; + data[1] = _y; + data[2] = _z; + data[3] = _t; + } + Vector4(const Vector4& v) + { + data[0] = v.data[0]; + data[1] = v.data[1]; + data[2] = v.data[2]; + data[3] = v.data[3]; + } + + #define _aliases(A, B, C, D) \ + inline T& A() { return data[0]; } \ + inline T& B() { return data[1]; } \ + inline T& C() { return data[2]; } \ + inline T& D() { return data[3]; } \ + inline T A() const { return data[0]; } \ + inline T B() const { return data[1]; } \ + inline T C() const { return data[2]; } \ + inline T D() const { return data[3]; } + + _aliases(x, y, z, t); + _aliases(x1, y1, w, h); + _aliases(r, g, b, a); + _aliases(left, right, top, bottom); + + #undef _aliases + + private: + T data[4]; + + }; + + } // namespace Math +} // namespace JinEngine + +#endif \ No newline at end of file -- cgit v1.1-26-g67d0