Compare commits
1 commit
| Author | SHA1 | Date | |
|---|---|---|---|
| 84020abe5c |
2 changed files with 16 additions and 9 deletions
|
|
@ -23,6 +23,9 @@ const EPS = 0.1
|
||||||
const PI = math.Pi
|
const PI = math.Pi
|
||||||
|
|
||||||
const SOFT_SHADOW_COEFF = 16
|
const SOFT_SHADOW_COEFF = 16
|
||||||
|
const LIGHT_POWER_EXP = 2.0
|
||||||
|
|
||||||
|
var LIGHT_POWER_FAC float64 = 2.0
|
||||||
|
|
||||||
var lightPos Vector3
|
var lightPos Vector3
|
||||||
var scene SDF
|
var scene SDF
|
||||||
|
|
@ -33,8 +36,8 @@ var screenPhysicalSize float64
|
||||||
var cameraPos Vector3
|
var cameraPos Vector3
|
||||||
|
|
||||||
func init() {
|
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}
|
cameraPos = Vector3{0, -10, 25}
|
||||||
screenSpherical = Vector3{10, PI/2 + 0.2, PI / 2}
|
screenSpherical = Vector3{10, PI/2 + 0.2, PI / 2}
|
||||||
screenPhysicalSize = 5
|
screenPhysicalSize = 5
|
||||||
|
|
|
||||||
|
|
@ -9,26 +9,30 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func phongShading(point Vector3, normal Vector3, mat Material) Vector3 {
|
func phongShading(point Vector3, normal Vector3, mat Material) Vector3 {
|
||||||
lightVec := (lightPos.Sub(point)).Normalized()
|
lightVecNormalized := (lightPos.Sub(point)).Normalized()
|
||||||
reflectLight := Reflect(lightVec, normal)
|
|
||||||
|
reflectLight := Reflect(lightVecNormalized, normal)
|
||||||
eyeVec := (cameraPos.Sub(point)).Normalized()
|
eyeVec := (cameraPos.Sub(point)).Normalized()
|
||||||
brutMat := mat.GetMaterialBrut()
|
brutMat := mat.GetMaterialBrut()
|
||||||
ambiant := brutMat.ambiantColor.GetColor(point)
|
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)
|
diffuse := brutMat.diffuseColor.GetColor(point).Scale(diffusePower)
|
||||||
|
|
||||||
specularPower := brutMat.specularFac * math.Pow(max(0, reflectLight.Dot(eyeVec)), brutMat.specularExp)
|
specularPower := brutMat.specularFac * math.Pow(max(0, reflectLight.Dot(eyeVec)), brutMat.specularExp)
|
||||||
specular := brutMat.specularColor.GetColor(point).Scale(specularPower)
|
specular := brutMat.specularColor.GetColor(point).Scale(specularPower)
|
||||||
|
|
||||||
return ambiant.Add(diffuse).Add(specular)
|
return (ambiant.Add(diffuse).Add(specular))
|
||||||
}
|
}
|
||||||
|
|
||||||
func shadow(point Vector3) float64 {
|
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))
|
p := point.Add(direction.Scale(5 * EPS))
|
||||||
res := 1.0
|
res := 1.0
|
||||||
dist_total := 0.0
|
dist_total := 0.0
|
||||||
|
lightPower := LIGHT_POWER_FAC / math.Pow(lightVecLength, LIGHT_POWER_EXP)
|
||||||
for range MAX_STEP {
|
for range MAX_STEP {
|
||||||
dist := scene.Distance(p)
|
dist := scene.Distance(p)
|
||||||
if dist < EPS {
|
if dist < EPS {
|
||||||
|
|
@ -41,7 +45,7 @@ func shadow(point Vector3) float64 {
|
||||||
dist_total += dist
|
dist_total += dist
|
||||||
p.Radd(direction.Scale(dist))
|
p.Radd(direction.Scale(dist))
|
||||||
}
|
}
|
||||||
return res
|
return res * lightPower
|
||||||
}
|
}
|
||||||
|
|
||||||
func reflect(point Vector3, direction Vector3, normal Vector3, mat Material, reflectNumber int) Vector3 {
|
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)
|
mat := scene.GetMaterial(p)
|
||||||
normal := Gradient(scene, p, EPS).Normalized()
|
normal := Gradient(scene, p, EPS).Normalized()
|
||||||
phongShade := phongShading(p, normal, mat)
|
phongShade := phongShading(p, normal, mat)
|
||||||
shadowCoeff := max(shadow(p), 0.3)
|
shadowCoeff := max(shadow(p), 0.1)
|
||||||
reflectColor := reflect(p, direction, normal, mat, reflectNumber)
|
reflectColor := reflect(p, direction, normal, mat, reflectNumber)
|
||||||
return true, phongShade.Add(reflectColor).Scale(shadowCoeff)
|
return true, phongShade.Add(reflectColor).Scale(shadowCoeff)
|
||||||
// return true, phongShading(p, normal, *mat)
|
// return true, phongShading(p, normal, *mat)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue