From 60730edb7b5fd10f940e1ceeddcd0f3246da4b6b Mon Sep 17 00:00:00 2001 From: Crizomb Date: Fri, 26 Sep 2025 22:57:06 +0200 Subject: [PATCH] bug solving phong specular --- material.go | 4 ++-- ray_marching.go | 20 ++++++++++---------- vec3.go | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/material.go b/material.go index f78f30f..51f0b16 100644 --- a/material.go +++ b/material.go @@ -42,8 +42,8 @@ func DefaultMaterial(ambiantColor Color) Material { ambiantColor: ambiantColor, diffuseColor: Vector3{255, 255, 255}, diffuseFac: 1.0, - specularColor: Vector3{1.0, 1.0, 1.0}, - specularFac: 0.5, + specularColor: Vector3{0, 255, 0}, + specularFac: 1.0, specularExp: 32.0, reflectanceFac: 0.0, reflectanceTint: Vector3{1.0, 1.0, 1.0}, diff --git a/ray_marching.go b/ray_marching.go index e1b3f47..b077eff 100644 --- a/ray_marching.go +++ b/ray_marching.go @@ -11,34 +11,33 @@ import ( ) const MAX_DIST = 1000.0 -const MAX_STEP = 100 -const EPS = 0.1 +const MAX_STEP = 1000 +const EPS = 0.01 const WIDTH = 500 const HEIGHT = 500 const PI = math.Pi -var lightPos Vector3 = Vector3{0, 200, 400} -var sphere Sphere = Sphere{Vector3{0, 100, 10}, 10, WHITE_GREY_GRID_MAT} +var lightPos Vector3 = Vector3{0, -200, 600} +var sphere Sphere = Sphere{Vector3{0, 100, 10}, 10, RED_MAT} var plane Plane = Plane{Vector3{0, 0, 1}, -10, GRID_OF_GRID_MAT} var scene UnionSDF = UnionSDF{sphere, plane} -var cameraPos Vector3 = Vector3{0, -10, 0} +var cameraPos Vector3 = Vector3{0, -10, 30} // radius, theta, phi -var screenSpherical Vector3 = Vector3{10, PI / 2, PI / 2} +var screenSpherical Vector3 = Vector3{10, PI/2 + 0.2, PI / 2} var screenPhysicalSize float64 = 5 func phongShading(point Vector3, normal Vector3, mat Material) Vector3 { lightVec := (lightPos.Sub(point)).Normalized() - reflectLight := Reflect(lightVec, normal) + reflectLight := Reflect(lightVec.Scale(-1), normal) eyeVec := (cameraPos.Sub(point)).Normalized() - ambiant := mat.ambiantColor.GetColor(point).Scale(0.1) - diffusePower := mat.diffuseFac * max(0, normal.Dot(lightVec)) * 0.5 + diffusePower := mat.diffuseFac * max(0, normal.Dot(lightVec)) diffuse := mat.diffuseColor.GetColor(point).Scale(diffusePower) - specularPower := mat.specularFac * math.Pow(max(0, reflectLight.Dot(eyeVec)), mat.specularExp) + 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) @@ -89,6 +88,7 @@ func genImage() *rl.Image { } 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}) } diff --git a/vec3.go b/vec3.go index 8bc99af..1b68568 100644 --- a/vec3.go +++ b/vec3.go @@ -69,7 +69,7 @@ func (u Vector3) Min(x float64) Vector3 { // i incident, n normal. Both vector should be normalized func Reflect(i Vector3, n Vector3) Vector3 { y := i.Dot(n) - return i.Add(n.Scale(2 * y)) + return (i.Add(n.Scale(2 * y))).Normalized() } // Todo : Refract