mirror of
https://github.com/ssloy/tinyraycaster.git
synced 2025-01-17 05:08:22 +01:00
108 lines
5.2 KiB
C++
108 lines
5.2 KiB
C++
#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);
|
|
}
|
|
}
|
|
|