summaryrefslogtreecommitdiff
path: root/src/shaders
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/shaders
parent3f44877edfe4c301b258d522bcb4e8d9b6e92382 (diff)
*misc
Diffstat (limited to 'src/shaders')
-rw-r--r--src/shaders/common.c21
-rw-r--r--src/shaders/common.h (renamed from src/shaders/common_header.h)8
-rw-r--r--src/shaders/default.c51
-rw-r--r--src/shaders/unlit.c38
4 files changed, 96 insertions, 22 deletions
diff --git a/src/shaders/common.c b/src/shaders/common.c
new file mode 100644
index 0000000..f36e272
--- /dev/null
+++ b/src/shaders/common.c
@@ -0,0 +1,21 @@
+#include "common.h"
+
+Vec3 normal_from_color(Color32 c32) {
+ Vec3 normal = {
+ c32.r * 2 - 1,
+ c32.g * 2 - 1,
+ c32.b * 2 - 1,
+ };
+ return normal;
+}
+
+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;
+}
diff --git a/src/shaders/common_header.h b/src/shaders/common.h
index 9741a36..2779c7b 100644
--- a/src/shaders/common_header.h
+++ b/src/shaders/common.h
@@ -5,4 +5,12 @@
extern void ssrR_putline(int x0, int y0, int x1, int y1, Color color);
+/*
+** shader built in functions
+*/
+Vec3 normal_from_color(Color32 c32);
+
+Mat4 mat4(Vec4* c1, Vec4* c2, Vec4* c3, Vec4* c4);
+Mat3 mat3(Vec3* c1, Vec3* c2, Vec3* c3);
+
#endif \ No newline at end of file
diff --git a/src/shaders/default.c b/src/shaders/default.c
index b1345e0..49a80d1 100644
--- a/src/shaders/default.c
+++ b/src/shaders/default.c
@@ -1,20 +1,51 @@
-#include "common_header.h"
+#include "common.h"
/*uniforms*/
-#define lightdir UV3(0)
-#define shinees UN(0)
+#define object2world UM4(0)
+#define light UV3(0)
+#define maintex UTEX(0)
+#define noramltex UTEX(1)
+#define roughnesstex UTEX(2)
+#define metalnesstex UTEX(3)
-/*vertex output*/
-#define uv reg_v2_00
+/*varyings*/
+#define rough reg_num_00
-static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* homocoord) {
+static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoord) {
static Vec4 p; p.xyz = in->vertex->position; p.w = 1;
- mat4_applytovec4(uniforms->mvp, &p, homocoord);
+ mat4_applytovec4(uniforms->mvp, &p, clipcoord);
+ Vec4 normal = {
+ in->vertex->normal.x,
+ in->vertex->normal.y,
+ in->vertex->normal.z,
+ 1
+ };
+ Vec4 worldnormal; mat4_applytovec4(object2world, &normal, &worldnormal);
+ vec3_normalize(light, light);
+ *reg_num_00 = 1 - vec3_dot(&worldnormal, light);
}
-static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color* color) {
-
+static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color) {
+ vec3_normalize(light, light);
+ vec3_normalize(&in->normal, &in->normal);
+ float roughness = *reg_num_00;
+ //(*color).r = 1;
+ //(*color).g = 1;
+ //(*color).b = 1;
+ //(*color).a = 1;
+ //return 1;
+ //float rough = 1- vec3_dot(&in->normal, light);
+ Color32 c = tex2d(maintex, in->texcoord);
+ //Color32 nc = tex2d(noramltex, in->texcoord);
+ //vec3_scale(&c, roughness, &c);
+ color32_saturate(&c);
+ *color = c;
return 1;
}
-Program ssr_built_in_shader_default = { vert, frag }; \ No newline at end of file
+Program ssr_built_in_shader_default = {
+ vert, frag,
+ VARYING_TEXCOORD |
+ VARYING_NORMAL |
+ VARYING_NUM_00
+}; \ No newline at end of file
diff --git a/src/shaders/unlit.c b/src/shaders/unlit.c
index fe692f0..e5abdce 100644
--- a/src/shaders/unlit.c
+++ b/src/shaders/unlit.c
@@ -1,28 +1,42 @@
-#include "common_header.h"
+#include "common.h"
-#define light UV3(0)
-#define maintex TEX(0)
+/*uniforms*/
+#define object2world UM4(0)
+#define light UV3(0)
+#define maintex UTEX(0)
-#define vert_color reg_v4_00
+/*varyings*/
+#define rough reg_num_00
static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoord) {
static Vec4 p; p.xyz = in->vertex->position; p.w = 1;
mat4_applytovec4(uniforms->mvp, &p, clipcoord);
- color_tocolor32(in->vertex->color, vert_color);
+ Vec4 normal = {
+ in->vertex->normal.x,
+ in->vertex->normal.y,
+ in->vertex->normal.z,
+ 1
+ };
+ Vec4 worldnormal; mat4_applytovec4(object2world, &normal, &worldnormal);
+ vec3_normalize(light, light);
+ *reg_num_00 = 1 - vec3_dot(&worldnormal, light);
}
-static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color* color) {
+static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color) {
vec3_normalize(light, light);
vec3_normalize(&in->normal, &in->normal);
- float strongness = vec3_dot(light, &in->normal);
- vec3_scale(vert_color, 1 - clamp(strongness, 0, 1), vert_color);
- *color = color32_tocolor(vert_color);
- //Color32 c = texture2d(maintex, &in->texcoord);
- //*color = color32_tocolor(&c);
+ //float rough = 1- vec3_dot(&in->normal, light);
+ float roughness = *reg_num_00;
+ Color32 c = tex2d(maintex, in->texcoord);
+ //vec3_scale(&c, roughness, &c);
+ //color32_saturate(&c);
+ *color = c;
return 1;
}
Program ssr_built_in_shader_unlit = {
vert, frag,
- VARYING_V4_00 | VARYING_TEXCOORD
+ VARYING_TEXCOORD |
+ VARYING_NORMAL |
+ VARYING_NUM_00
};