Author Topic: Sky cylinder  (Read 1362 times)

Mopz

  • Guest
Sky cylinder
« on: August 23, 2016, 06:09:43 PM »
I was asked for a solution to draw fancy skies in combination with raycasting on the naalaa forum. So I wrote these lines of code that creates some lookups for rendering an image on the inside of a cylinder:

Code: [Select]
' Sky cylinder.

set window 32, 32, 640, 360

' Load a sky image. This image can be of any size. The image will be mapped to
' the inside of a cylinder.
load image 1, "sky.png"

' Field of view.
fov# = 90.0

' Create some lookup tables.
w = width(primary)
h = height(primary)
hw = w/2
hh = h/2
unit# = float(hw)/tan(fov*0.5)
angleOffset#[w]
cylHeight[w]
for x = 0 to hw - 1
angleOffset[hw + x] = atan(float(x)/unit)
angleOffset[hw - 1 - x] = -angleOffset[hw + x]
cylHeight[hw + x] = int(float(h)/cos(angleOffset[hw + x]))
cylHeight[hw - 1 - x] = cylHeight[hw + x]
next

' Player's angle.
viewAngle# = 0.0

set redraw off

do
' Increase angle.
viewAngle = viewAngle + 1.0
_WrapAngle viewAngle

' Set alpha to 0, since the alpha channel represents the "fog density"
' when using the raster commands.
set color 255, 255, 255, 0

' Draw.
for x = 0 to w - 1
tileHeight = cylHeight[x]
a# = viewAngle + angleOffset[x]
_WrapAngle a
u# = a/360.0
draw vraster 1, x, hh - tileHeight/2, hh + tileHeight/2, u, 0.0, u, 1.0
next

redraw
wait 16
loop

procedure WrapAngle(&a#)
while a < 0.0; a = a + 360.0; wend
while a > 360.0; a = a - 360.0; wend
endproc

The attachment contains the sourcecode, assets and an executable.

I know that naalaa is not a live language in these neighborhoods, but the only weird thing in the code above, that possibly couldn't be directly translated to other languages, is the 'draw vraster' command. 'draw vraster' and 'draw hraster' simply draws textured vertical and horizontal lines.
« Last Edit: August 23, 2016, 06:13:47 PM by Mopz »

B+

  • Guest
Re: Sky cylinder
« Reply #1 on: August 23, 2016, 09:19:37 PM »
Nice! The trick is to find a sky that starts and ends the same (and doesn't make you dizzy).  ;)


ZXDunny

  • Guest
Re: Sky cylinder
« Reply #2 on: August 23, 2016, 11:32:40 PM »
Nice little demo, thanks. Obligatory SpecBAS port:

Code: [Select]
10 REM Sky Cylinder
20 GRAPHIC NEW s LOAD "sky.png": PALETTE COPY s,0,256 TO 0: gw=GFXw s,gh=GFXh s: WINDOW NEW sky,0,0,gw,gh: WINDOW HIDE sky: WINDOW PUT GRAPHIC s,sky,0,0: WINDOW 0
30 fov=90,hw=SCRw/2,hh=SCRh/2,unit=hw/TAN(fov*.5): DIM ao(SCRw) BASE 0;ch(SCRw) BASE 0
40 FOR x=0 TO hw-1: ao(hw+x)=ATN(x/unit),ao(hw-1-x)=-ao(hw+x),ch(hw+x)=INT(SCRh/COS ao(hw+x)),ch(hw-1-x)=ch(hw+x): NEXT x
50 va=180: DO: va+=1: FOR x=0 TO SCRw-1: th=ch(x),th2=th/2,u=0: INC u,va+ao(x),0 TO 359: WINDOW COPY sky,(u/360)*gw,0,1,gh TO 0,x,hh-th2,1,hh+th2: NEXT x: WAIT SCREEN: LOOP



The textured drawing wasn't much of a problem.