-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathtinyraycaster.cpp
107 lines (92 loc) · 5.2 KB
/
tinyraycaster.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
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
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <cassert>
#include "utils.h"
#include "tinyraycaster.h"
int wall_x_texcoord(const float hitx, const float hity, const Texture &tex_walls) {
float x = hitx - floor(hitx+.5); // x and y contain (signed) fractional parts of hitx and hity,
float y = hity - floor(hity+.5); // they vary between -0.5 and +0.5, and one of them is supposed to be very close to 0
int tex = x*tex_walls.size;
if (std::abs(y)>std::abs(x)) // we need to determine whether we hit a "vertical" or a "horizontal" wall (w.r.t the map)
tex = y*tex_walls.size;
if (tex<0) // do not forget x_texcoord can be negative, fix that
tex += tex_walls.size;
assert(tex>=0 && tex<(int)tex_walls.size);
return tex;
}
void draw_map(FrameBuffer &fb, const std::vector<Sprite> &sprites, const Texture &tex_walls, const Map &map, const size_t cell_w, const size_t cell_h) {
for (size_t j=0; j<map.h; j++) { // draw the map itself
for (size_t i=0; i<map.w; i++) {
if (map.is_empty(i, j)) continue; // skip empty spaces
size_t rect_x = i*cell_w;
size_t rect_y = j*cell_h;
size_t texid = map.get(i, j);
assert(texid<tex_walls.count);
fb.draw_rectangle(rect_x, rect_y, cell_w, cell_h, tex_walls.get(0, 0, texid)); // the color is taken from the upper left pixel of the texture #texid
}
}
for (size_t i=0; i<sprites.size(); i++) { // show the monsters
fb.draw_rectangle(sprites[i].x*cell_w-3, sprites[i].y*cell_h-3, 6, 6, pack_color(255, 0, 0));
}
}
void draw_sprite(FrameBuffer &fb, const Sprite &sprite, const std::vector<float> &depth_buffer, const Player &player, const Texture &tex_sprites) {
// absolute direction from the player to the sprite (in radians)
float sprite_dir = atan2(sprite.y - player.y, sprite.x - player.x);
while (sprite_dir - player.a > M_PI) sprite_dir -= 2*M_PI; // remove unncesessary periods from the relative direction
while (sprite_dir - player.a < -M_PI) sprite_dir += 2*M_PI;
size_t sprite_screen_size = std::min(2000, static_cast<int>(fb.h/sprite.player_dist)); // screen sprite size
int h_offset = (sprite_dir - player.a)*(fb.w/2)/(player.fov) + (fb.w/2)/2 - sprite_screen_size/2; // do not forget the 3D view takes only a half of the framebuffer, thus fb.w/2 for the screen width
int v_offset = fb.h/2 - sprite_screen_size/2;
for (size_t i=0; i<sprite_screen_size; i++) {
if (h_offset+int(i)<0 || h_offset+i>=fb.w/2) continue;
if (depth_buffer[h_offset+i]<sprite.player_dist) continue; // this sprite column is occluded
for (size_t j=0; j<sprite_screen_size; j++) {
if (v_offset+int(j)<0 || v_offset+j>=fb.h) continue;
uint32_t color = tex_sprites.get(i*tex_sprites.size/sprite_screen_size, j*tex_sprites.size/sprite_screen_size, sprite.tex_id);
uint8_t r,g,b,a;
unpack_color(color, r, g, b, a);
if (a>128)
fb.set_pixel(fb.w/2 + h_offset+i, v_offset+j, color);
}
}
}
void render(FrameBuffer &fb, const GameState &gs) {
const Map &map = gs.map;
const Player &player = gs.player;
const std::vector<Sprite> &sprites = gs.monsters;
const Texture &tex_walls = gs.tex_walls;
const Texture &tex_monst = gs.tex_monst;
fb.clear(pack_color(255, 255, 255)); // clear the screen
const size_t cell_w = fb.w/(map.w*2); // size of one map cell on the screen
const size_t cell_h = fb.h/map.h;
std::vector<float> depth_buffer(fb.w/2, 1e3);
for (size_t i=0; i<fb.w/2; i++) { // draw the visibility cone AND the "3D" view
float angle = player.a-player.fov/2 + player.fov*i/float(fb.w/2);
for (float t=0; t<20; t+=.01) { // ray marching loop
float x = player.x + t*cos(angle);
float y = player.y + t*sin(angle);
fb.set_pixel(x*cell_w, y*cell_h, pack_color(190, 190, 190)); // this draws the visibility cone
if (map.is_empty(x, y)) continue;
size_t texid = map.get(x, y); // our ray touches a wall, so draw the vertical column to create an illusion of 3D
assert(texid<tex_walls.count);
float dist = t*cos(angle-player.a);
depth_buffer[i] = dist;
size_t column_height = std::min(2000, int(fb.h/dist));
int x_texcoord = wall_x_texcoord(x, y, tex_walls);
std::vector<uint32_t> column = tex_walls.get_scaled_column(texid, x_texcoord, column_height);
int pix_x = i + fb.w/2; // we are drawing at the right half of the screen, thus +fb.w/2
for (size_t j=0; j<column_height; j++) { // copy the texture column to the framebuffer
int pix_y = j + fb.h/2 - column_height/2;
if (pix_y>=0 && pix_y<(int)fb.h) {
fb.set_pixel(pix_x, pix_y, column[j]);
}
}
break;
} // ray marching loop
} // field of view ray sweeping
draw_map(fb, sprites, tex_walls, map, cell_w, cell_h);
for (size_t i=0; i<sprites.size(); i++) { // draw the sprites
draw_sprite(fb, sprites[i], depth_buffer, player, tex_monst);
}
}