bug solving phong specular
This commit is contained in:
parent
a7b5306fa9
commit
60730edb7b
3 changed files with 13 additions and 13 deletions
|
|
@ -42,8 +42,8 @@ func DefaultMaterial(ambiantColor Color) Material {
|
||||||
ambiantColor: ambiantColor,
|
ambiantColor: ambiantColor,
|
||||||
diffuseColor: Vector3{255, 255, 255},
|
diffuseColor: Vector3{255, 255, 255},
|
||||||
diffuseFac: 1.0,
|
diffuseFac: 1.0,
|
||||||
specularColor: Vector3{1.0, 1.0, 1.0},
|
specularColor: Vector3{0, 255, 0},
|
||||||
specularFac: 0.5,
|
specularFac: 1.0,
|
||||||
specularExp: 32.0,
|
specularExp: 32.0,
|
||||||
reflectanceFac: 0.0,
|
reflectanceFac: 0.0,
|
||||||
reflectanceTint: Vector3{1.0, 1.0, 1.0},
|
reflectanceTint: Vector3{1.0, 1.0, 1.0},
|
||||||
|
|
|
||||||
|
|
@ -11,34 +11,33 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const MAX_DIST = 1000.0
|
const MAX_DIST = 1000.0
|
||||||
const MAX_STEP = 100
|
const MAX_STEP = 1000
|
||||||
const EPS = 0.1
|
const EPS = 0.01
|
||||||
const WIDTH = 500
|
const WIDTH = 500
|
||||||
const HEIGHT = 500
|
const HEIGHT = 500
|
||||||
const PI = math.Pi
|
const PI = math.Pi
|
||||||
|
|
||||||
var lightPos Vector3 = Vector3{0, 200, 400}
|
var lightPos Vector3 = Vector3{0, -200, 600}
|
||||||
var sphere Sphere = Sphere{Vector3{0, 100, 10}, 10, WHITE_GREY_GRID_MAT}
|
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 plane Plane = Plane{Vector3{0, 0, 1}, -10, GRID_OF_GRID_MAT}
|
||||||
var scene UnionSDF = UnionSDF{sphere, plane}
|
var scene UnionSDF = UnionSDF{sphere, plane}
|
||||||
|
|
||||||
var cameraPos Vector3 = Vector3{0, -10, 0}
|
var cameraPos Vector3 = Vector3{0, -10, 30}
|
||||||
|
|
||||||
// radius, theta, phi
|
// 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
|
var screenPhysicalSize float64 = 5
|
||||||
|
|
||||||
func phongShading(point Vector3, normal Vector3, mat Material) Vector3 {
|
func phongShading(point Vector3, normal Vector3, mat Material) Vector3 {
|
||||||
lightVec := (lightPos.Sub(point)).Normalized()
|
lightVec := (lightPos.Sub(point)).Normalized()
|
||||||
reflectLight := Reflect(lightVec, normal)
|
reflectLight := Reflect(lightVec.Scale(-1), normal)
|
||||||
eyeVec := (cameraPos.Sub(point)).Normalized()
|
eyeVec := (cameraPos.Sub(point)).Normalized()
|
||||||
|
|
||||||
ambiant := mat.ambiantColor.GetColor(point).Scale(0.1)
|
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)
|
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)
|
specular := mat.specularColor.GetColor(point).Scale(specularPower)
|
||||||
|
|
||||||
return ambiant.Add(diffuse).Add(specular)
|
return ambiant.Add(diffuse).Add(specular)
|
||||||
|
|
@ -89,6 +88,7 @@ func genImage() *rl.Image {
|
||||||
} else {
|
} else {
|
||||||
fr, fg, fb = 0, 0, 0
|
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)
|
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})
|
rl.ImageDrawPixel(image, int32(pixel_x), int32(pixel_y), rl.Color{R: r, G: g, B: b, A: 255})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
vec3.go
2
vec3.go
|
|
@ -69,7 +69,7 @@ func (u Vector3) Min(x float64) Vector3 {
|
||||||
// i incident, n normal. Both vector should be normalized
|
// i incident, n normal. Both vector should be normalized
|
||||||
func Reflect(i Vector3, n Vector3) Vector3 {
|
func Reflect(i Vector3, n Vector3) Vector3 {
|
||||||
y := i.Dot(n)
|
y := i.Dot(n)
|
||||||
return i.Add(n.Scale(2 * y))
|
return (i.Add(n.Scale(2 * y))).Normalized()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Todo : Refract
|
// Todo : Refract
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue