The sprites now display and everything seems to work.

master
Zed A. Shaw 2 months ago
parent 93b7faa369
commit e3596aeaa2
  1. 6
      scratchpad/raycaster_sprites.cpp
  2. 113
      sfmlcaster.cpp

@ -335,7 +335,11 @@ int main(int /*argc*/, char */*argv*/[])
for(int i = 0; i < numSprites; i++)
{
spriteOrder[i] = i;
spriteDistance[i] = ((posX - sprite[i].x) * (posX - sprite[i].x) + (posY - sprite[i].y) * (posY - sprite[i].y)); //sqrt not taken, unneeded
spriteDistance[i] =
((posX - sprite[i].x) *
(posX - sprite[i].x) +
(posY - sprite[i].y) *
(posY - sprite[i].y)); //sqrt not taken, unneeded
}
sortSprites(spriteOrder, spriteDistance, numSprites);

@ -15,7 +15,7 @@ using namespace fmt;
#define texHeight 64 // must be power of two
#define numSprites 1
#define numSprites 3
#define numTextures 11
struct Sprite {
@ -24,6 +24,11 @@ struct Sprite {
int texture;
};
//parameters for scaling and moving the sprites
#define uDiv 1
#define vDiv 1
#define vMove 0.0
const int RAY_VIEW_WIDTH=1280;
const int RAY_VIEW_HEIGHT=720;
const int RAY_VIEW_X=0;
@ -69,8 +74,10 @@ double planeY = 0.66;
#define rgba_color(r,g,b,a) (r<<(0*8))|(g<<(1*8))|(b<<(2*8))|(a<<(3*8))
#define gray_color(c) rgba_color(c, c, c, 255)
Sprite sprites[numSprites] = {
{20.5, 11.5, 8}
Sprite SPRITE[numSprites] = {
{3.0, 4.5, 8},
{3.4, 1.95, 9},
{7.34, 5.5, 10}
};
double ZBuffer[RAY_VIEW_WIDTH];
@ -186,6 +193,7 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) {
int w = RAY_VIEW_WIDTH;
int h = RAY_VIEW_HEIGHT;
// WALL CASTING
for(int x = 0; x < w; x++) {
// calculate ray position and direction
double cameraX = 2 * x / double(w) - 1; // x-coord in camera space
@ -288,6 +296,87 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) {
pixels[pixcoord(x, y)] = color;
}
// SET THE ZBUFFER FOR THE SPRITE CASTING
ZBuffer[x] = perpWallDist;
}
// SPRITE CASTING
// sort sprites from far to close
for(int i = 0; i < numSprites; i++) {
spriteOrder[i] = i;
// this is just the distance calculation
spriteDistance[i] = ((posX - SPRITE[i].x) *
(posX - SPRITE[i].x) +
(posY - SPRITE[i].y) *
(posY - SPRITE[i].y));
}
sortSprites(spriteOrder, spriteDistance, numSprites);
// after sorting the sprites, do the projection
for(int i = 0; i < numSprites; i++) {
int sprite_index = spriteOrder[i];
double spriteX = SPRITE[sprite_index].x - posX;
double spriteY = SPRITE[sprite_index].y - posY;
int sprite_texture_number = SPRITE[sprite_index].texture;
auto sprite_texture = texture[sprite_texture_number];
//transform sprite with the inverse camera matrix
// [ 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 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);
int spriteScreenX = int((w / 2) * (1 + transformX / transformY));
int vMoveScreen = int(vMove / transformY);
// calculate the height of the sprite on screen
//using "transformY" instead of the real distance prevents fisheye
int spriteHeight = abs(int(h / transformY)) / vDiv;
//calculate lowest and highest pixel to fill in current stripe
int drawStartY = -spriteHeight / 2 + h / 2 + vMoveScreen;
if(drawStartY < 0) drawStartY = 0;
int drawEndY = spriteHeight / 2 + h / 2 + vMoveScreen;
if(drawEndY >= h) drawEndY = h - 1;
// calculate width the the sprite
// same as height of sprite, given that it's square
int spriteWidth = abs(int(h / transformY)) / uDiv;
int drawStartX = -spriteWidth / 2 + spriteScreenX;
if(drawStartX < 0) drawStartX = 0;
int drawEndX = spriteWidth / 2 + spriteScreenX;
if(drawEndX > w) drawEndX = w;
//loop through every vertical stripe of the sprite on screen
for(int stripe = drawStartX; stripe < drawEndX; stripe++) {
int texX = int(256 * (stripe - (-spriteWidth / 2 + spriteScreenX)) * texWidth / spriteWidth) / 256;
// the conditions in the if are:
// 1) it's in front of the camera plane so you don't see things behind you
// 2) ZBuffer, with perpendicular distance
if(transformY > 0 && transformY < ZBuffer[stripe]) {
for(int y = drawStartY; y < drawEndY; y++) {
//256 and 128 factors to avoid floats
int d = (y - vMoveScreen) * 256 - h * 128 + spriteHeight * 128;
int texY = ((d * texHeight) / spriteHeight) / 256;
//get current color from the texture
uint32_t color = sprite_texture[texWidth * texY + texX];
// poor person's transparency, get current color from the texture
if((color & 0x00FFFFFF) != 0) {
pixels[pixcoord(stripe, y)] = color;
}
}
}
}
}
}
@ -391,6 +480,11 @@ int main() {
while(window.isOpen()) {
draw_everything(window);
if(KB::isKeyPressed(KB::X)) {
println("player position: {},{}",
posX, posY);
}
if(KB::isKeyPressed(KB::W)) {
if(empty_space(int(posX + dirX * moveSpeed), int(posY))) posX += dirX * moveSpeed;
if(empty_space(int(posX), int(posY + dirY * moveSpeed))) posY += dirY * moveSpeed;
@ -439,5 +533,18 @@ int main() {
void sortSprites(int* order, double* dist, int amount)
{
std::vector<std::pair<double, int>> sprites(amount);
for(int i = 0; i < amount; i++) {
sprites[i].first = dist[i];
sprites[i].second = order[i];
}
std::sort(sprites.begin(), sprites.end());
// restore in reverse order
for(int i = 0; i < amount; i++) {
dist[i] = sprites[amount - i - 1].first;
order[i] = sprites[amount - i - 1].second;
}
}

Loading…
Cancel
Save