the difference whit ray tracing :

- ray tracing : a point traveling trou space until hitting somthing : slow

- ray casting : a line in space intersecting something : fast

error :

- i only get the backgound color

i set backgound to gray so i can SOME drawing

Code: Select all

`''ray casting 1.0`

''bluatigro

''start : 10 may 2014

declare function mix( kla as integer , f as single , klb as single ) as integer

const as single pi = atn( 1 ) * 4

const as integer black = &h000000

const as integer red = &hff0000

const as integer green = &h00ff00

const as integer yellow = &hffff00

const as integer blue = &h0000ff

const as integer magenta = &hff00ff

const as integer cyan = &h00ffff

const as integer white = &hffffff

const as integer gray = &h7f7f7f

const as integer pink = &hff7f7f

const as integer orange = &hff7f00

const as integer purple = &h7f007f

dim shared as integer backcolor

type t3d

x as single

y as single

z as single

declare constructor()

declare constructor ( x as single , y as single , z as single )

declare sub fill( x as single , y as single , z as single )

declare function dot( a as t3d , b as t3d ) as single

end type

constructor t3d()

this.x = 0

this.y = 0

this.z = 0

end constructor

constructor t3d( x as single , y as single , z as single )

this.x = x

this.y = y

this.z = z

end constructor

operator +( a as t3d , b as t3d ) as t3d

return type( a.x + b.x , a.y + b.y , a.z + b.z )

end operator

operator *( a as t3d , d as single ) as t3d

return type( a.x * d , a.y * d , a.z * d )

end operator

operator -( a as t3d , b as t3d ) as t3d

return type( a.x - b.x , a.y - b.y , a.z - b.z )

end operator

operator /( a as t3d , d as single ) as t3d

return type( a.x / d , a.y / d , a.z / d )

end operator

sub t3d.fill( x as single , y as single , z as single )

this.x = x

this.y = y

this.z = z

end sub

function t3d.dot( a as t3d , b as t3d ) as single

return a.x * b.x + a.y * b.y + a.z * b.z

end function

declare function length( q as t3d ) as single

function length( q as t3d ) as single

return sqr( q.x * q.x + q.y * q.y + q.z * q.z ) + 1e-7

end function

declare function anlge( a as t3d , b as t3d ) as single

function angle( a as t3d , b as t3d ) as single

return acos( a.dot( a , b ) _

/ ( length( a ) * length( b ) ) )

end function

type tsphere

m as t3d

r as single

clr as integer

declare sub fill( x as single , y as single , z as single , r as single , clr as integer )

declare function hit( q as t3d , d as t3d ) as integer

declare function dist( q as t3d , d as t3d ) as single

declare function normal( q as t3d ) as t3d

end type

sub tsphere.fill( x as single , y as single , z as single , r as single , clr as integer )

m.fill x , y , z

this.r = r

this.clr = clr

end sub

const as integer false = 0

const as integer true = not false

function tsphere.hit( q as t3d , d as t3d ) as integer

dim as single b , c , d2

b = 2 * d.x * ( q.x - this.m.x ) _

+ 2 * d.y * ( q.y - this.m.y ) _

+ 2 * d.z * ( q.z - this.m.z )

c = ( q.x - this.m.x ) ^ 2 _

+ ( q.y - this.m.y ) ^ 2 _

+ ( q.z - this.m.z ) ^ 2 _

- this.r ^ 2

d2 = b * - 4 * c

return d2 > 0

end function

function tsphere.dist( q as t3d , d as t3d ) as single

dim as single b , c

b = 2 * d.x * ( q.x - this.m.x ) _

+ 2 * d.y * ( q.y - this.m.y ) _

+ 2 * d.z * ( q.z - this.m.z )

c = ( q.x - this.m.x ) ^ 2 _

+ ( q.y - this.m.y ) ^ 2 _

+ ( q.z - this.m.z ) ^ 2 _

- this.r ^ 2

return ( b * -1 - sqr( b * b - 4 * c ) ) / 2

end function

function tsphere.normal( q as t3d ) as t3d

return ( q - this.m ) / length( q - this.m )

end function

declare function ray( n as t3d , d as t3d ) as integer

declare function range( min as single , max as single ) as single

dim shared sphere( 10 ) as tsphere , light( 2 ) as t3d , i as integer

''fill spheres with random values

for i = 0 to ubound( sphere )

sphere( i ).fill range( -500 , 500 ) _

, range( -350 , 350 ) _

, range( 100 , 500 ) _

, range( 10 , 300 ) _

, rgb( rnd * 255 , rnd * 255 , rnd * 255 )

next i

declare function shade( kl as integer _

, f as t3d ) as integer

function shade( kl as integer , f as t3d ) as integer

dim as integer r , g , b

r = ( kl shr 16 ) and 255

g = ( kl shr 8 ) and 255

b = kl and 255

return rgb( r * f.x , g * f.y , b * f.z )

end function

light( 0 ).fill -500,1000,0

light( 1 ).fill 0 , 1000 , 0

light( 2 ).fill 500,1000,0

dim x as single , y as single , n as t3d , d as t3d

screen 20 , 32

backcolor = gray

for x = -500 to 500

for y = -350 to 350

n.fill x , y , 0

d.fill x*1000 , y*1000 , 5e4

pset( x + 1024 / 2 , 768 / 2 - y ) , ray( n , d )

next y

if inkey <> "" then exit for

next x

print "ready"

while inkey = ""

wend

end

function ray( n as t3d , d as t3d ) as integer

''shoot a ray into the world and look if there is something

''retur color of object or if nothing then darkgray

dim done as integer , i as integer , uit as integer _

, tel as integer , led as t3d , bol as t3d

dim as single l , dist

uit = backcolor

done = 0

for i = 0 to ubound( sphere )

if sphere( i ).hit( n , d ) then

uit = sphere( i ).clr

tel = i

done = 1

end if

next i

if not done then

return backcolor

else

l = length( d )

dist = sphere( tel ).dist( n , d )

bol = ( d - n ) / l * dist

led = bol - n

dim as t3d h

h.x = angle( led , light( 0 ) )

h.y = angle( led , light( 1 ) )

h.z = angle( led , light( 2 ) )

uit = shade( uit , h / pi )

end if

return uit

end function

function mix( kla as integer , f as single , klb as single ) as integer

''mix 2 colors info 1 new color

''for colortransitions

dim as integer ra , ga , ba , rb , gb , bb , r , g , b

ra = ( kla shr 16 ) and 255

ga = ( kla shr 8 ) and 255

ba = kla and 255

rb = ( klb shr 16 ) and 255

gb = ( klb shr 8 ) and 255

bb = klb and 255

r = ra + ( rb - ra ) * f

g = ga + ( gb - ga ) * f

b = ba + ( bb - ba ) * f

return rgb( r , g , b )

end function

function range( min as single , max as single ) as single

''returns a random number between min and max

return rnd * ( max - min ) + min

end function