aboutsummaryrefslogtreecommitdiff
path: root/src/fb/pixfb.cpp
blob: d49566eb87885fbca54568f28e5478cb64e39bf7 (plain) (blame)
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
#include "fb/pixfb.h"
#include <cstdint>
#include <algorithm>
#include "math/vector.h"
#include "math/quat.h"
#include "o3d/vertex_data.h"
#include "o3d/camera.h"

using namespace engine::fb;
using
    engine::math::Vector3,
    engine::o3d::VertexData,
    engine::o3d::Camera;

PixelFrameBuffer::PixelFrameBuffer(unsigned int w, unsigned int h) {
    resize(w, h);
}

void PixelFrameBuffer::resize(unsigned int w, unsigned int h) {
    this->w = w;
    this->h = h;
    pixels_vector.resize(w * h);
    clear();
}

void PixelFrameBuffer::clear() {
    std::fill(pixels_vector.begin(), pixels_vector.end(), 0x000000FF);
}

extern Camera* camera;

void PixelFrameBuffer::draw_point(int x, int y, const Vector3& loc, const VertexData& vd, const Vector3& normal) {
    (void) loc;
    (void) vd;
    auto u = vd.world_loc - camera->transform.loc;
    float u_len_sq = u.length_squared();
    u = u.normalize();
    float attenuation = Vector3(0.f, 0.f, -1.f).rot(camera->transform.rot).dot(u);
    if (attenuation < .7f) attenuation = 0.f;
    else if (attenuation > .8f) attenuation = 1.f;
    else attenuation = (attenuation - .7f) / .1f;
    float light = -normal.dot(u) / u_len_sq;
    if (light < 0.f) light = 0.f;
    float final_light = .05f + light * attenuation * .8f * .95f;
    if (final_light > 1.f) final_light = 1.f;
    std::uint32_t c = (int) (final_light * 255.f);
    pixels_vector[x + y * w] = c << 24 | c << 16 | c << 8 | 0xff;
}