diff --git a/src/material.go b/src/material.go index bca37d7..d9075ee 100644 --- a/src/material.go +++ b/src/material.go @@ -31,8 +31,11 @@ func (grid GridColor) GetColor(p Vector3) Vector3 { return grid.color2.GetColor(p) } -// Ambre je te copie -type Material struct { +type Material interface { + GetMaterialBrut() MaterialBrut +} + +type MaterialBrut struct { ambiantColor Color diffuseColor Color diffuseFac float64 @@ -45,23 +48,12 @@ type Material struct { refractIndice float64 } -func DefaultMaterial(diffuseColor Color) Material { - return Material{ - ambiantColor: ScaledColor{diffuseColor, 0.2}, - diffuseColor: diffuseColor, - diffuseFac: 1, - specularColor: Vector3{255, 255, 255}, - specularFac: 1, - specularExp: 35.0, - reflectanceFac: 0.0, - reflectanceTint: Vector3{255, 255, 255}, - refractFac: 0.0, - refractIndice: 1.0, - } +func (mat MaterialBrut) GetMaterialBrut() MaterialBrut { + return mat } -func MixMat(mat1 Material, mat2 Material, t float64, p Vector3) Material { - return Material{ +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, @@ -75,6 +67,32 @@ func MixMat(mat1 Material, mat2 Material, t float64, p Vector3) Material { } } +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{ + ambiantColor: ScaledColor{diffuseColor, 0.2}, + diffuseColor: diffuseColor, + diffuseFac: 1, + specularColor: Vector3{255, 255, 255}, + specularFac: 1, + specularExp: 35.0, + reflectanceFac: 0.0, + reflectanceTint: Vector3{255, 255, 255}, + refractFac: 0.0, + refractIndice: 1.0, + } +} + // Colors var RED = Vector3{255, 0, 0} diff --git a/src/primitives_sdf.go b/src/primitives_sdf.go index ba173ab..9999c1e 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 65e6bf3..0dc2551 100644 --- a/src/ray_marching.go +++ b/src/ray_marching.go @@ -6,6 +6,8 @@ package main import ( "math" + "runtime" + "sync" rl "github.com/gen2brain/raylib-go/raylib" ) @@ -13,8 +15,8 @@ import ( const MAX_DIST = 1000.0 const MAX_STEP = 1000 const EPS = 0.01 -const WIDTH = 250 -const HEIGHT = 250 +const WIDTH = 500 +const HEIGHT = 500 const PI = math.Pi const SOFT_SHADOW_COEFF = 32 @@ -36,7 +38,7 @@ func init() { sphereMat := RED_MAT sphereMat.specularFac = 0.5 - sphere := TranslatedSDF{Sphere{5, sphereMat}, Vector3{0, 100, 10}} + sphere := TranslatedSDF{Sphere{10, sphereMat}, Vector3{0, 100, 10}} // boxMat := BLUE_MAT // box := TranslatedSDF{Box{Vector3{10, 2, 10}, boxMat}, Vector3{0, 100, 10}} @@ -44,8 +46,8 @@ func init() { // sphere := RepeatSDF{Sphere{20, sphereMat}, Vector3{50, 50, 50}} plane := Plane{Vector3{0, 0, 1}, WHITE_GREY_GRID_MAT} - // scene = SmoothUnionSDF{SubstractionSDF{box, sphere}, plane, 2.5} - scene = UnionSDF{plane, sphere} + scene = SmoothUnionSDF{sphere, plane, 2.5} + // scene = UnionSDF{plane, sphere} } @@ -53,13 +55,14 @@ func phongShading(point Vector3, normal Vector3, mat Material) Vector3 { lightVec := (lightPos.Sub(point)).Normalized() reflectLight := Reflect(lightVec, normal) eyeVec := (cameraPos.Sub(point)).Normalized() - ambiant := mat.ambiantColor.GetColor(point) + brutMat := mat.GetMaterialBrut() + ambiant := brutMat.ambiantColor.GetColor(point) - diffusePower := mat.diffuseFac * max(0, normal.Dot(lightVec)) - diffuse := mat.diffuseColor.GetColor(point).Scale(diffusePower) + diffusePower := brutMat.diffuseFac * max(0, normal.Dot(lightVec)) + diffuse := brutMat.diffuseColor.GetColor(point).Scale(diffusePower) - specularPower := mat.specularFac * math.Pow(max(0, reflectLight.Dot(eyeVec)), mat.specularExp) - specular := mat.specularColor.GetColor(point).Scale(specularPower) + specularPower := brutMat.specularFac * math.Pow(max(0, reflectLight.Dot(eyeVec)), brutMat.specularExp) + specular := brutMat.specularColor.GetColor(point).Scale(specularPower) return ambiant.Add(diffuse).Add(specular) } @@ -90,7 +93,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 @@ -101,32 +104,52 @@ func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) { } func genImage() *rl.Image { - screenCartesian, _, theta_vec, phi_vec := screenSpherical.SphericalToCartesian() + screenCartesian, _, thetaVec, phiVec := screenSpherical.SphericalToCartesian() screenCenter := cameraPos.Add(screenCartesian) image := rl.GenImageColor(WIDTH, HEIGHT, rl.Black) - 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) + numWorkers := runtime.NumCPU() + var wg sync.WaitGroup + wg.Add(numWorkers) - origin := screenCenter.Add(phi_vec.Scale(-sx)).Add(theta_vec.Scale(sy)) - direction := (origin.Sub(cameraPos)).Normalized() + bandHeight := HEIGHT / numWorkers - 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 - } - 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}) + 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}) + } + } + }(startY, endY) } + wg.Wait() return image } diff --git a/src/sdf.go b/src/sdf.go index a43c447..64f52c0 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) - mat := MixMat(mat1, mat2, 0.5, p) - return d, mat + var mat Material = MixedMaterial{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) - mat := MixMat(mat2, mat1, t, p) - return d, mat + var mat Material = MixedMaterial{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) - mat := MixMat(mat2, mat1, t, p) - return d, mat + var mat Material = MixedMaterial{mat1, mat2, 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) - mat := MixMat(mat2, mat1, t, p) - return d, mat + var mat Material = MixedMaterial{mat1, mat2, t, p} + return d, &mat }