summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2020-07-12 13:31:28 +0800
committerchai <chaifix@163.com>2020-07-12 13:31:28 +0800
commita0439c8d387579a1727ad00c2e105e7602aedbb6 (patch)
tree8ea09b13e179a7d8cd9ac6518410cd10035f48f4 /src
parentec7aa42781a9108901fbde7210d8285bbbeaf5fc (diff)
+mathlib
Diffstat (limited to 'src')
-rw-r--r--src/example/04_bloom/postprocess.c2
-rw-r--r--src/example/04_bloom/preprocess.c2
-rw-r--r--src/extend/camera.c9
-rw-r--r--src/gizmo/gizmo.c2
-rw-r--r--src/main.c2
-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
-rw-r--r--src/shaders/common/core.c (renamed from src/shaders/common.c)11
-rw-r--r--src/shaders/common/core.h (renamed from src/shaders/common.h)16
-rw-r--r--src/shaders/common/light.c0
-rw-r--r--src/shaders/common/light.h0
-rw-r--r--src/shaders/common/mathlib.c138
-rw-r--r--src/shaders/common/mathlib.h63
-rw-r--r--src/shaders/common/shadow.c0
-rw-r--r--src/shaders/common/shadow.h6
-rw-r--r--src/shaders/default.c2
-rw-r--r--src/shaders/pbr.c19
-rw-r--r--src/shaders/unlit.c2
21 files changed, 420 insertions, 153 deletions
diff --git a/src/example/04_bloom/postprocess.c b/src/example/04_bloom/postprocess.c
index 06cb105..ca35594 100644
--- a/src/example/04_bloom/postprocess.c
+++ b/src/example/04_bloom/postprocess.c
@@ -1,4 +1,4 @@
-#include "../../shaders/common.h"
+#include "../../shaders/common/core.h"
/*uniforms*/
#define _frag_tex UTEX(0)
diff --git a/src/example/04_bloom/preprocess.c b/src/example/04_bloom/preprocess.c
index d7d4b21..9e5bb55 100644
--- a/src/example/04_bloom/preprocess.c
+++ b/src/example/04_bloom/preprocess.c
@@ -1,4 +1,4 @@
-#include "../../shaders/common.h"
+#include "../../shaders/common/core.h"
/*uniforms*/
diff --git a/src/extend/camera.c b/src/extend/camera.c
index 016b1d5..108668a 100644
--- a/src/extend/camera.c
+++ b/src/extend/camera.c
@@ -1,7 +1,9 @@
-#include "../shaders/common.h"
+#include "../shaders/common/core.h"
#include "../core/device.h"
#include "camera.h"
+//#define USE_CURSOR_WHEEL_ICON
+
typedef enum {
CursorType_Arrow = 0,
CursorType_Hand,
@@ -170,6 +172,7 @@ void camera_onevent(Camera* cam, wog_Event* e, float dt) {
return ;
//printf("%d\n",e->type);
if (e->type == WOG_EMOUSEWHEEL) {
+#if defined(USE_CURSOR_WHEEL_ICON)
if (e->wheel > 0)
{
setMouseCursor(CursorType_ZoomIn, cursor_zoomIn);
@@ -181,6 +184,7 @@ void camera_onevent(Camera* cam, wog_Event* e, float dt) {
if(cam->wheel_scroll <= 0)
wog_setMouseCapture(cam->wnd);
cam->wheel_scroll = 0.3;
+#endif
_onwheelscroll(cam, e->wheel, dt);
}
else if (e->type == WOG_EMOUSEBUTTONDOWN) {
@@ -218,13 +222,14 @@ void camera_onevent(Camera* cam, wog_Event* e, float dt) {
}
void camera_onupdate(Camera* cam, float dt) {
+#if defined(USE_CURSOR_WHEEL_ICON)
cam->wheel_scroll -= dt;
if (cam->wheel_scroll + dt > 0 && cam->wheel_scroll <= 0)
{
setMouseArrow();
wog_releaseMouseCapture(cam->wnd);
}
-
+#endif
if (cam->look_around)
_onlookaround(cam, dt);
if (cam->move_around)
diff --git a/src/gizmo/gizmo.c b/src/gizmo/gizmo.c
index ddd7045..3069cb4 100644
--- a/src/gizmo/gizmo.c
+++ b/src/gizmo/gizmo.c
@@ -1,7 +1,7 @@
#include "gizmo.h"
#include "../core/device.h"
#include "../math/math.h"
-#include "../shaders/common.h"
+#include "../shaders/common/core.h"
struct {
bool show_grid, g_pressing;
diff --git a/src/main.c b/src/main.c
index 13048ac..9fae2b9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -5,7 +5,7 @@
#include "ssr.h"
#include "example/example.h"
#include "extern/wog.h"
-#include "shaders/common.h"
+#include "shaders/common/core.h"
#include "extend/camera.h"
#include "extend/scene.h"
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;
+}
diff --git a/src/shaders/common.c b/src/shaders/common/core.c
index 333b433..703458c 100644
--- a/src/shaders/common.c
+++ b/src/shaders/common/core.c
@@ -1,4 +1,4 @@
-#include "common.h"
+#include "core.h"
Vec4 _proj_params;
Vec2 _time;
@@ -15,15 +15,6 @@ Vec3 unpacknormal(Color32 c32) {
Mat4 mat4(Vec4* c1, Vec4* c2, Vec4* c3, Vec4* c4);
-Mat3 mat3(Vec3* c1, Vec3* c2, Vec3* c3) {
- Mat3 m = {
- c1->x,c1->y,c1->z,
- c2->x,c2->y,c2->z,
- c3->x,c3->y,c3->z,
- };
- return m;
-}
-
Vec2 texsize(Texture* texture) {
Vec2 size = {texture->width, texture->height};
return size;
diff --git a/src/shaders/common.h b/src/shaders/common/core.h
index 8d0ac83..623409e 100644
--- a/src/shaders/common.h
+++ b/src/shaders/common/core.h
@@ -1,19 +1,7 @@
#ifndef _SOFTSHADEROOM_COMMON_HEADER_H_
#define _SOFTSHADEROOM_COMMON_HEADER_H_
-#include "../core/shader.h"
-
-/************************************************************************/
-/* constants */
-/************************************************************************/
-
-#define SSR_PI 3.14159265359f
-#define SSR_TWO_PI 6.28318530718f
-#define SSR_FOUR_PI 12.56637061436f
-#define SSR_INV_PI 0.31830988618f
-#define SSR_INV_TWO_PI 0.15915494309f
-#define SSR_INV_FOUR_PI 0.07957747155f
-#define SSR_HALF_PI 1.57079632679f
-#define SSR_INV_HALF_PI 0.636619772367f
+#include "../../core/shader.h"
+#include "mathlib.h"
/************************************************************************/
/* variables */
diff --git a/src/shaders/common/light.c b/src/shaders/common/light.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/shaders/common/light.c
diff --git a/src/shaders/common/light.h b/src/shaders/common/light.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/shaders/common/light.h
diff --git a/src/shaders/common/mathlib.c b/src/shaders/common/mathlib.c
new file mode 100644
index 0000000..356357c
--- /dev/null
+++ b/src/shaders/common/mathlib.c
@@ -0,0 +1,138 @@
+#include "mathlib.h"
+#include "../../math/math.h"
+
+#define INTERNAL_FUNCTION(T, NAME)\
+T NAME(T v1, T v2) {\
+ T out; \
+ internal_##NAME(&v1, &v2, &out);\
+ return out;\
+}
+
+/************************************************************************/
+/* Vec2 */
+/************************************************************************/
+Vec2 vec2(float x, float y) {
+ Vec2 v = {x, y};
+ return v;
+}
+
+INTERNAL_FUNCTION(Vec2, vec2_minus);
+INTERNAL_FUNCTION(Vec2, vec2_plus);
+
+float vec2_dot(Vec2 v1, Vec2 v2) {
+ return internal_vec2_dot(&v1, &v2);
+}
+
+/************************************************************************/
+/* Vec3 */
+/************************************************************************/
+Vec3 vec3(float x, float y, float z) {
+ Vec3 v = {x, y, z};
+ return v;
+}
+
+INTERNAL_FUNCTION(Vec3, vec3_minus);
+INTERNAL_FUNCTION(Vec3, vec3_plus);
+INTERNAL_FUNCTION(Vec3, vec3_cross);
+
+float vec3_dot(Vec3 v1, Vec3 v2) {
+ return internal_vec3_dot(&v1, &v2);
+}
+
+Vec3 vec3_lerp(Vec3 v1, Vec3 v2, float t) {
+ Vec3 out;
+ internal_vec3_lerp(&v1, &v2, t, &out);
+ return out;
+}
+
+Vec3 vec3_slerp(Vec3 v1, Vec3 v2, float t) {
+ Vec3 out;
+ internal_vec3_slerp(&v1, &v2, t, &out);
+ return out;
+}
+
+Vec3 vec3_scale(Vec3 v, float scale) {
+ Vec3 out;
+ internal_vec3_scale(&v, scale, &out);
+ return out;
+}
+
+Vec3 vec3_normalize(Vec3 v) {
+ Vec3 out;
+ internal_vec3_normalize(&v, &out);
+ return out;
+}
+
+/************************************************************************/
+/* Vec4 */
+/************************************************************************/
+
+Vec4 vec4(float x, float y, float z, float w) {
+ Vec4 v = {x, y, z, w};
+ return v;
+}
+
+INTERNAL_FUNCTION(Vec4, vec4_minus);
+INTERNAL_FUNCTION(Vec4, vec4_plus);
+
+Vec4 vec4_scale(Vec4 v, float scale) {
+ Vec4 out;
+ internal_vec4_scale(&v, scale, &out);
+ return out;
+}
+
+Vec4 vec4_saturate(Vec4 v) {
+ Vec4 out;
+ out.x = clamp(v.x, 0, 1);
+ out.y = clamp(v.y, 0, 1);
+ out.z = clamp(v.z, 0, 1);
+ out.w = clamp(v.w, 0, 1);
+ return out;
+}
+
+/************************************************************************/
+/* Matrix */
+/************************************************************************/
+
+Mat3 mat3(Vec3 c1, Vec3 c2, Vec3 c3) {
+ Mat3 m = {c1, c2, c3};
+ return m;
+}
+
+Vec3 mat3_mulvec3(Mat3 m, Vec3 v) {
+ Vec3 out;
+ internal_mat3_multvec3(&m, &v, &out);
+ return out;
+}
+
+Mat3 mat3_mulmat3(Mat3 m, Mat3 m2) {
+ Mat3 out;
+ internal_mat3_multmat3(&m, &m2, &out);
+ return out;
+}
+
+Mat4 mat4(Vec4 c1, Vec4 c2, Vec4 c3, Vec4 c4) {
+ Mat4 m ;
+ m.axisx = c1;
+ m.axisy = c2;
+ m.axisz = c3;
+ m.pos = c4;
+ return m;
+}
+
+Vec3 mat4_mulvec3(Mat4 m, Vec3 v) {
+ Vec4 v2 = {v.x, v.y, v.z, 0};
+ return mat4_mulvec4(m, v2).xyz;
+}
+
+Vec4 mat4_mulvec4(Mat4 m, Vec4 v) {
+ Vec4 out;
+ internal_mat4_mulvec4(&m, &v, &out);
+ return out;
+}
+
+Mat4 mat4_mulmat4(Mat4 m1, Mat4 m2) {
+ Mat4 out;
+ internal_mat4_multiply(&m1, &m2, &out);
+ return out;
+}
diff --git a/src/shaders/common/mathlib.h b/src/shaders/common/mathlib.h
new file mode 100644
index 0000000..fcc54ea
--- /dev/null
+++ b/src/shaders/common/mathlib.h
@@ -0,0 +1,63 @@
+#ifndef SSR_SHADER_MATH_H
+#define SSR_SHADER_MATH_H
+
+#include "../../math/structs.h"
+
+/************************************************************************/
+/* constants */
+/************************************************************************/
+
+#define SSR_PI 3.14159265359f
+#define SSR_TWO_PI 6.28318530718f
+#define SSR_FOUR_PI 12.56637061436f
+#define SSR_INV_PI 0.31830988618f
+#define SSR_INV_TWO_PI 0.15915494309f
+#define SSR_INV_FOUR_PI 0.07957747155f
+#define SSR_HALF_PI 1.57079632679f
+#define SSR_INV_HALF_PI 0.636619772367f
+
+/************************************************************************/
+/* Vec3 */
+/************************************************************************/
+Vec2 vec2(float x, float y);
+Vec2 vec2_minus(Vec2 v1, Vec2 v2);
+Vec2 vec2_plus(Vec2 v1, Vec2 v2);
+float vec2_dot(Vec2 v1, Vec2 v2);
+
+/************************************************************************/
+/* Vec3 */
+/************************************************************************/
+Vec3 vec3(float x, float y, float z);
+Vec3 vec3_minus(Vec3 v1, Vec3 v2);
+Vec3 vec3_plus(Vec3 v1, Vec3 v2);
+float vec3_dot(Vec3 v1, Vec3 v2);
+Vec3 vec3_cross(Vec3 v1, Vec3 v2);
+Vec3 vec3_lerp(Vec3 v1, Vec3 v2, float t);
+Vec3 vec3_slerp(Vec3 v1, Vec3 v2, float t);
+Vec3 vec3_scale(Vec3 v, float scale);
+Vec3 vec3_normalize(Vec3 v);
+
+/************************************************************************/
+/* Vec4 */
+/************************************************************************/
+Vec4 vec4(float x, float y, float z, float w);
+Vec4 vec4_minus(Vec4 v1, Vec4 v2);
+Vec4 vec4_plus(Vec4 v1, Vec4 v2);
+Vec4 vec4_scale(Vec4 v, float scale);
+Vec4 vec4_saturate(Vec4 v);
+Vec4 vec4_normalize(Vec4 v);
+
+/************************************************************************/
+/* Matrix */
+/************************************************************************/
+Mat3 mat3(Vec3 colum1, Vec3 colum2, Vec3 colum3); // colum major
+Vec3 mat3_mulvec3(Mat3 m, Vec3 v);
+Mat3 mat3_mulmat3(Mat3 m, Mat3 m2);
+
+Mat4 mat4(Vec4 colum1, Vec4 colum2, Vec4 colum3, Vec4 colum4);
+Vec3 mat4_mulvec3(Mat4 m, Vec3 v);
+Vec4 mat4_mulvec4(Mat4 m, Vec4 v);
+Mat4 mat4_mulmat4(Mat4 m, Mat4 m2);
+
+
+#endif \ No newline at end of file
diff --git a/src/shaders/common/shadow.c b/src/shaders/common/shadow.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/shaders/common/shadow.c
diff --git a/src/shaders/common/shadow.h b/src/shaders/common/shadow.h
new file mode 100644
index 0000000..2fef82d
--- /dev/null
+++ b/src/shaders/common/shadow.h
@@ -0,0 +1,6 @@
+#ifndef _SSR_SHADOW_H_
+#define _SSR_SHADOW_H_
+
+
+
+#endif \ No newline at end of file
diff --git a/src/shaders/default.c b/src/shaders/default.c
index 6d12c8e..d348cdf 100644
--- a/src/shaders/default.c
+++ b/src/shaders/default.c
@@ -1,4 +1,4 @@
-#include "common.h"
+#include "common/core.h"
/*uniforms*/
#define object2world UM4(0)
diff --git a/src/shaders/pbr.c b/src/shaders/pbr.c
index 89556af..bbf1c2b 100644
--- a/src/shaders/pbr.c
+++ b/src/shaders/pbr.c
@@ -1,4 +1,4 @@
-#include "common.h"
+#include "common/core.h"
/*uniforms*/
#define _object2world UM4(0)
@@ -22,14 +22,8 @@
static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoord) {
static Vec4 p; p.xyz = in->vertex->position; p.w = 1;
object2clip(&p, clipcoord);
- Vec4 normal = {
- in->vertex->normal.x,
- in->vertex->normal.y,
- in->vertex->normal.z,
- 1
- };
- Vec4 worldnormal; internal_mat4_mulvec4(_object2world, &normal, &worldnormal);
- internal_vec3_normalize(_light, _light);
+ Vec3 worldnormal = mat4_mulvec3(*_object2world, in->vertex->normal);
+ worldnormal = vec3_normalize(worldnormal);
//*rough = 1 - internal_vec3_dot(&worldnormal, light);
//*vnormal = in->vertex->normal;
*_texcoord = in->vertex->texcoord;
@@ -37,7 +31,7 @@ static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoor
_clip_pos->y = clipcoord->w;
}
-static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color) {
+static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Vec4* color) {
//internal_vec3_normalize(light, light);
//internal_vec3_normalize(vnormal, vnormal);
//float roughness = *rough;
@@ -50,11 +44,10 @@ static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* col
float depth = _clip_pos->x / _clip_pos->y;
depth = (depth + 1) / 2;
depth = linear01depth(depth);
- Color32 c = tex2d(_albedo_tex, _texcoord);
+ Vec4 c = tex2d(_albedo_tex, _texcoord);
//Color32 nc = tex2d(noramltex, in->texcoord);
//internal_vec3_scale(&c, roughness, &c);
- color32_saturate(&c);
- *color = c;
+ *color = vec4_saturate(c);
//internal_vec3_scale(color, 1 - depth, color);
return 1;
}
diff --git a/src/shaders/unlit.c b/src/shaders/unlit.c
index e887d07..5e1b1c1 100644
--- a/src/shaders/unlit.c
+++ b/src/shaders/unlit.c
@@ -1,4 +1,4 @@
-#include "common.h"
+#include "common/core.h"
/*uniforms*/
#define object2world UM4(0)