diff --git a/pics/bluestone.png b/assets/textures/walls/bluestone.png similarity index 100% rename from pics/bluestone.png rename to assets/textures/walls/bluestone.png diff --git a/pics/colorstone.png b/assets/textures/walls/colorstone.png similarity index 100% rename from pics/colorstone.png rename to assets/textures/walls/colorstone.png diff --git a/pics/greystone.png b/assets/textures/walls/greystone.png similarity index 100% rename from pics/greystone.png rename to assets/textures/walls/greystone.png diff --git a/pics/mossy.png b/assets/textures/walls/mossy.png similarity index 100% rename from pics/mossy.png rename to assets/textures/walls/mossy.png diff --git a/pics/purplestone.png b/assets/textures/walls/purplestone.png similarity index 100% rename from pics/purplestone.png rename to assets/textures/walls/purplestone.png diff --git a/pics/redbrick.png b/assets/textures/walls/redbrick.png similarity index 100% rename from pics/redbrick.png rename to assets/textures/walls/redbrick.png diff --git a/pics/wood.png b/assets/textures/walls/wood.png similarity index 100% rename from pics/wood.png rename to assets/textures/walls/wood.png diff --git a/pics/barrel.png b/pics/barrel.png deleted file mode 100755 index eea212d..0000000 Binary files a/pics/barrel.png and /dev/null differ diff --git a/pics/eagle.png b/pics/eagle.png deleted file mode 100755 index f9394d6..0000000 Binary files a/pics/eagle.png and /dev/null differ diff --git a/pics/greenlight.png b/pics/greenlight.png deleted file mode 100755 index 2345e8e..0000000 Binary files a/pics/greenlight.png and /dev/null differ diff --git a/pics/pillar.png b/pics/pillar.png deleted file mode 100755 index f5b4766..0000000 Binary files a/pics/pillar.png and /dev/null differ diff --git a/ray-cast-vectors.fnl b/ray-cast-vectors.fnl deleted file mode 100644 index 802ff9f..0000000 --- a/ray-cast-vectors.fnl +++ /dev/null @@ -1,132 +0,0 @@ -; Much of this is derived from https://lodev.org/cgtutor/raycasting.html -(local pi math.pi) -(love.graphics.setColor 1 1 1) -(love.graphics.setNewFont 30) - -; Define map (0 is empty space, 1 is wall) -(var map [[1 1 1 1 1 ] - [2 0 0 0 4 ] - [2 0 0 0 4 ] - [2 0 0 0 4 ] - [1 3 3 3 1 ]]) - -; This sets the wall texture data -(var walls []) -(var wall-textures (love.filesystem.getDirectoryItems "textures/walls")) - -(each [_ v (ipairs wall-textures)] - (local wall {}) - (tset wall :t (love.graphics.newImage (.. "textures/walls/" v))) - (tset wall :w (love.graphics.getWidth (. wall :t))) - (tset wall :h (love.graphics.getHeight (. wall :t))) - (table.insert walls wall)) - -; Map size -(var map-height (length map)) -(var map-width (length (. map 1))) - -; Player position and direction -; pos{x,y}: position vector of the player -; dir{x,y}: direction vector of the player -; pln{x,y}: camera plane of the player -(var player {:posx 3 :posy 3 - :dirx -1 :diry 0 - :plnx 0 :plny 0.66}) - -; Screen size -(var screen-width 1920) -(var screen-height 1080) -(var texel-width 64) -(var texel-height 64) - -; Function to handle player movement -(fn move-player [move-speed] - (let [new-x (+ player.posx (* (math.cos player.dir) move-speed)) - new-y (+ player.y (* (math.sin player.dir) move-speed))] - ; Check for collisions with walls - (when (= (. map (math.floor new-y) (math.floor new-x)) 0) - (set player.x new-x) - (set player.y new-y)))) - -; Function to handle player rotation -(fn rotate-player [rot] - (local (o-dx o-px) (values player.dirx player.plnx)) - (set player.dirx (- (* player.dirx (math.cos rot)) (* player.diry (math.sin rot)))) - (set player.plnx (- (* player.plnx (math.cos rot)) (* player.plny (math.sin rot)))) - (set player.diry (+ (* o-dx (math.sin rot)) (* player.diry (math.cos rot)))) - (set player.plny (+ (* o-px (math.sin rot)) (* player.plny (math.cos rot)))) -) - -; Draw function for rendering -{:draw (fn love.draw [] - (love.graphics.clear) - - ; For each vertical slice of the screen - (for [i 0 (- screen-width 1)] - ; Setup a bunch of variables - ; Camera Space x-coordinate, kind of the ray position - (local cam-x (- (/ (* 2 i) screen-width) 1)) - ; Ray direction vector values - (local (ray-dir-x ray-dir-y) (values (+ player.dirx (* player.plnx cam-x)) (+ player.diry (* player.plny cam-x)))) - ; Current player position on the map grid - (var (map-x map-y) (values (math.floor player.posx) (math.floor player.posy))) - ; Length of ray from first x/y to the next x/y - (local (delta-dist-x delta-dist-y) (values - (math.sqrt (+ 1 (/ (* ray-dir-y ray-dir-y) (* ray-dir-x ray-dir-x)))) - (math.sqrt (+ 1 (/ (* ray-dir-x ray-dir-x) (* ray-dir-y ray-dir-y)))))) - ; Side hit (n/s or e/w) - (var side nil) - ; Calculate step and distance from player to first x/y-side (as per Love graphics grid) - (var (step-x side-dist-x) (if (< ray-dir-x 0) - (values -1 (* (- player.posx map-x) delta-dist-x)) - (values 1 (* (- player.posx (+ map-x 1)) delta-dist-x)))) - (var (step-y side-dist-y) (if (< ray-dir-y 0) - (values -1 (* (- player.posy map-y) delta-dist-y)) - (values 1 (* (- player.posy (+ map-y 1)) delta-dist-y)))) - ; Shoot ze ray until it hits something - (var wall-hit false) - (while (not wall-hit) - (if (< side-dist-x side-dist-y) - (set (side-dist-x map-x side) (values (+ side-dist-x delta-dist-x) (+ map-x step-x) 0)) - (set (side-dist-y map-y side) (values (+ side-dist-y delta-dist-y) (+ map-y step-y) 1))) - (set wall-hit (if (> (. map map-x map-y) 0) true false))) - ; Set the perpindicular length from the camera plane to the wall - (var ray-length (if (= side 0) (- side-dist-x delta-dist-x) (- side-dist-y delta-dist-y))) - ; Determine pixel-column height - (var line-height (math.floor (/ screen-height ray-length))) - (var (draw-start draw-end) (values (+ (/ (* -1 line-height) 2) (/ screen-height 2)) (+ (/ line-height 2) (/ screen-height 2)))) - (if (< draw-start 0) (set draw-start 0)) - (if (>= draw-end screen-height) (set draw-end (- screen-height 1))) - ; Determine exactly where along the wall the ray hits - (var wall-col (if (= side 0) (+ player.posy (* ray-length ray-dir-y)) (+ player.posx (* ray-length ray-dir-x)))) - (set wall-col (- wall-col (math.floor wall-col))) - ; Select the texture data based on the grid number - (local wall-texture (. walls (. map map-x map-y))) - ; Calculate the part of the texture to paint, and then do so - (var texture-x (math.floor (* wall-col (. wall-texture :w)))) - (if (and (= side 0) (> ray-dir-x 0)) (set texture-x (- (- (. wall-texture :w) texture-x) 1))) - (if (and (= side 1) (< ray-dir-y 0)) (set texture-x (- (- (. wall-texture :w) texture-x) 1))) - (love.graphics.setColor 1 1 1) - (love.graphics.draw (. wall-texture :t) - (love.graphics.newQuad texture-x 0 1 (. wall-texture :h) (. wall-texture :w) (. wall-texture :h)) - i draw-start 0 1 (/ line-height (. wall-texture :h))) - ; Draw simple lines - ; (love.graphics.line i draw-start i draw-end) - (love.graphics.setColor 1 0 0) - (love.graphics.print (.. "player-x: " player.posx ", player-y:" player.posy) 50 300) - (love.graphics.print (.. "map-x: " map-x ", map-y:" map-y ", val:" (. map map-x map-y)) 50 330) - (love.graphics.print (.. "side: " side ", ray-length:" ray-length) 50 370) - (love.graphics.print (.. "step-x: " step-x ", step-y:" step-y) 50 400) - ) - ) - - :update (fn update [dt] - (when (love.keyboard.isDown "j") (rotate-player 0.1)) - (when (love.keyboard.isDown "l") (rotate-player -0.1))) - - :keypressed (fn keypressed [key set-mode] - (when (= key "j") (rotate-player 0.05)) - (when (= key "l") (rotate-player -0.05)) - (when (= key "i") (move-player 1)) - (when (= key "k") (move-player -1)) - (when (= key "x") (love.event.quit)))} diff --git a/ray-cast.fnl b/ray-cast.fnl deleted file mode 100644 index 2b1ff9e..0000000 --- a/ray-cast.fnl +++ /dev/null @@ -1,61 +0,0 @@ -(local pi (math.pi)) - -; Define map (0 is empty space, 1 is wall) -(var map [[1 1 1 1 1] - [1 0 0 0 1] - [1 0 1 0 1] - [1 0 0 0 1] - [1 1 1 1 1]]) - -; Map size -(var map-width (length map)) -(var map-height (length (first map))) - -; Player position and direction -(var player {:x 2.5 :y 2.5 :dir 0 :fov (/ pi 3)}) - -; Screen size -(var screen-width 640) -(var screen-height 480) - -; Ray-casting function -(fn cast-ray [ray-angle] - (local dx (math.cos ray-angle)) - (local dy (math.sin ray-angle)) - - (var distance 0) - (while true - (var ray-x (+ player.x (* dx distance))) - (var ray-y (+ player.y (* dy distance))) - - ; Check if ray hits a wall (1) on the map - (when (or (>= (math.floor ray-x) map-width) - (>= (math.floor ray-y) map-height) - (<= ray-x 0) - (<= ray-y 0) - (= (nth (nth map (math.floor ray-y)) (math.floor ray-x)) 1)) - (return distance)) - - ; Increment distance - (set distance (+ distance 0.01)))) - -; Draw function for rendering -(fn love.draw [] - (love.graphics.clear) - - ; For each vertical slice of the screen - (for [i 0 (- screen-width 1)] - ; Calculate angle of ray relative to player direction - (local ray-angle (+ player.dir - (- (* (/ i screen-width) player.fov) (/ player.fov 2)))) - - ; Cast the ray to find distance to the nearest wall - (local distance (cast-ray ray-angle)) - - ; Calculate height of the wall slice - (local wall-height (math.floor (/ screen-height distance))) - - ; Draw the wall slice (centered vertically) - (love.graphics.line i (/ (- screen-height wall-height) 2) - i (/ (+ screen-height wall-height) 2)))) - diff --git a/old-raycaster.fnl b/reference/chatgpt/old-raycaster.fnl similarity index 100% rename from old-raycaster.fnl rename to reference/chatgpt/old-raycaster.fnl diff --git a/reference/lodev/lodev_floor-casting-example.cpp b/reference/lodev/lodev_floor-casting-example.cpp new file mode 100644 index 0000000..8957c65 --- /dev/null +++ b/reference/lodev/lodev_floor-casting-example.cpp @@ -0,0 +1,57 @@ +//FLOOR CASTING +for(int y = 0; y < h; y++) +{ + // rayDir for leftmost ray (x = 0) and rightmost ray (x = w) + float rayDirX0 = dirX - planeX; + float rayDirY0 = dirY - planeY; + float rayDirX1 = dirX + planeX; + float rayDirY1 = dirY + planeY; + + // Current y position compared to the center of the screen (the horizon) + int p = y - screenHeight / 2; + + // Vertical position of the camera. + float posZ = 0.5 * screenHeight; + + // 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. + float rowDistance = posZ / 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 weight in the inner loop + float floorStepX = rowDistance * (rayDirX1 - rayDirX0) / screenWidth; + float floorStepY = rowDistance * (rayDirY1 - rayDirY0) / screenWidth; + + // 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; + + for(int x = 0; x < screenWidth; ++x) + { + // the cell coord is simply got from the integer parts of floorX and floorY + int cellX = (int)(floorX); + int cellY = (int)(floorY); + + // get the texture coordinate from the fractional part + int tx = (int)(texWidth * (floorX - cellX)) & (texWidth - 1); + int ty = (int)(texHeight * (floorY - cellY)) & (texHeight - 1); + + floorX += floorStepX; + floorY += floorStepY; + + // choose texture and draw the pixel + int floorTexture = 3; + int ceilingTexture = 6; + Uint32 color; + + // floor + color = texture[floorTexture][texWidth * ty + tx]; + color = (color >> 1) & 8355711; // make a bit darker + buffer[y][x] = color; + + //ceiling (symmetrical, at screenHeight - y - 1 instead of y) + color = texture[ceilingTexture][texWidth * ty + tx]; + color = (color >> 1) & 8355711; // make a bit darker + buffer[screenHeight - y - 1][x] = color; + } +} diff --git a/raycaster_flat.cpp b/reference/lodev/raycaster_flat.cpp similarity index 100% rename from raycaster_flat.cpp rename to reference/lodev/raycaster_flat.cpp diff --git a/raycaster_textured.cpp b/reference/lodev/raycaster_textured.cpp similarity index 100% rename from raycaster_textured.cpp rename to reference/lodev/raycaster_textured.cpp diff --git a/min-love2d-fennel-readme.org b/reference/love2d-fennel-project/min-love2d-fennel-readme.org similarity index 100% rename from min-love2d-fennel-readme.org rename to reference/love2d-fennel-project/min-love2d-fennel-readme.org diff --git a/sample-macros.fnl b/reference/love2d-fennel-project/sample-macros.fnl similarity index 100% rename from sample-macros.fnl rename to reference/love2d-fennel-project/sample-macros.fnl diff --git a/notes.md b/reference/notes.md similarity index 100% rename from notes.md rename to reference/notes.md diff --git a/textures/walls/bluestone.png b/textures/walls/bluestone.png deleted file mode 100755 index e3bc499..0000000 Binary files a/textures/walls/bluestone.png and /dev/null differ diff --git a/textures/walls/colorstone.png b/textures/walls/colorstone.png deleted file mode 100755 index 507f12e..0000000 Binary files a/textures/walls/colorstone.png and /dev/null differ diff --git a/textures/walls/greystone.png b/textures/walls/greystone.png deleted file mode 100755 index 18b51dd..0000000 Binary files a/textures/walls/greystone.png and /dev/null differ diff --git a/textures/walls/mossy.png b/textures/walls/mossy.png deleted file mode 100755 index 22ca59f..0000000 Binary files a/textures/walls/mossy.png and /dev/null differ diff --git a/textures/walls/purplestone.png b/textures/walls/purplestone.png deleted file mode 100755 index 425bd71..0000000 Binary files a/textures/walls/purplestone.png and /dev/null differ diff --git a/textures/walls/redbrick.png b/textures/walls/redbrick.png deleted file mode 100755 index 3eb620f..0000000 Binary files a/textures/walls/redbrick.png and /dev/null differ diff --git a/textures/walls/wood.png b/textures/walls/wood.png deleted file mode 100755 index c30c317..0000000 Binary files a/textures/walls/wood.png and /dev/null differ