-
Notifications
You must be signed in to change notification settings - Fork 0
/
simp.c
446 lines (335 loc) · 11.8 KB
/
simp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#ifndef SIMP_C_
#define SIMP_C_
#ifdef __cplusplus
extern "C" {
#endif
// includes
#include <stdint.h>
#include <stdlib.h>
#if defined(__STDC__) && (__STDC__VERSION__ >= 1999901L) // stdbool is supported (not for windows, they utilize MSC)
#include <stdbool.h>
#elif !defined(__cplusplus) && !defined(bool) // otherwise, we define it ourselves
typedef enum bool { false = 0, true = !false } bool;
#endif
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <math.h>
#ifndef PI
#define PI 3.14159265358979f
#endif
#ifndef TAU
#define TAU (PI*2.0f)
#endif
// return defer utilizes a result value that is defined beforehand
// assigns the value and goes to a predefined label called defer
#define returnDefer(value) do { result = (value); goto defer; } while(0)
typedef int Errno;
#define getColorPart(color, index) (((color)>>(8*(index)))&0xFF)
#define getRed(color) getColorPart(color, 0)
#define getGreen(color) getColorPart(color, 1)
#define getBlue(color) getColorPart(color, 2)
#define getAlpha(color) getColorPart(color, 3)
// represents a single renderable frame
/*typedef struct Frame {*/
/* size_t width;*/
/* size_t height;*/
/* uint32_t *colors;*/
/*} Frame;*/
float simpcGetAnimationTime(size_t frameIndex, size_t fps) {
float dt = 1.0f / fps;
return dt * frameIndex;
}
int max(int a, int b) { return a > b ? a : b; }
int min(int a, int b) { return a < b ? a : b; }
float simpcLerp(float a, float b, float t) {
return a + (b - a) * t;
}
uint32_t simpcLerpColor(uint32_t a, uint32_t b, float t) {
uint32_t result = 0;
uint8_t aa = getAlpha(a);
uint8_t ar = getRed(a);
uint8_t ag = getGreen(a);
uint8_t ab = getBlue(a);
uint8_t ba = getAlpha(b);
uint8_t br = getRed(b);
uint8_t bg = getGreen(b);
uint8_t bb = getBlue(b);
result |= (uint32_t)simpcLerp(aa, ba, t);
result <<= 8;
result |= (uint32_t)simpcLerp(ab, bb, t);
result <<= 8;
result |= (uint32_t)simpcLerp(ag, bg, t);
result <<= 8;
result |= (uint32_t)simpcLerp(ar, br, t);
return result;
}
uint32_t simpcColorFromRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
uint32_t color = 0;
// 0xAABBGGRR
color |= a;
color <<= 8;
color |= b;
color <<= 8;
color |= g;
color <<= 8;
color |= r;
return color;
}
uint32_t simpcColorFromRGB(uint8_t r, uint8_t g, uint8_t b) {
return simpcColorFromRGBA(r, g, b, 255);
}
void simpcAlphaMix(uint32_t *originalColor, uint32_t mixColor) {
uint8_t alpha = getAlpha(mixColor);
*originalColor = simpcLerpColor(*originalColor, mixColor, alpha / 255.0f);
}
Errno simpcSavePPM(uint32_t *pixels, size_t width, size_t height, const char *filePath) {
int result = 0;
// open for write and binary mode
FILE *file = fopen(filePath, "wb");
if(file == NULL) returnDefer(errno);
// utilizes P6 version of PPM
fprintf(file, "P6\n%zu %zu 255\n", width, height);
if(ferror(file)) returnDefer(errno);
for (size_t i = 0; i < width*height; i++) {
uint32_t pixel = pixels[i];
uint8_t bytes[3] = {
getRed(pixel),
getGreen(pixel),
getBlue(pixel)
};
fwrite(bytes, sizeof(bytes), 1, file);
if(ferror(file)) returnDefer(errno);
}
defer:
if(file) fclose(file);
return result;
}
void simpcFill(uint32_t *pixels, size_t width, size_t height, uint32_t color) {
for (size_t i = 0; i < width*height; i++)
simpcAlphaMix(&pixels[i], color);
}
void simpcFillRect(uint32_t *pixels, size_t pixelsWidth, size_t pixelsHeight, int x, int y, size_t width, size_t height, uint32_t color) {
size_t ix = (size_t)max(x, 0);
size_t iy = (size_t)max(y, 0);
size_t ex = (size_t)min(x + width, pixelsWidth - 1);
size_t ey = (size_t)min(y + height, pixelsHeight - 1);
for (size_t cy = iy; cy <= ey; cy++)
for (size_t cx = ix; cx <= ex; cx++)
simpcAlphaMix(&pixels[cy*pixelsWidth + cx], color);
}
bool simpcIsWithinRadius(int dx, int dy, size_t radius) {
return (size_t)(dx*dx + dy*dy) <= (radius*radius);
}
void simpcFillCircle(uint32_t *pixels, size_t width, size_t height, int x, int y, size_t radius, uint32_t color) {
int tlx = x - radius;
int tly = y - radius;
int brx = x + radius;
int bry = y + radius;
size_t ix = max(tlx, 0);
size_t iy = max(tly, 0);
size_t ex = min(brx, width - 1);
size_t ey = min(bry, height - 1);
for (size_t cy = iy; cy <= ey; cy++) {
for (size_t cx = ix; cx <= ex; cx++) {
if(simpcIsWithinRadius(cx - x, cy - y, radius) == false) continue;
simpcAlphaMix(&pixels[cy*width + cx], color);
}
}
}
void simpcColorAdd(uint32_t *r, uint32_t *g, uint32_t *b, uint32_t color) {
*r += getRed(color);
*g += getGreen(color);
*b += getBlue(color);
}
void simpcFillCircleSSAA(uint32_t *pixels, size_t width, size_t height, int x, int y, size_t radius, uint32_t color) {
int tlx = x - radius;
int tly = y - radius;
int brx = x + radius;
int bry = y + radius;
size_t ix = max(tlx, 0);
size_t iy = max(tly, 0);
size_t ex = min(brx, width - 1);
size_t ey = min(bry, height - 1);
const size_t RES_SCALAR = 2;
const size_t TOTAL_SUB_PIXELS = RES_SCALAR*RES_SCALAR;
for (size_t cy = iy; cy <= ey; cy++) {
for (size_t cx = ix; cx <= ex; cx++) {
int x2 = x * RES_SCALAR;
int y2 = y * RES_SCALAR;
size_t r2 = radius * RES_SCALAR;
uint32_t r = 0;
uint32_t g = 0;
uint32_t b = 0;
for (size_t cx2 = cx * RES_SCALAR; cx2 <= (cx * RES_SCALAR + 1); cx2++) {
for (size_t cy2 = cy * RES_SCALAR; cy2 <= (cy * RES_SCALAR + 1); cy2++) {
uint32_t bgColor = pixels[cy*width + cx];
if(simpcIsWithinRadius(cx2 - x2, cy2 - y2, r2) == false) {
simpcColorAdd(&r, &g, &b, bgColor);
continue;
}
// else
// add normal circle color;
simpcColorAdd(&r, &g, &b, color);
}
}
r /= TOTAL_SUB_PIXELS;
g /= TOTAL_SUB_PIXELS;
b /= TOTAL_SUB_PIXELS;
// calculate averaged color
// set current pixel color with averaged color
uint32_t resultColor = simpcColorFromRGBA(r, g, b, getAlpha(color));
simpcAlphaMix(&pixels[cy*width + cx], resultColor);
}
}
}
void simpcDrawArc(uint32_t *pixels, size_t width, size_t height, int x, int y, size_t radius, size_t thickness, float startAngle, float endAngle, uint32_t color) {
size_t outerRadius = radius + thickness/2;
size_t innerRadius = radius - thickness/2;
int tlx = x - outerRadius;
int tly = y - outerRadius;
int brx = x + outerRadius;
int bry = y + outerRadius;
size_t ix = max(tlx, 0);
size_t iy = max(tly, 0);
size_t ex = min(brx, width - 1);
size_t ey = min(bry, height - 1);
for (size_t cy = iy; cy <= ey; cy++) {
for (size_t cx = ix; cx <= ex; cx++) {
float dx = (float)cx - (float)x;
float dy = (float)cy - (float)y;
size_t distSqr = (cx - x)*(cx - x) + (cy - y)*(cy - y);
if(distSqr > (outerRadius*outerRadius)) continue;
if(distSqr < (innerRadius*innerRadius)) continue;
float ca = atan2f(dy, dx);
if(ca < 0) ca = TAU + ca;
if(ca > endAngle || ca < startAngle) continue;
simpcAlphaMix(&pixels[cy*width + cx], color);
}
}
}
void simpcFillSector(uint32_t *pixels, size_t width, size_t height, int x, int y, size_t radius, float startAngle, float endAngle, uint32_t color) {
int tlx = x - radius;
int tly = y - radius;
int brx = x + radius;
int bry = y + radius;
size_t ix = max(tlx, 0);
size_t iy = max(tly, 0);
size_t ex = min(brx, width - 1);
size_t ey = min(bry, height - 1);
for (size_t cy = iy; cy <= ey; cy++) {
for (size_t cx = ix; cx <= ex; cx++) {
float dx = (float)cx - (float)x;
float dy = (float)cy - (float)y;
size_t distSqr = ((cx - x)*(cx - x) + (cy - y)*(cy - y));
if(distSqr > (radius*radius)) continue;
float ca = atan2f(dy, dx);
if(ca < 0) ca = TAU + ca;
if(ca > endAngle || ca < startAngle) continue;
simpcAlphaMix(&pixels[cy*width + cx], color);
}
}
}
float simpcAbsf(float a) { return a < 0.0f ? -a : a; }
float triangleArea(int x1, int y1, int x2, int y2, int x3, int y3)
{
return simpcAbsf((x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))/2.0f);
}
bool isInsideTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y)
{
/* Calculate triangleArea of triangle ABC */
float A = triangleArea(x1, y1, x2, y2, x3, y3);
/* Calculate triangleAreaof triangle PBC */
float A1 = triangleArea(x, y, x2, y2, x3, y3);
/* Calculate triangleAreaof triangle PAC */
float A2 = triangleArea(x1, y1, x, y, x3, y3);
/* Calculate triangleAreaof triangle PAB */
float A3 = triangleArea(x1, y1, x2, y2, x, y);
/* Check if sum of A1, A2 and A3 is same as A */
return (A == A1 + A2 + A3);
}
void simpcFillTriangle(uint32_t *pixels, size_t width, size_t height, int x1, int y1, int x2, int y2, int x3, int y3, uint32_t color) {
int lx = min(x1, min(x2, x3));
int rx = max(x1, max(x2, x3));
int ly = min(y1, min(y2, y3));
int ry = max(y1, max(y2, y3));
size_t ix = (size_t)max(lx, 0);
size_t iy = (size_t)max(ly, 0);
size_t ex = (size_t)min(rx, width - 1);
size_t ey = (size_t)min(ry, height - 1);
for (size_t cy = iy; cy <= ey; cy++)
for (size_t cx = ix; cx <= ex; cx++)
if(isInsideTriangle(x1, y1, x2, y2, x3, y3, cx, cy))
simpcAlphaMix(&pixels[cy*width + cx], color);
}
int simpcClampInt(int value, int minValue, int maxValue) {
return min(max(value, minValue), maxValue);
}
void simpcDrawLine(uint32_t *pixels, size_t width, size_t height, int x1, int y1, int x2, int y2, uint32_t color) {
// y = mx + c
// m = (y - c) / m
// m = (y2 - y1) / (x2 - x1)
// c = y - mx
size_t iy = max(min(y1, y2), 0);
size_t ey = min(max(y1, y2), height - 1);
if(x2 == x1) {
for (size_t cy = iy; cy <= ey; cy++)
simpcAlphaMix(&pixels[cy*width + x1], color);
return;
}
size_t ix = max(min(x1, x2), 0);
size_t ex = min(max(x1, x2), width - 1);
float m = (float)(y2 - y1) / (x2 - x1);
float c = y1 - m * x1;
int lastY = m * ix + c;
for (size_t cx = ix; cx <= ex; cx++) {
int targetY = m * cx + c;
// filling inbetween the gaps of the line
int cy = lastY;
while (true) {
if(m >= 0 ? cy > targetY : cy < targetY)
break;
// else
if(cy < 0 || (size_t)cy >= height) {
cy += m >= 0 ? 1 : -1;
continue;
}
simpcAlphaMix(&pixels[cy*width + cx], color);
cy += m >= 0 ? 1 : -1;
}
lastY = targetY;
}
}
void simpcSampleBezier(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, float *resultX, float *resultY, float t) {
float lx1 = simpcLerp(x1, x2, t);
float ly1 = simpcLerp(y1, y2, t);
float lx2 = simpcLerp(x2, x3, t);
float ly2 = simpcLerp(y2, y3, t);
float lx3 = simpcLerp(x3, x4, t);
float ly3 = simpcLerp(y3, y4, t);
float lx11 = simpcLerp(lx1, lx2, t);
float ly11 = simpcLerp(ly1, ly2, t);
float lx12 = simpcLerp(lx2, lx3, t);
float ly12 = simpcLerp(ly2, ly3, t);
*resultX = simpcLerp(lx11, lx12, t);
*resultY = simpcLerp(ly11, ly12, t);
}
void simpcDrawBezier(uint32_t *pixels, size_t width, size_t height, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, uint32_t color, size_t resolution) {
for (size_t currentSection = 0; currentSection < resolution; currentSection++) {
float t1 = 1.0f / resolution * currentSection;
float t2 = 1.0f / resolution * (currentSection + 1);
float lineX1, lineY1;
float lineX2, lineY2;
simpcSampleBezier(x1, y1, x2, y2, x3, y3, x4, y4, &lineX1, &lineY1, t1);
simpcSampleBezier(x1, y1, x2, y2, x3, y3, x4, y4, &lineX2, &lineY2, t2);
simpcDrawLine(pixels, width, height, lineX1, lineY1, lineX2, lineY2, color);
}
}
// TODO: implement line culling in line algorithm
// TODO: re implement fill triangle to use horizontal fill algorithm
// TODO: implement basic 3D view of a Cube
// TODO: add implementation of vectors
#ifdef __cplusplus
}
#endif
#endif /* ifndef SIMP_C_ */