module Data.Glome.Solid where
import Data.Glome.Vec
import Data.Glome.Clr
import Data.List hiding (group)
data Rayint = RayHit {
depth :: !Flt,
pos :: !Vec,
norm :: !Vec,
texture :: Texture
} | RayMiss deriving Show
nearest :: Rayint -> Rayint -> Rayint
nearest a RayMiss = a
nearest RayMiss b = b
nearest !rha@(RayHit da _ _ _) !rhb@(RayHit db _ _ _) =
if da < db
then rha
else rhb
furthest :: Rayint -> Rayint -> Rayint
furthest !a !RayMiss = RayMiss
furthest !RayMiss !b = RayMiss
furthest !(RayHit da pa na ta) !(RayHit db pb nb tb) =
if da > db
then RayHit da pa na ta
else RayHit db pb nb tb
hit :: Rayint -> Bool
hit (RayHit _ _ _ _) = True
hit RayMiss = False
dist :: Rayint -> Flt
dist RayMiss = infinity
dist (RayHit d _ _ _) = d
data PacketResult = PacketResult !Rayint !Rayint !Rayint !Rayint
packetmiss = PacketResult RayMiss RayMiss RayMiss RayMiss
nearest_packetresult :: PacketResult -> PacketResult -> PacketResult
nearest_packetresult !(PacketResult a1 a2 a3 a4) !(PacketResult b1 b2 b3 b4) =
PacketResult (nearest a1 b1)
(nearest a2 b2)
(nearest a3 b3)
(nearest a4 b4)
rayint_advance :: SolidItem -> Ray -> Flt -> Texture -> Flt -> Rayint
rayint_advance s r d t adv =
let a = adv+delta
in
case (rayint s (ray_move r a) (da) t) of
RayMiss -> RayMiss
RayHit depth pos norm tex -> RayHit (depth+a) pos norm tex
--MATERIALS--
data Material = Material {clr :: !Color,
refl, refr, ior,
kd, ks, shine :: !Flt} deriving Show
type Texture = Rayint -> Material
showTexture :: Texture -> String
showTexture t = show $ t RayMiss
instance Show Texture where
show = showTexture
m_white = (Material c_white 0 0 0 1 0 2)
t_white ri = m_white
t_uniform :: Material -> Texture
t_uniform m = \x -> m
interp :: Flt -> Flt -> Flt -> Flt
interp scale a b =
scale*a + (1scale)*b
m_interp :: Material -> Material -> Flt -> Material
m_interp m1 m2 scale =
let (Material m1c m1refl m1refr m1ior m1kd m1ks m1shine) = m1
(Material m2c m2refl m2refr m2ior m2kd m2ks m2shine) = m2
intp = interp scale
c = cadd (cscale m1c scale) (cscale m2c (1scale))
refl = intp m1refl m2refl
refr = intp m1refr m2refr
ior = intp m1ior m2ior
kd = intp m1kd m2kd
ks = intp m1ks m2ks
shine = intp m1shine m2shine
in (Material c refl refr ior kd ks shine)
newtype Pcount = Pcount (Int,Int,Int) deriving Show
pcadd :: Pcount -> Pcount -> Pcount
pcadd (Pcount (a1,a2,a3)) (Pcount (b1,b2,b3)) = Pcount (a1+b1, a2+b2, a3+b3)
asbound :: Pcount -> Pcount
asbound (Pcount (a,b,c)) = Pcount (0,b,a+c)
pcsinglexfm :: Pcount
pcsinglexfm = Pcount (0,1,0)
pcsingleprim :: Pcount
pcsingleprim = Pcount (1,0,0)
pcsinglebound :: Pcount
pcsinglebound = Pcount (0,0,1)
pcnone :: Pcount
pcnone = Pcount (0,0,0)
debug_wrap :: (Rayint,Int) -> Int -> (Rayint,Int)
debug_wrap (ri,a) b = (ri,(a+b))
nearest_debug :: (Rayint,Int) -> (Rayint,Int) -> (Rayint,Int)
nearest_debug (ari, an) (bri, bn) = ((nearest ari bri),(an+bn))
class (Show a) => Solid a where
rayint :: a
-> Ray
-> Flt
-> Texture
-> Rayint
rayint_debug :: a -> Ray -> Flt -> Texture -> (Rayint, Int)
packetint :: a -> Ray -> Ray -> Ray -> Ray -> Flt -> Texture -> PacketResult
shadow :: a -> Ray -> Flt -> Bool
inside :: a -> Vec -> Bool
bound :: a -> Bbox
tolist :: a -> [SolidItem]
transform :: a -> [Xfm] -> SolidItem
transform_leaf :: a -> [Xfm] -> SolidItem
flatten_transform :: a -> [SolidItem]
primcount :: a -> Pcount
rayint_debug s !r !d t = ((rayint s r d t),0)
packetint s !r1 !r2 !r3 !r4 !d t =
PacketResult (rayint s r1 d t)
(rayint s r2 d t)
(rayint s r3 d t)
(rayint s r4 d t)
shadow s !r !d =
case (rayint s r d t_white) of
RayHit _ _ _ _ -> True
RayMiss -> False
tolist a = [SolidItem (a)]
transform a xfm = SolidItem $ Instance (SolidItem a) (compose xfm)
transform_leaf = transform
flatten_transform = tolist
primcount s = pcsingleprim
data SolidItem = forall a. Solid a => SolidItem a
instance Solid SolidItem where
rayint (SolidItem s) !r !d t = rayint s r d t
packetint (SolidItem s) !r1 !r2 !r3 !r4 !d t = packetint s r1 r2 r3 r4 d t
rayint_debug (SolidItem s) r d t = rayint_debug s r d t
shadow (SolidItem s) !r !d = shadow s r d
inside (SolidItem s) pt = inside s pt
bound (SolidItem s) = bound s
tolist (SolidItem s) = tolist s
transform (SolidItem s) xfm = transform s xfm
transform_leaf (SolidItem s) xfm = transform_leaf s xfm
flatten_transform (SolidItem s) = [SolidItem (flatten_transform s)]
primcount (SolidItem s) = primcount s
instance Show SolidItem where
show (SolidItem s) = "SI " ++ show s
group :: [SolidItem] -> SolidItem
group [] = SolidItem Void
group (sld:[]) = sld
group slds = SolidItem (flatten_group slds)
flatten_group :: [SolidItem] -> [SolidItem]
flatten_group slds = concat (map tolist slds)
rayint_group :: [SolidItem] -> Ray -> Flt -> Texture -> Rayint
rayint_group [] _ _ _ = RayMiss
rayint_group (x:xs) !r !d t = nearest (rayint x r d t) (rayint_group xs r d t)
packetint_group :: [SolidItem] -> Ray -> Ray -> Ray -> Ray -> Flt -> Texture -> PacketResult
packetint_group [] !r1 !r2 !r3 !r4 !d t = packetmiss
packetint_group (x:xs) !r1 !r2 !r3 !r4 !d t =
nearest_packetresult (packetint x r1 r2 r3 r4 d t)
(packetint_group xs r1 r2 r3 r4 d t)
rayint_debug_group :: [SolidItem] -> Ray -> Flt -> Texture -> (Rayint,Int)
rayint_debug_group [] _ _ _ = (RayMiss,0)
rayint_debug_group (x:xs) !r !d t =
nearest_debug (rayint_debug x r d t)
(rayint_debug_group xs r d t)
shadow_group :: [SolidItem] -> Ray -> Flt -> Bool
shadow_group [] !r !d = False
shadow_group (x:xs) r d = (shadow x r d) || (shadow_group xs r d)
inside_group :: [SolidItem] -> Vec -> Bool
inside_group slds pt =
foldl' (||) False (map (\x -> inside x pt) slds)
bound_group :: [SolidItem] -> Bbox
bound_group slds =
foldl' bbjoin empty_bbox (map bound slds)
transform_leaf_group :: [SolidItem] -> [Xfm] -> SolidItem
transform_leaf_group slds xfms =
SolidItem $ map (\x -> transform_leaf x xfms) (tolist slds)
primcount_group :: [SolidItem] -> Pcount
primcount_group slds = foldl (pcadd) (Pcount (0,0,0)) (map primcount slds)
instance Solid [SolidItem] where
rayint = rayint_group
packetint = packetint_group
rayint_debug = rayint_debug_group
shadow = shadow_group
inside = inside_group
bound = bound_group
tolist a = concat $ map tolist a
transform_leaf = transform_leaf_group
flatten_transform a = concat $ map flatten_transform a
primcount = primcount_group
data Void = Void deriving Show
nothing = SolidItem Void
instance Solid Void where
rayint Void _ _ _ = RayMiss
packetint Void _ _ _ _ _ _ = packetmiss
shadow Void _ _ = False
inside Void _ = False
bound Void = empty_bbox
tolist Void = []
transform Void xfms = SolidItem Void
data Instance = Instance SolidItem Xfm deriving Show
rayint_instance :: Instance -> Ray -> Flt -> Texture -> Rayint
rayint_instance !(Instance sld xfm) !(Ray orig dir) !d t =
let newdir = invxfm_vec xfm dir
neworig = invxfm_point xfm orig
lenscale = vlen newdir
invlenscale = 1/lenscale
in
case (rayint sld (Ray neworig (vscale newdir invlenscale)) (d*lenscale) t) of
RayMiss -> RayMiss
RayHit depth pos n tex -> RayHit (depth*invlenscale)
(xfm_point xfm pos)
(vnorm (invxfm_norm xfm n))
tex
packetint_instance :: Instance -> Ray -> Ray -> Ray -> Ray -> Flt -> Texture -> PacketResult
packetint_instance !(Instance sld xfm) !(Ray orig1 dir1) !(Ray orig2 dir2)
!(Ray orig3 dir3) !(Ray orig4 dir4) d t =
let newdir1 = invxfm_vec xfm dir1
newdir2 = invxfm_vec xfm dir2
newdir3 = invxfm_vec xfm dir3
newdir4 = invxfm_vec xfm dir4
neworig1 = invxfm_point xfm orig1
neworig2 = invxfm_point xfm orig2
neworig3 = invxfm_point xfm orig3
neworig4 = invxfm_point xfm orig4
lenscale1 = vlen newdir1
lenscale2 = vlen newdir2
lenscale3 = vlen newdir3
lenscale4 = vlen newdir4
invlenscale1 = 1/lenscale1
invlenscale2 = 1/lenscale2
invlenscale3 = 1/lenscale3
invlenscale4 = 1/lenscale4
in
let pr = packetint sld (Ray neworig1 (vscale newdir1 invlenscale1))
(Ray neworig2 (vscale newdir2 invlenscale2))
(Ray neworig3 (vscale newdir3 invlenscale3))
(Ray neworig4 (vscale newdir4 invlenscale4))
(d*lenscale1) t
PacketResult ri1 ri2 ri3 ri4 = pr
fix ri ils =
case ri of
RayMiss -> RayMiss
RayHit depth pos n tex -> RayHit (depth*ils)
(xfm_point xfm pos)
(vnorm (invxfm_norm xfm n))
tex
in PacketResult (fix ri1 invlenscale1)
(fix ri2 invlenscale2)
(fix ri3 invlenscale3)
(fix ri4 invlenscale4)
rayint_debug_instance :: Instance -> Ray -> Flt -> Texture -> (Rayint,Int)
rayint_debug_instance (Instance sld xfm) (Ray orig dir) d t =
let newdir = invxfm_vec xfm dir
neworig = invxfm_point xfm orig
lenscale = vlen newdir
invlenscale = 1/lenscale
in
case (rayint_debug sld (Ray neworig (vscale newdir invlenscale)) (d*lenscale) t) of
(RayMiss, count) -> (RayMiss, count)
(RayHit depth pos n tex, count) -> (RayHit (depth*invlenscale)
(xfm_point xfm pos)
(vnorm (invxfm_norm xfm n))
tex, count)
shadow_instance :: Instance -> Ray -> Flt -> Bool
shadow_instance !(Instance sld xfm) !(Ray orig dir) !d =
let newdir = invxfm_vec xfm dir
neworig = invxfm_point xfm orig
lenscale = vlen newdir
invlenscale = 1/lenscale
in
shadow sld (Ray neworig (vscale newdir invlenscale)) (d*lenscale)
inside_instance :: Instance -> Vec -> Bool
inside_instance (Instance s xfm) pt =
inside s (xfm_point xfm pt)
bound_instance :: Instance -> Bbox
bound_instance (Instance sld xfm) =
let (Bbox (Vec p1x p1y p1z) (Vec p2x p2y p2z)) = bound sld
pxfm = xfm_point xfm
in
bbpts [(pxfm (Vec x y z)) | x <- [p1x,p2x],
y <- [p1y,p2y],
z <- [p1z,p2z]]
transform_instance :: Instance -> [Xfm] -> SolidItem
transform_instance (Instance s xfm2) xfm1 =
transform s [compose ([xfm2]++xfm1) ]
transform_leaf_instance :: Instance -> [Xfm] -> SolidItem
transform_leaf_instance (Instance s xfm2) xfm1 =
transform_leaf s [compose ([xfm2]++xfm1) ]
flatten_transform_instance :: Instance -> [SolidItem]
flatten_transform_instance (Instance s xfm) =
[SolidItem $ transform_leaf s [xfm]]
primcount_instance :: Instance -> Pcount
primcount_instance (Instance s xfm) = pcadd (primcount s) pcsinglexfm
instance Solid Instance where
rayint = rayint_instance
packetint = packetint_instance
rayint_debug = rayint_debug_instance
shadow = shadow_instance
inside = inside_instance
bound = bound_instance
transform = transform_instance
transform_leaf = transform_leaf_instance
flatten_transform = flatten_transform_instance
primcount = primcount_instance