Code archives/Graphics/Textured voxel demo

This code has been declared by its author to be Public Domain code.

Download source code

Textured voxel demo by jfk EO-111102004
Get that original Apache feeling :)
; Voxel/Raycasting with heightmap, texture and "fog" - flight over landscape
; by CSP just4fun
Graphics 320,240,16,2
SetBuffer BackBuffer()

; can also use heightmap and texture files...
Global hm
hm=CreateImage(256,256)

Dim mount(1000,3)
For i=0 To 10
 mount(i,0)=Rand(0,256)
 mount(i,1)=Rand(0,256)
 mount(i,2)=Rand(30,127)
Next

Cls
Print "Creating Heightmap - please stand by"
Flip
SetBuffer ImageBuffer(hm)
For i=0 To 255
 For i2=0 To 10
  r=mount(i2,2)-i
  If r>0
   Color i+i,i+i,i+i
   Oval mount(i2,0)-r/2,mount(i2,1)-r/2,r,r,0
   Oval 1+mount(i2,0)-r/2,mount(i2,1)-r/2,r,r,0
   Oval mount(i2,0)-r/2,1+mount(i2,1)-r/2,r,r,0
  EndIf
 Next
Next
SetBuffer BackBuffer()

Cls
Print "Creating Terrain Texture - wait a second..."
Flip
Global mossy
mossy=CreateImage(256,256)

For i=0 To 1000
 mount(i,0)=Rand(0,256)
 mount(i,1)=Rand(0,256)
 mount(i,2)=Rand(0,20)
Next
SetBuffer ImageBuffer(mossy)

For i=0 To 50
 For i2=0 To 1000
  r=mount(i2,2)-i
  If r>0
   Color Rand(50)+i*4,Rand(50)+i*10,0
   Oval mount(i2,0)-r/2,mount(i2,1)-r/2,r,r,0
   Oval 1+mount(i2,0)-r/2,mount(i2,1)-r/2,r,r,0
   Oval mount(i2,0)-r/2,1+mount(i2,1)-r/2,r,r,0
  EndIf
 Next
Next

SetBuffer BackBuffer()

Global grw=GraphicsWidth()
Global grh=GraphicsHeight()
Global grwh=grw/2
Global grhh=grh/2
Global my
Global px#=8.0
Global pz#=8.0
Global a#

Color 127,127,127
MoveMouse grwh,0
;__________________________MAINLOOP_________________________
While KeyDown(1)=0
 Cls
 If KeyDown(200) Then ; up
  px=px+Sin(a)
  pz=pz+Cos(a)
 EndIf
 If KeyDown(208) Then ; down
  px=px-Sin(a)
  pz=pz-Cos(a)
 EndIf
 a=(a-mxs#) Mod 360 ; use mouse to steer
 raycast()
 ; Text 0,0, "x:"+px+ " z:"+pz
 Text 0,0,"Use Mouse + Arrows"
 Flip
 my=MouseY()
 If my>130 Then my=130
 mxs#=MouseXSpeed()/3.0 ; used by steer
 MoveMouse GraphicsWidth()/2,my
Wend
End
;________________________eo mainloop__________________________


Function raycast()
 For i=-grwh To grwh-1 Step 4
  row=grh+1
;  igrwh=i+grwh
  grwh_mi=grwh-i
  rayx#=px
  rayz#=pz
  stepx#=Sin(a+0.125*i)
  stepz#=Cos(a+0.125*i)
  count#=0

  LockBuffer BackBuffer()
  LockBuffer ImageBuffer(hm)
  LockBuffer ImageBuffer(mossy)
  While count<200 ; camerarange
   rayx#=rayx+stepx
   rayz#=rayz+stepz
   If rayx>-127 And rayx<127
    If rayz>-127 And rayz<127
     c#=ReadPixelFast(rayx+127,rayz+127,ImageBuffer(hm)) And $ff
   h#=((15000.0-my*100)/count)-((.1*my+30)*c)/count
   If h<row
    If c=0 Then 
     co=$9dd0 ; water
    Else
     ;co=((c Xor $ff)Shl 7)And $ff00 Or (c Shl 16)
     co=ReadPixelFast(rayx+127,rayz+127,ImageBuffer(mossy)) And $ffff Or (c Xor $FF)
    EndIf
    For ii= h To row-1
     x=grwh_mi
     y=ii
     If x>=0 And x<=grw And y>=0 And y<=grh
      WritePixelFast x,y,co,BackBuffer()
     EndIf
    Next
    row=h
   EndIf
   EndIf
   EndIf
   count=count+1
  Wend
  UnlockBuffer BackBuffer()
  UnlockBuffer ImageBuffer(hm)
  UnlockBuffer ImageBuffer(mossy)
 Next
End Function

Comments

bobbo2004
What is it?

If r&gt;0


It does not compile under BlitzPlus...


Beaker2004
replace:
&gt;
with:
>
and
&lt;
with:
<


bobbo2004
Yes thank you, I figured it out too, it seems a bug of this forum to replace HTML codes for > and <.


Subirenihil2006
???


Rook Zimbabwe2006
Hmmm... interesting effect, but... Why does it just create every other line of the image?


Stevie G2006
This gives me a mav at one of the writepixel commands?


DareDevil2006
Hi all

I have modified your code for fast speed.



Warren2006
I get the same MAV in DareDevil's version.


DareDevil2006
i have test the release and not crash.

whats release blitz use?

bye


impixi2006
I don't know why, but these days "writepixelfast" and "readpixelfast" cause MAVs for me. If I change them to their respective "writepixel" and "readpixel" equivalents the code runs without problems.

EDIT: Actually, the -"fast" commands cause MAVs only in windowed mode. If I change the code to run fullscreen then it runs without problems.


DareDevil2006
change settings bios enable

agp fast access read!
agp fast access write!


bytecode772006
yeah, on my school pc, it is working properly. but at home MAV.

you have to determinate pixels out of the screen range

at 640x480 it is (0-639)x(0-479)


Zenith2006
To fix the mav, one must only fix the boundary checks for the texture output. Line 154, change it to:
If x>=0 And x<grw And y>=0 And y<grh


In my opinion, it'd be faster to use writepixelfast WITHOUT the boundary checks, just know where x and y will lead.. ReadPixel would certainly have faster boundary checks, considering it's written below blitz.


impixi2006
Here's a rough BlitzMax port + modification:




Code Archives Forum