Quick and dirty idea for a poor-man's lighting effect.
This commit is contained in:
parent
d6a2f83f10
commit
6533f950d2
1 changed files with 30 additions and 12 deletions
|
@ -85,8 +85,25 @@ double spriteDistance[numSprites];
|
||||||
|
|
||||||
std::vector<uint32_t> texture[numTextures];
|
std::vector<uint32_t> texture[numTextures];
|
||||||
|
|
||||||
|
union RGBA {
|
||||||
|
struct {
|
||||||
|
uint8_t r;
|
||||||
|
uint8_t g;
|
||||||
|
uint8_t b;
|
||||||
|
uint8_t a;
|
||||||
|
} color;
|
||||||
|
|
||||||
|
uint32_t out;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline void RGBA_brightness(RGBA& pixel, double distance) {
|
||||||
|
pixel.color.r /= distance;
|
||||||
|
pixel.color.g /= distance;
|
||||||
|
pixel.color.b /= distance;
|
||||||
|
}
|
||||||
|
|
||||||
#define pixcoord(X, Y) ((Y) * RAY_VIEW_WIDTH) + (X)
|
#define pixcoord(X, Y) ((Y) * RAY_VIEW_WIDTH) + (X)
|
||||||
uint32_t pixels[RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT] = {0};
|
RGBA pixels[RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT] = {{.out=0}};
|
||||||
|
|
||||||
sf::Texture view_texture;
|
sf::Texture view_texture;
|
||||||
sf::Sprite view_sprite;
|
sf::Sprite view_sprite;
|
||||||
|
@ -166,18 +183,19 @@ void draw_line(sf::RenderWindow &window, Point start, Point end, uint32_t color)
|
||||||
y = y + sy;
|
y = y + sy;
|
||||||
}
|
}
|
||||||
|
|
||||||
pixels[pixcoord(x,y)] = color;
|
pixels[pixcoord(x,y)].out = color;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear(sf::RenderWindow &window) {
|
void clear(sf::RenderWindow &window) {
|
||||||
std::fill_n(pixels, RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT, 0);
|
// std::fill_n((uint32_t *)pixels, RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT, 0);
|
||||||
window.clear();
|
// window.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ray_casting(sf::RenderWindow &window, Matrix& map) {
|
void ray_casting(sf::RenderWindow &window, Matrix& map) {
|
||||||
int w = RAY_VIEW_WIDTH;
|
int w = RAY_VIEW_WIDTH;
|
||||||
int h = RAY_VIEW_HEIGHT;
|
int h = RAY_VIEW_HEIGHT;
|
||||||
|
double perpWallDist;
|
||||||
|
|
||||||
// WALL CASTING
|
// WALL CASTING
|
||||||
for(int x = 0; x < w; x++) {
|
for(int x = 0; x < w; x++) {
|
||||||
|
@ -197,7 +215,6 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) {
|
||||||
// length of ray from one x or y-side to next x or y-side
|
// length of ray from one x or y-side to next x or y-side
|
||||||
double deltaDistX = std::abs(1.0 / rayDirX);
|
double deltaDistX = std::abs(1.0 / rayDirX);
|
||||||
double deltaDistY = std::abs(1.0 / rayDirY);
|
double deltaDistY = std::abs(1.0 / rayDirY);
|
||||||
double perpWallDist;
|
|
||||||
|
|
||||||
int stepX = 0;
|
int stepX = 0;
|
||||||
int stepY = 0;
|
int stepY = 0;
|
||||||
|
@ -276,14 +293,13 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) {
|
||||||
for(int y = drawStart; y < drawEnd; y++) {
|
for(int y = drawStart; y < drawEnd; y++) {
|
||||||
int texY = (int)texPos & (texHeight - 1);
|
int texY = (int)texPos & (texHeight - 1);
|
||||||
texPos += step;
|
texPos += step;
|
||||||
uint32_t color = texture[texNum][texHeight * texY + texX];
|
RGBA pixel{.out=texture[texNum][texHeight * texY + texX]};
|
||||||
|
RGBA_brightness(pixel, perpWallDist);
|
||||||
pixels[pixcoord(x, y)] = color;
|
pixels[pixcoord(x, y)] = pixel;
|
||||||
}
|
}
|
||||||
|
|
||||||
// SET THE ZBUFFER FOR THE SPRITE CASTING
|
// SET THE ZBUFFER FOR THE SPRITE CASTING
|
||||||
ZBuffer[x] = perpWallDist;
|
ZBuffer[x] = perpWallDist;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SPRITE CASTING
|
// SPRITE CASTING
|
||||||
|
@ -357,7 +373,9 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) {
|
||||||
uint32_t color = sprite_texture[texWidth * texY + texX];
|
uint32_t color = sprite_texture[texWidth * texY + texX];
|
||||||
// poor person's transparency, get current color from the texture
|
// poor person's transparency, get current color from the texture
|
||||||
if((color & 0x00FFFFFF) != 0) {
|
if((color & 0x00FFFFFF) != 0) {
|
||||||
pixels[pixcoord(stripe, y)] = color;
|
RGBA pixel{.out=color};
|
||||||
|
RGBA_brightness(pixel, perpWallDist);
|
||||||
|
pixels[pixcoord(stripe, y)] = pixel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -421,11 +439,11 @@ void draw_ceiling_floor(sf::RenderWindow &window) {
|
||||||
|
|
||||||
// FLOOR
|
// FLOOR
|
||||||
color = texture[floorTexture][texWidth * ty + tx];
|
color = texture[floorTexture][texWidth * ty + tx];
|
||||||
pixels[pixcoord(x, y)] = color;
|
pixels[pixcoord(x, y)].out = color;
|
||||||
|
|
||||||
// CEILING
|
// CEILING
|
||||||
color = texture[ceilingTexture][texWidth * ty + tx];
|
color = texture[ceilingTexture][texWidth * ty + tx];
|
||||||
pixels[pixcoord(x, screenHeight - y - 1)] = color;
|
pixels[pixcoord(x, screenHeight - y - 1)].out = color;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue