Add support for randomising colours

This commit is contained in:
Abdelrahman Said 2024-07-31 22:11:11 +01:00
parent 39b40cdf5f
commit 5f44936dcd
3 changed files with 22 additions and 4 deletions

View File

@ -5,6 +5,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
enum {
TINY_EXIT_SUCCESS,
@ -20,6 +21,8 @@ int main(void) {
return TINY_EXIT_ARENA_INIT_FAILED;
}
srand(time(NULL));
Colour bg = {.r = 42, .g = 45, .b = 52, .a = 255};
Colour teal = {.r = 14, .g = 156, .b = 208, .a = 255};
Colour orange = {.r = 242, .g = 100, .b = 48, .a = 255};
@ -34,8 +37,8 @@ int main(void) {
}
clear_image(&img, bg);
render_model(&model, &img, teal, RENDER_TYPE_FILLED);
render_model(&model, &img, orange, RENDER_TYPE_WIREFRAME);
render_model(&model, &img, teal, RENDER_TYPE_FILLED, COLOUR_TYPE_RANDOM);
render_model(&model, &img, orange, RENDER_TYPE_WIREFRAME, COLOUR_TYPE_FIXED);
save_image(&img, "result.pam");
wapp_mem_arena_destroy(&arena);

View File

@ -4,7 +4,9 @@
#include "mem_arena.h"
#include "typed_list.h"
#include "utils.h"
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#define TRIANGLE_VERTICES 3
#define dot_v2(V1, V2) ((f32)V1.x * (f32)V2.x + (f32)V1.y * (f32)V2.y)
@ -111,11 +113,17 @@ Model load_obj_file(Arena *arena, const char *filename) {
}
void render_model(const Model *model, Image *img, Colour colour,
RenderType type) {
RenderType type, ColourType colour_type) {
Triangle triangle;
for (u64 i = 0; i < model->triangles->count; ++i) {
triangle = list_get(model->triangles, i);
if (colour_type == COLOUR_TYPE_RANDOM) {
colour = (Colour){.r = rand() % UINT8_MAX,
.g = rand() % UINT8_MAX,
.b = rand() % UINT8_MAX,
.a = 255};
}
render_triangle(&triangle, model, img, colour, type);
}
}

View File

@ -30,6 +30,13 @@ typedef enum {
COUNT_RENDER_TYPES,
} RenderType;
typedef enum {
COLOUR_TYPE_FIXED,
COLOUR_TYPE_RANDOM,
COUNT_COLOUR_TYPE,
} ColourType;
MAKE_LIST_TYPE(Vertex);
MAKE_LIST_TYPE(Triangle);
@ -41,6 +48,6 @@ struct model {
Model load_obj_file(Arena *arena, const char *filename);
void render_model(const Model *model, Image *img, Colour colour,
RenderType type);
RenderType type, ColourType colour_type);
#endif // OBJ_H