summaryrefslogtreecommitdiff
path: root/src/example/04_bloom/04_bloom.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/example/04_bloom/04_bloom.c')
-rw-r--r--src/example/04_bloom/04_bloom.c120
1 files changed, 120 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();
+}