summaryrefslogtreecommitdiff
path: root/src/math
diff options
context:
space:
mode:
Diffstat (limited to 'src/math')
-rw-r--r--src/math/mat.c51
-rw-r--r--src/math/math.h117
-rw-r--r--src/math/structs.h111
-rw-r--r--src/math/vec2.c6
-rw-r--r--src/math/vec4.c14
5 files changed, 191 insertions, 108 deletions
diff --git a/src/math/mat.c b/src/math/mat.c
index e3c6ce3..3bb571a 100644
--- a/src/math/mat.c
+++ b/src/math/mat.c
@@ -12,6 +12,9 @@ static Mat4 sharedMat2;
static Vec3 sharedVec3;
static Vec4 sharedVec4;
+static Mat3 sharedMat3;
+static Mat3 sharedMat32;
+
Mat4 mat4identity = {
1,0,0,0,
0,1,0,0,
@@ -19,6 +22,17 @@ Mat4 mat4identity = {
0,0,0,1
};
+#define shrmat3(p)\
+do{\
+sharedMat3 = *p;\
+p = &sharedMat3;\
+}while(0)
+#define shrmat32(p)\
+do{\
+sharedMat32 = *p;\
+p = &sharedMat32;\
+}while(0)
+
#define shrmat(p) \
do{\
sharedMat = *p;\
@@ -121,6 +135,8 @@ void internal_mat4_multiply(Mat4* m1, Mat4* m2, Mat4* out) {
out->e30 = mul(3, 0); out->e31 = mul(3, 1); out->e32 = mul(3, 2); out->e33 = mul(3, 3);
}
+#undef mul
+
void internal_mat4_setscale(float kx, float ky, float kz, Mat4* out) {
ssr_assert(out);
internal_mat4_zero(out);
@@ -748,20 +764,49 @@ void internal_mat4_toquat(Mat4* in, Quat* out) {
internal_quat_normalize(out, out);
}
-void mat3_multvec3(Mat3* m, Vec3* v, Vec3* out) {
+bool internal_mat3_isidentity(Mat3* m) {
+ ssr_assert(m);
+ return
+ compare(m->c1.x, 1) && compare(m->c2.x, 0) && compare(m->c3.x, 0) &&
+ compare(m->c1.y, 0) && compare(m->c2.y, 1) && compare(m->c3.y, 0) &&
+ compare(m->c1.z, 0) && compare(m->c2.z, 0) && compare(m->c3.z, 1);
+}
+
+void internal_mat3_multvec3(Mat3* m, Vec3* v, Vec3* out) {
ssr_assert(m && v && out);
out->x = m->e00 * v->x + m->e01 * v->y + m->e02 * v->z;
out->y = m->e10 * v->x + m->e11 * v->y + m->e12 * v->z;
out->z = m->e20 * v->x + m->e21 * v->y + m->e22 * v->z;
}
-void mat23_applytovec3(Mat23* m, Vec3* v, Vec2* out) {
+#define MAT3(_m, r, c) (_m->m[c][r])
+
+static float _mul3(float* r, float* c) {
+ return c[0] * r[0] + c[1] * r[3] + c[2] * r[6];
+}
+
+#define mul(r, c) _mul3(&MAT3(m1,r,0), &MAT3(m2,0,c))
+
+void internal_mat3_multmat3(Mat3* m1, Mat3* m2, Mat3* out) {
+ ssr_assert(m1 && m2 && out);
+ if (internal_mat3_isidentity(m1)) { if (m2 != out)*out = *m2; return; }
+ if (internal_mat3_isidentity(m2)) { if (m1 != out)*out = *m1; return; }
+ if (out == m1) shrmat3(m1);
+ if (out == m2) shrmat32(m2);
+ out->e00 = mul(0, 0); out->e01 = mul(0, 1); out->e02 = mul(0, 2);
+ out->e10 = mul(1, 0); out->e11 = mul(1, 1); out->e12 = mul(1, 2);
+ out->e20 = mul(2, 0); out->e21 = mul(2, 1); out->e22 = mul(2, 2);
+}
+
+#undef mul
+
+void internal_mat23_applytovec3(Mat23* m, Vec3* v, Vec2* out) {
ssr_assert(m && v && out);
out->x = m->e00 * v->x + m->e01 * v->y + m->e02 * v->z;
out->y = m->e10 * v->x + m->e11 * v->y + m->e12 * v->z;
}
-void mat43_applytovec3(Mat43* m, Vec3* v, Vec4* out) {
+void internal_mat43_applytovec3(Mat43* m, Vec3* v, Vec4* out) {
ssr_assert(m && v && out);
out->x = m->e00 * v->x + m->e01 * v->y + m->e02 * v->z;
out->y = m->e10 * v->x + m->e11 * v->y + m->e12 * v->z;
diff --git a/src/math/math.h b/src/math/math.h
index 8f59137..8fa748b 100644
--- a/src/math/math.h
+++ b/src/math/math.h
@@ -6,14 +6,13 @@
#include <math.h>
#include <limits.h>
-#include "../util/type.h"
+#include "structs.h"
#define PI 3.141592653f
#define RAD2DEG 57.295779523f /*180.f/PI*/
#define DEG2RAG 0.0174532925f /*PI/180.f*/
#define EPSILON 1e-4f
-/* 用来打印的公共buffer */
extern char printbuffer[1024];
#define min(a, b) ((a) < (b) ? (a) : (b))
@@ -28,113 +27,15 @@ extern char printbuffer[1024];
float rsqrt(float n);
float lerp(float from, float to, float t);
-typedef struct {
- float x, y;
-} Vec2;
-
-typedef union {
- struct {
- float x, y, z;
- };
- struct {
- float A, B, C; /*重心坐标*/
- };
- Vec2 xy;
-} Vec3;
-
-typedef union {
- struct {
- float x, y, z, w;
- };
- struct {
- float r, g, b, a;
- };
- Vec3 xyz;
-} Vec4;
-
-typedef union { /*in degree, for visualize quaternion*/
- struct {
- float x, y, z;
- };
- struct {
- float pitch, yaw, roll;
- };
-} Euler;
-
-typedef struct {
- float x, y, z, w;
-} Quat;
-
-typedef union {
- float l[16];
- float m[4][4];
- struct {
- float
- e00, e10, e20, e30, /*colum 0*/
- e01, e11, e21, e31,
- e02, e12, e22, e32,
- e03, e13, e23, e33;
- };
- struct {
- Vec4 x;/*colum 0*/
- Vec4 y;
- Vec4 z;
- Vec4 w;
- } axis; /*轴*/
- struct {
- Vec4 x;/*colum 0*/
- Vec4 y;
- Vec4 z;
- Vec4 w;
- } basis; /*基向量*/
- struct {
- Vec4 axisx;
- Vec4 axisy;
- Vec4 axisz;
- Vec4 pos;
- };
- Vec4 colums[4];
-} Mat4;
-
-typedef union {
- struct {
- float
- e00, e10, e20, /*colum 0*/
- e01, e11, e21,
- e02, e12, e22;
- };
-} Mat3;
-
-typedef union {
- struct {
- float
- e00, e10, /*colum 0*/
- e01, e11,
- e02, e12;
- };
-} Mat23;
-
-typedef union {
- struct {
- float
- e00, e10, e20, e30, /*colum 0*/
- e01, e11, e21, e31,
- e02, e12, e22, e32;
- };
- struct { /*三个齐次裁剪坐标*/
- Vec4 p1;
- Vec4 p2;
- Vec4 p3;
- };
-} Mat43;
-
#define MAT(M, r, c) (M->m[c][r])
+
/************************************************************************/
/* Vec */
/************************************************************************/
void internal_vec2_scale(Vec2* v, float k, Vec2* out);
void internal_vec2_plus(Vec2* v1, Vec2* v2, Vec2* out);
+void internal_vec2_minus(Vec2* v1, Vec2* v2, Vec2* out);
void internal_vec2_offset(Vec2* v, float offset, Vec2* out);
void internal_vec2_rotate(Vec2* v, float angle, Vec2* out);
@@ -192,6 +93,10 @@ void internal_vec4_scale(Vec4* v, float t, Vec4* out);
void internal_vec4_add(Vec4* v1, Vec4* v2, Vec4* out);
+void internal_vec4_minus(Vec4* v1, Vec4* v2, Vec4* out);
+
+void internal_vec4_plus(Vec4* v1, Vec4* v2, Vec4* out);
+
/************************************************************************/
/* Matrix */
@@ -251,9 +156,11 @@ void internal_mat4_toquat(Mat4* in, Quat* out); /*in是正交矩阵*/
#define ROWMAT(A, ...)\
Mat4 A={__VA_ARGS__};internal_mat4_transpose(&A, &A);
-void mat3_multvec3(Mat3* m, Vec3* v, Vec3* out);
-void mat23_applytovec3(Mat23* m, Vec3* v, Vec2* out);
-void mat43_applytovec3(Mat43* m, Vec3* v, Vec4* out);
+void internal_mat3_multvec3(Mat3* m, Vec3* v, Vec3* out);
+void internal_mat3_multmat3(Mat3* m1, Mat3* m2, Mat3* out);
+void internal_mat23_applytovec3(Mat23* m, Vec3* v, Vec2* out);
+void internal_mat43_applytovec3(Mat43* m, Vec3* v, Vec4* out);
+bool internal_mat3_isidentity(Mat3* m);
/************************************************************************/
/* Quat */
diff --git a/src/math/structs.h b/src/math/structs.h
new file mode 100644
index 0000000..97af5bb
--- /dev/null
+++ b/src/math/structs.h
@@ -0,0 +1,111 @@
+#ifndef SSR_STRUCTS_H
+#define SSR_STRUCTS_H
+
+#include "../util/type.h"
+
+
+typedef struct {
+ float x, y;
+} Vec2;
+
+typedef union {
+ struct {
+ float x, y, z;
+ };
+ struct {
+ float A, B, C; /*重心坐标*/
+ };
+ Vec2 xy;
+} Vec3;
+
+typedef union {
+ struct {
+ float x, y, z, w;
+ };
+ struct {
+ float r, g, b, a;
+ };
+ Vec3 xyz;
+} Vec4;
+
+typedef union { /*in degree, for visualize quaternion*/
+ struct {
+ float x, y, z;
+ };
+ struct {
+ float pitch, yaw, roll;
+ };
+} Euler;
+
+typedef struct {
+ float x, y, z, w;
+} Quat;
+
+typedef union {
+ float l[16];
+ float m[4][4];
+ struct {
+ float
+ e00, e10, e20, e30, /*colum 0*/
+ e01, e11, e21, e31,
+ e02, e12, e22, e32,
+ e03, e13, e23, e33;
+ };
+ struct {
+ Vec4 x;/*colum 0*/
+ Vec4 y;
+ Vec4 z;
+ Vec4 w;
+ } axis; /*轴*/
+ struct {
+ Vec4 x;/*colum 0*/
+ Vec4 y;
+ Vec4 z;
+ Vec4 w;
+ } basis; /*基向量*/
+ struct {
+ Vec4 axisx;
+ Vec4 axisy;
+ Vec4 axisz;
+ Vec4 pos;
+ };
+ Vec4 colums[4];
+} Mat4;
+
+typedef union {
+ struct {
+ Vec3 c1, c2, c3;
+ };
+ float m[3][3];
+ struct {
+ float
+ e00, e10, e20, /*colum 0*/
+ e01, e11, e21,
+ e02, e12, e22;
+ };
+} Mat3;
+
+typedef union {
+ struct {
+ float
+ e00, e10, /*colum 0*/
+ e01, e11,
+ e02, e12;
+ };
+} Mat23;
+
+typedef union {
+ struct {
+ float
+ e00, e10, e20, e30, /*colum 0*/
+ e01, e11, e21, e31,
+ e02, e12, e22, e32;
+ };
+ struct { /*三个齐次裁剪坐标*/
+ Vec4 p1;
+ Vec4 p2;
+ Vec4 p3;
+ };
+} Mat43;
+
+#endif \ No newline at end of file
diff --git a/src/math/vec2.c b/src/math/vec2.c
index dc964e7..c807baf 100644
--- a/src/math/vec2.c
+++ b/src/math/vec2.c
@@ -16,6 +16,12 @@ void internal_vec2_plus(Vec2* v1, Vec2* v2, Vec2* out) {
out->y = v1->y + v2->y;
}
+void internal_vec2_minus(Vec2* v1, Vec2* v2, Vec2* out) {
+ ssr_assert(v1 && v2 && out);
+ out->x = v1->x - v2->x;
+ out->y = v1->y - v2->y;
+}
+
void internal_vec2_offset(Vec2* v, float offset, Vec2* out) {
ssr_assert(v && out);
out->x = v->x + offset;
diff --git a/src/math/vec4.c b/src/math/vec4.c
index 24df08f..f4648c7 100644
--- a/src/math/vec4.c
+++ b/src/math/vec4.c
@@ -54,3 +54,17 @@ void internal_vec4_add(Vec4* v1, Vec4* v2, Vec4* out) {
out->z = v1->z + v2->z;
out->w = v1->w + v2->w;
}
+
+void internal_vec4_minus(Vec4* v1, Vec4* v2, Vec4* out) {
+ out->x = v1->x + v2->x;
+ out->y = v1->y + v2->y;
+ out->z = v1->z + v2->z;
+ out->w = v1->w + v2->w;
+}
+
+void internal_vec4_plus(Vec4* v1, Vec4* v2, Vec4* out) {
+ out->x = v1->x - v2->x;
+ out->y = v1->y - v2->y;
+ out->z = v1->z - v2->z;
+ out->w = v1->w - v2->w;
+}