@@ -43,6 +43,9 @@ static int indices[] = {
4343static unsigned long vertices_size = sizeof (vertices ) / sizeof (vertices [0 ]);
4444static unsigned long indices_size = sizeof (indices ) / sizeof (indices [0 ]);
4545
46+ /* Default clear screen color */
47+ static csr_color clear_color = {40 , 40 , 40 };
48+
4649/*
4750 * Saves a framebuffer to a PPM image file.
4851 *
@@ -84,10 +87,9 @@ static void csr_test_stack_alloc(void)
8487 unsigned char memory_total [MEMORY_SIZE ] = {0 };
8588 void * memory = (void * )memory_total ;
8689
87- csr_color clear_color = {40 , 40 , 40 };
88- csr_context instance = {0 };
90+ csr_context context = {0 };
8991
90- if (!csr_init_model (& instance , memory , MEMORY_SIZE , WIDTH , HEIGHT , clear_color ))
92+ if (!csr_init_model (& context , memory , MEMORY_SIZE , WIDTH , HEIGHT ))
9193 {
9294 return ;
9395 }
@@ -99,7 +101,7 @@ static void csr_test_stack_alloc(void)
99101 v3 cam_position = vm_v3 (0.0f , 0.0f , 2.0f );
100102 float cam_fov = 90.0f ;
101103
102- m4x4 projection = vm_m4x4_perspective (vm_radf (cam_fov ), (float )instance .width / (float )instance .height , 0.1f , 1000.0f );
104+ m4x4 projection = vm_m4x4_perspective (vm_radf (cam_fov ), (float )context .width / (float )context .height , 0.1f , 1000.0f );
103105 m4x4 view = vm_m4x4_lookAt (cam_position , look_at_pos , up );
104106 m4x4 projection_view = vm_m4x4_mul (projection , view );
105107
@@ -112,11 +114,11 @@ static void csr_test_stack_alloc(void)
112114 {
113115 m4x4 model_view_projection = vm_m4x4_mul (projection_view , vm_m4x4_rotate (model_base , vm_radf (5.0f * (float )(frame + 1 )), rotation_axis ));
114116
115- PERF_PROFILE_WITH_NAME ({ csr_render_clear_screen (& instance ); }, "csr_clear_screen" );
116- PERF_PROFILE_WITH_NAME ({ csr_render (& instance , CSR_RENDER_SOLID , CSR_CULLING_CCW_BACKFACE , 6 , vertices , vertices_size , indices , indices_size , model_view_projection .e ); }, "csr_render_frame" );
117+ PERF_PROFILE_WITH_NAME ({ csr_render_clear_screen (& context , clear_color ); }, "csr_clear_screen" );
118+ PERF_PROFILE_WITH_NAME ({ csr_render (& context , CSR_RENDER_SOLID , CSR_CULLING_CCW_BACKFACE , 6 , vertices , vertices_size , indices , indices_size , model_view_projection .e ); }, "csr_render_frame" );
117119
118120 /* Save the result to a PPM file */
119- csr_save_ppm ("stack_%05d.ppm" , frame , & instance );
121+ csr_save_ppm ("stack_%05d.ppm" , frame , & context );
120122 }
121123 }
122124}
@@ -129,12 +131,11 @@ static void csr_test_cube_scene_with_memory_alloc(void)
129131 unsigned long memory_size = csr_memory_size (width , height );
130132 void * memory = malloc (memory_size );
131133
132- csr_color clear_color = {40 , 40 , 40 };
133- csr_context instance = {0 };
134+ csr_context context = {0 };
134135
135136 printf ("[csr] memory (MB): %10.4f\n" , (double )memory_size / 1024.0 / 1024.0 );
136137
137- if (!csr_init_model (& instance , memory , memory_size , width , height , clear_color ))
138+ if (!csr_init_model (& context , memory , memory_size , width , height ))
138139 {
139140 return ;
140141 }
@@ -146,7 +147,7 @@ static void csr_test_cube_scene_with_memory_alloc(void)
146147 v3 cam_position = vm_v3 (0.0f , 0.0f , 2.0f );
147148 float cam_fov = 90.0f ;
148149
149- m4x4 projection = vm_m4x4_perspective (vm_radf (cam_fov ), (float )instance .width / (float )instance .height , 0.1f , 1000.0f );
150+ m4x4 projection = vm_m4x4_perspective (vm_radf (cam_fov ), (float )context .width / (float )context .height , 0.1f , 1000.0f );
150151 m4x4 view = vm_m4x4_lookAt (cam_position , look_at_pos , up );
151152 m4x4 projection_view = vm_m4x4_mul (projection , view );
152153
@@ -160,23 +161,23 @@ static void csr_test_cube_scene_with_memory_alloc(void)
160161 m4x4 model = vm_m4x4_rotate (model_base , vm_radf (5.0f * (float )(frame + 1 )), rotation_axis );
161162 m4x4 model_view_projection = vm_m4x4_mul (projection_view , model );
162163
163- csr_render_clear_screen (& instance );
164+ csr_render_clear_screen (& context , clear_color );
164165
165166 /* Render first cube */
166- csr_render (& instance , CSR_RENDER_WIREFRAME , CSR_CULLING_DISABLED , 6 , vertices , vertices_size , indices , indices_size , model_view_projection .e );
167+ csr_render (& context , CSR_RENDER_WIREFRAME , CSR_CULLING_DISABLED , 6 , vertices , vertices_size , indices , indices_size , model_view_projection .e );
167168
168169 /* Render second cube */
169170 model = vm_m4x4_translate (vm_m4x4_identity , vm_v3 (-2.0 , 0.0f , -2.0f ));
170171 model_view_projection = vm_m4x4_rotate (vm_m4x4_mul (projection_view , model ), vm_radf (-2.5f * (float )(frame + 1 )), vm_v3 (1.0f , 1.0f , 1.0f ));
171- csr_render (& instance , CSR_RENDER_SOLID , CSR_CULLING_CCW_BACKFACE , 6 , vertices , vertices_size , indices , indices_size , model_view_projection .e );
172+ csr_render (& context , CSR_RENDER_SOLID , CSR_CULLING_CCW_BACKFACE , 6 , vertices , vertices_size , indices , indices_size , model_view_projection .e );
172173
173174 /* Render third cube */
174175 model = vm_m4x4_translate (vm_m4x4_identity , vm_v3 (4.0 , 0.0f , -5.0f ));
175176 model_view_projection = vm_m4x4_mul (projection_view , model );
176- csr_render (& instance , CSR_RENDER_SOLID , CSR_CULLING_CCW_BACKFACE , 6 , vertices , vertices_size , indices , indices_size , model_view_projection .e );
177+ csr_render (& context , CSR_RENDER_SOLID , CSR_CULLING_CCW_BACKFACE , 6 , vertices , vertices_size , indices , indices_size , model_view_projection .e );
177178
178179 /* Save the result to a PPM file */
179- csr_save_ppm ("cube_%05d.ppm" , frame , & instance );
180+ csr_save_ppm ("cube_%05d.ppm" , frame , & context );
180181 }
181182 }
182183
@@ -191,12 +192,11 @@ static void csr_test_teddy(void)
191192 unsigned long memory_size = csr_memory_size (width , height );
192193 void * memory = malloc (memory_size );
193194
194- csr_color clear_color = {40 , 40 , 40 };
195- csr_context instance = {0 };
195+ csr_context context = {0 };
196196
197197 printf ("[csr] memory (MB): %10.4f\n" , (double )memory_size / 1024.0 / 1024.0 );
198198
199- if (!csr_init_model (& instance , memory , memory_size , width , height , clear_color ))
199+ if (!csr_init_model (& context , memory , memory_size , width , height ))
200200 {
201201 return ;
202202 }
@@ -208,7 +208,7 @@ static void csr_test_teddy(void)
208208 v3 cam_position = vm_v3 (0.0f , 0.0f , 50.0f );
209209 float cam_fov = 90.0f ;
210210
211- m4x4 projection = vm_m4x4_perspective (vm_radf (cam_fov ), (float )instance .width / (float )instance .height , 0.1f , 1000.0f );
211+ m4x4 projection = vm_m4x4_perspective (vm_radf (cam_fov ), (float )context .width / (float )context .height , 0.1f , 1000.0f );
212212 m4x4 view = vm_m4x4_lookAt (cam_position , look_at_pos , up );
213213 m4x4 projection_view = vm_m4x4_mul (projection , view );
214214
@@ -222,9 +222,9 @@ static void csr_test_teddy(void)
222222 m4x4 model = vm_m4x4_rotate (model_base , vm_radf (5.0f * (float )(frame + 1 )), rotation_axis );
223223 m4x4 model_view_projection = vm_m4x4_mul (projection_view , model );
224224
225- csr_render_clear_screen (& instance );
226- csr_render (& instance , CSR_RENDER_SOLID , CSR_CULLING_DISABLED , 3 , teddy_vertices , teddy_vertices_size , teddy_indices , teddy_indices_size , model_view_projection .e );
227- csr_save_ppm ("teddy_%05d.ppm" , frame , & instance );
225+ csr_render_clear_screen (& context , clear_color );
226+ csr_render (& context , CSR_RENDER_SOLID , CSR_CULLING_DISABLED , 3 , teddy_vertices , teddy_vertices_size , teddy_indices , teddy_indices_size , model_view_projection .e );
227+ csr_save_ppm ("teddy_%05d.ppm" , frame , & context );
228228 }
229229 }
230230
@@ -247,12 +247,11 @@ void csr_test_voxelize_teddy(void)
247247 unsigned long memory_size = csr_memory_size (width , height );
248248 void * memory = malloc (memory_size );
249249
250- csr_color clear_color = {40 , 40 , 40 };
251- csr_context instance = {0 };
250+ csr_context context = {0 };
252251
253252 printf ("[csr] memory (MB): %10.4f\n" , (double )memory_size / 1024.0 / 1024.0 );
254253
255- if (!csr_init_model (& instance , memory , memory_size , width , height , clear_color ))
254+ if (!csr_init_model (& context , memory , memory_size , width , height ))
256255 {
257256 return ;
258257 }
@@ -275,7 +274,7 @@ void csr_test_voxelize_teddy(void)
275274 v3 cam_position = vm_v3 (0.0f , 0.0f , grid_z * 1.1f );
276275 float cam_fov = 90.0f ;
277276
278- m4x4 projection = vm_m4x4_perspective (vm_radf (cam_fov ), (float )instance .width / (float )instance .height , 0.1f , 1000.0f );
277+ m4x4 projection = vm_m4x4_perspective (vm_radf (cam_fov ), (float )context .width / (float )context .height , 0.1f , 1000.0f );
279278 m4x4 view = vm_m4x4_lookAt (cam_position , look_at_pos , up );
280279 m4x4 projection_view = vm_m4x4_mul (projection , view );
281280
@@ -285,7 +284,7 @@ void csr_test_voxelize_teddy(void)
285284 {
286285 transformation parent = vm_transformation_init ();
287286
288- csr_render_clear_screen (& instance );
287+ csr_render_clear_screen (& context , clear_color );
289288
290289 vm_tranformation_rotate (& parent , vm_v3 (0.0f , 1.0f , 0.0f ), vm_radf (5.0f * (float )(frame + 1 )));
291290
@@ -302,7 +301,7 @@ void csr_test_voxelize_teddy(void)
302301 model = vm_transformation_matrix (& child );
303302 model_view_projection = vm_m4x4_mul (projection_view , model );
304303
305- csr_render (& instance , CSR_RENDER_SOLID , CSR_CULLING_DISABLED , 3 , teddy_vertices , teddy_vertices_size , teddy_indices , teddy_indices_size , model_view_projection .e );
304+ csr_render (& context , CSR_RENDER_SOLID , CSR_CULLING_DISABLED , 3 , teddy_vertices , teddy_vertices_size , teddy_indices , teddy_indices_size , model_view_projection .e );
306305 }
307306
308307 /* Render voxelized teddy */
@@ -331,14 +330,14 @@ void csr_test_voxelize_teddy(void)
331330 model_view_projection = vm_m4x4_mul (projection_view , model );
332331
333332 /* Render voxel cube */
334- csr_render (& instance , CSR_RENDER_SOLID , CSR_CULLING_CCW_BACKFACE , 6 , vertices , vertices_size , indices , indices_size , model_view_projection .e );
333+ csr_render (& context , CSR_RENDER_SOLID , CSR_CULLING_CCW_BACKFACE , 6 , vertices , vertices_size , indices , indices_size , model_view_projection .e );
335334 }
336335 }
337336 }
338337 }
339338
340339 /* Save the result to a PPM file */
341- csr_save_ppm ("voxel_teddy_%05d.ppm" , frame , & instance );
340+ csr_save_ppm ("voxel_teddy_%05d.ppm" , frame , & context );
342341 }
343342 }
344343
@@ -362,12 +361,11 @@ void csr_test_voxelize_head(void)
362361 unsigned long memory_size = csr_memory_size (width , height );
363362 void * memory = malloc (memory_size );
364363
365- csr_color clear_color = {40 , 40 , 40 };
366- csr_context instance = {0 };
364+ csr_context context = {0 };
367365
368366 printf ("[csr] memory (MB): %10.4f\n" , (double )memory_size / 1024.0 / 1024.0 );
369367
370- if (!csr_init_model (& instance , memory , memory_size , width , height , clear_color ))
368+ if (!csr_init_model (& context , memory , memory_size , width , height ))
371369 {
372370 return ;
373371 }
@@ -390,7 +388,7 @@ void csr_test_voxelize_head(void)
390388 v3 cam_position = vm_v3 (0.0f , 0.0f , grid_head_z );
391389 float cam_fov = 90.0f ;
392390
393- m4x4 projection = vm_m4x4_perspective (vm_radf (cam_fov ), (float )instance .width / (float )instance .height , 0.1f , 1000.0f );
391+ m4x4 projection = vm_m4x4_perspective (vm_radf (cam_fov ), (float )context .width / (float )context .height , 0.1f , 1000.0f );
394392 m4x4 view = vm_m4x4_lookAt (cam_position , look_at_pos , up );
395393 m4x4 projection_view = vm_m4x4_mul (projection , view );
396394
@@ -400,7 +398,7 @@ void csr_test_voxelize_head(void)
400398 {
401399 transformation parent = vm_transformation_init ();
402400
403- csr_render_clear_screen (& instance );
401+ csr_render_clear_screen (& context , clear_color );
404402
405403 vm_tranformation_rotate (& parent , vm_v3 (0.0f , 1.0f , 0.0f ), vm_radf (5.0f * (float )(frame + 1 )));
406404
@@ -430,14 +428,14 @@ void csr_test_voxelize_head(void)
430428 model_view_projection = vm_m4x4_mul (projection_view , model );
431429
432430 /* Render voxel cube */
433- csr_render (& instance , CSR_RENDER_SOLID , CSR_CULLING_CCW_BACKFACE , 6 , vertices , vertices_size , indices , indices_size , model_view_projection .e );
431+ csr_render (& context , CSR_RENDER_SOLID , CSR_CULLING_CCW_BACKFACE , 6 , vertices , vertices_size , indices , indices_size , model_view_projection .e );
434432 }
435433 }
436434 }
437435 }
438436
439437 /* Save the result to a PPM file */
440- csr_save_ppm ("voxel_head_%05d.ppm" , frame , & instance );
438+ csr_save_ppm ("voxel_head_%05d.ppm" , frame , & context );
441439 }
442440 }
443441
0 commit comments