c++ code at :
https://sdm.scad.edu/faculty/mkesson/vs ... racer.html
error :
where is my yellow sphere ?
where is my shading ?
where is the shadow ?
Code: Select all
'' bluatigro 20 sept 2017
'' raytracer
''https://sdm.scad.edu/faculty
''/mkesson/vsfx419/wip/best/winter12/jonathan_mann/raytracer.html
const as double infinity = 1e300
const as double small = 1e-300
type dbl3d
x as double
y as double
z as double
declare constructor()
declare constructor ( x as double , y as double, z as double )
declare sub fill( x as double , y as double , z as double )
declare sub normalize
declare function toColor() as ulong
end type
constructor dbl3d()
this.x = 0
this.y = 0
this.z = 0
end constructor
constructor dbl3d( x as double , y as double , z as double )
this.x = x
this.y = y
this.z = z
end constructor
operator <>( a as dbl3d , b as dbl3d ) as integer
return a.x <> b.x or a.y <> b.y or a.z <> b.z
end operator
operator +( a as dbl3d , b as dbl3d ) as dbl3d
return type( a.x + b.x , a.y + b.y , a.z + b.z )
end operator
operator *( a as dbl3d , d as double ) as dbl3d
return type( a.x * d , a.y * d , a.z * d )
end operator
operator \( a as dbl3d , b as dbl3d ) as dbl3d
return type( a.y * b.z - a.z * b.y _
, a.z * b.x - a.x * b.z _
, a.x * b.y - a.y * b.x )
end operator
operator -( a as dbl3d , b as dbl3d ) as dbl3d
return type( a.x - b.x , a.y - b.y , a.z - b.z )
end operator
operator /( a as dbl3d , d as double ) as dbl3d
return type( a.x / d , a.y / d , a.z / d )
end operator
sub dbl3d.fill( x as double , y as double , z as double )
this.x = x
this.y = y
this.z = z
end sub
declare function dot( a as dbl3d , b as dbl3d ) as double
function dot( a as dbl3d , b as dbl3d ) as double
return a.x * b.x + a.y * b.y + a.z * b.z
end function
function length( q as dbl3d ) as double
return sqr( q.x * q.x + q.y * q.y + q.z * q.z ) + 1e-7
end function
function normalize( q as dbl3d ) as dbl3d
return q / length( q )
end function
function getangle( a as dbl3d , b as dbl3d ) as double
a = normalize( a )
b = normalize( b )
return acos( dot( a , b ) _
/ ( length( a ) * length( b ) ) )
end function
function dbl3d.toColor() as ulong
dim as integer r , g , b
r = cint( x * 255 ) and 255
g = cint( y * 255 ) and 255
b = cint( z * 255 ) and 255
return rgb( r , g , b )
end function
function dbl3dmix( a as dbl3d , f as double , b as dbl3d ) as dbl3d
if f < 0 then return a
if f > 1 then return b
return a + ( b - a ) * f
end function
dim shared as dbl3d light
light.fill -1 , 1 , -1
type tmaterial
dim as dbl3d diffuse
end type
dim shared as tmaterial material
type tSphere
dim as dbl3d center
dim as tmaterial mat
dim as double radius , radius2
declare sub fill( c as dbl3d , r as double )
declare function hit( o as dbl3d, d as dbl3d ) as double
declare function normal( nu as dbl3d ) as dbl3d
end type
sub tSphere.fill( c as dbl3d , r as double )
'' spot c.x , c.y , c.z
center = c
radius = r
radius2 = r * r
mat = material
end sub
function tSphere.hit( o as dbl3d , d as dbl3d ) as double
dim as double t , a , b , c , disc
dim as dbl3d temp = o - center
a = dot( d , d )
b = 2 * dot( temp , d )
c = dot( temp , temp ) - radius2
disc = b ^ 2 - 4 * a * c
if disc < 0 then
return infinity
else
dim as double e = sqr( disc )
dim as double demon = 2 * a
t = ( -b - e ) / demon
if t > small then
return t
end if
t = ( -b + e ) / demon
if t > small then
return t
end if
end if
return infinity
end function
dim shared as integer sphtel
dim shared as tsphere spheres( 100 )
sub add_sphere( x as double , y as double , z as double _
, r as double , kl as dbl3d )
material.diffuse = kl
spheres( sphtel ).fill dbl3d( x , y , z ) , r
sphtel += 1
end sub
function renderPixel( o as dbl3d , d as dbl3d _
, depth as integer ) as dbl3d
dim as double sphdist = infinity , q
dim as integer isph = -1 , i
for i = 0 to sphtel
q = spheres( i ).hit( o , d )
if q < sphdist then
sphdist = q
isph = i
end if
next i
if isph = -1 then return dbl3d()
dim as dbl3d p = o + normalize( d ) * sphdist
dim as dbl3d n = p - spheres( isph ).center
q = getangle( n , light )
dim as dbl3d kl = spheres( isph ).mat.diffuse
kl = kl * ( .5 + cos( q ) / 2 )
for i = 0 to sphtel
if i <> isph then
q = spheres( i ).hit( p , light - p )
if q < infinity then kl = dbl3d()
end if
next i
return kl
end function
screen 20 , 32
dim as integer winx , winy
screeninfo winx , winy
dim as dbl3d o , d
dim as double x , y
sphtel = 0
add_sphere 0 , 0 , 0 , 250 , dbl3d( 0 , 1 , 1 )
add_sphere -200,200,-200, 30 , dbl3d( 1 , 1 , 0 )
for x = -winx / 2 to winx / 2
for y = -winy / 2 to winy / 2
o.fill 0 , 0 , -1000
d.fill x , y , 1000
pset( x + winx / 2 , winy / 2 - y ) _
, renderPixel( o , d , 7 ).tocolor
next y
next x
print "ready"
sleep