Skip to content

Commit 64540cb

Browse files
committed
[MIN] cleanup and format code
1 parent 15256c0 commit 64540cb

File tree

4 files changed

+61
-68
lines changed

4 files changed

+61
-68
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ int main() {
6565
void *memory = (void *)memory_total;
6666

6767
csr_color clear_color = {40, 40, 40};
68-
csr_context instance = {0};
68+
csr_context context = {0};
6969

70-
if (!csr_init_model(&instance, memory, MEMORY_SIZE, WIDTH, HEIGHT, clear_color))
70+
if (!csr_init_model(&context, memory, MEMORY_SIZE, WIDTH, HEIGHT))
7171
{
7272
return 1;
7373
}
@@ -81,7 +81,7 @@ int main() {
8181
v3 cam_look_at_pos = vm_v3_zero;
8282
float cam_fov = 90.0f;
8383

84-
m4x4 projection = vm_m4x4_perspective(vm_radf(cam_fov), (float)instance.width / (float)instance.height, 0.1f, 1000.0f);
84+
m4x4 projection = vm_m4x4_perspective(vm_radf(cam_fov), (float)context.width / (float)context.height, 0.1f, 1000.0f);
8585
m4x4 view = vm_m4x4_lookAt(cam_position, cam_look_at_pos, world_up);
8686
m4x4 projection_view = vm_m4x4_mul(projection, view);
8787

@@ -99,19 +99,19 @@ int main() {
9999
);
100100

101101
/* Clear Screen Frame and Depth Buffer */
102-
csr_clear_screen(&instance);
102+
csr_render_clear_screen(&context, clear_color);
103103

104104
/* Render cube */
105105
csr_render(
106-
&instance,
106+
&context,
107107
CSR_RENDER_SOLID,
108108
CSR_CULLING_CCW_BACKFACE, 6,
109109
vertices, vertices_size,
110110
indices, indices_size,
111111
model_view_projection.e
112112
);
113113

114-
/* Afterwards you can write the instance.framebuffer to a screen or write to a file (like ppm format).
114+
/* Afterwards you can write the context.framebuffer to a screen or write to a file (like ppm format).
115115
116116
In the csr_test.c file we write each frame into a ppm file
117117
and then using ffmpeg (via the build.bat script) to generate a mp4 and gif file.

csr.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ typedef struct csr_context
134134

135135
int width; /* render area width in pixels */
136136
int height; /* render area height in pixels */
137-
csr_color clear_color; /* The default clear color for the screen */
138137
csr_color *framebuffer; /* memory pointer for framebuffer */
139138
float *zbuffer; /* memory pointer for zbuffer */
140139

@@ -149,7 +148,7 @@ CSR_API CSR_INLINE unsigned long csr_memory_size(int width, int height)
149148
);
150149
}
151150

152-
CSR_API CSR_INLINE int csr_init_model(csr_context *context, void *memory, unsigned long memory_size, int width, int height, csr_color clear_color)
151+
CSR_API CSR_INLINE int csr_init_model(csr_context *context, void *memory, unsigned long memory_size, int width, int height)
153152
{
154153
unsigned long memory_framebuffer_size = (unsigned long)(width * height) * (unsigned long)sizeof(csr_color);
155154

@@ -160,7 +159,6 @@ CSR_API CSR_INLINE int csr_init_model(csr_context *context, void *memory, unsign
160159

161160
context->width = width;
162161
context->height = height;
163-
context->clear_color = clear_color;
164162
context->framebuffer = (csr_color *)memory;
165163
context->zbuffer = (float *)((char *)memory + memory_framebuffer_size);
166164

@@ -185,19 +183,18 @@ CSR_API CSR_INLINE void csr_ndc_to_screen(csr_context *context, float result[3],
185183
result[2] = ndc_pos[2];
186184
}
187185

188-
CSR_API CSR_INLINE void csr_render_clear_screen(csr_context *context)
186+
CSR_API CSR_INLINE void csr_render_clear_screen(csr_context *context, csr_color clear_color)
189187
{
190188
int size = context->width * context->height;
191-
csr_color c = context->clear_color;
192189

193190
int i = 0;
194191

195192
for (; i + 4 <= size; i += 4)
196193
{
197-
context->framebuffer[i] = c;
198-
context->framebuffer[i + 1] = c;
199-
context->framebuffer[i + 2] = c;
200-
context->framebuffer[i + 3] = c;
194+
context->framebuffer[i] = clear_color;
195+
context->framebuffer[i + 1] = clear_color;
196+
context->framebuffer[i + 2] = clear_color;
197+
context->framebuffer[i + 3] = clear_color;
201198
context->zbuffer[i] = 1.0f;
202199
context->zbuffer[i + 1] = 1.0f;
203200
context->zbuffer[i + 2] = 1.0f;
@@ -206,7 +203,7 @@ CSR_API CSR_INLINE void csr_render_clear_screen(csr_context *context)
206203

207204
for (; i < size; ++i)
208205
{
209-
context->framebuffer[i] = c;
206+
context->framebuffer[i] = clear_color;
210207
context->zbuffer[i] = 1.0f;
211208
}
212209
}

tests/build.bat

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@ cc -s -O2 %DEF_FLAGS_COMPILER% -o %SOURCE_NAME%.exe %SOURCE_NAME%.c %DEF_FLAGS_L
99
%SOURCE_NAME%.exe
1010

1111
REM RENDER Videos and Gifs from PPM frames
12-
ffmpeg -y -framerate 30 -i stack_%%05d.ppm -c:v libx264 -pix_fmt yuv420p stack.mp4
13-
ffmpeg -y -framerate 30 -i cube_%%05d.ppm -c:v libx264 -pix_fmt yuv420p cube.mp4
14-
ffmpeg -y -framerate 30 -i teddy_%%05d.ppm -c:v libx264 -pix_fmt yuv420p teddy.mp4
15-
ffmpeg -y -framerate 30 -i voxel_teddy_%%05d.ppm -c:v libx264 -pix_fmt yuv420p voxel_teddy.mp4
16-
ffmpeg -y -framerate 30 -i voxel_head_%%05d.ppm -c:v libx264 -pix_fmt yuv420p voxel_head.mp4
17-
18-
ffmpeg -y -i stack.mp4 -filter_complex "[0:v] fps=10,scale=360:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse" stack.gif
19-
ffmpeg -y -i cube.mp4 -filter_complex "[0:v] fps=10,scale=360:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse" cube.gif
20-
ffmpeg -y -i teddy.mp4 -filter_complex "[0:v] fps=10,scale=360:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse" teddy.gif
21-
ffmpeg -y -i voxel_teddy.mp4 -filter_complex "[0:v] fps=10,scale=360:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse" voxel_teddy.gif
22-
ffmpeg -y -i voxel_head.mp4 -filter_complex "[0:v] fps=10,scale=360:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse" voxel_head.gif
23-
24-
25-
rm *.ppm
12+
REM ffmpeg -y -framerate 30 -i stack_%%05d.ppm -c:v libx264 -pix_fmt yuv420p stack.mp4
13+
REM ffmpeg -y -framerate 30 -i cube_%%05d.ppm -c:v libx264 -pix_fmt yuv420p cube.mp4
14+
REM ffmpeg -y -framerate 30 -i teddy_%%05d.ppm -c:v libx264 -pix_fmt yuv420p teddy.mp4
15+
REM ffmpeg -y -framerate 30 -i voxel_teddy_%%05d.ppm -c:v libx264 -pix_fmt yuv420p voxel_teddy.mp4
16+
REM ffmpeg -y -framerate 30 -i voxel_head_%%05d.ppm -c:v libx264 -pix_fmt yuv420p voxel_head.mp4
17+
REM
18+
REM ffmpeg -y -i stack.mp4 -filter_complex "[0:v] fps=10,scale=360:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse" stack.gif
19+
REM ffmpeg -y -i cube.mp4 -filter_complex "[0:v] fps=10,scale=360:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse" cube.gif
20+
REM ffmpeg -y -i teddy.mp4 -filter_complex "[0:v] fps=10,scale=360:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse" teddy.gif
21+
REM ffmpeg -y -i voxel_teddy.mp4 -filter_complex "[0:v] fps=10,scale=360:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse" voxel_teddy.gif
22+
REM ffmpeg -y -i voxel_head.mp4 -filter_complex "[0:v] fps=10,scale=360:-1:flags=lanczos,split [a][b];[a] palettegen [p];[b][p] paletteuse" voxel_head.gif
23+
REM rm *.ppm

tests/csr_test.c

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ static int indices[] = {
4343
static unsigned long vertices_size = sizeof(vertices) / sizeof(vertices[0]);
4444
static 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

Comments
 (0)