Fixed that crash and cleaned up more variables for some study next. I might also try out my debug macros.
This commit is contained in:
parent
7fb2d5cf26
commit
7a74877849
9 changed files with 162 additions and 92729 deletions
160
raycaster.cpp
160
raycaster.cpp
|
@ -28,78 +28,78 @@ inline uint32_t dumb_lighting(uint32_t pixel, double distance) {
|
|||
|
||||
|
||||
Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int height) :
|
||||
view_texture({(unsigned int)width, (unsigned int)height}),
|
||||
view_sprite(view_texture),
|
||||
$view_texture({(unsigned int)width, (unsigned int)height}),
|
||||
$view_sprite($view_texture),
|
||||
$width(width), $height(height),
|
||||
$window(window),
|
||||
$map(map),
|
||||
spriteOrder(textures.NUM_SPRITES),
|
||||
spriteDistance(textures.NUM_SPRITES),
|
||||
spriteOrder($textures.NUM_SPRITES),
|
||||
spriteDistance($textures.NUM_SPRITES),
|
||||
ZBuffer(width)
|
||||
{
|
||||
$window.setVerticalSyncEnabled(true);
|
||||
view_sprite.setPosition({0, 0});
|
||||
pixels = make_unique<RGBA[]>($width * $height);
|
||||
textures.load_textures();
|
||||
$view_sprite.setPosition({0, 0});
|
||||
$pixels = make_unique<RGBA[]>($width * $height);
|
||||
$textures.load_textures();
|
||||
}
|
||||
|
||||
void Raycaster::set_position(int x, int y) {
|
||||
view_sprite.setPosition({(float)x, (float)y});
|
||||
$view_sprite.setPosition({(float)x, (float)y});
|
||||
}
|
||||
|
||||
void Raycaster::position_camera(float player_x, float player_y) {
|
||||
// x and y start position
|
||||
posX = player_x;
|
||||
posY = player_y;
|
||||
$posX = player_x;
|
||||
$posY = player_y;
|
||||
}
|
||||
|
||||
void Raycaster::draw_pixel_buffer() {
|
||||
view_texture.update((uint8_t *)pixels.get(), {(unsigned int)$width, (unsigned int)$height}, {0, 0});
|
||||
$window.draw(view_sprite);
|
||||
$view_texture.update((uint8_t *)$pixels.get(), {(unsigned int)$width, (unsigned int)$height}, {0, 0});
|
||||
$window.draw($view_sprite);
|
||||
}
|
||||
|
||||
void Raycaster::clear() {
|
||||
std::fill_n(pixels.get(), $width * $height, 0);
|
||||
std::fill_n($pixels.get(), $width * $height, 0);
|
||||
$window.clear();
|
||||
}
|
||||
|
||||
void Raycaster::sprite_casting() {
|
||||
const int textureWidth = textures.TEXTURE_WIDTH;
|
||||
const int textureHeight = textures.TEXTURE_HEIGHT;
|
||||
const int textureWidth = $textures.TEXTURE_WIDTH;
|
||||
const int textureHeight = $textures.TEXTURE_HEIGHT;
|
||||
|
||||
// sort sprites from far to close
|
||||
for(int i = 0; i < textures.NUM_SPRITES; i++) {
|
||||
auto& sprite = textures.get_sprite(i);
|
||||
for(int i = 0; i < $textures.NUM_SPRITES; i++) {
|
||||
auto& sprite = $textures.get_sprite(i);
|
||||
spriteOrder[i] = i;
|
||||
// this is just the distance calculation
|
||||
spriteDistance[i] = ((posX - sprite.x) *
|
||||
(posX - sprite.x) +
|
||||
(posY - sprite.y) *
|
||||
(posY - sprite.y));
|
||||
spriteDistance[i] = (($posX - sprite.x) *
|
||||
($posX - sprite.x) +
|
||||
($posY - sprite.y) *
|
||||
($posY - sprite.y));
|
||||
}
|
||||
|
||||
sort_sprites(spriteOrder, spriteDistance, textures.NUM_SPRITES);
|
||||
sort_sprites(spriteOrder, spriteDistance, $textures.NUM_SPRITES);
|
||||
|
||||
// after sorting the sprites, do the projection
|
||||
for(int i = 0; i < textures.NUM_SPRITES; i++) {
|
||||
for(int i = 0; i < $textures.NUM_SPRITES; i++) {
|
||||
int sprite_index = spriteOrder[i];
|
||||
Sprite& sprite_rec = textures.get_sprite(sprite_index);
|
||||
auto& sprite_texture = textures.get_texture(sprite_rec.texture);
|
||||
Sprite& sprite_rec = $textures.get_sprite(sprite_index);
|
||||
auto& sprite_texture = $textures.get_texture(sprite_rec.texture);
|
||||
|
||||
double spriteX = sprite_rec.x - posX;
|
||||
double spriteY = sprite_rec.y - posY;
|
||||
double spriteX = sprite_rec.x - $posX;
|
||||
double spriteY = sprite_rec.y - $posY;
|
||||
|
||||
//transform sprite with the inverse camera matrix
|
||||
// [ planeX dirX ] -1 [ dirY -dirX ]
|
||||
// [ ] = 1/(planeX*dirY-dirX*planeY) * [ ]
|
||||
// [ planeY dirY ] [ -planeY planeX ]
|
||||
// [ $planeX $dirX ] -1 [ $dirY -$dirX ]
|
||||
// [ ] = 1/($planeX*$dirY-$dirX*$planeY) * [ ]
|
||||
// [ $planeY $dirY ] [ -$planeY $planeX ]
|
||||
|
||||
double invDet = 1.0 / (planeX * dirY - dirX * planeY); // required for correct matrix multiplication
|
||||
double invDet = 1.0 / ($planeX * $dirY - $dirX * $planeY); // required for correct matrix multiplication
|
||||
|
||||
double transformX = invDet * (dirY * spriteX - dirX * spriteY);
|
||||
double transformX = invDet * ($dirY * spriteX - $dirX * spriteY);
|
||||
//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 transformY = invDet * (-$planeY * spriteX + $planeX * spriteY);
|
||||
|
||||
int spriteScreenX = int(($width / 2) * (1 + transformX / transformY));
|
||||
|
||||
|
@ -136,11 +136,13 @@ void Raycaster::sprite_casting() {
|
|||
int texY = ((d * textureHeight) / spriteHeight) / 256;
|
||||
//get current color from the texture
|
||||
// BUG: this crashes sometimes when the math goes out of bounds
|
||||
uint32_t color = sprite_texture[textureWidth * texY + texX];
|
||||
size_t index = textureWidth * texY + texX;
|
||||
if(index < 0 || index >= sprite_texture.size()) continue;
|
||||
uint32_t color = sprite_texture[index];
|
||||
// poor person's transparency, get current color from the texture
|
||||
if((color & 0x00FFFFFF) != 0) {
|
||||
RGBA pixel = color;
|
||||
pixels[pixcoord(stripe, y)] = pixel;
|
||||
$pixels[pixcoord(stripe, y)] = pixel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -149,18 +151,20 @@ void Raycaster::sprite_casting() {
|
|||
}
|
||||
|
||||
void Raycaster::cast_rays() {
|
||||
constexpr static const int textureWidth = $textures.TEXTURE_WIDTH;
|
||||
constexpr static const int textureHeight = $textures.TEXTURE_HEIGHT;
|
||||
double perpWallDist;
|
||||
|
||||
// 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 rayDirX = $dirX + $planeX * cameraX;
|
||||
double rayDirY = $dirY + $planeY * cameraX;
|
||||
|
||||
// which box of the map we're in
|
||||
int mapX = int(posX);
|
||||
int mapY = int(posY);
|
||||
int mapX = int($posX);
|
||||
int mapY = int($posY);
|
||||
|
||||
// length of ray from current pos to next x or y-side
|
||||
double sideDistX;
|
||||
|
@ -178,18 +182,18 @@ void Raycaster::cast_rays() {
|
|||
// calculate step and initial sideDist
|
||||
if(rayDirX < 0) {
|
||||
stepX = -1;
|
||||
sideDistX = (posX - mapX) * deltaDistX;
|
||||
sideDistX = ($posX - mapX) * deltaDistX;
|
||||
} else {
|
||||
stepX = 1;
|
||||
sideDistX = (mapX + 1.0 - posX) * deltaDistX;
|
||||
sideDistX = (mapX + 1.0 - $posX) * deltaDistX;
|
||||
}
|
||||
|
||||
if(rayDirY < 0) {
|
||||
stepY = -1;
|
||||
sideDistY = (posY - mapY) * deltaDistY;
|
||||
sideDistY = ($posY - mapY) * deltaDistY;
|
||||
} else {
|
||||
stepY = 1;
|
||||
sideDistY = (mapY + 1.0 - posY) * deltaDistY;
|
||||
sideDistY = (mapY + 1.0 - $posY) * deltaDistY;
|
||||
}
|
||||
|
||||
// perform DDA
|
||||
|
@ -215,40 +219,40 @@ void Raycaster::cast_rays() {
|
|||
|
||||
int lineHeight = int($height / perpWallDist);
|
||||
|
||||
int drawStart = -lineHeight / 2 + $height / 2 + PITCH;
|
||||
int drawStart = -lineHeight / 2 + $height / 2 + $pitch;
|
||||
if(drawStart < 0) drawStart = 0;
|
||||
|
||||
int drawEnd = lineHeight / 2 + $height / 2 + PITCH;
|
||||
int drawEnd = lineHeight / 2 + $height / 2 + $pitch;
|
||||
if(drawEnd >= $height) drawEnd = $height - 1;
|
||||
|
||||
auto &texture = textures.get_texture($map[mapY][mapX] - 1);
|
||||
auto &texture = $textures.get_texture($map[mapY][mapX] - 1);
|
||||
|
||||
// calculate value of wallX
|
||||
double wallX; // where exactly the wall was hit
|
||||
if(side == 0) {
|
||||
wallX = posY + perpWallDist * rayDirY;
|
||||
wallX = $posY + perpWallDist * rayDirY;
|
||||
} else {
|
||||
wallX = posX + perpWallDist * rayDirX;
|
||||
wallX = $posX + perpWallDist * rayDirX;
|
||||
}
|
||||
wallX -= floor((wallX));
|
||||
|
||||
// x coorindate on the texture
|
||||
int texX = int(wallX * double(textures.TEXTURE_WIDTH));
|
||||
if(side == 0 && rayDirX > 0) texX = textures.TEXTURE_WIDTH - texX - 1;
|
||||
if(side == 1 && rayDirY < 0) texX = textures.TEXTURE_WIDTH - texX - 1;
|
||||
int texX = int(wallX * double(textureWidth));
|
||||
if(side == 0 && rayDirX > 0) texX = textureWidth - texX - 1;
|
||||
if(side == 1 && rayDirY < 0) texX = textureWidth - texX - 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 * textures.TEXTURE_HEIGHT / lineHeight;
|
||||
double step = 1.0 * textureHeight / lineHeight;
|
||||
// Starting texture coordinate
|
||||
double texPos = (drawStart - PITCH - $height / 2 + lineHeight / 2) * step;
|
||||
double texPos = (drawStart - $pitch - $height / 2 + lineHeight / 2) * step;
|
||||
|
||||
for(int y = drawStart; y < drawEnd; y++) {
|
||||
int texY = (int)texPos & (textures.TEXTURE_HEIGHT - 1);
|
||||
int texY = (int)texPos & (textureHeight - 1);
|
||||
texPos += step;
|
||||
RGBA pixel = texture[textures.TEXTURE_HEIGHT * texY + texX];
|
||||
pixels[pixcoord(x, y)] = dumb_lighting(pixel, perpWallDist);
|
||||
RGBA pixel = texture[textureHeight * texY + texX];
|
||||
$pixels[pixcoord(x, y)] = dumb_lighting(pixel, perpWallDist);
|
||||
}
|
||||
|
||||
// SET THE ZBUFFER FOR THE SPRITE CASTING
|
||||
|
@ -257,15 +261,15 @@ void Raycaster::cast_rays() {
|
|||
}
|
||||
|
||||
void Raycaster::draw_ceiling_floor() {
|
||||
const int textureWidth = textures.TEXTURE_WIDTH;
|
||||
const int textureHeight = textures.TEXTURE_HEIGHT;
|
||||
constexpr static const int textureWidth = $textures.TEXTURE_WIDTH;
|
||||
constexpr static const int textureHeight = $textures.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 rayDirX0 = $dirX - $planeX;
|
||||
float rayDirY0 = $dirY - $planeY;
|
||||
float rayDirX1 = $dirX + $planeX;
|
||||
float rayDirY1 = $dirY + $planeY;
|
||||
|
||||
// current y position compared to the horizon
|
||||
int p = y - $height / 2;
|
||||
|
@ -289,8 +293,8 @@ void Raycaster::draw_ceiling_floor() {
|
|||
|
||||
// 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 floorX = $posX + rowDistance * rayDirX0;
|
||||
float floorY = $posY + rowDistance * rayDirY0;
|
||||
|
||||
for(int x = 0; x < $width; ++x) {
|
||||
// the cell coord is simply taken from the int parts of
|
||||
|
@ -311,12 +315,12 @@ void Raycaster::draw_ceiling_floor() {
|
|||
// floorX cellX to find the texture x/y. How?
|
||||
|
||||
// FLOOR
|
||||
color = textures.floor[textureWidth * ty + tx];
|
||||
pixels[pixcoord(x, y)] = color;
|
||||
color = $textures.floor[textureWidth * ty + tx];
|
||||
$pixels[pixcoord(x, y)] = color;
|
||||
|
||||
// CEILING
|
||||
color = textures.ceiling[textureWidth * ty + tx];
|
||||
pixels[pixcoord(x, $height - y - 1)] = color;
|
||||
color = $textures.ceiling[textureWidth * ty + tx];
|
||||
$pixels[pixcoord(x, $height - y - 1)] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -358,22 +362,22 @@ void Raycaster::sort_sprites(std::vector<int>& order, std::vector<double>& dist,
|
|||
|
||||
void Raycaster::run(double speed, int dir) {
|
||||
double speed_and_dir = speed * dir;
|
||||
if(empty_space(int(posX + dirX * speed_and_dir), int(posY))) {
|
||||
posX += dirX * speed_and_dir;
|
||||
if(empty_space(int($posX + $dirX * speed_and_dir), int($posY))) {
|
||||
$posX += $dirX * speed_and_dir;
|
||||
}
|
||||
|
||||
if(empty_space(int(posX), int(posY + dirY * speed_and_dir))) {
|
||||
posY += dirY * speed_and_dir;
|
||||
if(empty_space(int($posX), int($posY + $dirY * speed_and_dir))) {
|
||||
$posY += $dirY * speed_and_dir;
|
||||
}
|
||||
}
|
||||
|
||||
void Raycaster::rotate(double speed, int dir) {
|
||||
double speed_and_dir = speed * dir;
|
||||
double oldDirX = dirX;
|
||||
dirX = dirX * cos(speed_and_dir) - dirY * sin(speed_and_dir);
|
||||
dirY = oldDirX * sin(speed_and_dir) + dirY * cos(speed_and_dir);
|
||||
double oldDirX = $dirX;
|
||||
$dirX = $dirX * cos(speed_and_dir) - $dirY * sin(speed_and_dir);
|
||||
$dirY = oldDirX * sin(speed_and_dir) + $dirY * cos(speed_and_dir);
|
||||
|
||||
double oldPlaneX = planeX;
|
||||
planeX = planeX * cos(speed_and_dir) - planeY * sin(speed_and_dir);
|
||||
planeY = oldPlaneX * sin(speed_and_dir) + planeY * cos(speed_and_dir);
|
||||
double oldPlaneX = $planeX;
|
||||
$planeX = $planeX * cos(speed_and_dir) - $planeY * sin(speed_and_dir);
|
||||
$planeY = oldPlaneX * sin(speed_and_dir) + $planeY * cos(speed_and_dir);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue