From ec111247c614663d8231245a17c314b9b8b4a28c Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 21 Dec 2019 22:24:15 +0800 Subject: *misc --- src/example/01_dot.c | 36 ---------- src/example/01_dot/01_dot.c | 36 ++++++++++ src/example/02_cube.c | 73 ------------------- src/example/02_cube/02_cube.c | 73 +++++++++++++++++++ src/example/03_texture.c | 122 -------------------------------- src/example/03_texture/03_texture.c | 137 ++++++++++++++++++++++++++++++++++++ src/example/04_bloom.c | 0 src/example/04_bloom/04_bloom.c | 120 +++++++++++++++++++++++++++++++ src/example/04_bloom/postprocess.c | 41 +++++++++++ src/example/04_bloom/preprocess.c | 31 ++++++++ src/example/05_fog.c | 0 src/example/05_fog/05_fog.c | 0 src/example/06_shadow/06_shadow.c | 1 + src/example/example.h | 1 + 14 files changed, 440 insertions(+), 231 deletions(-) delete mode 100644 src/example/01_dot.c create mode 100644 src/example/01_dot/01_dot.c delete mode 100644 src/example/02_cube.c create mode 100644 src/example/02_cube/02_cube.c delete mode 100644 src/example/03_texture.c create mode 100644 src/example/03_texture/03_texture.c delete mode 100644 src/example/04_bloom.c create mode 100644 src/example/04_bloom/04_bloom.c create mode 100644 src/example/04_bloom/postprocess.c create mode 100644 src/example/04_bloom/preprocess.c delete mode 100644 src/example/05_fog.c create mode 100644 src/example/05_fog/05_fog.c create mode 100644 src/example/06_shadow/06_shadow.c (limited to 'src/example') diff --git a/src/example/01_dot.c b/src/example/01_dot.c deleted file mode 100644 index 0a88084..0000000 --- a/src/example/01_dot.c +++ /dev/null @@ -1,36 +0,0 @@ -#include "example.h" -#include "../core/rasterizer.h" - -Vec3 pos = { 0, 0, 2 }, target = { 0,0,-1 }, up = { 0, 1, 0 }; - -float dot[] = { 0, 0, -2 }; - -void onloaddot(void* data) { - -} - -void oneventdot(void* data) { - SDL_Event* e = (SDL_Event*)data; -} - -void onupdatedot(void*data) { - uint dt = *(uint*)data; - ssr_matrixmode(MATRIX_VIEW); - ssr_lookat(&pos, &target, &up); - ssr_matrixmode(MATRIX_PROJECTION); -} - -float j = 0; - -void ondrawdot(void*data) { - ssr_clearcolor(0xffffffff); - - //for (int i = -50; i < 200; ++i) { - // ssr_putpoint(i * sin(j += 0.1f), 300 * cos(j), ssr_color(0xff, 0, 0xff, 0)); - //} - - ssrR_putline(-100, 0, 100, 200, 0xffff0000); - -// printf("%u\n", (UINT_MAX * (double)0.5F)); - -} diff --git a/src/example/01_dot/01_dot.c b/src/example/01_dot/01_dot.c new file mode 100644 index 0000000..e2902ca --- /dev/null +++ b/src/example/01_dot/01_dot.c @@ -0,0 +1,36 @@ +#include "../example.h" +#include "../../core/rasterizer.h" + +Vec3 pos = { 0, 0, 2 }, target = { 0,0,-1 }, up = { 0, 1, 0 }; + +float dot[] = { 0, 0, -2 }; + +void onloaddot(void* data) { + +} + +void oneventdot(void* data) { + SDL_Event* e = (SDL_Event*)data; +} + +void onupdatedot(void*data) { + uint dt = *(uint*)data; + ssr_matrixmode(MATRIX_VIEW); + ssr_lookat(&pos, &target, &up); + ssr_matrixmode(MATRIX_PROJECTION); +} + +float j = 0; + +void ondrawdot(void*data) { + ssr_clearcolor(0xffffffff); + + //for (int i = -50; i < 200; ++i) { + // ssr_putpoint(i * sin(j += 0.1f), 300 * cos(j), ssr_color(0xff, 0, 0xff, 0)); + //} + + ssrR_putline(-100, 0, 100, 200, 0xffff0000); + +// printf("%u\n", (UINT_MAX * (double)0.5F)); + +} diff --git a/src/example/02_cube.c b/src/example/02_cube.c deleted file mode 100644 index c1f2796..0000000 --- a/src/example/02_cube.c +++ /dev/null @@ -1,73 +0,0 @@ -#include "example.h" - -/*正方体*/ -Vec3 verts[] = { - // front face - {1, 1, 1}, {-1, 1, 1}, {-1, -1, 1}, {1, -1, 1}, - // back face - {1, 1, -1}, {-1, 1, -1}, {-1, -1, -1}, {1, -1, -1}, -}; - -Color colors[] = { - 0xffff0000, 0xff00ff00, 0xffff00ff, 0xff00ffff, - 0xff0000ff, 0xff000000, 0xffffff00, 0xffffffff, -}; - -int cube[] = { - 0, 2, 1, 0, 3, 2, - 1, 2, 5, 2, 6, 5, - 4, 5, 6, 4, 6, 7, - 0, 4, 7, 0, 7, 3, - 0, 1, 4, 1, 5, 4, - 2, 3, 6, 3, 7, 6 -}; -Mat4 m; -void onloadcube(void* data) { -} - -void oneventcube(void* data) { - SDL_Event* e = (SDL_Event*)data; -} - -float _t = 0; - -void onupdatecube(void*data) { - uint dt = *(uint*)data; - ssr_matrixmode(MATRIX_MODEL); - ssr_loadidentity(); - ssr_translate(0, 0, -3); - ssr_rotate(360 * sin(_t += 0.001f), 1, 1, 1); - ssr_matrixmode(MATRIX_PROJECTION); - ssr_loadidentity(); - ssr_perspective(100 + 20 * sin(_t * 10), 1.25f, -0.1f, -100); - ssr_matrixmode(MATRIX_VIEW); - ssr_loadidentity(); - Vec3 pos = { 0,0,0 }, target = { 0,0,-1 }, up = { 0,1,0 }; - ssr_lookat(&pos, &target, &up); - ssr_getmvp(&m); -} - -void ondrawcube(void*data) { - ssr_clearcolor(0); - Vec2 proj[8]; - - for (int i = 0; i < 8; ++i) { - Vec4 v = { verts[i].x, verts[i].y ,verts[i].z ,1 }, temp; - mat4_mulvec4(&m, &v, &temp); - temp.x /= temp.w; - temp.y /= temp.w; - temp.z /= temp.w; - //vec4_print(&temp); - proj[i].x = temp.x; - proj[i].y = temp.y; - } - for (int j = 1; j < sizeof(cube) / sizeof(int); ++j) { - int fromx = proj[cube[j]].x * 250.f + 250, fromy = 200 - proj[cube[j]].y * 200.f; - int tox = proj[cube[j - 1]].x * 250.f + 250, toy = 200 - proj[cube[j - 1]].y * 200.f; - ssrR_putline(fromx, fromy , tox, toy, 0xffff0000); - } - - Vec2 v1 = { 0, 0 }, v2 = { 3, 1 }, v3 = {1, 5}; - float area = ssrR_area(&v1, &v2, &v3); - printf("%f\n", area); -} diff --git a/src/example/02_cube/02_cube.c b/src/example/02_cube/02_cube.c new file mode 100644 index 0000000..4e540e0 --- /dev/null +++ b/src/example/02_cube/02_cube.c @@ -0,0 +1,73 @@ +#include "../example.h" + +/*正方体*/ +Vec3 verts[] = { + // front face + {1, 1, 1}, {-1, 1, 1}, {-1, -1, 1}, {1, -1, 1}, + // back face + {1, 1, -1}, {-1, 1, -1}, {-1, -1, -1}, {1, -1, -1}, +}; + +Color colors[] = { + 0xffff0000, 0xff00ff00, 0xffff00ff, 0xff00ffff, + 0xff0000ff, 0xff000000, 0xffffff00, 0xffffffff, +}; + +int cube[] = { + 0, 2, 1, 0, 3, 2, + 1, 2, 5, 2, 6, 5, + 4, 5, 6, 4, 6, 7, + 0, 4, 7, 0, 7, 3, + 0, 1, 4, 1, 5, 4, + 2, 3, 6, 3, 7, 6 +}; +Mat4 m; +void onloadcube(void* data) { +} + +void oneventcube(void* data) { + SDL_Event* e = (SDL_Event*)data; +} + +float _t = 0; + +void onupdatecube(void*data) { + uint dt = *(uint*)data; + ssr_matrixmode(MATRIX_MODEL); + ssr_loadidentity(); + ssr_translate(0, 0, -3); + ssr_rotate(360 * sin(_t += 0.001f), 1, 1, 1); + ssr_matrixmode(MATRIX_PROJECTION); + ssr_loadidentity(); + ssr_perspective(100 + 20 * sin(_t * 10), 1.25f, -0.1f, -100); + ssr_matrixmode(MATRIX_VIEW); + ssr_loadidentity(); + Vec3 pos = { 0,0,0 }, target = { 0,0,-1 }, up = { 0,1,0 }; + ssr_lookat(&pos, &target, &up); + ssr_getmvp(&m); +} + +void ondrawcube(void*data) { + ssr_clearcolor(0); + Vec2 proj[8]; + + for (int i = 0; i < 8; ++i) { + Vec4 v = { verts[i].x, verts[i].y ,verts[i].z ,1 }, temp; + mat4_mulvec4(&m, &v, &temp); + temp.x /= temp.w; + temp.y /= temp.w; + temp.z /= temp.w; + //vec4_print(&temp); + proj[i].x = temp.x; + proj[i].y = temp.y; + } + for (int j = 1; j < sizeof(cube) / sizeof(int); ++j) { + int fromx = proj[cube[j]].x * 250.f + 250, fromy = 200 - proj[cube[j]].y * 200.f; + int tox = proj[cube[j - 1]].x * 250.f + 250, toy = 200 - proj[cube[j - 1]].y * 200.f; + ssrR_putline(fromx, fromy , tox, toy, 0xffff0000); + } + + Vec2 v1 = { 0, 0 }, v2 = { 3, 1 }, v3 = {1, 5}; + float area = ssrR_area(&v1, &v2, &v3); + printf("%f\n", area); +} diff --git a/src/example/03_texture.c b/src/example/03_texture.c deleted file mode 100644 index 0e48fcb..0000000 --- a/src/example/03_texture.c +++ /dev/null @@ -1,122 +0,0 @@ -#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}, zerovec3, {1, 1}, 0xffff0000}, - {1, {-1, 1, 1}, {-1, 1, 1}, zerovec3, {0, 1},0xff00ff00}, - {2, {-1, -1, 1}, {-1, -1, 1}, zerovec3, {0, 0}, 0xff0000ff}, - {3, {1, -1, 1}, {1, -1, 1}, zerovec3, {1, 0}, 0xffff00ff}, - {4, {1, 1, -1}, {1, 1, -1}, zerovec3, {1, 0} , 0xffaa28aa}, - {5, {-1, 1, -1}, {-1, 1, -1}, zerovec3, {0, 0},0xffFFC58E}, - {6, {-1, -1, -1}, {-1, -1, -1}, zerovec3, {0, 1}, 0xffA100FF}, - {7, {1, -1, -1}, {1, -1, -1}, zerovec3, {1, 1} , 0xffFAFF00}, -}; - -extern Program ssr_built_in_shader_pbr; - -static Vec3 light = {-1, -1, -1}; - -static Texture* mech_albedo; -static Texture* mech_normal; -static Texture* mech_roughness; -static Texture* mech_metalness; -static Mesh* mech_mesh; - -static Texture* ground_albedo; -static Mesh* ground_mesh; - -static Texture* yingham_albedo; -static Mesh* yingham_mesh; - -static Texture* gun_albedo; -static Mesh* gun_mesh; - -void onloadtexture(void* data) { - ssr_matrixmode(MATRIX_PROJECTION); - ssr_loadidentity(); - ssr_perspective(90, ssr_getaspect(), 0.1f, 1500); - //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"); - mech_normal = texture_loadfromfile("res/dieselpunk/mech_normal.tga"); - mech_roughness = texture_loadfromfile("res/dieselpunk/mech_roughness.tga"); - mech_metalness = texture_loadfromfile("res/dieselpunk/mech_metalness.tga"); - mech_mesh = mesh_loadfromobj("res/dieselpunk/mech.obj"); - ground_albedo = texture_loadfromfile("res/dieselpunk/ground_basecolor.tga"); - 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"); -} - -void oneventtexture(void* data) { - SDL_Event* e = (SDL_Event*)data; -} - -static float _t = 0; -static Quat q; - -void onupdatetexture(void*data) { - uint dt = *(uint*)data; - _t += dt / 1000.f; - - ssr_matrixmode(MATRIX_VIEW); - ssr_loadidentity(); - float distance = 800; - Vec3 p = { distance * sin(_t), 300, distance * cos(_t) }, target = { 0, 0, 0 }; - //Vec3 p = { 0, 0, 700}, target = { 0, 0, 0 }; - ssr_lookat(&p, &target, &vec3up); - - ssr_matrixmode(MATRIX_MODEL); - ssr_loadidentity(); - //ssr_translate(0, 0, -700); - //ssr_rotate(_t * 10, 0, 1, 0); - - Vec3 light = {1, 0, 0}; - ssr_setuniformvec3(0, &light); - - Mat4 world2object; - ssr_getm(&world2object); - //mat4_invertgeneral3d(&world2object, &world2object); - //mat4_transpose(&world2object, &world2object); // transpose it - - ssr_setuniformmat4(0, &world2object); -} - -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); - -} diff --git a/src/example/03_texture/03_texture.c b/src/example/03_texture/03_texture.c new file mode 100644 index 0000000..a0493bb --- /dev/null +++ b/src/example/03_texture/03_texture.c @@ -0,0 +1,137 @@ +#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}, zerovec3, {1, 1}, 0xffff0000}, + {1, {-1, 1, 1}, {-1, 1, 1}, zerovec3, {0, 1},0xff00ff00}, + {2, {-1, -1, 1}, {-1, -1, 1}, zerovec3, {0, 0}, 0xff0000ff}, + {3, {1, -1, 1}, {1, -1, 1}, zerovec3, {1, 0}, 0xffff00ff}, + {4, {1, 1, -1}, {1, 1, -1}, zerovec3, {1, 0} , 0xffaa28aa}, + {5, {-1, 1, -1}, {-1, 1, -1}, zerovec3, {0, 0},0xffFFC58E}, + {6, {-1, -1, -1}, {-1, -1, -1}, zerovec3, {0, 1}, 0xffA100FF}, + {7, {1, -1, -1}, {1, -1, -1}, zerovec3, {1, 1} , 0xffFAFF00}, +}; + +extern Program ssr_built_in_shader_pbr; + +static Vec3 light = {-1, -1, -1}; + +static Texture* mech_albedo; +static Texture* mech_normal; +static Texture* mech_roughness; +static Texture* mech_metalness; +static Mesh* mech_mesh; + +static Texture* ground_albedo; +static Mesh* ground_mesh; + +static Texture* yingham_albedo; +static Mesh* yingham_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.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_setblendfunc(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA); + + mech_albedo = texture_loadfromfile("res/dieselpunk/mech_basecolor.tga"); + mech_normal = texture_loadfromfile("res/dieselpunk/mech_normal.tga"); + mech_roughness = texture_loadfromfile("res/dieselpunk/mech_roughness.tga"); + mech_metalness = texture_loadfromfile("res/dieselpunk/mech_metalness.tga"); + mech_mesh = mesh_loadfromobj("res/dieselpunk/mech.obj"); + ground_albedo = texture_loadfromfile("res/dieselpunk/ground_basecolor.tga"); + 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) { + SDL_Event* e = (SDL_Event*)data; +} + +static float _t = 0; +static Quat q; + +void onupdatetexture(void*data) { + uint dt = *(uint*)data; + _t += dt / 1000.f; + + ssr_matrixmode(MATRIX_VIEW); + ssr_loadidentity(); + 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); + + ssr_matrixmode(MATRIX_MODEL); + ssr_loadidentity(); + //ssr_translate(0, 0, -700); + //ssr_rotate(_t * 10, 0, 1, 0); + + Vec3 light = {1, 0, 0}; + ssr_setuniformvec3(0, &light); + + Mat4 world2object; + ssr_getm(&world2object); + //mat4_invertgeneral3d(&world2object, &world2object); + //mat4_transpose(&world2object, &world2object); // transpose it + + ssr_setuniformmat4(0, &world2object); +} + +void ondrawtexture(void*data) { + ssr_clearcolor(0xff202020); + ssr_cleardepth(); + 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 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.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/example/05_fog/05_fog.c b/src/example/05_fog/05_fog.c new file mode 100644 index 0000000..e69de29 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 -- cgit v1.1-26-g67d0