summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/cube.c136
-rw-r--r--examples/kothd.pngbin0 -> 108696 bytes
-rw-r--r--sponge.h35
3 files changed, 171 insertions, 0 deletions
diff --git a/examples/cube.c b/examples/cube.c
new file mode 100644
index 0000000..240c387
--- /dev/null
+++ b/examples/cube.c
@@ -0,0 +1,136 @@
+#include <assert.h>
+
+#include "../sponge.h"
+#define SPONGE_EXAMPLE_IMPLEMENTATION
+#define SPONGE_EXAMPLE_INIT_DEFINED
+#define SPONGE_EXAMPLE_MOUSE_MOVE_DEFINED
+#define SPONGE_EXAMPLE_DRAW_FRAME_3D_DEFINED
+#include "../example.h"
+
+#include "../stb_image.h"
+
+#define FOV (60.0f / PI * 2.0f)
+#define DISTANCE (10.0f)
+
+static sponge_Texture texture;
+static int32_t mouse_x;
+static int32_t mouse_y;
+
+void init() {
+ unsigned char *data = stbi_load("examples/kothd.png", &texture.width, &texture.height, NULL, 4);
+ if (!data) {
+ printf("%s\n", stbi_failure_reason());
+ assert(0);
+ }
+
+ texture.stride_pixels = texture.width;
+ texture.pixels = (sponge_Color32 *)data; // NOTE(kard): be careful if changing following loop
+
+ // converting from stbi's RGBA
+ for (size_t i = 0; i < texture.width * texture.height; i++) {
+ sponge_Color32 result;
+ result.r = data[i * 4 + 0];
+ result.g = data[i * 4 + 1];
+ result.b = data[i * 4 + 2];
+ result.a = data[i * 4 + 3];
+ texture.pixels[i] = result;
+ }
+}
+
+void mouse_move(int32_t x, int32_t y) {
+ mouse_x = x;
+ mouse_y = y;
+}
+
+void draw_frame_3d(sponge_Texture c, float *depths) {
+ sponge_clear_3d(c, depths, sponge_color32_make(0xFF000000));
+
+ // NOTE(kard): these directions are not intuitive at all, but it works
+ sponge_Vec3 positions[] = {
+ // front
+ sponge_vec3_make( 0.5f, 0.5f, -0.5f),
+ sponge_vec3_make(-0.5f, 0.5f, -0.5f),
+ sponge_vec3_make( 0.5f, -0.5f, -0.5f),
+ sponge_vec3_make(-0.5f, -0.5f, -0.5f),
+ // right
+ sponge_vec3_make(-0.5f, 0.5f, -0.5f),
+ sponge_vec3_make(-0.5f, 0.5f, 0.5f),
+ sponge_vec3_make(-0.5f, -0.5f, -0.5f),
+ sponge_vec3_make(-0.5f, -0.5f, 0.5f),
+ // left
+ sponge_vec3_make( 0.5f, 0.5f, 0.5f),
+ sponge_vec3_make( 0.5f, 0.5f, -0.5f),
+ sponge_vec3_make( 0.5f, -0.5f, 0.5f),
+ sponge_vec3_make( 0.5f, -0.5f, -0.5f),
+ // top
+ sponge_vec3_make( 0.5f, 0.5f, 0.5f),
+ sponge_vec3_make(-0.5f, 0.5f, 0.5f),
+ sponge_vec3_make( 0.5f, 0.5f, -0.5f),
+ sponge_vec3_make(-0.5f, 0.5f, -0.5f),
+ // bottom
+ sponge_vec3_make(-0.5f, -0.5f, 0.5f),
+ sponge_vec3_make( 0.5f, -0.5f, 0.5f),
+ sponge_vec3_make(-0.5f, -0.5f, -0.5f),
+ sponge_vec3_make( 0.5f, -0.5f, -0.5f),
+ // there is no back
+ };
+ // NOTE(kard): do not ask any questions
+ sponge_Vec2 uvs[] = {
+ // front
+ sponge_vec2_make(0.33f, 0.33f),
+ sponge_vec2_make(0.66f, 0.33f),
+ sponge_vec2_make(0.33f, 0.66f),
+ sponge_vec2_make(0.66f, 0.66f),
+ // right
+ sponge_vec2_make(0.66f, 0.33f),
+ sponge_vec2_make(1.00f, 0.00f),
+ sponge_vec2_make(0.66f, 0.66f),
+ sponge_vec2_make(1.00f, 1.00f),
+ // left
+ sponge_vec2_make(0.00f, 0.00f),
+ sponge_vec2_make(0.33f, 0.33f),
+ sponge_vec2_make(0.00f, 1.00f),
+ sponge_vec2_make(0.33f, 0.66f),
+ // top
+ sponge_vec2_make(0.00f, 0.00f),
+ sponge_vec2_make(1.00f, 0.00f),
+ sponge_vec2_make(0.33f, 0.33f),
+ sponge_vec2_make(0.66f, 0.33f),
+ // bottom
+ sponge_vec2_make(0.00f, 1.00f),
+ sponge_vec2_make(1.00f, 1.00f),
+ sponge_vec2_make(0.33f, 0.66f),
+ sponge_vec2_make(0.66f, 0.66f),
+ };
+
+ int32_t triangles[] = {
+ // front
+ 0, 1, 2,
+ 2, 1, 3,
+ // right
+ 4, 5, 6,
+ 6, 5, 7,
+ // left
+ 8, 9, 10,
+ 10, 9, 11,
+ // top
+ 12, 13, 14,
+ 14, 13, 15,
+ // bottom
+ 16, 17, 18,
+ 18, 17, 19,
+ };
+
+
+ sponge_Mat4 model = sponge_mat4_identity();
+
+ float rot_y = (float)mouse_x / (float)c.width * PI * 2.0f;
+ float rot_x = ((float)mouse_y / (float)c.height * PI) - (PI * 0.5f);
+ sponge_Mat4 view = sponge_mat4_identity();
+ view = sponge_mat4_mul_mat4(view, sponge_mat4_rotate(rot_x, rot_y, 0.0f));
+ view = sponge_mat4_mul_mat4(view, sponge_mat4_translate(0.0f, 0.0f, -5.0f));
+
+ sponge_Mat4 proj = sponge_mat4_projection(FOV, (float)c.width / (float)c.height, 1.0f, 100.0f);
+
+ sponge_draw_mesh_uv(c, depths, model, view, proj, positions, uvs, triangles, 30, texture);
+}
diff --git a/examples/kothd.png b/examples/kothd.png
new file mode 100644
index 0000000..698ca46
--- /dev/null
+++ b/examples/kothd.png
Binary files differ
diff --git a/sponge.h b/sponge.h
index 0db45c4..f5dcacb 100644
--- a/sponge.h
+++ b/sponge.h
@@ -151,6 +151,11 @@ void sponge_draw_mesh_col(
sponge_Texture c, float *depths,
sponge_Mat4 model, sponge_Mat4 view, sponge_Mat4 proj,
sponge_Vec3 *positions, sponge_Color32 *colors, int32_t *triangles, size_t triangles_count);
+void sponge_draw_mesh_uv(
+ sponge_Texture c, float *depths,
+ sponge_Mat4 model, sponge_Mat4 view, sponge_Mat4 proj,
+ sponge_Vec3 *positions, sponge_Vec2 *uvs, int32_t *triangles, size_t triangles_count,
+ sponge_Texture tex);
#define SPONGE_CLAMP(val, min, max) ((val) < (min) ? (min) : ((val) > (max) ? (max) : (val)))
@@ -592,4 +597,34 @@ void sponge_draw_mesh_col(
}
}
+void sponge_draw_mesh_uv(
+ sponge_Texture c, float *depths,
+ sponge_Mat4 model, sponge_Mat4 view, sponge_Mat4 proj,
+ sponge_Vec3 *positions, sponge_Vec2 *uvs, int32_t *triangles, size_t triangles_count,
+ sponge_Texture tex
+) {
+ sponge_Mat4 mvp = sponge_mat4_mul_mat4(sponge_mat4_mul_mat4(model, view), proj);
+ for (size_t i = 0; i < triangles_count; i += 3) {
+ sponge_DrawMeshTriangleContext ctx = sponge_draw_mesh_triangle_init(c, mvp, positions, triangles, i);
+ sponge_Color32 *row = c.pixels + (ctx.min_pixel.y * c.stride_pixels);
+ float *depth_row = depths + (ctx.min_pixel.y * c.stride_pixels);
+
+ for (int32_t y = ctx.min_pixel.y; y <= ctx.max_pixel.y; y++, row += c.stride_pixels, depth_row += c.stride_pixels) {
+ for (int32_t x = ctx.min_pixel.x; x <= ctx.max_pixel.x; x++) {
+ float w0, w1, w2;
+ if (sponge_draw_mesh_triangle_iter(ctx, sponge_vec2i_make(x, y), &depth_row[x], &w0, &w1, &w2)) {
+ sponge_Vec2 uv0 = uvs[ctx.t0];
+ sponge_Vec2 uv1 = uvs[ctx.t1];
+ sponge_Vec2 uv2 = uvs[ctx.t2];
+ sponge_Vec2 result = sponge_vec2_make(0.0f, 0.0f);
+ result = sponge_vec2_add(result, sponge_vec2_mul(uv0, w0));
+ result = sponge_vec2_add(result, sponge_vec2_mul(uv1, w1));
+ result = sponge_vec2_add(result, sponge_vec2_mul(uv2, w2));
+ row[x] = sponge_sample_texture(result, tex);
+ }
+ }
+ }
+ }
+}
+
#endif // SPONGE_IMPLEMENTATION