1
0
mirror of https://github.com/ssloy/tinyraycaster.git synced 2025-09-03 02:42:36 +02:00

render the visibility cone

This commit is contained in:
Dmitry V. Sokolov
2019-02-09 12:52:19 +01:00
parent 9475f8d10d
commit e7a8b510ae
2 changed files with 12 additions and 7 deletions

BIN
doc/005.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

View File

@@ -68,6 +68,7 @@ int main() {
float player_x = 3.456; // player x position
float player_y = 2.345; // player y position
float player_a = 1.523; // player view direction
const float fov = M_PI/3.; // field of view
for (size_t j = 0; j<win_h; j++) { // fill the screen with color gradients
for (size_t i = 0; i<win_w; i++) {
@@ -92,14 +93,18 @@ int main() {
// draw the player on the map
draw_rectangle(framebuffer, win_w, win_h, player_x*rect_w, player_y*rect_h, 5, 5, pack_color(255, 255, 255));
for (float t=0; t<20; t+=.05) {
float cx = player_x + t*cos(player_a);
float cy = player_y + t*sin(player_a);
if (map[int(cx)+int(cy)*map_w]!=' ') break;
for (size_t i=0; i<win_w; i++) { // draw the visibility cone
float angle = player_a-fov/2 + fov*i/float(win_w);
size_t pix_x = cx*rect_w;
size_t pix_y = cy*rect_h;
framebuffer[pix_x + pix_y*win_w] = pack_color(255, 255, 255);
for (float t=0; t<20; t+=.05) {
float cx = player_x + t*cos(angle);
float cy = player_y + t*sin(angle);
if (map[int(cx)+int(cy)*map_w]!=' ') break;
size_t pix_x = cx*rect_w;
size_t pix_y = cy*rect_h;
framebuffer[pix_x + pix_y*win_w] = pack_color(255, 255, 255);
}
}
drop_ppm_image("./out.ppm", framebuffer, win_w, win_h);