コード例 #1
0
func castRay(r geometry.Ray, scene geometry.Object, recursionLimit uint) (bool, geometry.Vector) {
	if scene.Collides(r) {
		depth, object, objectColour := scene.Collision(r)
		// if depth < 0 {
		// 	return false, geometry.MakeVector(0,0,0)
		// }
		// if recursionLimit == 4 {
		// 	fmt.Println("Reflected")
		// }
		return true, calculateColour(r, depth, scene, object, objectColour, recursionLimit)
	}
	return false, geometry.MakeVector(0, 0, 0)
}
コード例 #2
0
func calculateColour(r geometry.Ray,
	depth float64,
	scene geometry.Object,
	object geometry.Object,
	mat geometry.Material,
	recursionLimit uint) (colour geometry.Vector) {

	colour = mat.BaseColour.Mul(mat.CBase)

	if mat.CReflectance != 0 && recursionLimit != 0 {
		//calculate normal
		normal := object.Normal(r, depth)
		dir_proj := normal.Mul(normal.Dot(r.GetDirectionVector()))
		reflectedDirection := dir_proj.Mul(2).Sub(r.GetDirectionVector())

		incidence := r.Point(depth)
		reflectedRay := geometry.MakeRay(incidence.Add(reflectedDirection.Mul(0.01)), reflectedDirection) //add a small amount so that it doesn't intersect the mirror
		_, reflectedColour := castRay(reflectedRay, scene, recursionLimit-1)
		colour = colour.Add(reflectedColour.Mul(mat.CReflectance))
	}
	return
}