Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; so this is code I wrote to draw the floor and ceiling
- ;; in an old wolfenstein game I made
- ;; I couldn't figure out how to get perspective correct texture
- ;; sampling to work, so I ended up throwing this together
- ;; basically, it walks down from the bottom of the wall on each pixel
- ;; column , and draws multiple smaller polygons to distract from the
- ;; texture distortion that you get with affine texture sampling.
- ;; e.g. instead of drawing the floor here as one polygon
- |------|
- | wall |
- --------
- \ floor|
- \ |
- \ |
- -----
- ;; it split the vertical space on the screen up into mutiple polygons
- |------|
- | wall |
- --------
- \------|
- \-----|
- \----|
- -----
- ;; This is also done on a per vertical pixel column basis, so it's fairly slow
- (expand-samples
- ;; expand samples takes the gl:tex-coord and gl:vertex commands verbatim and splices them into
- ;; a single gl:begin block. The last chunk passed in is an epilogue. I
- ;; don't remember why it works that way
- ;;; loop from bottom of wall on this pixel column down to the bottom of the screen
- ;;; if inc is smaller, the texture sampling will be more accurate, but slower
- (loop for y from start-floor by inc
- for i from 1
- while (<= i (1- floor-samples)) collect
- ;; loop from i until we reach the maximum number of samples allowed
- ;; variable bindings (with type declarations)
- ;; distance to current pixel?
- (*let ((cur-dist single-float (calc-dist y height))
- ;; i don't remember what the weight was
- (cur-weight single-float (/ cur-dist wall-dist))
- ;; floor position
- (cur-floor-x single-float (floor-texel cur-weight floor-x-wall pos-x))
- (cur-floor-y single-float (floor-texel cur-weight floor-y-wall pos-y))
- ;; affine texture coords
- (cur-tex-x integer (tex-coord cur-floor-x +texture-width+))
- (cur-tex-y integer (tex-coord cur-floor-y +texture-height+)))
- (prog2
- (list
- ;; floor samples
- (gl:tex-coord prev-tex-x prev-tex-y)
- (gl:vertex x prev-pix-y)
- (gl:tex-coord cur-tex-x cur-tex-y)
- (gl:vertex x y)
- ;; ceiling samples
- (gl:tex-coord prev-tex-x prev-tex-y)
- (gl:vertex x (- height prev-pix-y))
- (gl:tex-coord cur-tex-x cur-tex-y)
- (gl:vertex x (- height y)))
- ;; move texture positions for next polygon
- (setf prev-tex-x cur-tex-x
- prev-tex-y cur-tex-y
- prev-pix-y y))))
- ;; epilogue
- ;;floor
- ((gl:tex-coord prev-tex-x prev-tex-y)
- (gl:vertex x prev-pix-y)
- (gl:tex-coord near-tex-x near-tex-y)
- (gl:vertex x (- height 1))
- ;; ceiling
- (gl:tex-coord prev-tex-x prev-tex-y)
- (gl:vertex x (- height prev-pix-y))
- (gl:tex-coord near-tex-x near-tex-y)
- (gl:vertex x 1)))
- ;; even after that, the are some really nasty artifacts at the edges of two floor chunks
- /
- |------| |
- | wall | ------------------------------------| side-wall
- -------- no-wall, draw floor from horizon |
- \ | <- nasty texture mis-alignment |
- \ | on this edge here |
- \ | \
- \---- \
Advertisement
Add Comment
Please, Sign In to add comment