Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let hitSphere radius (texture:ITexture) =
- let innerHit o dir =
- match o, dir with
- | p, v ->
- let a = (pown (Vector.getX v) 2) + (pown (Vector.getY v) 2) + (pown (Vector.getZ v) 2)
- let b = 2.0*(Point.getX p * Vector.getX v + Point.getY p * Vector.getY v + Point.getZ p * Vector.getZ v )
- let c = (pown (Point.getX p) 2) + (pown (Point.getY p) 2) + (pown (Point.getZ p) 2) - (pown radius 2)
- let t1 = (-b + (sqrt((pown b 2) - 4.0 * a*c))) / (2.0 * a)
- let t2 = (-b - (sqrt((pown b 2) - 4.0 * a * c))) / (2.0 * a)
- let correctSolution = getCorrectPolySolution t1 t2
- if (correctSolution.IsSome) then
- let t = correctSolution.Value
- let hp = o + (t * dir)
- let v = Vector.normalize (Vector.mkVector ((Point.getX hp) / radius) ((Point.getY hp) / radius) ((Point.getZ hp) / radius))
- let n =
- if Vector.dotProduct v dir > 0.0 then
- let x = - Vector.getX v
- let y = - Vector.getY v
- let z = - Vector.getZ v
- Vector.mkVector x y z
- else v
- //Texture mapping
- let nx, ny, nz = Vector.getCoord n
- let theta = acos ny
- let phi' = atan2 nx nz
- let phi = if phi' < 0.0
- then phi' + 2.0 * pi
- else phi'
- let u = theta / (2.0 * pi)
- let v = (1.0 - phi) / pi
- let mat = texture.f u v
- Some (t, n, mat, hp)
- else None
- innerHit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement