Converted all the variable names from Lode's tutorial style to the one I use in prep for some review and cleanup.

master
Zed A. Shaw 4 weeks ago
parent 0cbe20af35
commit 51972b0c35
  1. 38
      camera.cpp
  2. 16
      camera.hpp
  3. 6
      gui.cpp
  4. 1
      meson.build
  5. 238
      raycaster.cpp
  6. 14
      raycaster.hpp

@ -5,45 +5,45 @@
Point CameraLOL::plan_move(Raycaster &rayview, int dir, bool strafe) {
t = 0.0;
if(strafe) {
targetX = rayview.$posX + int(-rayview.$dirY * 1.5 * dir);
targetY = rayview.$posY + int(rayview.$dirX * 1.5 * dir);
target_x = rayview.$pos_x + int(-rayview.$dir_y * 1.5 * dir);
target_y = rayview.$pos_y + int(rayview.$dir_x * 1.5 * dir);
} else {
targetX = rayview.$posX + int(rayview.$dirX * 1.5 * dir);
targetY = rayview.$posY + int(rayview.$dirY * 1.5 * dir);
target_x = rayview.$pos_x + int(rayview.$dir_x * 1.5 * dir);
target_y = rayview.$pos_y + int(rayview.$dir_y * 1.5 * dir);
}
return {size_t(targetX), size_t(targetY)};
return {size_t(target_x), size_t(target_y)};
}
void CameraLOL::plan_rotate(Raycaster &rayview, int dir) {
t = 0.0;
double angle_dir = std::numbers::pi * 0.25 * dir;
targetDirX = rayview.$dirX * cos(angle_dir) - rayview.$dirY * sin(angle_dir);
targetDirY = rayview.$dirX * sin(angle_dir) + rayview.$dirY * cos(angle_dir);
target_dir_x = rayview.$dir_x * cos(angle_dir) - rayview.$dir_y * sin(angle_dir);
target_dir_y = rayview.$dir_x * sin(angle_dir) + rayview.$dir_y * cos(angle_dir);
targetPlaneX = rayview.$planeX * cos(angle_dir) - rayview.$planeY * sin(angle_dir);
targetPlaneY = rayview.$planeX * sin(angle_dir) + rayview.$planeY * cos(angle_dir);
target_plane_x = rayview.$plane_x * cos(angle_dir) - rayview.$plane_y * sin(angle_dir);
target_plane_y = rayview.$plane_x * sin(angle_dir) + rayview.$plane_y * cos(angle_dir);
}
bool CameraLOL::play_rotate(Raycaster &rayview) {
t += rotSpeed;
rayview.$dirX = std::lerp(rayview.$dirX, targetDirX, t);
rayview.$dirY = std::lerp(rayview.$dirY, targetDirY, t);
rayview.$planeX = std::lerp(rayview.$planeX, targetPlaneX, t);
rayview.$planeY = std::lerp(rayview.$planeY, targetPlaneY, t);
t += rot_speed;
rayview.$dir_x = std::lerp(rayview.$dir_x, target_dir_x, t);
rayview.$dir_y = std::lerp(rayview.$dir_y, target_dir_y, t);
rayview.$plane_x = std::lerp(rayview.$plane_x, target_plane_x, t);
rayview.$plane_y = std::lerp(rayview.$plane_y, target_plane_y, t);
return t > 1.0;
}
bool CameraLOL::play_move(Raycaster &rayview) {
t += moveSpeed;
rayview.$posX = std::lerp(rayview.$posX, targetX, t);
rayview.$posY = std::lerp(rayview.$posY, targetY, t);
t += move_speed;
rayview.$pos_x = std::lerp(rayview.$pos_x, target_x, t);
rayview.$pos_y = std::lerp(rayview.$pos_y, target_y, t);
return t >= 1.0;
}
void CameraLOL::abort_plan(Raycaster &rayview) {
targetX = rayview.$posX;
targetY = rayview.$posY;
target_x = rayview.$pos_x;
target_y = rayview.$pos_y;
}

@ -3,14 +3,14 @@
struct CameraLOL {
double t = 0.0;
double moveSpeed = 0.1;
double rotSpeed = 0.06;
double targetX = 0.0;
double targetY = 0.0;
double targetDirX = 0.0;
double targetDirY = 0.0;
double targetPlaneX = 0.0;
double targetPlaneY = 0.0;
double move_speed = 0.1;
double rot_speed = 0.06;
double target_x = 0.0;
double target_y = 0.0;
double target_dir_x = 0.0;
double target_dir_y = 0.0;
double target_plane_x = 0.0;
double target_plane_y = 0.0;
Point plan_move(Raycaster &rayview, int dir, bool strafe);
void plan_rotate(Raycaster &rayview, int dir);

@ -135,7 +135,7 @@ namespace gui {
void FSM::MOVING(Event ) {
if($camera.play_move($rayview)) {
System::plan_motion(*$level.world, {size_t($camera.targetX), size_t($camera.targetY)});
System::plan_motion(*$level.world, {size_t($camera.target_x), size_t($camera.target_y)});
run_systems();
state(State::IDLE);
}
@ -272,8 +272,8 @@ namespace gui {
"pos: {:>2.02},{:>2.02}\n\n",
$stats.mean(), $stats.stddev(), $stats.min,
$stats.max, $stats.n, VSYNC,
FRAME_LIMIT, DEBUG_BUILD, $rayview.$dirX,
$rayview.$dirY, $rayview.$posX, $rayview.$posY));
FRAME_LIMIT, DEBUG_BUILD, $rayview.$dir_x,
$rayview.$dir_y, $rayview.$pos_x, $rayview.$pos_y));
$window.draw($text);
}

@ -90,6 +90,7 @@ executable('runtests', sources + [
], override_options: exe_defaults,
dependencies: dependencies + [catch2])
executable('zedcaster',
sources + [ 'main.cpp' ],
cpp_args: cpp_args,

@ -38,7 +38,7 @@ Raycaster::Raycaster(TexturePack &textures, int width, int height) :
$view_texture({(unsigned int)width, (unsigned int)height}),
$view_sprite($view_texture),
$width(width), $height(height),
ZBuffer(width),
$zbuffer(width),
$anim(256, 256, 10, "assets/monster-1.ogg")
{
$view_sprite.setPosition({0, 0});
@ -52,8 +52,8 @@ void Raycaster::set_position(int x, int y) {
void Raycaster::position_camera(float player_x, float player_y) {
// x and y start position
$posX = player_x;
$posY = player_y;
$pos_x = player_x;
$pos_y = player_y;
}
void Raycaster::draw_pixel_buffer() {
@ -61,12 +61,12 @@ void Raycaster::draw_pixel_buffer() {
}
void Raycaster::sprite_casting(sf::RenderTarget &target) {
const int textureWidth = TEXTURE_WIDTH;
const int textureHeight = TEXTURE_HEIGHT;
const int halfHeight = TEXTURE_HEIGHT / 2;
constexpr const int texture_width = TEXTURE_WIDTH;
constexpr const int texture_height = TEXTURE_HEIGHT;
constexpr const int half_height = TEXTURE_HEIGHT / 2;
// sort sprites from far to close
auto sprite_order = $level.collision->distance_sorted({(size_t)$posX, (size_t)$posY});
auto sprite_order = $level.collision->distance_sorted({(size_t)$pos_x, (size_t)$pos_y});
// after sorting the sprites, do the projection
for(auto& rec : sprite_order) {
@ -75,67 +75,67 @@ void Raycaster::sprite_casting(sf::RenderTarget &target) {
auto& sf_sprite = $sprites.at(rec.second).sprite;
auto sprite_pos = $level.world->get<components::Position>(rec.second);
double spriteX = double(sprite_pos.location.x) - $posX + 0.5;
double spriteY = double(sprite_pos.location.y) - $posY + 0.5;
double sprite_x = double(sprite_pos.location.x) - $pos_x + 0.5;
double sprite_y = double(sprite_pos.location.y) - $pos_y + 0.5;
//transform sprite with the inverse camera matrix
// [ $planeX $dirX ] -1 [ $dirY -$dirX ]
// [ ] = 1/($planeX*$dirY-$dirX*$planeY) * [ ]
// [ $planeY $dirY ] [ -$planeY $planeX ]
// [ $plane_x $dir_x ] -1 [ $dir_y -$dir_x ]
// [ ] = 1/($plane_x*$dir_y-$dir_x*$plane_y) * [ ]
// [ $plane_y $dir_y ] [ -$plane_y $plane_x ]
double invDet = 1.0 / ($planeX * $dirY - $dirX * $planeY); // required for correct matrix multiplication
double inv_det = 1.0 / ($plane_x * $dir_y - $dir_x * $plane_y); // required for correct matrix multiplication
double transformX = invDet * ($dirY * spriteX - $dirX * spriteY);
double transform_x = inv_det * ($dir_y * sprite_x - $dir_x * sprite_y);
//this is actually the depth inside the screen, that what Z is in 3D, the distance of sprite to player, matching sqrt(spriteDistance[i])
double transformY = invDet * (-$planeY * spriteX + $planeX * spriteY);
double transform_y = inv_det * (-$plane_y * sprite_x + $plane_x * sprite_y);
int spriteScreenX = int(($width / 2) * (1 + transformX / transformY));
int sprite_screen_x = int(($width / 2) * (1 + transform_x / transform_y));
// calculate the height of the sprite on screen
//using "transformY" instead of the real distance prevents fisheye
int spriteHeight = abs(int($height / transformY));
//using "transform_y" instead of the real distance prevents fisheye
int sprite_height = abs(int($height / transform_y));
// calculate width the the sprite
// same as height of sprite, given that it's square
int spriteWidth = abs(int($height / transformY));
int sprite_width = abs(int($height / transform_y));
int drawStartX = -spriteWidth / 2 + spriteScreenX;
if(drawStartX < 0) drawStartX = 0;
int drawEndX = spriteWidth / 2 + spriteScreenX;
if(drawEndX > $width) drawEndX = $width;
int draw_start_x = -sprite_width / 2 + sprite_screen_x;
if(draw_start_x < 0) draw_start_x = 0;
int draw_end_x = sprite_width / 2 + sprite_screen_x;
if(draw_end_x > $width) draw_end_x = $width;
int stripe = drawStartX;
for(; stripe < drawEndX; stripe++) {
int stripe = draw_start_x;
for(; stripe < draw_end_x; stripe++) {
//the conditions in the if are:
//1) it's in front of camera plane so you don't see things behind you
//2) ZBuffer, with perpendicular distance
if(!(transformY > 0 && transformY < ZBuffer[stripe])) break;
//2) $zbuffer, with perpendicular distance
if(!(transform_y > 0 && transform_y < $zbuffer[stripe])) break;
}
int texX_end = int(textureWidth * (stripe - (-spriteWidth / 2 + spriteScreenX)) * textureWidth / spriteWidth) / textureWidth;
int tex_x_end = int(texture_width * (stripe - (-sprite_width / 2 + sprite_screen_x)) * texture_width / sprite_width) / texture_width;
if(drawStartX < drawEndX && transformY > 0 && transformY < ZBuffer[drawStartX]) {
if(draw_start_x < draw_end_x && transform_y > 0 && transform_y < $zbuffer[draw_start_x]) {
//calculate lowest and highest pixel to fill in current stripe
int drawStartY = -spriteHeight / 2 + $height / 2;
if(drawStartY < 0) drawStartY = 0;
int drawEndY = spriteHeight / 2 + $height / 2;
if(drawEndY >= $height) drawEndY = $height - 1;
int draw_start_y = -sprite_height / 2 + $height / 2;
if(draw_start_y < 0) draw_start_y = 0;
int draw_end_y = sprite_height / 2 + $height / 2;
if(draw_end_y >= $height) draw_end_y = $height - 1;
int texX = int(textureWidth * (drawStartX - (-spriteWidth / 2 + spriteScreenX)) * textureWidth / spriteWidth) / textureWidth;
int texRenderWidth = texX_end - texX;
if(texRenderWidth <= 0) continue;
int tex_x = int(texture_width * (draw_start_x - (-sprite_width / 2 + sprite_screen_x)) * texture_width / sprite_width) / texture_width;
int tex_render_width = tex_x_end - tex_x;
if(tex_render_width <= 0) continue;
float x = float(drawStartX + RAY_VIEW_X);
float y = float(drawStartY + RAY_VIEW_Y);
float sprite_w = float(spriteWidth) / float(textureWidth);
float sprite_h = float(spriteHeight) / float(textureHeight);
float x = float(draw_start_x + RAY_VIEW_X);
float y = float(draw_start_y + RAY_VIEW_Y);
float sprite_w = float(sprite_width) / float(texture_width);
float sprite_h = float(sprite_height) / float(texture_height);
int d = y * textureHeight - $height * halfHeight + spriteHeight * halfHeight;
int texY = ((d * textureHeight) / spriteHeight) / textureHeight;
int d = y * texture_height - $height * half_height + sprite_height * half_height;
int tex_y = ((d * texture_height) / sprite_height) / texture_height;
sf_sprite->setScale({sprite_w, sprite_h});
$anim.step(*sf_sprite, texX, texY, texRenderWidth, textureHeight);
$anim.step(*sf_sprite, tex_x, tex_y, tex_render_width, texture_height);
sf_sprite->setPosition({x, y});
$brightness.setUniform("offsetFactor", sf::Glsl::Vec2{0.0f, 0.0f});
@ -148,125 +148,125 @@ void Raycaster::sprite_casting(sf::RenderTarget &target) {
}
void Raycaster::cast_rays() {
constexpr static const int textureWidth = TEXTURE_WIDTH;
constexpr static const int textureHeight = TEXTURE_HEIGHT;
double perpWallDist;
constexpr static const int texture_width = TEXTURE_WIDTH;
constexpr static const int texture_height = TEXTURE_HEIGHT;
double perp_wall_dist;
// WALL CASTING
for(int x = 0; x < $width; x++) {
// calculate ray position and direction
double cameraX = 2 * x / double($width) - 1; // x-coord in camera space
double rayDirX = $dirX + $planeX * cameraX;
double rayDirY = $dirY + $planeY * cameraX;
double ray_dir_x = $dir_x + $plane_x * cameraX;
double ray_dir_y = $dir_y + $plane_y * cameraX;
// which box of the map we're in
int mapX = int($posX);
int mapY = int($posY);
int map_x = int($pos_x);
int map_y = int($pos_y);
// length of ray from current pos to next x or y-side
double sideDistX;
double sideDistY;
double side_dist_x;
double side_dist_y;
// length of ray from one x or y-side to next x or y-side
double deltaDistX = std::abs(1.0 / rayDirX);
double deltaDistY = std::abs(1.0 / rayDirY);
double delta_dist_x = std::abs(1.0 / ray_dir_x);
double delta_dist_y = std::abs(1.0 / ray_dir_y);
int stepX = 0;
int stepY = 0;
int step_x = 0;
int step_y = 0;
int hit = 0;
int side = 0;
// calculate step and initial sideDist
if(rayDirX < 0) {
stepX = -1;
sideDistX = ($posX - mapX) * deltaDistX;
if(ray_dir_x < 0) {
step_x = -1;
side_dist_x = ($pos_x - map_x) * delta_dist_x;
} else {
stepX = 1;
sideDistX = (mapX + 1.0 - $posX) * deltaDistX;
step_x = 1;
side_dist_x = (map_x + 1.0 - $pos_x) * delta_dist_x;
}
if(rayDirY < 0) {
stepY = -1;
sideDistY = ($posY - mapY) * deltaDistY;
if(ray_dir_y < 0) {
step_y = -1;
side_dist_y = ($pos_y - map_y) * delta_dist_y;
} else {
stepY = 1;
sideDistY = (mapY + 1.0 - $posY) * deltaDistY;
step_y = 1;
side_dist_y = (map_y + 1.0 - $pos_y) * delta_dist_y;
}
// perform DDA
while(hit == 0) {
if(sideDistX < sideDistY) {
sideDistX += deltaDistX;
mapX += stepX;
if(side_dist_x < side_dist_y) {
side_dist_x += delta_dist_x;
map_x += step_x;
side = 0;
} else {
sideDistY += deltaDistY;
mapY += stepY;
side_dist_y += delta_dist_y;
map_y += step_y;
side = 1;
}
if($map[mapY][mapX] > 0) hit = 1;
if($map[map_y][map_x] > 0) hit = 1;
}
if(side == 0) {
perpWallDist = (sideDistX - deltaDistX);
perp_wall_dist = (side_dist_x - delta_dist_x);
} else {
perpWallDist = (sideDistY - deltaDistY);
perp_wall_dist = (side_dist_y - delta_dist_y);
}
int lineHeight = int($height / perpWallDist);
int line_height = int($height / perp_wall_dist);
int drawStart = -lineHeight / 2 + $height / 2 + $pitch;
if(drawStart < 0) drawStart = 0;
int draw_start = -line_height / 2 + $height / 2 + $pitch;
if(draw_start < 0) draw_start = 0;
int drawEnd = lineHeight / 2 + $height / 2 + $pitch;
if(drawEnd >= $height) drawEnd = $height - 1;
int draw_end = line_height / 2 + $height / 2 + $pitch;
if(draw_end >= $height) draw_end = $height - 1;
auto texture = $textures.get_surface($map[mapY][mapX] - 1);
auto texture = $textures.get_surface($map[map_y][map_x] - 1);
// calculate value of wallX
double wallX; // where exactly the wall was hit
// calculate value of wall_x
double wall_x; // where exactly the wall was hit
if(side == 0) {
wallX = $posY + perpWallDist * rayDirY;
wall_x = $pos_y + perp_wall_dist * ray_dir_y;
} else {
wallX = $posX + perpWallDist * rayDirX;
wall_x = $pos_x + perp_wall_dist * ray_dir_x;
}
wallX -= floor((wallX));
wall_x -= floor((wall_x));
// x coorindate on the texture
int texX = int(wallX * double(textureWidth));
if(side == 0 && rayDirX > 0) texX = textureWidth - texX - 1;
if(side == 1 && rayDirY < 0) texX = textureWidth - texX - 1;
int tex_x = int(wall_x * double(texture_width));
if(side == 0 && ray_dir_x > 0) tex_x = texture_width - tex_x - 1;
if(side == 1 && ray_dir_y < 0) tex_x = texture_width - tex_x - 1;
// LODE: an integer-only bresenham or DDA like algorithm could make the texture coordinate stepping faster
// How much to increase the texture coordinate per screen pixel
double step = 1.0 * textureHeight / lineHeight;
double step = 1.0 * texture_height / line_height;
// Starting texture coordinate
double texPos = (drawStart - $pitch - $height / 2 + lineHeight / 2) * step;
double tex_pos = (draw_start - $pitch - $height / 2 + line_height / 2) * step;
for(int y = drawStart; y < drawEnd; y++) {
int texY = (int)texPos & (textureHeight - 1);
texPos += step;
RGBA pixel = texture[textureHeight * texY + texX];
$pixels[pixcoord(x, y)] = dumb_lighting(pixel, perpWallDist);
for(int y = draw_start; y < draw_end; y++) {
int tex_y = (int)tex_pos & (texture_height - 1);
tex_pos += step;
RGBA pixel = texture[texture_height * tex_y + tex_x];
$pixels[pixcoord(x, y)] = dumb_lighting(pixel, perp_wall_dist);
}
// SET THE ZBUFFER FOR THE SPRITE CASTING
ZBuffer[x] = perpWallDist;
$zbuffer[x] = perp_wall_dist;
}
}
void Raycaster::draw_ceiling_floor() {
constexpr static const int textureWidth = TEXTURE_WIDTH;
constexpr static const int textureHeight = TEXTURE_HEIGHT;
constexpr static const int texture_width = TEXTURE_WIDTH;
constexpr static const int texture_height = TEXTURE_HEIGHT;
for(int y = $height / 2 + 1; y < $height; ++y) {
// rayDir for leftmost ray (x=0) and rightmost (x = w)
float rayDirX0 = $dirX - $planeX;
float rayDirY0 = $dirY - $planeY;
float rayDirX1 = $dirX + $planeX;
float rayDirY1 = $dirY + $planeY;
float ray_dir_x0 = $dir_x - $plane_x;
float ray_dir_y0 = $dir_y - $plane_y;
float ray_dir_x1 = $dir_x + $plane_x;
float ray_dir_y1 = $dir_y + $plane_y;
// current y position compared to the horizon
int p = y - $height / 2;
@ -275,53 +275,53 @@ void Raycaster::draw_ceiling_floor() {
// 0.5 will the camera at the center horizon. For a
// different value you need a separate loop for ceiling
// and floor since they're no longer symmetrical.
float posZ = 0.5 * $height;
float pos_z = 0.5 * $height;
// horizontal distance from the camera to the floor for the current row
// 0.5 is the z position exactly in the middle between floor and ceiling
// See NOTE in Lode's code for more.
float rowDistance = posZ / p;
float row_distance = pos_z / p;
// calculate the real world step vector we have to add for each x (parallel to camera plane)
// adding step by step avoids multiplications with a wight in the inner loop
float floorStepX = rowDistance * (rayDirX1 - rayDirX0) / $width;
float floorStepY = rowDistance * (rayDirY1 - rayDirY0) / $width;
float floor_step_x = row_distance * (ray_dir_x1 - ray_dir_x0) / $width;
float floor_step_y = row_distance * (ray_dir_y1 - ray_dir_y0) / $width;
// real world coordinates of the leftmost column.
// This will be updated as we step to the right
float floorX = $posX + rowDistance * rayDirX0;
float floorY = $posY + rowDistance * rayDirY0;
float floor_x = $pos_x + row_distance * ray_dir_x0;
float floor_y = $pos_y + row_distance * ray_dir_y0;
auto floor_texture = (const uint32_t *)$textures.floor.getPixelsPtr();
auto ceiling_texture = (const uint32_t *)$textures.ceiling.getPixelsPtr();
for(int x = 0; x < $width; ++x) {
// the cell coord is simply taken from the int parts of
// floorX and floorY.
int cellX = int(floorX);
int cellY = int(floorY);
// floor_x and floor_y.
int cell_x = int(floor_x);
int cell_y = int(floor_y);
// get the texture coordinate from the fractional part
int tx = int(textureWidth * (floorX - cellX)) & (textureWidth - 1);
int ty = int(textureWidth * (floorY - cellY)) & (textureHeight - 1);
int tx = int(texture_width * (floor_x - cell_x)) & (texture_width - 1);
int ty = int(texture_width * (floor_y - cell_y)) & (texture_height - 1);
floorX += floorStepX;
floorY += floorStepY;
floor_x += floor_step_x;
floor_y += floor_step_y;
double d = std::sqrt(($posX - floorX) * ($posX - floorX) + ($posY - floorY) * ($posY - floorY));
double d = std::sqrt(($pos_x - floor_x) * ($pos_x - floor_x) + ($pos_y - floor_y) * ($pos_y - floor_y));
// now get the pixel from the texture
uint32_t color;
// this uses the previous ty/tx fractional parts of
// floorX cellX to find the texture x/y. How?
// floor_x cell_x to find the texture x/y. How?
// FLOOR
color = floor_texture[textureWidth * ty + tx];
color = floor_texture[texture_width * ty + tx];
$pixels[pixcoord(x, y)] = dumb_lighting(color, d);
// CEILING
color = ceiling_texture[textureWidth * ty + tx];
color = ceiling_texture[texture_width * ty + tx];
$pixels[pixcoord(x, $height - y - 1)] = dumb_lighting(color, d);
}
}

@ -15,16 +15,16 @@ struct Raycaster {
sf::Clock $clock;
sf::Shader $brightness;
TexturePack &$textures;
double $posX = 0;
double $posY = 0;
double $pos_x = 0;
double $pos_y = 0;
// initial direction vector
double $dirX = -1;
double $dirY = 0;
double $dir_x = -1;
double $dir_y = 0;
// the 2d raycaster version of camera plane
double $planeX = 0;
double $planeY = 0.66;
double $plane_x = 0;
double $plane_y = 0.66;
sf::Texture $view_texture;
sf::Sprite $view_sprite;
@ -35,7 +35,7 @@ struct Raycaster {
GameLevel $level;
Matrix $map;
std::unordered_map<DinkyECS::Entity, SpriteTexture> $sprites;
std::vector<double> ZBuffer; // width
std::vector<double> $zbuffer; // width
Animator $anim;
Raycaster(TexturePack &textures, int width, int height);

Loading…
Cancel
Save