summaryrefslogtreecommitdiff
path: root/examples/teapot.c
diff options
context:
space:
mode:
authorkkard2 <[email protected]>2025-09-21 15:16:44 +0200
committerkkard2 <[email protected]>2025-09-21 15:16:58 +0200
commite972fcd0eb1e3b828334983240e1dc98a2a598a9 (patch)
tree71fa631022e4b9fd502653aec6e992c59e98e8b5 /examples/teapot.c
parent9463e07e2813cea94b9fd430b2fad938cad82673 (diff)
add teapot example
Diffstat (limited to 'examples/teapot.c')
-rw-r--r--examples/teapot.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/examples/teapot.c b/examples/teapot.c
new file mode 100644
index 0000000..889b6de
--- /dev/null
+++ b/examples/teapot.c
@@ -0,0 +1,73 @@
+#include <stdio.h>
+#include <assert.h>
+#include <stdlib.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"
+
+#define FOV (60.0f / PI * 2.0f)
+#define DISTANCE (10.0f)
+
+static int32_t mouse_x;
+static int32_t mouse_y;
+
+static sponge_Vec3 *positions;
+static sponge_Color32 *colors;
+static int32_t *triangles;
+static size_t triangles_count;
+
+// TODO(kard): make proper error checking
+void init() {
+ FILE *handle = fopen("./examples/teapot.txt", "r");
+ assert(handle);
+ fscanf(handle, "%zu", &triangles_count);
+ triangles_count *= 3;
+ positions = malloc(triangles_count * sizeof(sponge_Vec3));
+ colors = malloc(triangles_count * sizeof(sponge_Color32));
+ triangles = malloc(triangles_count * sizeof(int32_t));
+ assert(positions && colors && triangles);
+
+ for (size_t i = 0; i < triangles_count; i++) {
+ fscanf(handle, "%f %f %f", &positions[i].x, &positions[i].y, &positions[i].z);
+ sponge_Vec3 normal;
+ fscanf(handle, "%f %f %f", &normal.x, &normal.y, &normal.z);
+ sponge_ColorF color;
+ color.a = 255.0f;
+ color.r = (normal.x + 1.0f) * (255.0f * 0.5f);
+ color.g = (normal.y + 1.0f) * (255.0f * 0.5f);
+ color.b = (normal.z + 1.0f) * (255.0f * 0.5f);
+ colors[i] = sponge_colorf_to_color32(color);
+
+ // invert winding
+ int32_t t = (i / 3) * 3;
+ triangles[i] = t + (2 - (i % 3));
+ }
+
+ fclose(handle);
+}
+
+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));
+
+ sponge_Mat4 model = sponge_mat4_identity();
+ model = sponge_mat4_mul_mat4(model, sponge_mat4_translate(0.0f, -2.0f, 0.0f));
+
+ 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, -10.0f));
+
+
+ sponge_Mat4 proj = sponge_mat4_projection(FOV, (float)c.width / (float)c.height, 1.0f, 100.0f);
+ sponge_draw_mesh_col(c, depths, model, view, proj, positions, colors, triangles, triangles_count);
+}