summaryrefslogtreecommitdiff
path: root/sponge.h
blob: 0914bafb4c7a78d6613d66a898b106d003b279de (plain)
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
#include <stdint.h>

typedef struct {
    uint32_t *pixels; // AARRGGBB
    uint32_t width;
    uint32_t height;
    uint32_t stride;
} sponge_Texture;

typedef struct {
    float x;
    float y;
} sponge_Vec2;

sponge_Vec2 sponge_add2(sponge_Vec2 v0, sponge_Vec2 v1);
sponge_Vec2 sponge_sub2(sponge_Vec2 v0, sponge_Vec2 v1);
float       sponge_dot2(sponge_Vec2 v0, sponge_Vec2 v1);

int  sponge_canvas_valid      (sponge_Texture c);
void sponge_clear             (sponge_Texture c, uint32_t color);
void sponge_draw_rect         (sponge_Texture c, uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t color);
void sponge_draw_line         (sponge_Texture c,  int32_t x0,  int32_t y0,  int32_t x1,  int32_t y1, uint32_t color);
void sponge_draw_texture      (sponge_Texture c, uint32_t x0, uint32_t y0, sponge_Texture tex);
void sponge_draw_triangle_col (sponge_Texture c,  int32_t x0,  int32_t y0,  int32_t x1,  int32_t y1,  int32_t x2,  int32_t y2, uint32_t color);
void sponge_draw_triangle_col3(
    sponge_Texture c, int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2,
    uint32_t color0, uint32_t color1, uint32_t color2);

// TODO(kard): prefix stripping

#ifdef SPONGE_IMPLEMENTATION

// can be defined as nop in release builds
#ifndef SPONGE_ASSERT
#include <assert.h>
#define SPONGE_ASSERT(x) assert(x)
#endif // SPONGE_ASSERT

#define SPONGE__CLAMP(val, min, max) ((val) < (min) ? (min) : ((val) > (max) ? (max) : (val)))
#define SPONGE__MIN(x, y) ((x) < (y) ? (x) : (y))
#define SPONGE__MAX(x, y) ((x) > (y) ? (x) : (y))
#define SPONGE__ABS(x) ((x) >= 0 ? (x) : -(x))

typedef struct {
    float a;
    float r;
    float g;
    float b;
} sponge__ColorF;

sponge__ColorF sponge__colf_unpack(uint32_t col) {
    uint32_t b = col & 0xFF;
    col = col >> 8;
    uint32_t g = col & 0xFF;
    col = col >> 8;
    uint32_t r = col & 0xFF;
    col = col >> 8;
    uint32_t a = col & 0xFF;
    sponge__ColorF result = { .a = (float)a, .r = (float)r, .g = (float)g, .b = (float)b };
    return result;
}

uint32_t sponge__colf_pack(sponge__ColorF color) {
    uint32_t result = 0;
    result |= SPONGE__CLAMP((uint32_t)color.a, 0x00, 0xFF);
    result = result << 8;
    result |= SPONGE__CLAMP((uint32_t)color.r, 0x00, 0xFF);
    result = result << 8;
    result |= SPONGE__CLAMP((uint32_t)color.g, 0x00, 0xFF);
    result = result << 8;
    result |= SPONGE__CLAMP((uint32_t)color.b, 0x00, 0xFF);
    return result;
}

sponge__ColorF sponge__colf_mul(sponge__ColorF color, float f) {
    color.a *= f;
    color.r *= f;
    color.g *= f;
    color.b *= f;
    return color;
}

sponge__ColorF sponge__colf_add(sponge__ColorF color0, sponge__ColorF color1) {
    sponge__ColorF result;
    result.a = color0.a + color1.a;
    result.r = color0.r + color1.r;
    result.g = color0.g + color1.g;
    result.b = color0.b + color1.b;
    return result;
}


typedef struct {
    sponge_Vec2 v0;
    sponge_Vec2 v1;
    float d00;
    float d01;
    float d11;
    float denom;
} sponge__BarycentricContext;

// TODO(kard): measure if caching this makes sense
sponge__BarycentricContext sponge__barycentric_init(sponge_Vec2 t0, sponge_Vec2 t1, sponge_Vec2 t2) {
    sponge__BarycentricContext result;
    result.v0 = sponge_sub2(t1, t0);
    result.v1 = sponge_sub2(t2, t0);
    result.d00 = sponge_dot2(result.v0, result.v0);
    result.d01 = sponge_dot2(result.v0, result.v1);
    result.d11 = sponge_dot2(result.v1, result.v1);
    result.denom = (result.d00 * result.d11) - (result.d01 * result.d01);
    return result;
}

void sponge__barycentric(
    sponge__BarycentricContext ctx, sponge_Vec2 p,
    sponge_Vec2 t0,
    sponge_Vec2 t1,
    sponge_Vec2 t2,
    float *u, float *v, float *w
) {
    sponge_Vec2 v2 = sponge_sub2(p, t0);
    float d20 = sponge_dot2(v2, ctx.v0);
    float d21 = sponge_dot2(v2, ctx.v1);
    float vr = (ctx.d11 * d20 - ctx.d01 * d21) / ctx.denom;
    float wr = (ctx.d00 * d21 - ctx.d01 * d20) / ctx.denom;
    float ur = 1.0f - vr - wr;
    *u = ur;
    *v = vr;
    *w = wr;
}


sponge_Vec2 sponge_add2(sponge_Vec2 v0, sponge_Vec2 v1) {
    sponge_Vec2 result;
    result.x = v0.x + v1.x;
    result.y = v0.y + v1.y;
    return result;
}

sponge_Vec2 sponge_sub2(sponge_Vec2 v0, sponge_Vec2 v1) {
    sponge_Vec2 result;
    result.x = v0.x - v1.x;
    result.y = v0.y - v1.y;
    return result;
}

float sponge_dot2(sponge_Vec2 v0, sponge_Vec2 v1) {
    return (v0.x * v1.x) + (v0.y * v1.y);
}

int sponge_canvas_valid(sponge_Texture c) {
    return c.width != 0 && c.height != 0;
}

void sponge_clear(sponge_Texture c, uint32_t color) {
    SPONGE_ASSERT(sponge_canvas_valid(c));

    sponge_draw_rect(c, 0, 0, c.width - 1, c.height - 1, color);
}

// TODO(kard): probably bounds checking
// TODO(kard): alpha blending maybe
void sponge_draw_rect(sponge_Texture c, uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t color) {
    SPONGE_ASSERT(sponge_canvas_valid(c));

    uint32_t *row = c.pixels + (y0 * c.stride);

    for (uint32_t y = y0; y <= y1; y++, row += c.stride) {
        for (uint32_t x = x0; x <= x1; x++) {
            row[x] = color;
        }
    }
}

void sponge_draw_line(sponge_Texture c, int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t color) {
    SPONGE_ASSERT(sponge_canvas_valid(c));

    int32_t dx = x1 - x0;
    dx = SPONGE__ABS(dx);
    int32_t dy = y1 - y0;
    dy = SPONGE__ABS(dy);

    int32_t sx = (x0 < x1) ? 1 : -1;
    int32_t sy = (y0 < y1) ? 1 : -1;
    int32_t err = dx - dy;

    while (1) {
        if (x0 >= 0 && x0 < (int32_t)c.width && y0 >= 0 && y0 < (int32_t)c.height)
            c.pixels[(y0 * c.stride) + x0] = color;

        if (x0 == x1 && y0 == y1) break;

        int32_t e2 = 2 * err;
        if (e2 > -dy) { err -= dy; x0 += sx; }
        if (e2 < dx)  { err += dx; y0 += sy; }
    }
}

void sponge_draw_texture(sponge_Texture c, uint32_t x0, uint32_t y0, sponge_Texture tex) {
    SPONGE_ASSERT(sponge_canvas_valid(c));

    uint32_t *row_dst = c.pixels + (y0 * c.stride);
    uint32_t *row_src = tex.pixels;
    uint32_t x1 = x0 + tex.width;
    uint32_t y1 = y0 + tex.height;

    if (x1 >= c.width)
        x1 = c.width - 1;
    if (y1 >= c.height)
        y1 = c.height - 1;

    for (uint32_t y = y0; y <= y1; y++, row_src += tex.stride, row_dst += c.stride) {
        // TODO(kard): memcpy?
        for (uint32_t x = x0, x_src = 0; x <= x1; x++, x_src++) {
            row_dst[x] = row_src[x_src];
        }
    }
}

void sponge_draw_triangle_col(sponge_Texture c, int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color) {
    sponge_draw_triangle_col3(c, x0, y0, x1, y1, x2, y2, color, color, color);
}

// NOTE(kard): requires vertices to be in clockwise order
void sponge_draw_triangle_col3(
    sponge_Texture c, int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2,
    uint32_t color0, uint32_t color1, uint32_t color2) {
    SPONGE_ASSERT(sponge_canvas_valid(c));

    sponge_Vec2 t0 = { .x = (float)x0, .y = (float)y0 };
    sponge_Vec2 t1 = { .x = (float)x1, .y = (float)y1 };
    sponge_Vec2 t2 = { .x = (float)x2, .y = (float)y2 };

    int32_t smin_x = SPONGE__MIN(x0, SPONGE__MIN(x1, x2));
    int32_t smax_x = SPONGE__MAX(x0, SPONGE__MAX(x1, x2));
    int32_t smin_y = SPONGE__MIN(y0, SPONGE__MIN(y1, y2));
    int32_t smax_y = SPONGE__MAX(y0, SPONGE__MAX(y1, y2));

    uint32_t min_x = (uint32_t)SPONGE__CLAMP(smin_x, 0, (int32_t)(c.width - 1));
    uint32_t max_x = (uint32_t)SPONGE__CLAMP(smax_x, 0, (int32_t)(c.width - 1));
    uint32_t min_y = (uint32_t)SPONGE__CLAMP(smin_y, 0, (int32_t)(c.height - 1));
    uint32_t max_y = (uint32_t)SPONGE__CLAMP(smax_y, 0, (int32_t)(c.height - 1));

    sponge__BarycentricContext ctx = sponge__barycentric_init(t0, t1, t2);

    uint32_t *row = c.pixels + (min_y * c.stride);

    sponge__ColorF color0f = sponge__colf_unpack(color0);
    sponge__ColorF color1f = sponge__colf_unpack(color1);
    sponge__ColorF color2f = sponge__colf_unpack(color2);

    int32_t edge0 = ((y0 - y1) * min_x) + ((x1 - x0) * min_y) + ((x0 * y1) - (x1 * y0));
    int32_t edge1 = ((y1 - y2) * min_x) + ((x2 - x1) * min_y) + ((x1 * y2) - (x2 * y1));
    int32_t edge2 = ((y2 - y0) * min_x) + ((x0 - x2) * min_y) + ((x2 * y0) - (x0 * y2));

    for (uint32_t y = min_y; y <= max_y; y++, row += c.stride) {
        int32_t edge0a = edge0;
        int32_t edge1a = edge1;
        int32_t edge2a = edge2;
        edge0 += (x1 - x0);
        edge1 += (x2 - x1);
        edge2 += (x0 - x2);

        for (uint32_t x = min_x; x <= max_x; x++) {
            int32_t edge0b = edge0a;
            int32_t edge1b = edge1a;
            int32_t edge2b = edge2a;
            edge0a += (y0 - y1);
            edge1a += (y1 - y2);
            edge2a += (y2 - y0);
            if (edge0b < 0 || edge1b < 0 || edge2b < 0) {
                continue;
            }

            float u, v, w;
            sponge_Vec2 p = { .x = (float)x, .y = (float)y };
            sponge__barycentric(ctx, p, t0, t1, t2, &u, &v, &w);
            if (u > 0.0f && v > 0.0f && w > 0.0f)
            {
                sponge__ColorF c0 = sponge__colf_mul(color0f, u);
                sponge__ColorF c1 = sponge__colf_mul(color1f, v);
                sponge__ColorF c2 = sponge__colf_mul(color2f, w);
                sponge__ColorF result = sponge__colf_add(c0, sponge__colf_add(c1, c2));
                row[x] = sponge__colf_pack(result);
            }
        }
    }
}

#endif // SPONGE_IMPLEMENTATION