diff --git a/src/material.go b/src/material.go index d9075ee..bca37d7 100644 --- a/src/material.go +++ b/src/material.go @@ -31,11 +31,8 @@ func (grid GridColor) GetColor(p Vector3) Vector3 { return grid.color2.GetColor(p) } -type Material interface { - GetMaterialBrut() MaterialBrut -} - -type MaterialBrut struct { +// Ambre je te copie +type Material struct { ambiantColor Color diffuseColor Color diffuseFac float64 @@ -48,38 +45,8 @@ type MaterialBrut struct { refractIndice float64 } -func (mat MaterialBrut) GetMaterialBrut() MaterialBrut { - return mat -} - -func MixMat(mat1 MaterialBrut, mat2 MaterialBrut, t float64, p Vector3) MaterialBrut { - return MaterialBrut{ - (mat1.ambiantColor.GetColor(p).Scale(1 - t)).Add((mat2.ambiantColor.GetColor(p)).Scale(t)), - (mat1.diffuseColor.GetColor(p).Scale(1 - t)).Add((mat2.diffuseColor.GetColor(p)).Scale(t)), - mat1.diffuseFac*(1-t) + mat2.diffuseFac*t, - mat1.specularColor.GetColor(p).Scale(1 - t).Add((mat2.specularColor.GetColor(p)).Scale(t)), - mat1.specularFac*(1-t) + mat2.specularFac*t, - mat1.specularExp*(1-t) + mat2.specularExp*t, - (mat1.reflectanceFac*(1-t) + mat2.reflectanceFac) * t, - mat1.reflectanceTint.GetColor(p).Scale(1 - t).Add((mat2.reflectanceTint.GetColor(p)).Scale(t)), - mat1.refractFac*(1-t) + mat2.refractFac*t, - mat1.refractIndice*(1-t) + mat2.refractIndice*t, - } -} - -type MixedMaterial struct { - mat1 *Material - mat2 *Material - t float64 - p Vector3 -} - -func (mixedMat MixedMaterial) GetMaterialBrut() MaterialBrut { - return MixMat((*mixedMat.mat1).GetMaterialBrut(), (*mixedMat.mat2).GetMaterialBrut(), mixedMat.t, mixedMat.p) -} - -func DefaultMaterial(diffuseColor Color) MaterialBrut { - return MaterialBrut{ +func DefaultMaterial(diffuseColor Color) Material { + return Material{ ambiantColor: ScaledColor{diffuseColor, 0.2}, diffuseColor: diffuseColor, diffuseFac: 1, @@ -93,6 +60,21 @@ func DefaultMaterial(diffuseColor Color) MaterialBrut { } } +func MixMat(mat1 Material, mat2 Material, t float64, p Vector3) Material { + return Material{ + (mat1.ambiantColor.GetColor(p).Scale(1 - t)).Add((mat2.ambiantColor.GetColor(p)).Scale(t)), + (mat1.diffuseColor.GetColor(p).Scale(1 - t)).Add((mat2.diffuseColor.GetColor(p)).Scale(t)), + mat1.diffuseFac*(1-t) + mat2.diffuseFac*t, + mat1.specularColor.GetColor(p).Scale(1 - t).Add((mat2.specularColor.GetColor(p)).Scale(t)), + mat1.specularFac*(1-t) + mat2.specularFac*t, + mat1.specularExp*(1-t) + mat2.specularExp*t, + (mat1.reflectanceFac*(1-t) + mat2.reflectanceFac) * t, + mat1.reflectanceTint.GetColor(p).Scale(1 - t).Add((mat2.reflectanceTint.GetColor(p)).Scale(t)), + mat1.refractFac*(1-t) + mat2.refractFac*t, + mat1.refractIndice*(1-t) + mat2.refractIndice*t, + } +} + // Colors var RED = Vector3{255, 0, 0} diff --git a/src/primitives_sdf.go b/src/primitives_sdf.go index 9999c1e..ba173ab 100644 --- a/src/primitives_sdf.go +++ b/src/primitives_sdf.go @@ -6,8 +6,8 @@ type Sphere struct { material Material } -func (s Sphere) Distance(p Vector3) (float64, *Material) { - return p.Length() - s.radius, &s.material +func (s Sphere) Distance(p Vector3) (float64, Material) { + return p.Length() - s.radius, s.material } // Box @@ -16,9 +16,9 @@ type Box struct { material Material } -func (s Box) Distance(p Vector3) (float64, *Material) { +func (s Box) Distance(p Vector3) (float64, Material) { q := p.Abs().Sub(s.dimensions) - return q.Max(0.0).Length() + min(max(q.X, max(q.Y, q.Z)), 0.0), &s.material + return q.Max(0.0).Length() + min(max(q.X, max(q.Y, q.Z)), 0.0), s.material } // Plane @@ -27,6 +27,6 @@ type Plane struct { material Material } -func (s Plane) Distance(p Vector3) (float64, *Material) { - return p.Dot(s.normal), &s.material +func (s Plane) Distance(p Vector3) (float64, Material) { + return p.Dot(s.normal), s.material } diff --git a/src/ray_marching.go b/src/ray_marching.go index 0dc2551..65e6bf3 100644 --- a/src/ray_marching.go +++ b/src/ray_marching.go @@ -6,8 +6,6 @@ package main import ( "math" - "runtime" - "sync" rl "github.com/gen2brain/raylib-go/raylib" ) @@ -15,8 +13,8 @@ import ( const MAX_DIST = 1000.0 const MAX_STEP = 1000 const EPS = 0.01 -const WIDTH = 500 -const HEIGHT = 500 +const WIDTH = 250 +const HEIGHT = 250 const PI = math.Pi const SOFT_SHADOW_COEFF = 32 @@ -38,7 +36,7 @@ func init() { sphereMat := RED_MAT sphereMat.specularFac = 0.5 - sphere := TranslatedSDF{Sphere{10, sphereMat}, Vector3{0, 100, 10}} + sphere := TranslatedSDF{Sphere{5, sphereMat}, Vector3{0, 100, 10}} // boxMat := BLUE_MAT // box := TranslatedSDF{Box{Vector3{10, 2, 10}, boxMat}, Vector3{0, 100, 10}} @@ -46,8 +44,8 @@ func init() { // sphere := RepeatSDF{Sphere{20, sphereMat}, Vector3{50, 50, 50}} plane := Plane{Vector3{0, 0, 1}, WHITE_GREY_GRID_MAT} - scene = SmoothUnionSDF{sphere, plane, 2.5} - // scene = UnionSDF{plane, sphere} + // scene = SmoothUnionSDF{SubstractionSDF{box, sphere}, plane, 2.5} + scene = UnionSDF{plane, sphere} } @@ -55,14 +53,13 @@ func phongShading(point Vector3, normal Vector3, mat Material) Vector3 { lightVec := (lightPos.Sub(point)).Normalized() reflectLight := Reflect(lightVec, normal) eyeVec := (cameraPos.Sub(point)).Normalized() - brutMat := mat.GetMaterialBrut() - ambiant := brutMat.ambiantColor.GetColor(point) + ambiant := mat.ambiantColor.GetColor(point) - diffusePower := brutMat.diffuseFac * max(0, normal.Dot(lightVec)) - diffuse := brutMat.diffuseColor.GetColor(point).Scale(diffusePower) + diffusePower := mat.diffuseFac * max(0, normal.Dot(lightVec)) + diffuse := mat.diffuseColor.GetColor(point).Scale(diffusePower) - specularPower := brutMat.specularFac * math.Pow(max(0, reflectLight.Dot(eyeVec)), brutMat.specularExp) - specular := brutMat.specularColor.GetColor(point).Scale(specularPower) + specularPower := mat.specularFac * math.Pow(max(0, reflectLight.Dot(eyeVec)), mat.specularExp) + specular := mat.specularColor.GetColor(point).Scale(specularPower) return ambiant.Add(diffuse).Add(specular) } @@ -93,7 +90,7 @@ func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) { dist, mat := scene.Distance(p) if dist < EPS { normal := Gradient(scene, p, EPS).Normalized() - return true, phongShading(p, normal, *mat).Scale(max(shadow(p), 0.3)) + return true, phongShading(p, normal, mat).Scale(max(shadow(p), 0.3)) } if dist > MAX_DIST { break @@ -104,52 +101,32 @@ func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) { } func genImage() *rl.Image { - screenCartesian, _, thetaVec, phiVec := screenSpherical.SphericalToCartesian() + screenCartesian, _, theta_vec, phi_vec := screenSpherical.SphericalToCartesian() screenCenter := cameraPos.Add(screenCartesian) image := rl.GenImageColor(WIDTH, HEIGHT, rl.Black) - numWorkers := runtime.NumCPU() - var wg sync.WaitGroup - wg.Add(numWorkers) + for pixel_x := range WIDTH { + for pixel_y := range HEIGHT { + sx := screenPhysicalSize * (float64(pixel_x)/WIDTH - 0.5) + sy := screenPhysicalSize * (float64(pixel_y)/WIDTH - 0.5) - bandHeight := HEIGHT / numWorkers + origin := screenCenter.Add(phi_vec.Scale(-sx)).Add(theta_vec.Scale(sy)) + direction := (origin.Sub(cameraPos)).Normalized() - for worker := range numWorkers { - startY := worker * bandHeight - endY := startY + bandHeight - if worker == numWorkers-1 { - endY = HEIGHT - } - - go func(y0, y1 int) { - defer wg.Done() - - for pixelY := y0; pixelY < y1; pixelY++ { - for pixelX := range WIDTH { - sx := screenPhysicalSize * (float64(pixelX)/WIDTH - 0.5) - sy := screenPhysicalSize * (float64(pixelY)/HEIGHT - 0.5) - - origin := screenCenter.Add(phiVec.Scale(-sx)).Add(thetaVec.Scale(sy)) - direction := (origin.Sub(cameraPos)).Normalized() - - hit, colorVec := rayMarch(origin, direction) - var fr, fg, fb float64 - if hit { - fr, fg, fb = colorVec.Unpack() - } else { - fr, fg, fb = 0, 0, 0 - } - fr, fg, fb = Clamp(fr, 0, 255), Clamp(fg, 0, 255), Clamp(fb, 0, 255) - r, g, b := uint8(fr), uint8(fg), uint8(fb) - - rl.ImageDrawPixel(image, int32(pixelX), int32(pixelY), rl.Color{R: r, G: g, B: b, A: 255}) - } + hit, color_vec := rayMarch(origin, direction) + var fr, fg, fb float64 + if hit { + fr, fg, fb = color_vec.Unpack() + } else { + fr, fg, fb = 0, 0, 0 } - }(startY, endY) + fr, fg, fb = Clamp(fr, 0, 255), Clamp(fg, 0, 255), Clamp(fb, 0, 255) + r, g, b := uint8(fr), uint8(fg), uint8(fb) + rl.ImageDrawPixel(image, int32(pixel_x), int32(pixel_y), rl.Color{R: r, G: g, B: b, A: 255}) + } } - wg.Wait() return image } diff --git a/src/sdf.go b/src/sdf.go index 64f52c0..a43c447 100644 --- a/src/sdf.go +++ b/src/sdf.go @@ -5,7 +5,7 @@ import ( ) type SDF interface { - Distance(Vector3) (float64, *Material) + Distance(Vector3) (float64, Material) } func DistanceOnly(sdf SDF, p Vector3) float64 { @@ -27,7 +27,7 @@ type TranslatedSDF struct { translate Vector3 } -func (s TranslatedSDF) Distance(p Vector3) (float64, *Material) { +func (s TranslatedSDF) Distance(p Vector3) (float64, Material) { return s.primitive.Distance(p.Sub(s.translate)) } @@ -37,7 +37,7 @@ type RotatedSDF struct { angle float64 } -func (s RotatedSDF) Distance(p Vector3) (float64, *Material) { +func (s RotatedSDF) Distance(p Vector3) (float64, Material) { rotated_p := Rotate(p, s.rotVector, s.angle) return s.primitive.Distance(rotated_p) } @@ -47,7 +47,7 @@ type ScaledSDF struct { scale float64 } -func (s ScaledSDF) Distance(p Vector3) (float64, *Material) { +func (s ScaledSDF) Distance(p Vector3) (float64, Material) { dist, color := s.primitive.Distance(p.Scale(1 / s.scale)) return dist * s.scale, color } @@ -57,7 +57,7 @@ type RepeatSDF struct { cellSize Vector3 } -func (s RepeatSDF) Distance(p Vector3) (float64, *Material) { +func (s RepeatSDF) Distance(p Vector3) (float64, Material) { x, y, z := p.Unpack() sx, sy, sz := s.cellSize.Unpack() round := math.RoundToEven @@ -70,7 +70,7 @@ type UnionSDF struct { primitive2 SDF } -func (s UnionSDF) Distance(p Vector3) (float64, *Material) { +func (s UnionSDF) Distance(p Vector3) (float64, Material) { d1, color1 := s.primitive1.Distance(p) d2, color2 := s.primitive2.Distance(p) d := math.Min(d1, d2) @@ -87,7 +87,7 @@ type SubstractionSDF struct { primitive2 SDF } -func (s SubstractionSDF) Distance(p Vector3) (float64, *Material) { +func (s SubstractionSDF) Distance(p Vector3) (float64, Material) { d1, color1 := s.primitive1.Distance(p) d2, _ := s.primitive2.Distance(p) @@ -100,14 +100,14 @@ type IntersectionSDF struct { primitive2 SDF } -func (s IntersectionSDF) Distance(p Vector3) (float64, *Material) { +func (s IntersectionSDF) Distance(p Vector3) (float64, Material) { d1, mat1 := s.primitive1.Distance(p) d2, mat2 := s.primitive2.Distance(p) d := math.Max(d1, d2) - var mat Material = MixedMaterial{mat1, mat2, 0.5, p} - return d, &mat + mat := MixMat(mat1, mat2, 0.5, p) + return d, mat } type SmoothUnionSDF struct { @@ -116,15 +116,15 @@ type SmoothUnionSDF struct { k float64 } -func (s SmoothUnionSDF) Distance(p Vector3) (float64, *Material) { +func (s SmoothUnionSDF) Distance(p Vector3) (float64, Material) { k := 4 * s.k d1, mat1 := s.primitive1.Distance(p) d2, mat2 := s.primitive2.Distance(p) h := math.Max(k-math.Abs(d1-d2), 0.0) d := math.Min(d1, d2) - h*h*0.25/k t := SmoothStep(d2-d1, -k, k) - var mat Material = MixedMaterial{mat2, mat1, t, p} - return d, &mat + mat := MixMat(mat2, mat1, t, p) + return d, mat } type SmoothSubstractionSDF struct { @@ -133,15 +133,15 @@ type SmoothSubstractionSDF struct { k float64 } -func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, *Material) { +func (s SmoothSubstractionSDF) Distance(p Vector3) (float64, Material) { k := 4 * s.k d1, mat1 := s.primitive1.Distance(p) d2, mat2 := s.primitive2.Distance(p) h := math.Max(k-math.Abs(-d1-d2), 0.0) d := math.Max(d1, -d2) + h*h*0.25/k t := SmoothStep(d1-d2, -k, k) - var mat Material = MixedMaterial{mat1, mat2, t, p} - return d, &mat + mat := MixMat(mat2, mat1, t, p) + return d, mat } type SmoothIntersectionSDF struct { @@ -150,13 +150,13 @@ type SmoothIntersectionSDF struct { k float64 } -func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, *Material) { +func (s SmoothIntersectionSDF) Distance(p Vector3) (float64, Material) { k := 4 * s.k d1, mat1 := s.primitive1.Distance(p) d2, mat2 := s.primitive2.Distance(p) h := math.Max(k-math.Abs(d1-d2), 0.0) d := math.Max(d1, d2) + h*h*0.25/k t := SmoothStep(d2-d1, -k, k) - var mat Material = MixedMaterial{mat1, mat2, t, p} - return d, &mat + mat := MixMat(mat2, mat1, t, p) + return d, mat }