raytracer : shadow problem

General FreeBASIC programming questions.
Post Reply
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

raytracer : shadow problem

Post by bluatigro »

i have tryed to make shadows form othere shapes to appeer
on my shapes i got triangles and spheres
the problem is in renderPixel [ see REMed part ]
my pc freezes

i only build shaows in my spheres

other question :
how do i use ambient,specular,emision and shininess in my color's

Code: Select all

dim shared as t3d light
light.fill -1 , 3 , 1
function renderPixel( o as t3d , d as t3d _
  , dept as integer ) as t3d
  dim as integer i , id = -1 , id2 = -1 
  dim as double dist , tridist = 1e9 , sphdist = 1e9 , a 
  dim as t3d kl , kl2 , ambient , diffuse _
  , specular , emision , q , p , shade
  dim as double shininess , reflection 
  for i = 0 to tritel
    dist = triangles( i ).hit( o , d )
    if dist > 0 andalso dist < tridist then
      id = i
      tridist = dist
    end if
  next i
  for i = 0 to spheretel
    dist = spheres( i ).hit( o , d )
    if dist > 0 andalso dist < sphdist then
      id2 = i
      sphdist = dist
    end if
  next i
    
  if tridist < sphdist then 
    reflection = triangles( id ).mat.reflection
    ''color objects from triangle
    ''how do i calc the end color ?
    ambient = triangles( id ).mat.ambient
    diffuse = triangles( id ).mat.diffuse
    specular = triangles( id ).mat.specular
    emision = triangles( id ).mat.emision
    shininess = triangles( id ).mat.shininess
    a = angle( triangles( id ).n , light )
    kl = diffuse * ( cos( a ) / 2 + 0.5 )
    p = o + d * tridist
''    if renderPixel( p , light , -1 ) <> t3d() then 
''      kl = t3d()
''    end if
    if reflection < 0.001 or dept < 0 then
      return kl
    else
      q = triangles( id ).n
      d = mirror( d , q )
      kl2 = renderPixel( p , d , dept - 1 )
      return kl + ( kl2 - kl ) * reflection
    end if
  else
    if sphdist = 1e9 then return t3d()
    reflection = spheres( id2 ).mat.reflection
    ''color objects from sphere
    ''how do i calc the end color ?
    ambient = spheres( id2 ).mat.ambient
    diffuse = spheres( id2 ).mat.diffuse
    specular = spheres( id2 ).mat.specular
    emision = spheres( id2 ).mat.emision
    shininess = spheres( id2 ).mat.shininess
    p = o + d * sphdist
    q = spheres( id2 ).normal( p )
    q.normalize()
    a = angle( q , light )
    kl = diffuse * ( cos( a ) / 2 + 0.5 )
''    if a < pi/2 then
''      kl2 = renderpixel( p , light , -1 )
''      if kl2.x or kl2.y or kl2.z then
''        return t3d()
''      end if
''    end if
    if reflection < 0.001 or dept < 0 then
      return kl
    else
      d = mirror( d , q )
      kl2 = renderPixel( p , d , dept - 1 )
      return kl + ( kl2 - kl ) * reflection
    end if
  end if
  return t3d()
end function

t3d is my 3dvector whitch i also use for colors
Post Reply