generated from 32blit/32blit-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite3d.cpp
59 lines (42 loc) · 1.75 KB
/
sprite3d.cpp
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
#include "sprite3d.hpp"
#include "graphics/surface.hpp"
#include "math/constants.hpp"
#include "camera.hpp"
using namespace blit;
void Sprite3D::update(const Camera &cam) {
auto screen_pos = cam.world_to_screen(world_pos, screen_scale, z);
screen_x = screen_pos.x;
screen_y = screen_pos.y;
}
void Sprite3D::render(const Camera &cam) {
int frame = 0;
int transform = 0;
if(rotation_frames) {
// we have sprites for half the rotation, fliped for the other half (-2 for the front/back views)
const int total_frames = rotation_frames + rotation_frames - 2;
Vec3 cam_to_sprite = world_pos - cam.pos;
Vec3 sprite_look = look_dir;
// ignore y
cam_to_sprite.y = 0;
sprite_look.y = 0;
cam_to_sprite.normalize();
sprite_look.normalize();
float ang = std::atan2(cam_to_sprite.x * sprite_look.z - cam_to_sprite.z * sprite_look.x, cam_to_sprite.dot(sprite_look));
ang = (pi * 2.0f) - ang;
int rot = std::round((ang / (pi * 2.0f)) * total_frames);
frame = (rot + 15) % total_frames;
if(frame >= rotation_frames) {
// go back through for the other half
frame = (rotation_frames - 2) - (frame - rotation_frames);
transform = SpriteTransform::HORIZONTAL;
}
}
float fade_start = cam.far * 0.6f;
if(z > fade_start)
screen.alpha = std::max(0, static_cast<int>((1.0f - (z - fade_start) / (cam.far - fade_start)) * 255.0f * alpha));
else
screen.alpha = alpha * 255.0f;
screen.sprites = spritesheet;
screen.sprite(Rect(sheet_x + frame * size_w, sheet_y, size_w, size_h), {screen_x, screen_y}, {origin_x, origin_y}, screen_scale * scale, transform);
screen.alpha = 255;
}