The sprites now display and everything seems to work.

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

@ -1,25 +1,25 @@
/* /*
Copyright (c) 2004-2020, Lode Vandevenne Copyright (c) 2004-2020, Lode Vandevenne
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <cmath> #include <cmath>
#include <string> #include <string>
@ -30,9 +30,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using namespace QuickCG; using namespace QuickCG;
/* /*
g++ *.cpp -lSDL -O3 -W -Wall -ansi -pedantic g++ *.cpp -lSDL -O3 -W -Wall -ansi -pedantic
g++ *.cpp -lSDL g++ *.cpp -lSDL
*/ */
#define screenWidth 640 #define screenWidth 640
@ -335,7 +335,11 @@ int main(int /*argc*/, char */*argv*/[])
for(int i = 0; i < numSprites; i++) for(int i = 0; i < numSprites; i++)
{ {
spriteOrder[i] = 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); sortSprites(spriteOrder, spriteDistance, numSprites);
@ -359,9 +363,9 @@ int main(int /*argc*/, char */*argv*/[])
int spriteScreenX = int((w / 2) * (1 + transformX / transformY)); int spriteScreenX = int((w / 2) * (1 + transformX / transformY));
//parameters for scaling and moving the sprites //parameters for scaling and moving the sprites
#define uDiv 1 #define uDiv 1
#define vDiv 1 #define vDiv 1
#define vMove 0.0 #define vMove 0.0
int vMoveScreen = int(vMove / transformY); int vMoveScreen = int(vMove / transformY);
//calculate height of the sprite on screen //calculate height of the sprite on screen

@ -15,7 +15,7 @@ using namespace fmt;
#define texHeight 64 // must be power of two #define texHeight 64 // must be power of two
#define numSprites 1 #define numSprites 3
#define numTextures 11 #define numTextures 11
struct Sprite { struct Sprite {
@ -24,6 +24,11 @@ struct Sprite {
int texture; 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_WIDTH=1280;
const int RAY_VIEW_HEIGHT=720; const int RAY_VIEW_HEIGHT=720;
const int RAY_VIEW_X=0; 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 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) #define gray_color(c) rgba_color(c, c, c, 255)
Sprite sprites[numSprites] = { Sprite SPRITE[numSprites] = {
{20.5, 11.5, 8} {3.0, 4.5, 8},
{3.4, 1.95, 9},
{7.34, 5.5, 10}
}; };
double ZBuffer[RAY_VIEW_WIDTH]; double ZBuffer[RAY_VIEW_WIDTH];
@ -186,6 +193,7 @@ 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;
// WALL CASTING
for(int x = 0; x < w; x++) { for(int x = 0; x < w; x++) {
// calculate ray position and direction // calculate ray position and direction
double cameraX = 2 * x / double(w) - 1; // x-coord in camera space 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; 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()) { while(window.isOpen()) {
draw_everything(window); draw_everything(window);
if(KB::isKeyPressed(KB::X)) {
println("player position: {},{}",
posX, posY);
}
if(KB::isKeyPressed(KB::W)) { if(KB::isKeyPressed(KB::W)) {
if(empty_space(int(posX + dirX * moveSpeed), int(posY))) posX += dirX * moveSpeed; if(empty_space(int(posX + dirX * moveSpeed), int(posY))) posX += dirX * moveSpeed;
if(empty_space(int(posX), int(posY + dirY * moveSpeed))) posY += dirY * 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) 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