diff options
author | Joe Crayne <joe@jerkface.net> | 2019-07-23 02:14:49 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-07-23 02:14:49 -0400 |
commit | 6f8fc2ca99d1943977697920940d4bcadaef0374 (patch) | |
tree | e9e75fa2e120d6cb00565d3ead03def31e5e1777 | |
parent | 4026bea0ec0e2604ebcac6e31ae9ba79a85a034a (diff) |
Fancier specular lighting.
-rw-r--r-- | hello_obj2.lc | 21 |
1 files 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 | |||
46 | -- The illum 0 can be implemented using illum 1 code by setting | 46 | -- The illum 0 can be implemented using illum 1 code by setting |
47 | -- KaIa = Kd | 47 | -- KaIa = Kd |
48 | -- Ij = 0 forall j | 48 | -- Ij = 0 forall j |
49 | -- | ||
50 | -- The spec seems to require settings for Ia and Ij (j = light number). | ||
51 | -- Ka should probably be calculated similar to Kd: using a Ka_map texture. | ||
49 | lambertianReflectance cam lightpos color texture prims = prims | 52 | lambertianReflectance cam lightpos color texture prims = prims |
50 | & mapPrimitives (\(p,n,uvw) -> | 53 | & mapPrimitives (\(p,n,uvw) -> |
51 | let light_vector = normalize $ -- light direction from surface | 54 | let light_vector = normalize $ -- light direction from surface |
@@ -78,17 +81,21 @@ blinnPhong cam lightpos color texture specular prims = prims | |||
78 | if lightpos%w == 0 | 81 | if lightpos%w == 0 |
79 | then lightpos%xyz | 82 | then lightpos%xyz |
80 | else homoproj lightpos - homoproj p | 83 | else homoproj lightpos - homoproj p |
81 | campos = cam *. V4 0 0 0 1 | ||
82 | view_vector = normalize $ homoproj campos - homoproj p | ||
83 | h = normalize $ light_vector + view_vector | ||
84 | refl = dot h (normalize n) | ||
85 | lambertian = dot light_vector (normalize n) | 84 | lambertian = dot light_vector (normalize n) |
86 | in ( coordmap cam p, V2 uvw%x (1 - uvw%y), lambertian, refl )) | 85 | in ( coordmap cam p, V2 uvw%x (1 - uvw%y), lambertian, n, p )) |
87 | & rasterizePrimitives (TriangleCtx CullBack PolygonFill NoOffset LastVertex) (Smooth,Smooth,Smooth) | 86 | & rasterizePrimitives (TriangleCtx CullBack PolygonFill NoOffset LastVertex) (Smooth,Smooth,Smooth,Smooth) |
88 | & mapFragments (\(uv,lambertian, refl) -> | 87 | & mapFragments (\(uv,lambertian, n, p) -> |
89 | let ct = texture2D (Sampler PointFilter MirroredRepeat texture) uv | 88 | let ct = texture2D (Sampler PointFilter MirroredRepeat texture) uv |
90 | ns = specular%w | 89 | ns = specular%w |
91 | ks = point $ specular%xyz | 90 | ks = point $ specular%xyz |
91 | campos = cam *. V4 0 0 0 1 | ||
92 | view_vector = normalize $ homoproj campos - homoproj p | ||
93 | h = normalize $ light_vector + view_vector | ||
94 | refl = dot h (normalize n) | ||
95 | light_vector = normalize $ -- light direction from surface | ||
96 | if lightpos%w == 0 | ||
97 | then lightpos%xyz | ||
98 | else homoproj lightpos - homoproj p | ||
92 | in (( color * ct *! (0.5 + 0.5*lambertian) + if refl>0 then ks *! pow refl ns else zero )) ) | 99 | in (( color * ct *! (0.5 + 0.5*lambertian) + if refl>0 then ks *! pow refl ns else zero )) ) |
93 | & accumulateWith (DepthOp Less True, ColorOp NoBlending (V4 True True True True)) | 100 | & accumulateWith (DepthOp Less True, ColorOp NoBlending (V4 True True True True)) |
94 | 101 | ||