summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/maths/primitives.c64
-rw-r--r--src/maths/primitives.h2
-rw-r--r--src/renderer/render_types.h12
-rw-r--r--src/resources/gltf.c2
4 files changed, 72 insertions, 8 deletions
diff --git a/src/maths/primitives.c b/src/maths/primitives.c
index c0af140..3931b93 100644
--- a/src/maths/primitives.c
+++ b/src/maths/primitives.c
@@ -112,7 +112,7 @@ geometry_data geo_create_cuboid(f32x3 extents) {
.vertices = vertices,
.has_indices = true,
.indices = indices, // FIXME: make darray methods that return stack allocated struct
- .colour = vec3(0, 0, 0),
+ .colour = (rgba){ 0, 0, 0, 1 }
};
return geo;
@@ -133,6 +133,64 @@ geometry_data geo_create_cuboid(f32x3 extents) {
// --- Spheres
-geometry_data geo_create_uvsphere(f32 radius, f32 north_south_lines, f32 east_west_lines) {
- // TODO
+vec3 spherical_to_cartesian_coords(f32 rho, f32 theta, f32 phi) {
+ f32 x = rho * sin(phi) * cos(theta);
+ f32 y = rho * sin(phi) * sin(theta);
+ f32 z = rho * cos(phi);
+ return vec3(x, y, z);
+}
+
+geometry_data geo_create_uvsphere(f32 radius, u32 north_south_lines, u32 east_west_lines) {
+ assert(east_west_lines >= 3); // sphere will be degenerate and look gacked without at least 3
+ assert(north_south_lines >= 3);
+
+ vertex_darray* vertices = vertex_darray_new(2 + (east_west_lines - 1) * north_south_lines);
+
+ // Create a UV sphere with spherical coordinates
+ // a point P on the unit sphere can be represented P(r, theta, phi)
+ // for each vertex we must convert that to a cartesian R3 coordinate
+
+ // Top point
+ vertex top = { .static_3d = { .position = vec3(0, 0, radius),
+ .normal = vec3(0, 0, radius),
+ .tex_coords = vec2(0, 0) } };
+
+ // parallels
+ for (u32 i = 0; i < (east_west_lines - 1); i++) {
+ // phi should range from 0 to pi
+ f32 phi = PI * ((i + 1) / east_west_lines);
+
+ // meridians
+ for (u32 j = 0; j < east_west_lines; j++) {
+ // theta should range from 0 to 2PI
+ f32 theta = TAU * (j / north_south_lines);
+ vec3 position = spherical_to_cartesian_coords(radius, theta, phi);
+ f32 d = vec3_len(position);
+ assert(d == radius); // all points on the sphere should be 'radius' away from the origin
+ vertex v = { .static_3d = {
+ .position = position,
+ .normal = position, // normal vector on sphere is same as position
+ .tex_coords = vec2(0, 0) // TODO
+ } };
+ vertex_darray_push(vertices, v);
+ }
+ }
+
+ // Bottom point
+ vertex bot = { .static_3d = { .position = vec3(0, 0, -radius),
+ .normal = vec3(0, 0, -radius),
+ .tex_coords = vec2(0, 0) } };
+
+ // TODO: generate indices for for each flat quad on the UV sphere and triangles
+ // where they meet the north and south poles
+ u32_darray* indices = u32_darray_new(1);
+
+ geometry_data geo = {
+ .format = VERTEX_STATIC_3D,
+ .vertices = vertices,
+ .has_indices = true,
+ .indices = indices,
+ .colour = RED_800,
+ };
+ return geo;
}
diff --git a/src/maths/primitives.h b/src/maths/primitives.h
index 3e0cc5f..be2c6ff 100644
--- a/src/maths/primitives.h
+++ b/src/maths/primitives.h
@@ -9,5 +9,5 @@
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_uvsphere(f32 radius, u32 north_south_lines, u32 east_west_lines);
geometry_data geo_create_icosphere(f32 radius, f32 n_subdivisions); \ No newline at end of file
diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h
index b8ed512..1485ae4 100644
--- a/src/renderer/render_types.h
+++ b/src/renderer/render_types.h
@@ -13,6 +13,7 @@
#include "defines.h"
#include "ral.h"
#include "ral_types.h"
+#include "colours.h"
#if defined(CEL_PLATFORM_WINDOWS)
// #include "backend_dx11.h"
#endif
@@ -31,7 +32,7 @@ typedef struct geometry_data {
vertex_darray* vertices; // TODO: make it not a pointer
bool has_indices;
u32_darray* indices;
- vec3 colour; /** Optional: set vertex colours */
+ rgba colour; /** Optional: set vertex colours */
} geometry_data;
// 'Upload' a geometry_data (to GPU) -> get back a mesh
@@ -65,9 +66,10 @@ typedef struct texture_data {
typedef enum material_kind {
MAT_BLINN_PHONG,
MAT_PBR,
+ MAT_PBR_PARAMS, // uses float values to represent a surface uniformly
MAT_COUNT
} material_kind;
-static const char* material_kind_names[] = { "Blinn Phong", "PBR", "Count (This should be an error)"};
+static const char* material_kind_names[] = { "Blinn Phong", "PBR (Textures)", "PBR (Params)", "Count (This should be an error)"};
typedef struct blinn_phong_material {
char name[256];
@@ -85,7 +87,10 @@ typedef struct blinn_phong_material {
// typedef blinn_phong_material material;
typedef struct pbr_parameters {
-
+ vec3 albedo;
+ f32 metallic;
+ f32 roughness;
+ f32 ao;
} pbr_parameters;
typedef struct pbr_material {
@@ -101,6 +106,7 @@ typedef struct material {
material_kind kind;
union {
blinn_phong_material blinn_phong;
+ pbr_parameters pbr_params;
pbr_material pbr;
} mat_data;
char* name;
diff --git a/src/resources/gltf.c b/src/resources/gltf.c
index dcb3d96..46ad4f3 100644
--- a/src/resources/gltf.c
+++ b/src/resources/gltf.c
@@ -380,7 +380,7 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel
geometry_data *geometry = malloc(sizeof(geometry_data));
geometry->format = VERTEX_STATIC_3D;
- geometry->colour = vec3(1, 1, 1);
+ geometry->colour = (rgba){ 1, 1, 1, 1 };
geometry->vertices = geo_vertices;
geometry->indices = geo_indices;
geometry->has_indices = has_indices;