summaryrefslogtreecommitdiff
path: root/src/maths
diff options
context:
space:
mode:
authoromnisci3nce <omniscient.oce@gmail.com>2024-04-27 15:37:52 +1000
committeromnisci3nce <omniscient.oce@gmail.com>2024-04-27 15:37:52 +1000
commit4e6897aa8c8769a556ad58081eba5a90226e551d (patch)
tree8b87c4e75d07248360636ab6df3562f8871fb569 /src/maths
parentf5c7f55fcfb4df4b9aa67b11a847340f0d8c95c4 (diff)
wip
Diffstat (limited to 'src/maths')
-rw-r--r--src/maths/maths.h25
-rw-r--r--src/maths/maths_types.h33
-rw-r--r--src/maths/primitives.c187
-rw-r--r--src/maths/primitives.h162
4 files changed, 226 insertions, 181 deletions
diff --git a/src/maths/maths.h b/src/maths/maths.h
index e0d39d7..ad33981 100644
--- a/src/maths/maths.h
+++ b/src/maths/maths.h
@@ -319,27 +319,4 @@ static inline mat4 transform_to_mat(transform *tf) {
_Static_assert(alignof(vec3) == 4, "vec3 is 4 byte aligned");
_Static_assert(sizeof(vec3) == 12, "vec3 is 12 bytes so has no padding");
-_Static_assert(alignof(vec4) == 4, "vec4 is 4 byte aligned");
-
-// --- Some other types
-typedef struct u32x3 {
- union {
- struct {
- u32 x;
- u32 y;
- u32 z;
- };
- struct {
- u32 r;
- u32 g;
- u32 b;
- };
- };
-} u32x3;
-#define u32x3(x, y, z) ((u32x3){ x, y, z })
-
-typedef struct u32x2 {
- u32 x;
- u32 y;
-} u32x2;
-#define u32x2(x, y) ((u32x3){ x, y }) \ No newline at end of file
+_Static_assert(alignof(vec4) == 4, "vec4 is 4 byte aligned"); \ No newline at end of file
diff --git a/src/maths/maths_types.h b/src/maths/maths_types.h
index 53cac55..aa86eb0 100644
--- a/src/maths/maths_types.h
+++ b/src/maths/maths_types.h
@@ -68,4 +68,35 @@ typedef struct vec4i {
typedef struct vec4u {
u32 x, y, z, w;
-} vec4u; \ No newline at end of file
+} vec4u;
+
+// --- Some other types
+typedef struct u32x3 {
+ union {
+ struct {
+ u32 x;
+ u32 y;
+ u32 z;
+ };
+ struct {
+ u32 r;
+ u32 g;
+ u32 b;
+ };
+ };
+} u32x3;
+#define u32x3(x, y, z) ((u32x3){ x, y, z })
+
+typedef struct u32x2 {
+ u32 x;
+ u32 y;
+} u32x2;
+#define u32x2(x, y) ((u32x3){ x, y })
+
+// Type aliass
+
+typedef struct vec2 f32x2;
+#define f32x2(x, y) ((f32x2){ x, y })
+
+typedef struct vec3 f32x3;
+#define f32x3(x, y, z) ((f32x3){ x, y, z }) \ No newline at end of file
diff --git a/src/maths/primitives.c b/src/maths/primitives.c
new file mode 100644
index 0000000..42c51ea
--- /dev/null
+++ b/src/maths/primitives.c
@@ -0,0 +1,187 @@
+#include "primitives.h"
+
+// vertices
+f32 plane_vertex_positions[] = {
+ // triangle 1
+ -0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0, -0.5,
+ // triangle 2
+ 0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0, 0.5
+};
+
+geometry_data geo_create_plane(f32x2 extents) {
+ f32x2 half_extents = vec2_div(extents, 2.0);
+ vertex_format format = VERTEX_STATIC_3D;
+ vertex_darray* vertices = vertex_darray_new(4);
+
+ vertex_darray_push(
+ vertices,
+ (vertex) {.static_3d = {
+ .position =
+ }}
+ );
+
+ return (geometry_data) {
+ .format = format,
+ .vertices =
+ .has_indices = true,
+ }
+}
+
+
+// OLD
+
+static const vec3 BACK_BOT_LEFT = (vec3){ 0, 0, 0 };
+static const vec3 BACK_BOT_RIGHT = (vec3){ 1, 0, 0 };
+static const vec3 BACK_TOP_LEFT = (vec3){ 0, 1, 0 };
+static const vec3 BACK_TOP_RIGHT = (vec3){ 1, 1, 0 };
+static const vec3 FRONT_BOT_LEFT = (vec3){ 0, 0, 1 };
+static const vec3 FRONT_BOT_RIGHT = (vec3){ 1, 0, 1 };
+static const vec3 FRONT_TOP_LEFT = (vec3){ 0, 1, 1 };
+static const vec3 FRONT_TOP_RIGHT = (vec3){ 1, 1, 1 };
+
+static mesh prim_cube_mesh_create() {
+ mesh cube = { 0 };
+ cube.vertices = vertex_darray_new(36);
+
+ // back faces
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0, 1 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0, 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_NEG_Z, .uv = (vec2){ 1, 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_NEG_Z, .uv = (vec2){ 1, 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_NEG_Z, .uv = (vec2){ 1, 1 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0, 1 } });
+
+ // front faces
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_Z, .uv = (vec2){ 0, 1 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Z, .uv = (vec2){ 1, 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_Z, .uv = (vec2){ 0, 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_Z, .uv = (vec2){ 0, 1 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_Z, .uv = (vec2){ 1, 1 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Z, .uv = (vec2){ 1, 0 } });
+
+ // top faces
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_Y, .uv = (vec2){ 0, 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_Y, .uv = (vec2){ 0, 1 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Y, .uv = (vec2){ 1, 1 } });
+ vertex_darray_push(cube.vertices,
+ (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_Y, .uv = (vec2){ 0, 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Y, .uv = (vec2){ 1, 1 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_Y, .uv = (vec2){ 1, 0 } });
+
+ // bottom faces
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
+
+ // right faces
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0, 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_X, .uv = (vec2){ 1, 1 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_X, .uv = (vec2){ 1, 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_X, .uv = (vec2){ 1, 1 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0, 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0, 1 } });
+
+ // left faces
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
+ vertex_darray_push(
+ cube.vertices,
+ (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
+
+ cube.indices_len = cube.vertices->len;
+ cube.indices = malloc(sizeof(u32) * cube.indices_len);
+
+ for (u32 i = 0; i < cube.indices_len; i++) {
+ cube.indices[i] = i;
+ }
+
+ cube.has_indices = true;
+
+ return cube;
+}
+
+/** @brief create a new model with the shape of a cube */
+static model_handle prim_cube_new(core* core) {
+ model model = { 0 };
+ mesh cube = prim_cube_mesh_create();
+
+ mesh_darray_push(model.meshes, cube);
+ assert(mesh_darray_len(model.meshes) == 1);
+
+ u32 index = (u32)model_darray_len(core->models);
+ model_darray_push_copy(core->models, &model);
+ return (model_handle){ .raw = index };
+} \ No newline at end of file
diff --git a/src/maths/primitives.h b/src/maths/primitives.h
index ed52c8c..3e0cc5f 100644
--- a/src/maths/primitives.h
+++ b/src/maths/primitives.h
@@ -3,161 +3,11 @@
#include <assert.h>
#include <stdlib.h>
#include "core.h"
-#include "maths.h"
+#include "maths_types.h"
#include "render_types.h"
-static const vec3 BACK_BOT_LEFT = (vec3){ 0, 0, 0 };
-static const vec3 BACK_BOT_RIGHT = (vec3){ 1, 0, 0 };
-static const vec3 BACK_TOP_LEFT = (vec3){ 0, 1, 0 };
-static const vec3 BACK_TOP_RIGHT = (vec3){ 1, 1, 0 };
-static const vec3 FRONT_BOT_LEFT = (vec3){ 0, 0, 1 };
-static const vec3 FRONT_BOT_RIGHT = (vec3){ 1, 0, 1 };
-static const vec3 FRONT_TOP_LEFT = (vec3){ 0, 1, 1 };
-static const vec3 FRONT_TOP_RIGHT = (vec3){ 1, 1, 1 };
-
-static mesh prim_cube_mesh_create() {
- mesh cube = { 0 };
- cube.vertices = vertex_darray_new(36);
-
- // back faces
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0, 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_NEG_Z, .uv = (vec2){ 1, 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_NEG_Z, .uv = (vec2){ 1, 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_NEG_Z, .uv = (vec2){ 1, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Z, .uv = (vec2){ 0, 1 } });
-
- // front faces
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_Z, .uv = (vec2){ 0, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Z, .uv = (vec2){ 1, 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_Z, .uv = (vec2){ 0, 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_Z, .uv = (vec2){ 0, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_Z, .uv = (vec2){ 1, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Z, .uv = (vec2){ 1, 0 } });
-
- // top faces
- vertex_darray_push(cube.vertices,
- (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_Y, .uv = (vec2){ 0, 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_Y, .uv = (vec2){ 0, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Y, .uv = (vec2){ 1, 1 } });
- vertex_darray_push(cube.vertices,
- (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_Y, .uv = (vec2){ 0, 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_Y, .uv = (vec2){ 1, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_Y, .uv = (vec2){ 1, 0 } });
-
- // bottom faces
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_NEG_Y, .uv = (vec2){ 0 } });
-
- // right faces
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0, 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_X, .uv = (vec2){ 1, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_TOP_RIGHT, .normal = VEC3_X, .uv = (vec2){ 1, 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_RIGHT, .normal = VEC3_X, .uv = (vec2){ 1, 1 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0, 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_RIGHT, .normal = VEC3_X, .uv = (vec2){ 0, 1 } });
-
- // left faces
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_TOP_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = BACK_BOT_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_BOT_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
- vertex_darray_push(
- cube.vertices,
- (vertex){ .position = FRONT_TOP_LEFT, .normal = VEC3_NEG_X, .uv = (vec2){ 0 } });
-
- cube.indices_len = cube.vertices->len;
- cube.indices = malloc(sizeof(u32) * cube.indices_len);
-
- for (u32 i = 0; i < cube.indices_len; i++) {
- cube.indices[i] = i;
- }
-
- cube.has_indices = true;
-
- return cube;
-}
-
-/** @brief create a new model with the shape of a cube */
-static model_handle prim_cube_new(core* core) {
- model model = { 0 };
- mesh cube = prim_cube_mesh_create();
-
- mesh_darray_push(model.meshes, cube);
- assert(mesh_darray_len(model.meshes) == 1);
-
- u32 index = (u32)model_darray_len(core->models);
- model_darray_push_copy(core->models, &model);
- return (model_handle){ .raw = index };
-} \ No newline at end of file
+geometry_data geo_create_plane(f32x2 extents);
+geometry_data geo_create_cuboid(f32x3 extents);
+geometry_data geo_create_cylinder(f32 radius, f32 height, u32 resolution);
+geometry_data geo_create_uvsphere(f32 radius, f32 north_south_lines, f32 east_west_lines);
+geometry_data geo_create_icosphere(f32 radius, f32 n_subdivisions); \ No newline at end of file