somithing is wrong with ray/plane. indentation

This commit is contained in:
Nicole Dresselhaus 2014-12-03 12:46:21 +01:00
parent 89cefdb45c
commit 563ea7c26d
3 changed files with 44 additions and 45 deletions

View File

@ -1,5 +1,5 @@
# camera: eye, center, up, fovy, width, height # camera: eye, center, up, fovy, width, height
camera 5 2 5 0 0 0 0 1 0 45 500 500 camera 5 2 5 0 0 0 0 1 0 45 50 50
# recursion depth # recursion depth
depth 1 depth 1

View File

@ -227,7 +227,7 @@ parseMesh' s m = do
b = F.foldl' minmax (V3 (-infty) (-infty) (-infty), V3 infty infty infty) mv b = F.foldl' minmax (V3 (-infty) (-infty) (-infty), V3 infty infty infty) mv
minmax (maxin,minin) vec = (max <$> maxin <*> vec, min <$> minin <*> vec) minmax (maxin,minin) vec = (max <$> maxin <*> vec, min <$> minin <*> vec)
infty = 9999999999999 :: Float infty = 9999999999999 :: Float
return $ OpI Mesh return $ D.trace ("verts: "++show verts++"\nfaces:"++show faces) (OpI Mesh
{ meshShading = s { meshShading = s
, meshMaterial = m , meshMaterial = m
, meshVertices = mv , meshVertices = mv
@ -235,7 +235,7 @@ parseMesh' s m = do
, meshNormals = mn , meshNormals = mn
, meshFaceNormals = mfn , meshFaceNormals = mfn
, meshBounds = bounds , meshBounds = bounds
} })
parseTriangle :: Parser (V3 Int) parseTriangle :: Parser (V3 Int)
parseTriangle = do parseTriangle = do

View File

@ -121,53 +121,52 @@ intersect (Ray ro rd) s@(S (Sphere sc sr _)) = if (d > 0 && int > 0) then
Just $ Collision pos (normalize $ pos - sc) int s Just $ Collision pos (normalize $ pos - sc) int s
else else
Nothing Nothing
where where
a = dot rd rd a = dot rd rd
b = 2 * dot rd oc b = 2 * dot rd oc
c = dot oc oc - sr*sr c = dot oc oc - sr*sr
! d = b * b - 4 * a * c ! d = b * b - 4 * a * c
oc = ro - sc oc = ro - sc
pos = ro + (rd ^* int) pos = ro + (rd ^* int)
int = case ints of int = case ints of
[] -> 0 [] -> 0
a -> P.foldl1 min a a -> P.foldl1 min a
ints = P.filter (uncurry (&&).(&&&) (>epsilon) (not.isNaN)) [(-b-(sqrt d))/(2*a),(-b+(sqrt d))/(2*a)] ints = P.filter (uncurry (&&).(&&&) (>epsilon) (not.isNaN)) [(-b-(sqrt d))/(2*a),(-b+(sqrt d))/(2*a)]
intersect (Ray ro rd) p@(P (Plane pc pn _)) = if det == 0 || t < epsilon intersect (Ray ro rd) p@(P (Plane pc pn _)) = if det == 0 || t < epsilon
then Nothing then Nothing
else Just $ Collision pos pn t p else Just $ Collision pos pn t p
where where
pos = ro + t *^ rd' pos = ro + t *^ rd'
! det = dot rd' pn ! det = dot rd' pn
t = (dot (pc - ro) pn)/det t = (dot (pc - ro) pn)/det
rd' = normalize rd rd' = normalize rd
intersect (Ray ro rd) m@(M (Mesh s _ v f n fn b)) = case catMaybes . elems $ possHits of intersect (Ray ro rd) m@(M (Mesh s _ v f vn fn b)) = case catMaybes . elems $ possHits of
[] -> Nothing [] -> Nothing
a -> Just . P.head . L.sort $ a a -> Just . P.head . L.sort $ a
where where
possHits = case s of possHits = case s of
Flat -> hitsFlat v fn `IM.mapWithKey` f Flat -> hitsFlat v fn `IM.mapWithKey` f
--Phong -> hitsPhong v n <$> f --Phong -> hitsPhong v n <$> f
_ -> undefined _ -> undefined
hitsFlat :: IntMap (V3 Float) -> IntMap (V3 Float) -> Int -> V3 Int -> Maybe Collision hitsFlat :: IntMap (V3 Float) -> IntMap (V3 Float) -> Int -> V3 Int -> Maybe Collision
hitsFlat verts norm f (V3 w1 w2 w3) = hitsFlat verts norm f (V3 w1 w2 w3) =
if det == 0 || t < epsilon || not det2 if det == 0 || t < epsilon || not det2
then Nothing then Nothing
else Just $ Collision pos (norm IM.! f) t m else D.trace (show t ++ "\t" ++ show pos) (Just $ Collision pos (norm IM.! f) t m)
where where
! det = dot rd' (norm IM.! f) --do we hit the plane ! det = dot rd' (norm IM.! f) --do we hit the plane
rd' = normalize rd rd' = normalize rd
t = (dot ((verts IM.! w1) - ro) (norm IM.! f))/det --when do we hit the plane t = (dot ((verts IM.! w1) - ro) (norm IM.! f))/det --when do we hit the plane
pos = ro + t *^ rd' --where do we hit the plane pos = ro + t *^ rd' --where do we hit the plane
v1 = (verts IM.! w2) - (verts IM.! w1) v1 = (verts IM.! w2) - (verts IM.! w1)
v2 = (verts IM.! w3) - (verts IM.! w1) v2 = (verts IM.! w3) - (verts IM.! w1)
det2m = fromJust $ inv33 $ V3 v1 v2 (norm IM.! f) -- base-change-matrix into triangle-coordinates det2m = fromJust $ inv33 $ V3 v1 v2 (norm IM.! f) -- base-change-matrix into triangle-coordinates
det2v = det2m !* (pos - (verts IM.! w1)) det2v = det2m !* (pos - (verts IM.! w1))
-- fromJust is justified as we only make a base-change and all 3 -- fromJust is justified as we only make a base-change and all 3
-- vectors are linear independent. -- vectors are linear independent.
det2 = det2v ^. _x > 0 && det2v ^. _y > 0 det2 = det2v ^. _x > 0 && det2v ^. _y > 0
&& det2v ^. _x + det2v ^. _y < 1 && det2v ^. _x + det2v ^. _y < 1
--hitsPhong :: IntMap (V3 Float) -> IntMap (V3 Float) -> V3 Int -> Maybe Collision
--hitsPhong :: IntMap (V3 Float) -> IntMap (V3 Float) -> V3 Int -> Maybe Collision
-- deprecated - wrong calculation of rays. -- deprecated - wrong calculation of rays.
rotCam :: Float -> Float -> Int -> Int -> V3 Float -> V3 Float -> Float -> V3 Float rotCam :: Float -> Float -> Int -> Int -> V3 Float -> V3 Float -> Float -> V3 Float