From 6f8fc2ca99d1943977697920940d4bcadaef0374 Mon Sep 17 00:00:00 2001 From: Joe Crayne Date: Tue, 23 Jul 2019 02:14:49 -0400 Subject: Fancier specular lighting. --- hello_obj2.lc | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/hello_obj2.lc b/hello_obj2.lc index a6df7b7..00ba27c 100644 --- a/hello_obj2.lc +++ b/hello_obj2.lc @@ -46,6 +46,9 @@ ambientOnly cam color texture prims = prims -- The illum 0 can be implemented using illum 1 code by setting -- KaIa = Kd -- Ij = 0 forall j +-- +-- The spec seems to require settings for Ia and Ij (j = light number). +-- Ka should probably be calculated similar to Kd: using a Ka_map texture. lambertianReflectance cam lightpos color texture prims = prims & mapPrimitives (\(p,n,uvw) -> let light_vector = normalize $ -- light direction from surface @@ -78,17 +81,21 @@ blinnPhong cam lightpos color texture specular prims = prims if lightpos%w == 0 then lightpos%xyz else homoproj lightpos - homoproj p - campos = cam *. V4 0 0 0 1 - view_vector = normalize $ homoproj campos - homoproj p - h = normalize $ light_vector + view_vector - refl = dot h (normalize n) lambertian = dot light_vector (normalize n) - in ( coordmap cam p, V2 uvw%x (1 - uvw%y), lambertian, refl )) - & rasterizePrimitives (TriangleCtx CullBack PolygonFill NoOffset LastVertex) (Smooth,Smooth,Smooth) - & mapFragments (\(uv,lambertian, refl) -> + in ( coordmap cam p, V2 uvw%x (1 - uvw%y), lambertian, n, p )) + & rasterizePrimitives (TriangleCtx CullBack PolygonFill NoOffset LastVertex) (Smooth,Smooth,Smooth,Smooth) + & mapFragments (\(uv,lambertian, n, p) -> let ct = texture2D (Sampler PointFilter MirroredRepeat texture) uv ns = specular%w ks = point $ specular%xyz + campos = cam *. V4 0 0 0 1 + view_vector = normalize $ homoproj campos - homoproj p + h = normalize $ light_vector + view_vector + refl = dot h (normalize n) + light_vector = normalize $ -- light direction from surface + if lightpos%w == 0 + then lightpos%xyz + else homoproj lightpos - homoproj p in (( color * ct *! (0.5 + 0.5*lambertian) + if refl>0 then ks *! pow refl ns else zero )) ) & accumulateWith (DepthOp Less True, ColorOp NoBlending (V4 True True True True)) -- cgit v1.2.3