diff options
Diffstat (limited to 'hello_obj2.lc')
-rw-r--r-- | hello_obj2.lc | 101 |
1 files changed, 93 insertions, 8 deletions
diff --git a/hello_obj2.lc b/hello_obj2.lc index 7e7defc..a6df7b7 100644 --- a/hello_obj2.lc +++ b/hello_obj2.lc | |||
@@ -10,6 +10,88 @@ blendplane = -- NoBlending -- BlendLogicOp Xor | |||
10 | point :: Vec 3 Float -> Vec 4 Float | 10 | point :: Vec 3 Float -> Vec 4 Float |
11 | point p = V4 p%x p%y p%z 1 | 11 | point p = V4 p%x p%y p%z 1 |
12 | 12 | ||
13 | homoproj :: Vec 4 Float -> Vec 3 Float | ||
14 | homoproj v = v%xyz *! (1 / v%w) | ||
15 | |||
16 | type Position = Vec 4 Float | ||
17 | type TextureCoord = Vec 3 Float | ||
18 | type Normal = Vec 3 Float | ||
19 | type Overlay = ( (FragmentOperation Depth, FragmentOperation (Color (VecScalar 4 Float))) | ||
20 | , FragmentStream 1 ((Vec 4 Float)) ) | ||
21 | |||
22 | -- This lighting model ignores the normal vector and simply applies | ||
23 | -- ambient lighting and a texture map. | ||
24 | -- | ||
25 | -- "Color on and Ambient off" | ||
26 | -- inputs: Kd, map_Kd | ||
27 | -- | ||
28 | -- illum 0 -- color = Kd | ||
29 | ambientOnly :: Mat 4 4 Float -> Vec 4 Float -> Texture -> PrimitiveStream Triangle (Position,TextureCoord,Normal) | ||
30 | -> Overlay | ||
31 | ambientOnly cam color texture prims = prims | ||
32 | & mapPrimitives (\(p,n,uvw) -> ( coordmap cam p, V2 uvw%x (1 - uvw%y) )) | ||
33 | & rasterizePrimitives (TriangleCtx CullBack PolygonFill NoOffset LastVertex) ((Smooth)) | ||
34 | & mapFragments (\((uv)) -> ((color * texture2D (Sampler PointFilter MirroredRepeat texture) uv ))) | ||
35 | & accumulateWith (DepthOp Less True, ColorOp NoBlending (V4 True True True True)) | ||
36 | |||
37 | -- ½ ambient + ½ lambertian reflectance | ||
38 | -- lambertian reflectance = (light ∙ normal) * diffuse * intensity | ||
39 | -- I_d = (L ∙ N) C_d I_L | ||
40 | -- | ||
41 | -- "Color on and Ambient on" | ||
42 | -- Chrome example indicates: Ka, Kd, Ks, refl | ||
43 | -- | ||
44 | -- illum 1 -- color = KaIa + Kd { SUM j=1..ls, (N ∙ Lj)Ij } | ||
45 | -- | ||
46 | -- The illum 0 can be implemented using illum 1 code by setting | ||
47 | -- KaIa = Kd | ||
48 | -- Ij = 0 forall j | ||
49 | lambertianReflectance cam lightpos color texture prims = prims | ||
50 | & mapPrimitives (\(p,n,uvw) -> | ||
51 | let light_vector = normalize $ -- light direction from surface | ||
52 | if lightpos%w == 0 | ||
53 | then lightpos%xyz | ||
54 | else homoproj lightpos - homoproj p | ||
55 | lambertian = dot light_vector (normalize n) | ||
56 | in ( coordmap cam p, V2 uvw%x (1 - uvw%y), lambertian )) | ||
57 | & rasterizePrimitives (TriangleCtx CullBack PolygonFill NoOffset LastVertex) (Smooth,Smooth) | ||
58 | & mapFragments (\(uv,lambertian) -> ((color * texture2D (Sampler PointFilter MirroredRepeat texture) uv *! (0.5 + 0.5*lambertian) ))) | ||
59 | & accumulateWith (DepthOp Less True, ColorOp NoBlending (V4 True True True True)) | ||
60 | |||
61 | |||
62 | -- Blinn-Phong | ||
63 | -- | ||
64 | -- illum 0 -- color = Kd | ||
65 | -- illum 1 -- color = KaIa + Kd { SUM j=1..ls, (N ∙ Lj)Ij } | ||
66 | -- illum 2 -- color = KaIa + Kd { SUM j=1..ls, (N ∙ Lj)Ij } + Ks { SUM j=1..ls, ((H∙Hj)^Ns)Ij } | ||
67 | -- | ||
68 | -- The illum 1 can be implemented using illum2 code by setting | ||
69 | -- Ks = 0 (specular reflectance) | ||
70 | -- | ||
71 | -- TODO: Add (H∙Hj)^Ns term | ||
72 | -- | ||
73 | -- Okay, I don't understand why the spec says H∙Hj. I think it means N∙Hj. | ||
74 | -- So i'm implementing that. | ||
75 | blinnPhong cam lightpos color texture specular prims = prims | ||
76 | & mapPrimitives (\(p,n,uvw) -> | ||
77 | let light_vector = normalize $ -- light direction from surface | ||
78 | if lightpos%w == 0 | ||
79 | then lightpos%xyz | ||
80 | 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) | ||
86 | in ( coordmap cam p, V2 uvw%x (1 - uvw%y), lambertian, refl )) | ||
87 | & rasterizePrimitives (TriangleCtx CullBack PolygonFill NoOffset LastVertex) (Smooth,Smooth,Smooth) | ||
88 | & mapFragments (\(uv,lambertian, refl) -> | ||
89 | let ct = texture2D (Sampler PointFilter MirroredRepeat texture) uv | ||
90 | ns = specular%w | ||
91 | ks = point $ specular%xyz | ||
92 | 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)) | ||
94 | |||
13 | 95 | ||
14 | makeFrame (cubemap :: TextureCube) | 96 | makeFrame (cubemap :: TextureCube) |
15 | (skybox :: PrimitiveStream Triangle ((Vec 3 Float))) | 97 | (skybox :: PrimitiveStream Triangle ((Vec 3 Float))) |
@@ -17,7 +99,10 @@ makeFrame (cubemap :: TextureCube) | |||
17 | (cam :: Mat 4 4 Float) | 99 | (cam :: Mat 4 4 Float) |
18 | (color :: Vec 4 Float) | 100 | (color :: Vec 4 Float) |
19 | (texture :: Texture) | 101 | (texture :: Texture) |
20 | (prims :: PrimitiveStream Triangle (Vec 4 Float, Vec 3 Float, Vec 3 Float)) | 102 | (specular :: Vec 4 Float) |
103 | (prims0 :: PrimitiveStream Triangle (Vec 4 Float, Vec 3 Float, Vec 3 Float)) | ||
104 | (prims1 :: PrimitiveStream Triangle (Vec 4 Float, Vec 3 Float, Vec 3 Float)) | ||
105 | (prims2 :: PrimitiveStream Triangle (Vec 4 Float, Vec 3 Float, Vec 3 Float)) | ||
21 | (plane :: PrimitiveStream Triangle ((Vec 4 Float))) | 106 | (plane :: PrimitiveStream Triangle ((Vec 4 Float))) |
22 | (lines :: PrimitiveStream Line (Vec 3 Float, Vec 3 Float)) | 107 | (lines :: PrimitiveStream Line (Vec 3 Float, Vec 3 Float)) |
23 | (points :: PrimitiveStream Point (Vec 3 Float, Vec 3 Float)) | 108 | (points :: PrimitiveStream Point (Vec 3 Float, Vec 3 Float)) |
@@ -32,12 +117,9 @@ makeFrame (cubemap :: TextureCube) | |||
32 | & rasterizePrimitives (TriangleCtx CullNone PolygonFill NoOffset LastVertex) ((Smooth)) | 117 | & rasterizePrimitives (TriangleCtx CullNone PolygonFill NoOffset LastVertex) ((Smooth)) |
33 | & mapFragments (\((d)) -> (( textureCube cubemap d ))) | 118 | & mapFragments (\((d)) -> (( textureCube cubemap d ))) |
34 | & accumulateWith (DepthOp Always False, ColorOp NoBlending (V4 True True True True)) | 119 | & accumulateWith (DepthOp Always False, ColorOp NoBlending (V4 True True True True)) |
35 | `overlay` | 120 | `overlay` ambientOnly cam color texture prims0 |
36 | prims | 121 | `overlay` lambertianReflectance cam (V4 0 20 10 1) color texture prims1 |
37 | & mapPrimitives (\(p,n,uvw) -> ( coordmap cam p, V2 uvw%x (1 - uvw%y) )) | 122 | `overlay` blinnPhong cam (V4 0 20 10 1) color texture specular prims2 |
38 | & rasterizePrimitives (TriangleCtx CullBack PolygonFill NoOffset LastVertex) ((Smooth)) | ||
39 | & mapFragments (\((uv)) -> ((color * texture2D (Sampler PointFilter MirroredRepeat texture) uv ))) | ||
40 | & accumulateWith (DepthOp Less True, ColorOp NoBlending (V4 True True True True)) | ||
41 | `overlay` | 123 | `overlay` |
42 | plane | 124 | plane |
43 | & mapPrimitives (\((p)) -> let p' = coordmap cam $ plane_mat *. p in (p', p%xz)) | 125 | & mapPrimitives (\((p)) -> let p' = coordmap cam $ plane_mat *. p in (p', p%xz)) |
@@ -89,7 +171,10 @@ main = renderFrame $ | |||
89 | (Uniform "ViewProjection") | 171 | (Uniform "ViewProjection") |
90 | (Uniform "diffuseColor") | 172 | (Uniform "diffuseColor") |
91 | (Texture2DSlot "diffuseTexture") | 173 | (Texture2DSlot "diffuseTexture") |
92 | (fetch "objects" (Attribute "position", Attribute "normal", Attribute "uvw")) | 174 | (Uniform "specularReflectivity") |
175 | (fetch "objects0" (Attribute "position", Attribute "normal", Attribute "uvw")) | ||
176 | (fetch "objects1" (Attribute "position", Attribute "normal", Attribute "uvw")) | ||
177 | (fetch "objects2" (Attribute "position", Attribute "normal", Attribute "uvw")) | ||
93 | (fetch "plane" ((Attribute "position"))) | 178 | (fetch "plane" ((Attribute "position"))) |
94 | (fetch "Curve" (Attribute "position", Attribute "color")) | 179 | (fetch "Curve" (Attribute "position", Attribute "color")) |
95 | (fetch "Points" (Attribute "position", Attribute "color")) | 180 | (fetch "Points" (Attribute "position", Attribute "color")) |