reflection added
This commit is contained in:
parent
6fc7d7a4c3
commit
a6770d7fac
3 changed files with 24 additions and 6 deletions
|
|
@ -69,7 +69,7 @@ func compute_pixel(y0 int, y1 int, wg *sync.WaitGroup, screenCenter Vector3, phi
|
||||||
origin := screenCenter.Add(phiVec.Scale(-sx)).Add(thetaVec.Scale(sy))
|
origin := screenCenter.Add(phiVec.Scale(-sx)).Add(thetaVec.Scale(sy))
|
||||||
direction := (origin.Sub(cameraPos)).Normalized()
|
direction := (origin.Sub(cameraPos)).Normalized()
|
||||||
|
|
||||||
hit, colorVec := rayMarch(origin, direction)
|
hit, colorVec := rayMarch(origin, direction, 1)
|
||||||
var fr, fg, fb float64
|
var fr, fg, fb float64
|
||||||
if hit {
|
if hit {
|
||||||
fr, fg, fb = colorVec.Unpack()
|
fr, fg, fb = colorVec.Unpack()
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ func MixMat(mat1 MaterialBrut, mat2 MaterialBrut, t float64, p Vector3) Material
|
||||||
mat1.specularColor.GetColor(p).Scale(1 - t).Add((mat2.specularColor.GetColor(p)).Scale(t)),
|
mat1.specularColor.GetColor(p).Scale(1 - t).Add((mat2.specularColor.GetColor(p)).Scale(t)),
|
||||||
mat1.specularFac*(1-t) + mat2.specularFac*t,
|
mat1.specularFac*(1-t) + mat2.specularFac*t,
|
||||||
mat1.specularExp*(1-t) + mat2.specularExp*t,
|
mat1.specularExp*(1-t) + mat2.specularExp*t,
|
||||||
(mat1.reflectanceFac*(1-t) + mat2.reflectanceFac) * t,
|
mat1.reflectanceFac*(1-t) + mat2.reflectanceFac*t,
|
||||||
mat1.reflectanceTint.GetColor(p).Scale(1 - t).Add((mat2.reflectanceTint.GetColor(p)).Scale(t)),
|
mat1.reflectanceTint.GetColor(p).Scale(1 - t).Add((mat2.reflectanceTint.GetColor(p)).Scale(t)),
|
||||||
mat1.refractFac*(1-t) + mat2.refractFac*t,
|
mat1.refractFac*(1-t) + mat2.refractFac*t,
|
||||||
mat1.refractIndice*(1-t) + mat2.refractIndice*t,
|
mat1.refractIndice*(1-t) + mat2.refractIndice*t,
|
||||||
|
|
@ -88,7 +88,7 @@ func DefaultMaterial(diffuseColor Color) MaterialBrut {
|
||||||
specularColor: Vector3{255, 255, 255},
|
specularColor: Vector3{255, 255, 255},
|
||||||
specularFac: 1,
|
specularFac: 1,
|
||||||
specularExp: 35.0,
|
specularExp: 35.0,
|
||||||
reflectanceFac: 0.0,
|
reflectanceFac: 0.2,
|
||||||
reflectanceTint: Vector3{255, 255, 255},
|
reflectanceTint: Vector3{255, 255, 255},
|
||||||
refractFac: 0.0,
|
refractFac: 0.0,
|
||||||
refractIndice: 1.0,
|
refractIndice: 1.0,
|
||||||
|
|
@ -97,7 +97,7 @@ func DefaultMaterial(diffuseColor Color) MaterialBrut {
|
||||||
|
|
||||||
// Colors
|
// Colors
|
||||||
|
|
||||||
var RED = Vector3{255, 0, 0}
|
var RED = Vector3{125, 0, 0}
|
||||||
var GREEN = Vector3{0, 255, 0}
|
var GREEN = Vector3{0, 255, 0}
|
||||||
var BLUE = Vector3{0, 0, 255}
|
var BLUE = Vector3{0, 0, 255}
|
||||||
var WHITE = Vector3{255, 255, 255}
|
var WHITE = Vector3{255, 255, 255}
|
||||||
|
|
|
||||||
|
|
@ -44,13 +44,31 @@ func shadow(point Vector3) float64 {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func rayMarch(origin Vector3, direction Vector3) (bool, Vector3) {
|
func reflect(point Vector3, direction Vector3, normal Vector3, mat Material, reflectNumber int) Vector3 {
|
||||||
|
brutMat := mat.GetMaterialBrut()
|
||||||
|
if reflectNumber <= 0 || brutMat.reflectanceFac < EPS {
|
||||||
|
return Vector3{0, 0, 0}
|
||||||
|
}
|
||||||
|
reflected := Reflect(direction, normal).Scale(-1)
|
||||||
|
reflectRayOrigin := point.Add(reflected.Scale(5 * EPS))
|
||||||
|
hit, color := rayMarch(reflectRayOrigin, reflected, reflectNumber-1)
|
||||||
|
if !hit {
|
||||||
|
color = Vector3{0, 0, 0}
|
||||||
|
}
|
||||||
|
// fmt.Println("Reflect")
|
||||||
|
return color.Scale(brutMat.reflectanceFac)
|
||||||
|
}
|
||||||
|
|
||||||
|
func rayMarch(origin Vector3, direction Vector3, reflectNumber int) (bool, Vector3) {
|
||||||
p := origin
|
p := origin
|
||||||
for range MAX_STEP {
|
for range MAX_STEP {
|
||||||
dist, mat := scene.Distance(p)
|
dist, mat := scene.Distance(p)
|
||||||
if dist < EPS {
|
if dist < EPS {
|
||||||
normal := Gradient(scene, p, EPS).Normalized()
|
normal := Gradient(scene, p, EPS).Normalized()
|
||||||
return true, phongShading(p, normal, mat).Scale(max(shadow(p), 0.3))
|
phongShade := phongShading(p, normal, mat)
|
||||||
|
shadowCoeff := max(shadow(p), 0.3)
|
||||||
|
reflectColor := reflect(p, direction, normal, mat, reflectNumber)
|
||||||
|
return true, phongShade.Add(reflectColor).Scale(shadowCoeff)
|
||||||
// return true, phongShading(p, normal, *mat)
|
// return true, phongShading(p, normal, *mat)
|
||||||
}
|
}
|
||||||
if dist > MAX_DIST {
|
if dist > MAX_DIST {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue