summaryrefslogtreecommitdiff
path: root/src/example
diff options
context:
space:
mode:
Diffstat (limited to 'src/example')
-rw-r--r--src/example/01_dot/01_dot.c (renamed from src/example/01_dot.c)4
-rw-r--r--src/example/02_cube/02_cube.c (renamed from src/example/02_cube.c)2
-rw-r--r--src/example/03_texture/03_texture.c (renamed from src/example/03_texture.c)65
-rw-r--r--src/example/04_bloom.c0
-rw-r--r--src/example/04_bloom/04_bloom.c120
-rw-r--r--src/example/04_bloom/postprocess.c41
-rw-r--r--src/example/04_bloom/preprocess.c31
-rw-r--r--src/example/05_fog/05_fog.c (renamed from src/example/05_fog.c)0
-rw-r--r--src/example/06_shadow/06_shadow.c1
-rw-r--r--src/example/example.h1
10 files changed, 237 insertions, 28 deletions
diff --git a/src/example/01_dot.c b/src/example/01_dot/01_dot.c
index 0a88084..e2902ca 100644
--- a/src/example/01_dot.c
+++ b/src/example/01_dot/01_dot.c
@@ -1,5 +1,5 @@
-#include "example.h"
-#include "../core/rasterizer.h"
+#include "../example.h"
+#include "../../core/rasterizer.h"
Vec3 pos = { 0, 0, 2 }, target = { 0,0,-1 }, up = { 0, 1, 0 };
diff --git a/src/example/02_cube.c b/src/example/02_cube/02_cube.c
index c1f2796..4e540e0 100644
--- a/src/example/02_cube.c
+++ b/src/example/02_cube/02_cube.c
@@ -1,4 +1,4 @@
-#include "example.h"
+#include "../example.h"
/*正方体*/
Vec3 verts[] = {
diff --git a/src/example/03_texture.c b/src/example/03_texture/03_texture.c
index 0e48fcb..a0493bb 100644
--- a/src/example/03_texture.c
+++ b/src/example/03_texture/03_texture.c
@@ -1,5 +1,5 @@
-#include "example.h"
-#include "../extend/mesh.h"
+#include "../example.h"
+#include "../../extend/mesh.h"
static int cube[] = {
0, 1, 2, 0, 2, 3,
@@ -37,22 +37,19 @@ static Mesh* ground_mesh;
static Texture* yingham_albedo;
static Mesh* yingham_mesh;
-static Texture* gun_albedo;
-static Mesh* gun_mesh;
+static Texture* cyborg_albedo;
+static Mesh* cyborg_mesh;
void onloadtexture(void* data) {
ssr_matrixmode(MATRIX_PROJECTION);
ssr_loadidentity();
- ssr_perspective(90, ssr_getaspect(), 0.1f, 1500);
+ ssr_perspective(90, ssr_getaspect(), 0.1, 10);
//ssr_ortho(-5, 5, -4, 4, 0.1, 10);
ssr_bindvertices(verts, 8, cube, 12);
ssr_useprogram(&ssr_built_in_shader_pbr);
ssr_enable(ENABLE_BACKFACECULL | ENABLE_DEPTHTEST | ENABLE_WRITEDEPTH | ENABLE_BLEND);
- ssr_setwrapmode(WRAPMODE_CLAMP);
- ssr_setfiltermode(FILTERMODE_POINT);
-
ssr_setblendfunc(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA);
mech_albedo = texture_loadfromfile("res/dieselpunk/mech_basecolor.tga");
@@ -64,6 +61,9 @@ void onloadtexture(void* data) {
ground_mesh = mesh_loadfromobj("res/dieselpunk/ground.obj");
yingham_albedo = texture_loadfromfile("res/dieselpunk/yingham_basecolor.tga");
yingham_mesh = mesh_loadfromobj("res/dieselpunk/yingham.obj");
+
+ cyborg_albedo = texture_loadfromfile("res/cyborg/cyborg_diffuse.png");
+ cyborg_mesh = mesh_loadfromobj("res/cyborg/cyborg.obj");
}
void oneventtexture(void* data) {
@@ -79,8 +79,8 @@ void onupdatetexture(void*data) {
ssr_matrixmode(MATRIX_VIEW);
ssr_loadidentity();
- float distance = 800;
- Vec3 p = { distance * sin(_t), 300, distance * cos(_t) }, target = { 0, 0, 0 };
+ float distance = 2;
+ Vec3 p = { distance * sin(_t), 3, distance * cos(_t) }, target = { 0, 0, 0 };
//Vec3 p = { 0, 0, 700}, target = { 0, 0, 0 };
ssr_lookat(&p, &target, &vec3up);
@@ -103,20 +103,35 @@ void onupdatetexture(void*data) {
void ondrawtexture(void*data) {
ssr_clearcolor(0xff202020);
ssr_cleardepth();
-
- ssr_setuniformtex(0, mech_albedo);
- ssr_setuniformtex(1, mech_normal);
- ssr_setuniformtex(2, mech_roughness);
- ssr_setuniformtex(3, mech_metalness);
- ssr_bindvertices(mech_mesh->vertices, mech_mesh->verts_count, mech_mesh->triangles, mech_mesh->tris_count);
- ssr_draw(PRIMITIVE_TRIANGLE);
-
- ssr_setuniformtex(0, ground_albedo);
- ssr_bindvertices(ground_mesh->vertices, ground_mesh->verts_count, ground_mesh->triangles, ground_mesh->tris_count);
- ssr_draw(PRIMITIVE_TRIANGLE);
-
- ssr_setuniformtex(0, yingham_albedo);
- ssr_bindvertices(yingham_mesh->vertices, yingham_mesh->verts_count, yingham_mesh->triangles, yingham_mesh->tris_count);
- ssr_draw(PRIMITIVE_TRIANGLE);
+ ssr_clearstencil(0);
+///*
+// ssr_enable(ENABLE_STENCILTEST);
+// ssr_setstencilfunc(STENCILFUNC_ALWAYS, 1, 0xff);
+// ssr_setstencilop(STENCILOP_KEEP, STENCILOP_KEEP, STENCILOP_REPLACE);
+//*/
+// /*render mech*/
+// ssr_setuniformtex(0, mech_albedo);
+// ssr_setuniformtex(1, mech_normal);
+// ssr_setuniformtex(2, mech_roughness);
+// ssr_setuniformtex(3, mech_metalness);
+// ssr_bindvertices(mech_mesh->vertices, mech_mesh->vert_count, mech_mesh->triangles, mech_mesh->tris_count);
+// ssr_draw(PRIMITIVE_TRIANGLE);
+///*
+// ssr_setstencilfunc(STENCILFUNC_EQUAL, 1, 0xff);
+// ssr_setstencilop(STENCILOP_KEEP, STENCILOP_KEEP, STENCILOP_KEEP);
+//*/
+// /*render yingham*/
+// ssr_setuniformtex(0, yingham_albedo);
+// ssr_bindvertices(yingham_mesh->vertices, yingham_mesh->vert_count, yingham_mesh->triangles, yingham_mesh->tris_count);
+// ssr_draw(PRIMITIVE_TRIANGLE);
+//
+// /*render ground*/
+// ssr_setuniformtex(0, ground_albedo);
+// ssr_bindvertices(ground_mesh->vertices, ground_mesh->vert_count, ground_mesh->triangles, ground_mesh->tris_count);
+// ssr_draw(PRIMITIVE_TRIANGLE);
+//
+ ssr_setuniformtex(0, cyborg_albedo);
+ ssr_bindvertices(cyborg_mesh->vertices, cyborg_mesh->vert_count, cyborg_mesh->triangles, cyborg_mesh->tris_count);
+ ssr_draw(PRIMITIVE_TRIANGLE);
}
diff --git a/src/example/04_bloom.c b/src/example/04_bloom.c
deleted file mode 100644
index e69de29..0000000
--- a/src/example/04_bloom.c
+++ /dev/null
diff --git a/src/example/04_bloom/04_bloom.c b/src/example/04_bloom/04_bloom.c
new file mode 100644
index 0000000..6f37f62
--- /dev/null
+++ b/src/example/04_bloom/04_bloom.c
@@ -0,0 +1,120 @@
+#include "../example.h"
+#include "../../extend/mesh.h"
+
+static int cube[] = {
+ 0, 1, 2, 0, 2, 3,
+ 1, 5, 2, 2, 5, 6,
+ 4, 6, 5, 4, 7, 6,
+ 0, 3, 7, 0, 7, 4,
+ 0, 4, 1, 1, 4, 5,
+ 2, 6, 3, 3, 6, 7
+};
+
+static Vert verts[] = {
+ {0, {1, 1, 1}, {1, 1, 1}, zerovec4, {1, 1}, 0xffff0000},
+ {1, {-1, 1, 1}, {-1, 1, 1}, zerovec4, {0, 1},0xff00ff00},
+ {2, {-1, -1, 1}, {-1, -1, 1}, zerovec4, {0, 0}, 0xff0000ff},
+ {3, {1, -1, 1}, {1, -1, 1}, zerovec4, {1, 0}, 0xffff00ff},
+ {4, {1, 1, -1}, {1, 1, -1}, zerovec4, {1, 0} , 0xffaa28aa},
+ {5, {-1, 1, -1}, {-1, 1, -1}, zerovec4, {0, 0},0xffFFC58E},
+ {6, {-1, -1, -1}, {-1, -1, -1}, zerovec4, {0, 1}, 0xffA100FF},
+ {7, {1, -1, -1}, {1, -1, -1}, zerovec4, {1, 1} , 0xffFAFF00},
+};
+
+extern Program _04_bloom_preprocess;
+extern Program _04_bloom_postprocess;
+
+static Vec3 light = { -1, -1, -1 };
+
+static Texture* cyborg_albedo;
+static Mesh* cyborg_mesh;
+
+static FrameBuffer* fbo;
+static float* depth_buffer;
+static byte* stencil_buffer;
+
+static Texture* albedo_tex;
+static Texture* bright_tex;
+
+void onloadbloom(void* data) {
+ ssr_matrixmode(MATRIX_PROJECTION);
+ ssr_loadidentity();
+ ssr_perspective(90, ssr_getaspect(), 0.1, 10);
+ //ssr_ortho(-5, 5, -4, 4, 0.1, 10);
+
+
+ ssr_setblendfunc(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA);
+
+ albedo_tex = texture_create(600, 480);
+ bright_tex = texture_create(600, 480);
+
+ depth_buffer = ssrM_newvector(float, 600 * 480);
+ stencil_buffer = ssrM_newvector(byte, 600 * 480);
+
+ fbo = fbo_create();
+ fbo_attachrendertexture(fbo, 0, albedo_tex);
+ fbo_attachrendertexture(fbo, 1, bright_tex);
+ fbo_attachdepthbuffer(fbo, depth_buffer);
+ fbo_attachstencilbuffer(fbo, stencil_buffer);
+}
+
+void oneventbloom(void* data) {
+ SDL_Event* e = (SDL_Event*)data;
+}
+
+static float _t = 0;
+static Quat q;
+
+void onupdatebloom(void*data) {
+ uint dt = *(uint*)data;
+ _t += dt / 1000.f;
+
+ ssr_matrixmode(MATRIX_VIEW);
+ ssr_loadidentity();
+ float distance = 2;
+ Vec3 p = { distance * sin(_t), 5, distance * cos(_t) }, target = { 0, 0, 0 };
+ ssr_lookat(&p, &target, &vec3up);
+
+ ssr_matrixmode(MATRIX_MODEL);
+ ssr_loadidentity();
+}
+
+static void renderquad() {
+ /*全屏特效*/
+ static Vert verts[] = {
+ {0, {1, 1, 0}, zerovec3, zerovec3, {1, 1}, 0},
+ {1, {-1, 1, 0}, zerovec3, zerovec3, {0, 1},0},
+ {2, {-1, -1, 0}, zerovec3, zerovec3, {0, 0}, 0},
+ {3, {1, -1, 0}, zerovec3, zerovec3, {1, 0}, 0},
+ };
+ static int quad[] = {
+ 0, 1, 2, 0, 2, 3,
+ };
+ ssr_useprogram(&_04_bloom_postprocess);
+ ssr_bindvertices(verts, 4, quad, 2);
+ ssr_setuniformtex(0, albedo_tex);
+ ssr_setuniformtex(1, bright_tex);
+ ssr_draw(PRIMITIVE_TRIANGLE);
+}
+
+void ondrawbloom(void*data) {
+ ssr_enable(ENABLE_BACKFACECULL | ENABLE_DEPTHTEST | ENABLE_BLEND | ENABLE_WRITEDEPTH);
+
+ /*render cube*/
+ ssr_bindframebuffer(fbo);
+ ssr_clearcolor(0xff202020);
+ ssr_cleardepth();
+ ssr_clearstencil(0);
+ ssr_bindvertices(verts, 8, cube, 12);
+ ssr_useprogram(&_04_bloom_preprocess);
+ ssr_bindvertices(verts, 8, cube, 12);
+ ssr_draw(PRIMITIVE_TRIANGLE);
+
+ ssr_disable(ENABLE_BLEND | ENABLE_DEPTHTEST | ENABLE_WRITEDEPTH | ENABLE_BACKFACECULL);
+ ssr_unbindframebuffer();
+ ssr_clearcolor(0xff202020);
+ ssr_cleardepth();
+ ssr_clearstencil(0);
+
+ renderquad();
+}
diff --git a/src/example/04_bloom/postprocess.c b/src/example/04_bloom/postprocess.c
new file mode 100644
index 0000000..1ec10e4
--- /dev/null
+++ b/src/example/04_bloom/postprocess.c
@@ -0,0 +1,41 @@
+#include "../../shaders/common.h"
+
+/*uniforms*/
+#define _frag_tex UTEX(0)
+#define _bright_tex UTEX(1)
+
+/*varyings*/
+#define _texcoord reg_v2_00
+
+static float weight[5] = { 0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162 };
+
+static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoord) {
+ clipcoord->xyz = in->vertex->position;
+ clipcoord->w = 1;
+ *_texcoord = in->vertex->texcoord;
+}
+
+static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color) {
+ float off = 1 / 600.f;
+ //blur
+ *color = tex2d(_frag_tex, _texcoord);
+ vec4_scale(color, weight[0], color);
+ Vec2 p = {0, _texcoord->y};
+ Color32 c;
+ for (int i = 1; i < 5; ++i) {
+ p.x = _texcoord->x + off * i;
+ c = tex2d(_frag_tex, &p);
+ vec4_scale(&c, weight[i], &c);
+ vec4_add(color, &c, color);
+ p.x = _texcoord->x - off * i;
+ c = tex2d(_frag_tex, &p);
+ vec4_scale(&c, weight[i], &c);
+ vec4_add(color, &c, color);
+ }
+ return 1;
+}
+
+Program _04_bloom_postprocess = {
+ vert, frag,
+ VARYING_V2_00
+};
diff --git a/src/example/04_bloom/preprocess.c b/src/example/04_bloom/preprocess.c
new file mode 100644
index 0000000..def9fd8
--- /dev/null
+++ b/src/example/04_bloom/preprocess.c
@@ -0,0 +1,31 @@
+#include "../../shaders/common.h"
+
+/*uniforms*/
+
+/*varyings*/
+#define _color reg_v4_00
+
+static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoord) {
+ static Vec4 p; p.xyz = in->vertex->position; p.w = 1;
+ mat4_mulvec4(uniforms->mvp, &p, clipcoord);
+ color_tocolor32(in->vertex->color, _color);
+}
+
+/*multi target*/
+#define _frag_color out_color[0]
+#define _bright_color out_color[1]
+
+static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color) {
+ //_frag_color->r = 1;
+ //_frag_color->g = 1;
+ //_frag_color->b = 1;
+ //_frag_color->a = 1;
+ //*_bright_color = *_color;
+ *_frag_color = *_color;
+ return 1;
+}
+
+Program _04_bloom_preprocess = {
+ vert, frag,
+ VARYING_V4_00
+};
diff --git a/src/example/05_fog.c b/src/example/05_fog/05_fog.c
index e69de29..e69de29 100644
--- a/src/example/05_fog.c
+++ b/src/example/05_fog/05_fog.c
diff --git a/src/example/06_shadow/06_shadow.c b/src/example/06_shadow/06_shadow.c
new file mode 100644
index 0000000..015aafd
--- /dev/null
+++ b/src/example/06_shadow/06_shadow.c
@@ -0,0 +1 @@
+/*real time shadow*/
diff --git a/src/example/example.h b/src/example/example.h
index 15b547e..6cb9a64 100644
--- a/src/example/example.h
+++ b/src/example/example.h
@@ -19,5 +19,6 @@ EXAMPLE(cube);
EXAMPLE(line);
EXAMPLE(dot);
EXAMPLE(texture);
+EXAMPLE(bloom);
#endif \ No newline at end of file