aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 71ced79510b994e1f8703d8b5fb6a71bb21b72fe (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
#include <limits.h>
#include <float.h>
#include <stdint.h>
#include <math.h>

#include "vecmath.h"
#include "picture.h"

typedef enum {
    kAmbientLight,
    kPointLight,
    kDirectionalLight,
} LightType;

typedef struct {
    LightType type;
    float intensity;
    Vec3f vec;
} Light;

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

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,
    },
    {
        .center = {0, -5001, 0},
        .r = 5000,
    },
};

Light lights[] = {
    {
        .type = kAmbientLight,
        .intensity = 0.2,
    },
    {
        .type = kPointLight,
        .intensity = 0.6,
        .vec = {2.0, 1.0, 0.0},
    },
    {
        .type = kDirectionalLight,
        .intensity = 0.2,
        .vec = {1.0, 4.0, 4.0}
    },
};


Color gBackgroupColor = {0,0,0};

void init_color() {
    balls[0].color = icolor(0x95e1d3);
    balls[1].color = icolor(0xfce38a);
    balls[2].color = icolor(0xf38181);
    balls[3].color = icolor(0xeaffd0);
    gBackgroupColor = icolor(0xeaeaea);
}

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 ball_surface_color(Ball *ball, Vec3f point) {
    Vec3f norm = vec3f_normalize(vec3f_sub(point, ball->center));
    float amp = 0;
    for (int i = 0; i < sizeof(lights) / sizeof(Light); i++) {
        if (lights[i].type == kAmbientLight) {
            amp += lights[i].intensity;
        } else if (lights[i].type == kPointLight) {
            Vec3f l = vec3f_normalize(vec3f_sub(lights[i].vec, point));
            float prod = vec3f_dot(l, norm);
            if (prod > 0) amp += prod * lights[i].intensity;
        } else if (lights[i].type == kDirectionalLight) {
            Vec3f l = vec3f_normalize(lights[i].vec);
            float prod = vec3f_dot(l, norm);
            if (prod > 0) amp += prod * lights[i].intensity;
        }
    }
    return (Color){
        ball->color.r * amp,
        ball->color.g * amp,
        ball->color.b * amp
    };
}

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]);
        if (t < t_nearest && t < tmax && t > tmin) {
            t_nearest = t;
            nearest_idx = i;
        }
    }
    if (nearest_idx >= 0) {
        Vec3f intersection = vec3f_add(start, vec3f_mul(t_nearest, v));
        return ball_surface_color(&balls[nearest_idx], intersection);
    } else {
        return gBackgroupColor;
    }
}


int main() {
    init_color();
    int img_w = 1280*2;
    int img_h = 720*2;
    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));
        }
    }
    Picture newpic = picture_downscale_2x(pic);
    writeBMP("test.bmp", newpic);
    delete_picture(pic);
    delete_picture(newpic);
    return 0;
}