diff --git a/src/main.go b/src/main.go index 275e33d..1df88ac 100644 --- a/src/main.go +++ b/src/main.go @@ -23,6 +23,9 @@ const EPS = 0.1 const PI = math.Pi const SOFT_SHADOW_COEFF = 16 +const LIGHT_POWER_EXP = 2.0 + +var LIGHT_POWER_FAC float64 = 2.0 var lightPos Vector3 var scene SDF @@ -33,8 +36,8 @@ var screenPhysicalSize float64 var cameraPos Vector3 func init() { - lightPos = Vector3{100, -400, 400} - + lightPos = Vector3{0, -200, 100} + LIGHT_POWER_FAC = math.Pow(lightPos.Length(), LIGHT_POWER_EXP) * LIGHT_POWER_FAC cameraPos = Vector3{0, -10, 25} screenSpherical = Vector3{10, PI/2 + 0.2, PI / 2} screenPhysicalSize = 5 diff --git a/src/ray_marching.go b/src/ray_marching.go index 0cf1b17..b9c59b3 100644 --- a/src/ray_marching.go +++ b/src/ray_marching.go @@ -9,26 +9,30 @@ import ( ) func phongShading(point Vector3, normal Vector3, mat Material) Vector3 { - lightVec := (lightPos.Sub(point)).Normalized() - reflectLight := Reflect(lightVec, normal) + lightVecNormalized := (lightPos.Sub(point)).Normalized() + + reflectLight := Reflect(lightVecNormalized, normal) eyeVec := (cameraPos.Sub(point)).Normalized() brutMat := mat.GetMaterialBrut() ambiant := brutMat.ambiantColor.GetColor(point) - diffusePower := brutMat.diffuseFac * max(0, normal.Dot(lightVec)) + diffusePower := brutMat.diffuseFac * max(0, normal.Dot(lightVecNormalized)) diffuse := brutMat.diffuseColor.GetColor(point).Scale(diffusePower) 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) + return (ambiant.Add(diffuse).Add(specular)) } func shadow(point Vector3) float64 { - direction := (lightPos.Sub(point)).Normalized() + lightVec := (lightPos.Sub(point)) + lightVecLength := lightVec.Length() + direction := lightVec.Scale(1 / lightVecLength) p := point.Add(direction.Scale(5 * EPS)) res := 1.0 dist_total := 0.0 + lightPower := LIGHT_POWER_FAC / math.Pow(lightVecLength, LIGHT_POWER_EXP) for range MAX_STEP { dist := scene.Distance(p) if dist < EPS { @@ -41,7 +45,7 @@ func shadow(point Vector3) float64 { dist_total += dist p.Radd(direction.Scale(dist)) } - return res + return res * lightPower } func reflect(point Vector3, direction Vector3, normal Vector3, mat Material, reflectNumber int) Vector3 { @@ -67,7 +71,7 @@ func rayMarch(origin Vector3, direction Vector3, reflectNumber int) (bool, Vecto mat := scene.GetMaterial(p) normal := Gradient(scene, p, EPS).Normalized() phongShade := phongShading(p, normal, mat) - shadowCoeff := max(shadow(p), 0.3) + shadowCoeff := max(shadow(p), 0.1) reflectColor := reflect(p, direction, normal, mat, reflectNumber) return true, phongShade.Add(reflectColor).Scale(shadowCoeff) // return true, phongShading(p, normal, *mat)