aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: b25c21236ed45c30d57fe3d17ac9c358e70c7c2f (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
#include <limits.h>
#include <float.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>

typedef enum {
    kRetSucc,
    kRetFail,
} Ret;

typedef struct {
    int width;
    int height;
    float *buffer;
} Picture;

typedef struct {
    float r;
    float g;
    float b;
} Color;

typedef struct {
    int x;
    int y;
} Vec2i;


Picture new_picture(int width, int height) {
    Picture ret;
    ret.width = width;
    ret.height = height;
    ret.buffer = malloc(sizeof(float) * width * height * 3);
    return ret;
}

void delete_picture(Picture pic) {
    free(pic.buffer);
}

void set_pixel(Picture pic, Vec2i pos, Color c) {
    int idx = pos.y * pic.width * 3 + pos.x * 3;
    pic.buffer[idx] = c.b;
    pic.buffer[idx+1] = c.g;
    pic.buffer[idx+2] = c.r;
}

Ret fwrite_word16le(FILE* fp, uint16_t x) {
    uint8_t buf[2];
    buf[0] = x & 0xff;
    buf[1] = (x >> 8) & 0xff;
    int r = fwrite(buf, 1, 2, fp);
    if (r != 2) return kRetFail;
    return kRetSucc;
}

Ret fwrite_word32le(FILE* fp, uint32_t x) {
    Ret ret;
    uint16_t buf[2];

    buf[0] = x & 0xffff;
    buf[1] = (x >> 16) & 0xffff;
    ret = fwrite_word16le(fp, buf[0]);
    if (ret != kRetSucc) return ret;
    ret = fwrite_word16le(fp, buf[1]);
    if (ret != kRetSucc) return ret;
    return kRetSucc;
}


Ret writeBMP(const char* filename, Picture pic) {
    Ret ret = kRetFail;
    FILE *fp = NULL;
    uint8_t *databuf = NULL;

    fp = fopen(filename, "wb");
    if (!fp) goto end;

    int row_data_len = 3 * pic.width;
    int padding = (4 - row_data_len % 4) % 4;
    int row_len = row_data_len + padding;
    int img_len = row_len * pic.height;
    int file_len = 53 + img_len;

    ret = fwrite_word16le(fp, 0x4d42);
    if (ret != kRetSucc) goto end;
    ret = fwrite_word32le(fp, file_len);
    if (ret != kRetSucc) goto end;
    ret = fwrite_word32le(fp, 0);
    if (ret != kRetSucc) goto end;
    ret = fwrite_word32le(fp, 54);
    if (ret != kRetSucc) goto end;

    ret = fwrite_word32le(fp, 40);
    if (ret != kRetSucc) goto end;
    ret = fwrite_word32le(fp, pic.width);
    if (ret != kRetSucc) goto end;
    ret = fwrite_word32le(fp, -pic.height);
    if (ret != kRetSucc) goto end;
    ret = fwrite_word16le(fp, 1);
    if (ret != kRetSucc) goto end;
    ret = fwrite_word16le(fp, 24);
    if (ret != kRetSucc) goto end;
    ret = fwrite_word32le(fp, 0); // BI_RGB
    if (ret != kRetSucc) goto end;
    ret = fwrite_word32le(fp, img_len);
    if (ret != kRetSucc) goto end;
    ret = fwrite_word32le(fp, 0);
    if (ret != kRetSucc) goto end;
    ret = fwrite_word32le(fp, 0);
    if (ret != kRetSucc) goto end;
    ret = fwrite_word32le(fp, 0);
    if (ret != kRetSucc) goto end;
    ret = fwrite_word32le(fp, 0);
    if (ret != kRetSucc) goto end;

    databuf = malloc(img_len);
    memset(databuf, 0, img_len);
    for (int i = 0; i < pic.height; i++) {
        for (int j = 0; j < pic.width * 3; j++) {
            int fromidx = i * pic.width * 3 + j;
            int toidx = i * row_len + j;
            float value = pic.buffer[fromidx];
            if (value < 0) databuf[toidx] = 0;
            else if (value > 1.0) {
                databuf[toidx] = 255;
            } else databuf[toidx] = 255 * value;
        }
    }
    if (fwrite(databuf, 1, img_len, fp) != img_len) {
        ret = kRetFail;
        goto end;
    }
    ret = kRetSucc;

end:
    if (databuf != NULL) free(databuf);
    if (fp != NULL) fclose(fp);
    return ret;
}

typedef struct {
    float x;
    float y;
    float z;
} Vec3f;

Vec3f screen_proj(int width, int height, Vec2i sp) {
    Vec3f ret;
    float maxlen = width > height ? width : height; 
    ret.z = 1;
    ret.x = (sp.x - width / 2.0) / maxlen;
    ret.y = (-sp.y + height / 2.0) / maxlen;
    return ret;
}

Color icolor(int r, int g, int b) {
    return (Color){.r = r/255.0, .g = g/255.0, .b = b/255.0};
}

typedef struct {
    Vec3f center;
    float r;
    Color color;
} Ball;

Ball balls[] = {
    {
        .center = {.x = 0, .y = -1, .z = 3},
        .r = 1,
    },
    {
        .center = {.x = 2, .y = 0, .z = 4},
        .r = 1,
    },
    {
        .center = {.x = -2, .y = 0, .z = 4},
        .r = 1,
    },
};


Color gBackgroupColor = {0,0,0};

void init_color() {
    balls[0].color = icolor(0x95, 0xe1, 0xe3);
    balls[1].color = icolor(0xfc, 0xe3, 0x8a);
    balls[2].color = icolor(0xf3, 0x81, 0x81);
    gBackgroupColor = icolor(0xea, 0xea, 0xea);
}

Vec3f vec3f_sub(Vec3f lhs, Vec3f rhs) {
    return (Vec3f){
        .x = lhs.x - rhs.x,
        .y = lhs.y - rhs.y,
        .z = lhs.z - rhs.z,
    };
}

float vec3f_dot(Vec3f lhs, Vec3f rhs) {
    return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
}

float ball_intersect(Vec3f start, Vec3f ray, Ball *ball) {
    Vec3f sc = vec3f_sub(start, ball->center);
    float a = vec3f_dot(ray, ray);
    float b = 2 * vec3f_dot(sc, ray);
    float c = vec3f_dot(sc, sc) - ball->r * ball->r;
    float delta = b*b - 4*a*c;
    if (delta < 0) {
        return -1;
    }
    float t1 = (-b + sqrt(delta)) / 2*a;
    float t2 = (-b - sqrt(delta)) / 2*a;
    return t1 < t2 ? t1 : t2;
}

Color calc_color(Vec3f v) {
    Vec3f start = {.x = 0, .y = 0, .z = 0};
    float tmin = 0.1;
    float tmax = FLT_MAX;

    int nearest_idx = -1;
    float t_nearest = FLT_MAX;
    for (int i = 0; i < sizeof(balls) / sizeof(Ball); i++) {
        float t = ball_intersect(start, v, &balls[i]);
        // printf("t:%f\n", t);
        if (t < t_nearest && t < tmax && t > tmin) {
            t_nearest = t;
            nearest_idx = i;
        }
    }
    if (nearest_idx >= 0) {
        return balls[nearest_idx].color;
    } else {
        return gBackgroupColor;
    }
}

void normalize_picture(Picture pic) {
    float maxval = 0;
    for (size_t i = 0; i < pic.width * pic.height * 3; i++) {
        float val = pic.buffer[i];
        if (val < 0) val = 0;
        if (val > maxval) maxval = val;
    }
    if (maxval < 1.0) return;
    for (size_t i = 0; i < pic.width * pic.height * 3; i++) {
        pic.buffer[i] = pic.buffer[i] / maxval;
    }
}

int main() {
    init_color();
    int img_w = 1280;
    int img_h = 720;
    Picture pic = new_picture(img_w, img_h);
    for (int x = 0; x < img_w; x++) {
        for (int y = 0; y < img_h; y++) {
            Vec2i screen_pos = {x, y};
            Vec3f v = screen_proj(img_w, img_h, screen_pos);
            set_pixel(pic, screen_pos, calc_color(v));
        }
    }
    writeBMP("test.bmp", pic);
    delete_picture(pic);
    return 0;
}