added spec lighting

This commit is contained in:
Nicole Dresselhaus 2014-10-25 00:59:33 +02:00
parent 64dbcca6ef
commit 90fad1366b

View File

@ -38,10 +38,10 @@ render w h s index = PixelRGB8 (ci cr) (ci cg) (ci cb)
Just c@(Collision pos _ _ obj) -> Just c@(Collision pos _ _ obj) ->
-- ambient lighting -- ambient lighting
((ambColor . ambientLight $ s) * (materialAmbience . getMaterial $ obj)) ((ambColor . ambientLight $ s) * (materialAmbience . getMaterial $ obj))
-- + diffuse lighting -- + diffuse/spec lighting
+ (foldl1 (+) $ (diffuse c s) <$> sceneLights s) + (foldl1 (+) $ (diffuseAndSpec c s co) <$> sceneLights s)
-- + reflections - TODO -- + reflect -- TODO
ray = camRay x y (sceneCamera s) ray@(Ray co _) = camRay x y (sceneCamera s)
y = fromIntegral $ index `mod` w y = fromIntegral $ index `mod` w
x = fromIntegral $ index `div` w x = fromIntegral $ index `div` w
ci = floor . (clamp 0 255) . (*255) ci = floor . (clamp 0 255) . (*255)
@ -49,17 +49,26 @@ render w h s index = PixelRGB8 (ci cr) (ci cg) (ci cb)
--Ray (eye cam) $ rotCam x y w h (center cam - eye cam) (up cam) (fovy cam) --Ray (eye cam) $ rotCam x y w h (center cam - eye cam) (up cam) (fovy cam)
--cam = sceneCamera s --cam = sceneCamera s
diffuse :: Collision -> Scene -> Light -> V3 Float -- | Collision-Information, Scene, view-position, light
diffuse (Collision pos n _ obj) s (Light lpos color int) = diffuseAndSpec :: Collision -> Scene -> V3 Float-> Light -> V3 Float
diffuseAndSpec (Collision pos n _ obj) s co (Light lpos color int) =
case blocked of case blocked of
Nothing -> ill Nothing -> diff + spec
Just (Collision _ _ dist _) -> if dist < norm lightdir Just (Collision _ _ dist _) -> if dist < norm lightdir
then then
V3 0 0 0 --light is blocked -> no lighting from here. V3 0 0 0 --light is blocked -> no lighting from here.
else else
ill diff + spec
where where
ill = i * dot n (normalize lightdir) *^ color * materialDiffuse mat spec = if dot n (normalize lightdir) < 0 || dot r v < 0
then V3 0 0 0
else i * (dot r v ** materialShinyness mat) *^ color * materialSpec mat
r = (dot n ld * 2 *^ n) - ld
ld = normalize lightdir
v = normalize $ co - pos
diff = if dot n (normalize lightdir) < 0
then V3 0 0 0
else i * dot n (normalize lightdir) *^ color * materialDiffuse mat
mat = getMaterial obj mat = getMaterial obj
blocked = raytrace (Ray pos lightdir) s blocked = raytrace (Ray pos lightdir) s
lightdir = (lpos - pos) lightdir = (lpos - pos)
@ -107,8 +116,7 @@ intersect (Ray ro rd) s@(S (Sphere sc sr _)) = if (d > 0 && int > 0) then
ints = filter (uncurry (&&).(&&&) (>epsilon) (not.isNaN)) [(-b-(sqrt d))/(2*a),(-b+(sqrt d))/(2*a)] ints = 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 -- $ D.trace (show (det, t, pos)) else Just $ Collision pos pn t p
$ Collision pos pn t p
where where
pos = ro + t *^ rd' pos = ro + t *^ rd'
det = dot rd' pn det = dot rd' pn