Phong shading
This commit is contained in:
parent
f5d4a5cb7e
commit
9e8028f904
2 changed files with 24 additions and 3 deletions
|
|
@ -17,6 +17,7 @@ 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, RED_MAT}
|
||||
var plane Plane = Plane{Vector3{0, 0, 1}, -10, GREY_MAT}
|
||||
var scene UnionSDF = UnionSDF{sphere, plane}
|
||||
|
|
@ -27,6 +28,22 @@ var cameraPos Vector3 = Vector3{0, -10, 0}
|
|||
var screenSpherical Vector3 = Vector3{10, PI / 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)
|
||||
eyeVec := (cameraPos.Sub(point)).Normalized()
|
||||
|
||||
ambiant := mat.ambiantColor.GetColor(point).Scale(0.1)
|
||||
|
||||
diffusePower := mat.diffuseFac * max(0, normal.Dot(lightVec)) * 0.5
|
||||
diffuse := mat.diffuseColor.GetColor(point).Scale(diffusePower)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) {
|
||||
p := origin
|
||||
for range MAX_STEP {
|
||||
|
|
@ -34,7 +51,8 @@ func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) {
|
|||
// fmt.Println(dist)
|
||||
if dist < EPS {
|
||||
// fmt.Println("hit")
|
||||
return true, mat.diffuseColor.GetColor(p)
|
||||
normal := Gradient(scene, p, EPS).Normalized()
|
||||
return true, phongShading(p, normal, mat)
|
||||
}
|
||||
if dist > MAX_DIST {
|
||||
break
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue