Guest User

wolfenstein affine floor texturing

a guest
May 10th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;; so this is code I wrote to draw the floor and ceiling
  2. ;; in an old wolfenstein game I made
  3. ;; I couldn't figure out how to get perspective correct texture
  4. ;; sampling to work, so I ended up throwing this together
  5.  
  6. ;; basically, it walks down from the bottom of the wall on each pixel
  7. ;; column , and draws multiple smaller polygons to distract from the
  8. ;; texture distortion that you get with affine texture sampling.
  9.  
  10. ;; e.g. instead of drawing the floor here as one polygon
  11.  
  12. |------|
  13. | wall |
  14. --------
  15. \ floor|
  16.  \     |
  17.   \    |
  18.    -----
  19.  
  20. ;; it split the vertical space on the screen up into mutiple polygons
  21. |------|
  22. | wall |
  23. --------
  24. \------|
  25.  \-----|
  26.   \----|  
  27.    -----
  28.  
  29. ;; This is also done on a per vertical pixel column basis, so it's fairly slow
  30.  
  31. (expand-samples
  32.  ;; expand samples takes the gl:tex-coord and gl:vertex commands verbatim and splices them into
  33.  ;; a single gl:begin block.  The last chunk passed in is an epilogue.  I
  34.  ;; don't remember why it works that way
  35.  
  36.  
  37.  
  38.  ;;;   loop from bottom of wall on this pixel column down to the bottom of the screen
  39.  ;;; if inc is smaller, the texture sampling will be more accurate, but slower
  40.  (loop for y from start-floor by inc
  41.        for i from 1
  42.     while (<= i (1- floor-samples)) collect
  43.     ;; loop from i until we reach the maximum number of samples allowed
  44.      
  45.     ;; variable bindings (with type declarations)
  46.  
  47.              ;; distance to current pixel?
  48.       (*let ((cur-dist single-float (calc-dist y height))
  49.              ;; i don't remember what the weight was
  50.              (cur-weight single-float (/ cur-dist wall-dist))
  51.              ;; floor position
  52.              (cur-floor-x single-float (floor-texel cur-weight floor-x-wall pos-x))
  53.              (cur-floor-y single-float (floor-texel cur-weight floor-y-wall pos-y))
  54.              ;; affine texture coords
  55.              (cur-tex-x integer (tex-coord cur-floor-x +texture-width+))
  56.              (cur-tex-y integer (tex-coord cur-floor-y +texture-height+)))
  57.            
  58.             (prog2
  59.                 (list
  60.                  ;; floor samples
  61.                  (gl:tex-coord prev-tex-x prev-tex-y)
  62.                  (gl:vertex x prev-pix-y)
  63.                  (gl:tex-coord cur-tex-x cur-tex-y)
  64.                  (gl:vertex x y)
  65.                  ;; ceiling samples
  66.                  (gl:tex-coord prev-tex-x prev-tex-y)
  67.                  (gl:vertex x (- height prev-pix-y))
  68.                  (gl:tex-coord cur-tex-x cur-tex-y)
  69.                  (gl:vertex x (- height y)))
  70.                 ;; move texture positions for next polygon
  71.                 (setf prev-tex-x cur-tex-x
  72.                       prev-tex-y cur-tex-y
  73.                       prev-pix-y y))))
  74.  ;; epilogue
  75.  ;;floor
  76.  ((gl:tex-coord prev-tex-x prev-tex-y)
  77.   (gl:vertex x prev-pix-y)
  78.   (gl:tex-coord near-tex-x near-tex-y)
  79.   (gl:vertex x (- height 1))
  80.   ;; ceiling
  81.   (gl:tex-coord prev-tex-x prev-tex-y)
  82.   (gl:vertex x (- height prev-pix-y))
  83.   (gl:tex-coord near-tex-x near-tex-y)
  84.   (gl:vertex x 1)))
  85.  
  86. ;; even after that, the are some really nasty artifacts at the edges of two floor chunks
  87.                                                /
  88. |------|                                      |
  89. | wall |  ------------------------------------|  side-wall
  90. --------   no-wall, draw floor from horizon   |
  91. \      | <- nasty texture mis-alignment       |
  92.  \     |    on this edge here                 |
  93.   \    |                                       \
  94.    \----                                         \
Advertisement
Add Comment
Please, Sign In to add comment