SpecBAS: Voxel landscape

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
ZXDunny
Berk
Posts: 23
Joined: Tue Nov 14, 2017 3:45 pm

SpecBAS: Voxel landscape

Post by ZXDunny » Sat Dec 02, 2017 6:17 pm

Here's my latest adventure in coding in BASIC:



And here's the code - you'll need a bleeding edge build from the SVN to run it due to some new commands and a couple of bugfixes that surfaced while making it:

Code: Select all

10 REM Voxel landscape. Try changing rz for accuracy/speed tradeoff :)
20 GRAPHIC new gfx load "demos:3d/C1W.png"
30 PALETTE COPY gfx,0,255 TO 0
40 c$=peek$(gfx,0,1024*1024): cofs=straddr c$
50 graphic load gfx,"demos:3d/D1.png"
60 h$=peek$(gfx,0,1024*1024): hofs=straddr h$
70 xp=512,yp=512,a=0,hg=150,hzn=-50,sf=scrh/2,dist=256,rz=2,rzm1=rz-1
80 degrees: ink 0:paper 252: cls: screen lock
90 sina=sin a,cosa=cos a,dz=1,z=20: y$=FCHR$ SCRH*SCRW,yofs=straddr y$,a+=359
100 mo=(xp & 1023)+((yp & 1023)*1024),h=memrd(hofs+mo)+hg
110 do while z<dist:
    cz=cosa*z,sz=sina*z,
    plx=-cz-sz,
    ply=sz-cz,
    prx=cz-sz,
    pry=-sz-cz,
    dx=((prx-plx)/scrw)*rz,
    dy=((pry-ply)/scrw)*rz,
    plx+=xp,ply+=yp,
    iz=1/z*sf:
    yo=yofs,ly=h:
    for i=0 to scrw step rz:
       mo=(plx & 1023)+((ply & 1023)*1024),
       hs=(h-memrd(hofs+mo))*iz+hzn,
       y=Fmemrd yo:
       if hs<=y then
          rectangle ink memrd(cofs+mo);i,hs to i+rzm1,y fill:
          memwrtF yo,hs
120       plx+=dx,ply+=dy,yo+=8:
    next i:
    z+=dz,
    dz+=rz/100:
 loop:
 if inkey$="" Then
 wait screen: cls:
 yp+=1022,xp+=1021:
 go to 90
Didn't think SpecBAS was up to this, but it is :)
1 x

User avatar
R-Tape
Site Admin
Posts: 251
Joined: Thu Nov 09, 2017 11:46 am

Re: SpecBAS: Voxel landscape

Post by R-Tape » Sat Dec 02, 2017 9:09 pm

12 lines of code, one of which is a REM.

:shock:
0 x

User avatar
ZXDunny
Berk
Posts: 23
Joined: Tue Nov 14, 2017 3:45 pm

Re: SpecBAS: Voxel landscape

Post by ZXDunny » Sat Dec 02, 2017 9:34 pm

R-Tape wrote:
Sat Dec 02, 2017 9:09 pm
12 lines of code, one of which is a REM.

:shock:
To be honest, line 120 is only there because I couldn't be arsed to use an END IF. There's no practical limit (well, 2GB of characters and it might get weird to edit) to line length in SpecBAS so it can actually be done in 3 lines. What I need is more optimisation, either in the program itself or within SpecBAS.
0 x

Joefish
Berk
Posts: 49
Joined: Tue Nov 14, 2017 10:26 am

Re: SpecBAS: Voxel landscape

Post by Joefish » Sat Dec 02, 2017 9:36 pm

I'm guessing the .PNG takes up a few more bytes than the code though...

Pretty amazing stuff.
0 x

User avatar
ZXDunny
Berk
Posts: 23
Joined: Tue Nov 14, 2017 3:45 pm

Re: SpecBAS: Voxel landscape

Post by ZXDunny » Sat Dec 02, 2017 9:43 pm

Yeah, the PNGs (one for the colour data and one for the heightmap) are both 1024x1024. They're loaded into a graphics bank and then PEEK$ transfers the bank's contents to a string.

cofs is the memory address of the colour data's string (c$) and hofs is the corresponding heightmap string's first character (h$). These addresses are read from to get the right data for the lines drawn on-screen. So that's 2MB of RAM gone straight away. Then there's the y-buffer (y$) which is the size of the screen in bytes too - even when scaled to 320x8, it's still a chunk of memory...

Not to mention all the temporary storage vars used to cut down on calculations in the main loop :)
0 x

Post Reply