summaryrefslogtreecommitdiff
path: root/src/math
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-12-15 00:39:18 +0800
committerchai <chaifix@163.com>2019-12-15 00:39:18 +0800
commit749bbc6a54e50c297ab49d9e515a3679651d1461 (patch)
tree097bbe044332e816aa481db1a4e325b8d3f63b0d /src/math
parent3f44877edfe4c301b258d522bcb4e8d9b6e92382 (diff)
*misc
Diffstat (limited to 'src/math')
-rw-r--r--src/math/mat.c15
-rw-r--r--src/math/math.h21
-rw-r--r--src/math/vec4.c11
3 files changed, 37 insertions, 10 deletions
diff --git a/src/math/mat.c b/src/math/mat.c
index 84a5a41..f889697 100644
--- a/src/math/mat.c
+++ b/src/math/mat.c
@@ -9,6 +9,7 @@
static Mat4 sharedMat;
static Mat4 sharedMat2;
+static Vec3 sharedVec3;
static Vec4 sharedVec4;
Mat4 mat4identity = {
@@ -333,6 +334,20 @@ void mat4_applytovec4(Mat4* mat, Vec4* v, Vec4* out) {
out->w = mat->e30 * v->x + mat->e31 * v->y + mat->e32 * v->z + mat->e33 * v->w;
}
+/*
+** mat3 apply to vec3
+*/
+void mat4_applytovec3(Mat4* mat, Vec3* v, Vec3* out) {
+ ssr_assert(mat && v && out);
+ if (v == out) {
+ sharedVec3 = *v;
+ v = &sharedVec3;
+ }
+ out->x = mat->e00 * v->x + mat->e01 * v->y + mat->e02 * v->z;
+ out->y = mat->e10 * v->x + mat->e11 * v->y + mat->e12 * v->z;
+ out->z = mat->e20 * v->x + mat->e21 * v->y + mat->e22 * v->z;
+}
+
#define trans(r, c) out->e##r##c = m->e##c##r
void mat4_transpose(Mat4* m, Mat4* out) {
diff --git a/src/math/math.h b/src/math/math.h
index 1db7df7..41b4607 100644
--- a/src/math/math.h
+++ b/src/math/math.h
@@ -27,11 +27,11 @@ extern char printbuffer[1024];
float rsqrt(float n);
float lerp(float from, float to, float t);
-typedef struct Vec2 {
+typedef struct {
float x, y;
} Vec2;
-typedef union Vec3 {
+typedef union {
struct {
float x, y, z;
};
@@ -41,7 +41,7 @@ typedef union Vec3 {
Vec2 xy;
} Vec3;
-typedef union Vec4 {
+typedef union {
struct {
float x, y, z, w;
};
@@ -51,7 +51,7 @@ typedef union Vec4 {
Vec3 xyz;
} Vec4;
-typedef union Euler { /*in degree, for visualize quaternion*/
+typedef union { /*in degree, for visualize quaternion*/
struct {
float x, y, z;
};
@@ -60,11 +60,11 @@ typedef union Euler { /*in degree, for visualize quaternion*/
};
} Euler;
-typedef struct Quat {
+typedef struct {
float x, y, z, w;
} Quat;
-typedef union Mat4 {
+typedef union {
float l[16];
float m[4][4];
struct {
@@ -95,7 +95,7 @@ typedef union Mat4 {
Vec4 colums[4];
} Mat4;
-typedef union Mat3 {
+typedef union {
struct {
float
e00, e10, e20, /*colum 0*/
@@ -104,7 +104,7 @@ typedef union Mat3 {
};
} Mat3;
-typedef union Mat23 {
+typedef union {
struct {
float
e00, e10, /*colum 0*/
@@ -113,7 +113,7 @@ typedef union Mat23 {
};
} Mat23;
-typedef union Mat43 {
+typedef union {
struct {
float
e00, e10, e20, e30, /*colum 0*/
@@ -184,6 +184,8 @@ void vec4_print(Vec4* v);
void vec4_lerp(Vec4* a, Vec4* b, float t, Vec4* out);
+void vec4_scale(Vec4* v, float t, Vec4* out);
+
/************************************************************************/
/* Matrix */
/************************************************************************/
@@ -234,6 +236,7 @@ void mat4_invertpos(Mat4* pos, Mat4* out); /* 对平移矩阵求逆 */
void mat4_decomposetrs(Mat4* src, Vec3* pos, Quat* quat, Vec3* scale); /*分解trs矩阵*/
void mat4_applytovec4(Mat4* m, Vec4* v, Vec4* out);
+void mat4_applytovec3(Mat4* m, Vec3* v, Vec3* out);
bool mat4_toeuler(Mat4* in, Euler* out); /* 计算YXZ旋转矩阵的欧拉角 */
void mat4_toquat(Mat4* in, Quat* out); /*in是正交矩阵*/
diff --git a/src/math/vec4.c b/src/math/vec4.c
index b79adca..202aabd 100644
--- a/src/math/vec4.c
+++ b/src/math/vec4.c
@@ -6,6 +6,8 @@
Vec4 vec4zero = { 0, 0, 0,0 };
+static Vec4 sharedmat4;
+
void vec4_dividew(Vec4* v, Vec3* out) {
ssr_assert(out && v);
float w = 1.f / v->w;
@@ -28,4 +30,11 @@ void vec4_lerp(Vec4* a, Vec4* b, float t, Vec4* out) {
out->y = lerp(a->y, b->y, t);
out->z = lerp(a->z, b->z, t);
out->w = lerp(a->w, b->w, t);
-} \ No newline at end of file
+}
+
+void vec4_scale(Vec4* v, float t, Vec4* out) {
+ out->x = v->x * t;
+ out->y = v->y * t;
+ out->z = v->z * t;
+ out->w = v->w * t;
+}