another version !! ,
i have to make pixel size big or else very very very slow rendering time
Rem Project: Ray Marching Version 2
Rem Created: Tuesday, September 27, 2011
Rem ***** Main Source File *****
//
Type rmVec
x as float
y as float
z as float
EndType
//
Dim RayDir(512*512) as rmVec
//
For i = 0 To 511
For j = 0 To 511
dx# = i - 256.0
dy# = j - 256.0
dz# = 256.0
rl# = 1.0 / Sqrt( dx# * dx# + dy# * dy# + dz# * dz# )
index = i + ( j << 9 )
RayDir(index).x = dx# * rl#
RayDir(index).y = dy# * rl#
RayDir(index).z = dz# * rl#
Next
Next
//
Sync On
Sync Rate 0
Set Display Mode 1024,768,32
`Set Window Position 40,0
Set Window Off
//
Ro As rmVec ` rays origin
Rd As rmVec ` rays direction
Rm As rmVec ` the marching ray
//
Ro.x = 0.0
Ro.y = 0.0
Ro.z = -256.0
Repeat
aa# = wrapvalue(aa#+4.0)
cc# = cos(aa#)
ss# = sin(aa#)
Cls 0x0
//
Lock Pixels
//
For i = 0 To 511 Step 4
//
For j = 0 To 511 Step 4
//
index = i + ( j << 9 )
//
Rd.x = RayDir(index).x
Rd.y = RayDir(index).y
Rd.z = RayDir(index).z
//
tx# = Rd.x
Rd.x = Rd.x * cc# - Rd.z * ss#
Rd.z = Rd.z * cc# + tx# * ss#
//
Rm.x = Ro.x
Rm.y = Ro.y
Rm.z = Ro.z
//
Flag = 0
//
For k = 0 To 255
//
distance# = SampleWorld( Rm )
//
If distance# > 100.0
//
Flag = 0
//
Exit
Endif
//
If distance# < 0.1
//
Flag = 1
//
Exit
Endif
//
Rm.x = Rm.x + distance# * Rd.x
Rm.y = Rm.y + distance# * Rd.y
Rm.z = Rm.z + distance# * Rd.z
Next
//
If Flag = 1
//
es# = 0.9
//
nx# = ( Rm.x - es# ) - ( Rm.x + es# )
ny# = ( Rm.y - es# ) - ( Rm.y + es# )
nz# = ( Rm.z - es# ) - ( Rm.z + es# )
//
nl# = 1.0 / Sqrt( Rd.x * Rd.x + Rd.y * Rd.y + Rd.z * Rd.z )
//
nx# = nx# * nl#
ny# = ny# * nl#
nz# = nz# * nl#
lx# = -128 - Rm.x
ly# = -228 - Rm.y
lz# = -528 - Rm.z
//
ll# = 1.0 / Sqrt( lx# * lx# + ly# * ly# + lz# * lz# )
//
lx# = lx# * ll#
ly# = ly# * ll#
lz# = lz# * ll#
//
lc# = lx# * nx# + ly# * ny# + lz# * nz#
//
If lc# > 0.0
ls# = lc# * 0.9
//
if distance# = rmPlane( Rm , -1.0 , 0.0 , 0.0 , 100.0 )
color = Rgb(255*ls#,0,0)
Endif
//
if distance# = rmPlane( Rm , 1.0 , 0.0 , 0.0 , 100.0 )
color = Rgb(0,255*ls#,0)
Endif
//
if distance# = rmPlane( Rm , 0.0 , -1.0 , 0.0 , 100.0 )
color = Rgb(50*ls#,50*ls#,255*ls#)
Endif
//
if distance# = rmPlane( Rm , 0.0 , 1.0 , 0.0 , 100.0 )
color = Rgb(255*ls#,50*ls#,50*ls#)
Endif
//
if distance# = rmPlane( Rm , 0.0 , 0.0 , -1.0 , 0.0 )
color = Rgb(255*ls#,255*ls#,255*ls#)
Endif
//
if distance# = rmCube( Rm , -10.0 , -10.0 , -240.0 , 2.0 )
color = Rgb(5*ls#,125*ls#,215*ls#)
Endif
//
if distance# = rmTwistedSphere( Rm , 50.0 , 50.0 , -10.0 , 40.0 )
color = Rgb(155*ls#,25*ls#,115*ls#)
Endif
//
if distance# = rmTorus( Rm , -42.0 , 46.0 , -49.0 , 40.0 , 20.0 )
color = Rgb(ls#,ls#,255*ls#)
Endif
//
if distance# = rmSphere( Rm , -50.0 , -50.0 , -610.0 , 34.0 )
color = Rgb(255*ls#,64*ls#,ls#)
Endif
//
Dotx2(i,j,color,4)
//
Endif
//
Flag = 0
//
Endif
//
Next
//
Next
//
Unlock Pixels
//
Text 0,600,"done - fps:"+Str$(screen fps())
//
FastSync
//
Until EscapeKey()
//
End
//
Function Dotx2(x,y,c,s)
if s > 0
Pixel = Get Pixels Pointer() + ( y * Get Pixels Pitch() + ( x << 2 ) )
*Pixel = c
s = s >> 1
Dotx2(x,y,c,s)
Dotx2(x,y+s,c,s)
Dotx2(x+s,y+s,c,s)
Dotx2(x+s,y,c,s)
endif
Endfunction
//
Function rmMin( a as float , b as float )
If a > b Then ExitFunction b
EndFunction a
//
Function rmMax( a as float , b as float )
If a < b Then ExitFunction b
EndFunction a
//
Function rmPlane( ray as rmVec , x as float , y as float , z as float , s as float )
//
d# = ( ray.x * x + ray.y * y + ray.z * z ) + s
//
EndFunction d#
//
Function rmCube( ray as rmVec , x as float , y as float , z as float , s as float )
//
rx# = ray.x - x
ry# = ray.y - y
rz# = ray.z - z
//
a# = Abs( rx# ) - s
b# = Abs( ry# ) - s
c# = Abs( rz# ) - s
//
d# = rmMax( a# , rmMax( b# , c# ) )
//
EndFunction d#
//
Function rmTwistedSphere( ray as rmVec , x as float , y as float , z as float , s as float )
//
a# = ray.x - x
b# = ray.y - y
c# = ray.z - z
//
a# = a# - Cos(b#*8.0)*22.0
c# = c# - Sin(b#*4.0)*22.0
//
d# = Sqrt( a# * a# + b# * b# + c# * c# ) - s
//
EndFunction d#
//
Function rmSphere( ray as rmVec , x as float , y as float , z as float , s as float )
//
a# = ray.x - x
b# = ray.y - y
c# = ray.z - z
//
d# = Sqrt( a# * a# + b# * b# + c# * c# ) - s
//
EndFunction d#
//
Function rmTorus( ray as rmVec , x as float , y as float , z as float , i as float , t as float )
//
a# = Sqrt( ( ray.x - x ) * ( ray.x - x ) + ( ray.y - y ) * ( ray.y - y ) ) - i
//
d# = Sqrt( a# * a# + ( ray.z - z ) * ( ray.z - z ) ) - t
//
EndFunction d#
//
Function SampleWorld( ray As rmVec )
//
sdf_1# = rmPlane( ray , -1.0 , 0.0 , 0.0 , 100.0 )
sdf_2# = rmPlane( ray , 1.0 , 0.0 , 0.0 , 100.0 )
sdf_3# = rmPlane( ray , 0.0 , -1.0 , 0.0 , 100.0 )
sdf_4# = rmPlane( ray , 0.0 , 1.0 , 0.0 , 100.0 )
sdf_5# = rmPlane( ray , 0.0 , 0.0 , -1.0 , 0.0 )
sdf_6# = rmCube( ray , -10.0 , -10.0 , -240.0 , 2.0 )
sdf_7# = rmTwistedSphere( ray , 50.0 , 50.0 , -10.0 , 40.0 )
sdf_8# = rmTorus( ray , -42.0 , 46.0 , -49.0 , 40.0 , 20.0 )
sdf_9# = rmSphere( ray , -50.0 , -50.0 , -610.0 , 34.0 )
//
d# = rmMin( sdf_1# , rmMin( sdf_2# , rmMin( sdf_3# , rmMin( sdf_4# , rmMin( sdf_5# , rmMin( sdf_6# , rmMin( sdf_7# , rmMin( sdf_8# , sdf_9# )) ) ) )) ) )
//
EndFunction d#
hexeg0n