summaryrefslogtreecommitdiff
path: root/src/example/04_bloom
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-12-21 22:24:15 +0800
committerchai <chaifix@163.com>2019-12-21 22:24:15 +0800
commitec111247c614663d8231245a17c314b9b8b4a28c (patch)
treea66058508161da488371c90316865ae850b8be15 /src/example/04_bloom
parentc3f45735ecfab6e567be371758f21395e92dfef6 (diff)
*misc
Diffstat (limited to 'src/example/04_bloom')
-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
3 files changed, 192 insertions, 0 deletions
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
+};