reflection added

This commit is contained in:
Crizomb 2025-09-28 04:46:54 +02:00
parent 6fc7d7a4c3
commit a6770d7fac
3 changed files with 24 additions and 6 deletions

View file

@ -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))
direction := (origin.Sub(cameraPos)).Normalized()
hit, colorVec := rayMarch(origin, direction)
hit, colorVec := rayMarch(origin, direction, 1)
var fr, fg, fb float64
if hit {
fr, fg, fb = colorVec.Unpack()

View file

@ -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.specularFac*(1-t) + mat2.specularFac*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.refractFac*(1-t) + mat2.refractFac*t,
mat1.refractIndice*(1-t) + mat2.refractIndice*t,
@ -88,7 +88,7 @@ func DefaultMaterial(diffuseColor Color) MaterialBrut {
specularColor: Vector3{255, 255, 255},
specularFac: 1,
specularExp: 35.0,
reflectanceFac: 0.0,
reflectanceFac: 0.2,
reflectanceTint: Vector3{255, 255, 255},
refractFac: 0.0,
refractIndice: 1.0,
@ -97,7 +97,7 @@ func DefaultMaterial(diffuseColor Color) MaterialBrut {
// Colors
var RED = Vector3{255, 0, 0}
var RED = Vector3{125, 0, 0}
var GREEN = Vector3{0, 255, 0}
var BLUE = Vector3{0, 0, 255}
var WHITE = Vector3{255, 255, 255}

View file

@ -44,13 +44,31 @@ func shadow(point Vector3) float64 {
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
for range MAX_STEP {
dist, mat := scene.Distance(p)
if dist < EPS {
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)
}
if dist > MAX_DIST {