bug :
the spheres dont have random positions
Code: Select all
''RAYTRACER
''bluatigro
''start : 19 apr 2012
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 a * ( 1 / 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
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 ) as integer
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 ) as integer
return this.r * this.r > q.dot( q , q ) - q.dot( this.m , 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
randomize timer
for i = 0 to 10
sphere( i ).fill range( -500 , 500 ) _
, range( -350 , 350 ) _
, range( 100 , 500 ) _
, 1 _
, rgb( rnd * 255 , rnd * 255 , rnd * 255 )
next i
light( 0 ).fill 0 , 1000 , 0
dim x as single , y as single , n as t3d , d as t3d
screen 20 , 32
for x = -500 to 500
for y = -350 to 350
n.fill x , y , 0
d.fill 0 , 0 , 1
pset( x + 1024 / 2 , y + 768 / 2 ) , ray( n , d )
next y
next x
print "ready"
while inkey$ = ""
wend
end
function ray( n as t3d , d as t3d ) as integer
dim done as integer , i as integer , uit as integer , tel as integer
while not done
n += d
for i = 0 to ubound( sphere )
if sphere( i ).hit( n ) then
uit = sphere( i ).clr
done = true
end if
next i
tel += 1
if tel > 1000 then done = true
wend
return uit
end function
function range( min as single , max as single ) as single
return rnd * ( max - min ) + min
end function