bug solving phong specular

This commit is contained in:
Crizomb 2025-09-26 22:57:06 +02:00
parent a7b5306fa9
commit 60730edb7b
3 changed files with 13 additions and 13 deletions

View file

@ -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})
}