Skip to content

Commit

Permalink
first animation
Browse files Browse the repository at this point in the history
  • Loading branch information
ssloy committed Feb 10, 2019
1 parent da22579 commit ff7d714
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
Binary file added doc/007.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/007.mkv
Binary file not shown.
63 changes: 41 additions & 22 deletions tinyraycaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <cmath>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <cstdint>
#include <cassert>
Expand Down Expand Up @@ -35,7 +37,7 @@ void draw_rectangle(std::vector<uint32_t> &img, const size_t img_w, const size_t
for (size_t j=0; j<h; j++) {
size_t cx = x+i;
size_t cy = y+j;
if (cx>=img_w || cy>=img_h) continue; // no need to check negative values, (unsigned variables)
if (cx>=img_w || cy>=img_h) continue; // no need to check for negative values (unsigned variables)
img[cx + cy*img_w] = color;
}
}
Expand Down Expand Up @@ -70,37 +72,54 @@ int main() {
float player_a = 1.523; // player view direction
const float fov = M_PI/3.; // field of view

const size_t ncolors = 10;
std::vector<uint32_t> colors(ncolors);
for (size_t i=0; i<ncolors; i++) {
colors[i] = pack_color(rand()%255, rand()%255, rand()%255);
}

const size_t rect_w = win_w/(map_w*2);
const size_t rect_h = win_h/map_h;
for (size_t j=0; j<map_h; j++) { // draw the map
for (size_t i=0; i<map_w; i++) {
if (map[i+j*map_w]==' ') continue; // skip empty spaces
size_t rect_x = i*rect_w;
size_t rect_y = j*rect_h;
draw_rectangle(framebuffer, win_w, win_h, rect_x, rect_y, rect_w, rect_h, pack_color(0, 255, 255));
for (size_t frame=0; frame<360; frame++) {
std::stringstream ss;
ss << std::setfill('0') << std::setw(5) << frame << ".ppm";
player_a += 2*M_PI/360;

framebuffer = std::vector<uint32_t>(win_w*win_h, pack_color(255, 255, 255)); // clear the screen

for (size_t j=0; j<map_h; j++) { // draw the map
for (size_t i=0; i<map_w; i++) {
if (map[i+j*map_w]==' ') continue; // skip empty spaces
size_t rect_x = i*rect_w;
size_t rect_y = j*rect_h;
size_t icolor = map[i+j*map_w] - '0';
assert(icolor<ncolors);
draw_rectangle(framebuffer, win_w, win_h, rect_x, rect_y, rect_w, rect_h, colors[icolor]);
}
}
}

for (size_t i=0; i<win_w/2; i++) { // draw the visibility cone AND the "3D" view
float angle = player_a-fov/2 + fov*i/float(win_w/2);
for (float t=0; t<20; t+=.05) {
float cx = player_x + t*cos(angle);
float cy = player_y + t*sin(angle);
for (size_t i=0; i<win_w/2; i++) { // draw the visibility cone AND the "3D" view
float angle = player_a-fov/2 + fov*i/float(win_w/2);
for (float t=0; t<20; t+=.01) {
float cx = player_x + t*cos(angle);
float cy = player_y + t*sin(angle);

size_t pix_x = cx*rect_w;
size_t pix_y = cy*rect_h;
framebuffer[pix_x + pix_y*win_w] = pack_color(160, 160, 160); // this draws the visibility cone
size_t pix_x = cx*rect_w;
size_t pix_y = cy*rect_h;
framebuffer[pix_x + pix_y*win_w] = pack_color(160, 160, 160); // this draws the visibility cone

if (map[int(cx)+int(cy)*map_w]!=' ') { // our ray touches a wall, so draw the vertical column to create an illusion of 3D
size_t column_height = win_h/t;
draw_rectangle(framebuffer, win_w, win_h, win_w/2+i, win_h/2-column_height/2, 1, column_height, pack_color(0, 255, 255));
break;
if (map[int(cx)+int(cy)*map_w]!=' ') { // our ray touches a wall, so draw the vertical column to create an illusion of 3D
size_t icolor = map[int(cx)+int(cy)*map_w] - '0';
assert(icolor<ncolors);
size_t column_height = win_h/t;
draw_rectangle(framebuffer, win_w, win_h, win_w/2+i, win_h/2-column_height/2, 1, column_height, colors[icolor]);
break;
}
}
}
drop_ppm_image(ss.str(), framebuffer, win_w, win_h);
}

drop_ppm_image("./out.ppm", framebuffer, win_w, win_h);

return 0;
}

0 comments on commit ff7d714

Please sign in to comment.