Compare commits
	
		
			2 Commits
		
	
	
		
			b6e7b9c213
			...
			34d0d17b76
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 34d0d17b76 | |||
| fee04607a7 | 
							
								
								
									
										37
									
								
								src/obj.c
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								src/obj.c
									
									
									
									
									
								
							| @@ -12,6 +12,7 @@ | |||||||
| #include <string.h> | #include <string.h> | ||||||
|  |  | ||||||
| #define TRIANGLE_VERTICES 3 | #define TRIANGLE_VERTICES 3 | ||||||
|  | #define CAMERA_DISTANCE 5.0f | ||||||
|  |  | ||||||
| #define V2(T, ELEM_T, X0, Y0, X1, Y1)                                          \ | #define V2(T, ELEM_T, X0, Y0, X1, Y1)                                          \ | ||||||
|   ((T){(ELEM_T)X1 - (ELEM_T)X0, (ELEM_T)Y1 - (ELEM_T)Y0}) |   ((T){(ELEM_T)X1 - (ELEM_T)X0, (ELEM_T)Y1 - (ELEM_T)Y0}) | ||||||
| @@ -62,6 +63,14 @@ struct f32x3 { | |||||||
|   f32 z; |   f32 z; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | typedef struct f32x4 V4f; | ||||||
|  | struct f32x4 { | ||||||
|  |   f32 x; | ||||||
|  |   f32 y; | ||||||
|  |   f32 z; | ||||||
|  |   f32 w; | ||||||
|  | }; | ||||||
|  |  | ||||||
| typedef struct u64x3 V3u; | typedef struct u64x3 V3u; | ||||||
| struct u64x3 { | struct u64x3 { | ||||||
|   u64 x; |   u64 x; | ||||||
| @@ -76,8 +85,17 @@ struct f32_3x3 { | |||||||
|   V3f row2; |   V3f row2; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | typedef struct f32_4x4 M4x4f; | ||||||
|  | struct f32_4x4 { | ||||||
|  |   V4f row0; | ||||||
|  |   V4f row1; | ||||||
|  |   V4f row2; | ||||||
|  |   V4f row3; | ||||||
|  | }; | ||||||
|  |  | ||||||
| internal void render_triangle(const Triangle *triangle, const Model *model, | internal void render_triangle(const Triangle *triangle, const Model *model, | ||||||
|                               Render *render, Colour colour, RenderType type); |                               Render *render, Colour colour, RenderType type, | ||||||
|  |                               ProjectionType projection); | ||||||
| internal void fill_triangle(Render *render, Vertex vertices[TRIANGLE_VERTICES], | internal void fill_triangle(Render *render, Vertex vertices[TRIANGLE_VERTICES], | ||||||
|                             TexCoord coordinates[TRIANGLE_VERTICES], |                             TexCoord coordinates[TRIANGLE_VERTICES], | ||||||
|                             Colour colour, f32 intensity, Image *texture); |                             Colour colour, f32 intensity, Image *texture); | ||||||
| @@ -94,6 +112,15 @@ internal u64 ndc_to_image_coordinate(f32 value, u64 max); | |||||||
|  |  | ||||||
| V3f g_light_dir = {0.0f, 0.0f, -1.0f}; | V3f g_light_dir = {0.0f, 0.0f, -1.0f}; | ||||||
|  |  | ||||||
|  | // clang-format off | ||||||
|  | M4x4f g_cam_matrix = { | ||||||
|  |   .row0 = {1.0f, 0.0f,                    0.0f, 0.0f}, | ||||||
|  |   .row1 = {0.0f, 1.0f,                    0.0f, 0.0f}, | ||||||
|  |   .row2 = {0.0f, 0.0f,                    1.0f, 0.0f}, | ||||||
|  |   .row3 = {0.0f, 0.0f, -1.0f / CAMERA_DISTANCE, 1.0f}, | ||||||
|  | }; | ||||||
|  | // clang-format on | ||||||
|  |  | ||||||
| Model load_obj_file(Arena *arena, const char *filename, const char *texture) { | Model load_obj_file(Arena *arena, const char *filename, const char *texture) { | ||||||
|   if (!arena) { |   if (!arena) { | ||||||
|     return NULL_MODEL; |     return NULL_MODEL; | ||||||
| @@ -177,7 +204,8 @@ bool init_render(Arena *arena, Render *render, u64 width, u64 height) { | |||||||
| } | } | ||||||
|  |  | ||||||
| void render_model(const Model *model, Render *render, Colour colour, | void render_model(const Model *model, Render *render, Colour colour, | ||||||
|                   RenderType type, ColourType colour_type) { |                   RenderType type, ColourType colour_type, | ||||||
|  |                   ProjectionType projection) { | ||||||
|   Triangle triangle; |   Triangle triangle; | ||||||
|  |  | ||||||
|   for (u64 i = 0; i < model->triangles->count; ++i) { |   for (u64 i = 0; i < model->triangles->count; ++i) { | ||||||
| @@ -188,12 +216,13 @@ void render_model(const Model *model, Render *render, Colour colour, | |||||||
|                         .b = rand() % UINT8_MAX, |                         .b = rand() % UINT8_MAX, | ||||||
|                         .a = 255}; |                         .a = 255}; | ||||||
|     } |     } | ||||||
|     render_triangle(&triangle, model, render, colour, type); |     render_triangle(&triangle, model, render, colour, type, projection); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| internal void render_triangle(const Triangle *triangle, const Model *model, | internal void render_triangle(const Triangle *triangle, const Model *model, | ||||||
|                               Render *render, Colour colour, RenderType type) { |                               Render *render, Colour colour, RenderType type, | ||||||
|  |                               ProjectionType projection) { | ||||||
|   Image *img = &(render->img); |   Image *img = &(render->img); | ||||||
|   Vertex vertices[TRIANGLE_VERTICES] = { |   Vertex vertices[TRIANGLE_VERTICES] = { | ||||||
|       list_get(model->vertices, triangle->p0), |       list_get(model->vertices, triangle->p0), | ||||||
|   | |||||||
							
								
								
									
										10
									
								
								src/obj.h
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								src/obj.h
									
									
									
									
									
								
							| @@ -47,6 +47,13 @@ typedef enum { | |||||||
|   COUNT_COLOUR_TYPE, |   COUNT_COLOUR_TYPE, | ||||||
| } ColourType; | } ColourType; | ||||||
|  |  | ||||||
|  | typedef enum { | ||||||
|  |   PROJECTION_TYPE_ORTHOGRAPHIC, | ||||||
|  |   PROJECTION_TYPE_PERSPECTIVE, | ||||||
|  |  | ||||||
|  |   COUNT_PROJECTION_TYPE, | ||||||
|  | } ProjectionType; | ||||||
|  |  | ||||||
| MAKE_LIST_TYPE(Vertex); | MAKE_LIST_TYPE(Vertex); | ||||||
| MAKE_LIST_TYPE(TexCoord); | MAKE_LIST_TYPE(TexCoord); | ||||||
| MAKE_LIST_TYPE(Triangle); | MAKE_LIST_TYPE(Triangle); | ||||||
| @@ -68,6 +75,7 @@ struct render { | |||||||
| Model load_obj_file(Arena *arena, const char *filename, const char *texture); | Model load_obj_file(Arena *arena, const char *filename, const char *texture); | ||||||
| bool init_render(Arena *arena, Render *render, u64 width, u64 height); | bool init_render(Arena *arena, Render *render, u64 width, u64 height); | ||||||
| void render_model(const Model *model, Render *render, Colour colour, | void render_model(const Model *model, Render *render, Colour colour, | ||||||
|                   RenderType type, ColourType colour_type); |                   RenderType type, ColourType colour_type, | ||||||
|  |                   ProjectionType projection); | ||||||
|  |  | ||||||
| #endif // OBJ_H | #endif // OBJ_H | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user