From ff64be2bc65dd6a333c45eb2551fad8b063eee37 Mon Sep 17 00:00:00 2001 From: Jonathan Hughes <10040216+jphsd@users.noreply.github.com> Date: Mon, 19 Aug 2024 15:27:31 -0700 Subject: [PATCH] Additional surface fixes for FRGBA --- cmd/component.go | 2 +- surface/bumpmap.go | 8 ++------ surface/surface.go | 40 +++++++++++++++++++--------------------- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/cmd/component.go b/cmd/component.go index a6b9956..a18fbbe 100644 --- a/cmd/component.go +++ b/cmd/component.go @@ -31,7 +31,7 @@ func main() { img = texture.NewRGBA(width, height, alpha, 0, 0, 1, 1) draw.Draw(res, image.Rect(width, 0, 2*width, height), img, image.Point{}, draw.Src) // Bump Map - bm := &surface.BumpMap{nil, nil, nil, cf.Vector} + bm := &surface.BumpMap{surface.DefaultAmbient, nil, nil, cf.Vector} img = texture.NewRGBA(width, height, bm, 0, 0, 1, 1) draw.Draw(res, image.Rect(2*width, 0, 3*width, height), img, image.Point{}, draw.Src) gi.SaveImage(res, fmt.Sprintf("%06d", cnt)) diff --git a/surface/bumpmap.go b/surface/bumpmap.go index 10b905d..cf2112c 100644 --- a/surface/bumpmap.go +++ b/surface/bumpmap.go @@ -8,7 +8,7 @@ import ( // BumpMap collects the ambient light, a direct light, a material, and normal map required to describe // an area. If the normal map is nil then the standard normal is use {0, 0, 1} type BumpMap struct { - Ambient *Ambient + Ambient Ambient Direct Light Mat Material Normals texture.VectorField @@ -30,11 +30,7 @@ func (bm *BumpMap) Eval2(x, y float64) color.Color { _, amb, diff, _, _, _ := material.Eval2(x, y) // Ambient - ambient := bm.Ambient - if ambient == nil { - ambient = &DefaultAmbient - } - col := amb.Prod(ambient.Color) + col := amb.Prod(bm.Ambient.Color) // Diffuse direct := bm.Direct diff --git a/surface/surface.go b/surface/surface.go index e65d123..58b3398 100644 --- a/surface/surface.go +++ b/surface/surface.go @@ -39,11 +39,9 @@ func (s *Surface) Eval2(x, y float64) col.Color { lamb := amb.Prod(acol) // Ambient col := em // Emissive col = col.Add(lamb) - /* - if diff == nil { - return col - } - */ + if diff.A < 0.00001 { + return col + } // Cummulative diffuse and specular for all lights normal := normals.Eval2(x, y) @@ -64,24 +62,24 @@ func (s *Surface) Eval2(x, y float64) col.Color { lcol = lcol.Scale(pow / (dist * dist)) } cdiff = cdiff.Add(lcol.Prod(diff.Scale(lambert))) // Diffuse - //if spec != nil { - if blinn { - // Blinn-Phong - half := Unit([]float64{dir[0] + view[0], dir[1] + view[1], dir[2] + view[2]}) - dp := Dot(half, normal) - if dp > 0 { - phong := math.Pow(dp, shine*4) - cspec = cspec.Add(lcol.Prod(spec.Scale(phong))) // Specular - } - } else { - // Phong - dp := Dot(Reflect(dir, normal), view) - if dp > 0 { - phong := math.Pow(dp, shine) - cspec = cspec.Add(lcol.Prod(spec.Scale(phong))) // Specular + if spec.A > 0.00001 { + if blinn { + // Blinn-Phong + half := Unit([]float64{dir[0] + view[0], dir[1] + view[1], dir[2] + view[2]}) + dp := Dot(half, normal) + if dp > 0 { + phong := math.Pow(dp, shine*4) + cspec = cspec.Add(lcol.Prod(spec.Scale(phong))) // Specular + } + } else { + // Phong + dp := Dot(Reflect(dir, normal), view) + if dp > 0 { + phong := math.Pow(dp, shine) + cspec = cspec.Add(lcol.Prod(spec.Scale(phong))) // Specular + } } } - //} } col = col.Add(cdiff) col = col.Add(cspec)