Example #1
0
func DiffusePhoton(scene []*geometry.Shape, emitter *geometry.Shape, ray geometry.Ray, colour geometry.Vec3, result chan<- PhotonHit, alpha geometry.Float, depth int, rand *rand.Rand) {
	if geometry.Float(rand.Float32()) > alpha {
		return
	}
	if shape, distance := ClosestIntersection(scene, ray); shape != nil {
		impact := ray.Origin.Add(ray.Direction.Mult(distance))

		if depth == 0 && emitter == shape {
			// Leave the emitter first
			nextRay := geometry.Ray{impact, ray.Direction}
			DiffusePhoton(scene, emitter, nextRay, colour, result, alpha, depth, rand)
		} else {
			normal := shape.NormalDir(impact).Normalize()
			reverse := ray.Direction.Mult(-1)
			outgoing := normal
			if normal.Dot(reverse) < 0 {
				outgoing = normal.Mult(-1)
			}
			strength := colour.Mult(alpha / (1 + distance))
			result <- PhotonHit{impact, strength, ray.Direction, uint8(depth)}

			if shape.Material == geometry.DIFFUSE {
				// Random bounce for color bleeding
				u := normal.Cross(reverse).Normalize().Mult(geometry.Float(rand.NormFloat64() * 0.5))
				v := u.Cross(normal).Normalize().Mult(geometry.Float(rand.NormFloat64() * 0.5))
				bounce := geometry.Vec3{
					u.X + outgoing.X + v.X,
					u.Y + outgoing.Y + v.Y,
					u.Z + outgoing.Z + v.Z,
				}
				bounceRay := geometry.Ray{impact, bounce.Normalize()}
				bleedColour := colour.MultVec(shape.Colour).Mult(alpha / (1 + distance))
				DiffusePhoton(scene, shape, bounceRay, bleedColour, result, alpha*0.66, depth+1, rand)
			}
			// Store Shadow Photons
			shadowRay := geometry.Ray{impact, ray.Direction}
			DiffusePhoton(scene, shape, shadowRay, geometry.Vec3{0, 0, 0}, result, alpha*0.66, depth+1, rand)
		}
	}
}
Example #2
0
func PhotonChunk(scene []*geometry.Shape, traceFunc RayFunc, shape *geometry.Shape, factor, start, chunksize int, result chan<- PhotonHit, done chan<- bool, rand *rand.Rand) {
	for i := 0; i < chunksize; i++ {
		longitude := (start*chunksize + i) / factor
		latitude := (start*chunksize + i) % factor

		//fmt.Println("Lo La:", longitude, latitude)

		sign := -2.0*float64(longitude%2.0) + 1.0
		phi := 2.0 * math.Pi * float64(longitude) / float64(factor)
		theta := math.Pi * float64(latitude) / float64(factor)

		//fmt.Println("S, T, P:", sign, theta, phi)

		x, y, z := math.Sin(theta)*math.Cos(phi),
			sign*math.Cos(theta),
			math.Sin(theta)*math.Sin(phi)

		direction := geometry.Vec3{geometry.Float(x), geometry.Float(y), geometry.Float(z)}
		ray := geometry.Ray{shape.Position, direction.Normalize()}
		traceFunc(scene, shape, ray, shape.Emission, result, 1.0, 0, rand)
	}
	done <- true
}
Example #3
0
func Radiance(ray geometry.Ray, scene *geometry.Scene, diffuseMap /*, causticsMap*/ *kd.KDNode, depth int, alpha float64, rand *rand.Rand) geometry.Vec3 {

	if depth > Config.MinDepth && rand.Float64() > alpha {
		return geometry.Vec3{0, 0, 0}
	}

	if shape, distance := ClosestIntersection(scene.Objects, ray); shape != nil {
		impact := ray.Origin.Add(ray.Direction.Mult(distance))
		normal := shape.NormalDir(impact).Normalize()
		reverse := ray.Direction.Mult(-1)

		contribution := shape.Emission
		outgoing := normal
		if normal.Dot(reverse) < 0 {
			outgoing = normal.Mult(-1)
		}

		if shape.Material == geometry.DIFFUSE {
			var /*causticLight,*/ directLight geometry.Vec3

			/*nodes := causticsMap.Neighbors(impact, 0.1)
			for _, e := range nodes {
				photon := causticPhotons[e.Position]
				dist := photon.Location.Distance(impact)
				light := photon.Photon.Mult(outgoing.Dot(photon.Incomming.Mult(-1 / math.Pi * (1 + dist))))
				causticLight.AddInPlace(light)
			}
			if len(nodes) > 0 {
				causticLight = causticLight.Mult(1.0 / float64(len(nodes)))
			}*/

			directLight = EmitterSampling(impact, normal, scene.Objects, rand)

			u := normal.Cross(reverse).Normalize().Mult(geometry.Float(rand.NormFloat64() * 0.5))
			v := u.Cross(normal).Normalize().Mult(geometry.Float(rand.NormFloat64() * 0.5))

			bounceDirection := geometry.Vec3{
				u.X + outgoing.X + v.X,
				u.Y + outgoing.Y + v.Y,
				u.Z + outgoing.Z + v.Z,
			}
			bounceRay := geometry.Ray{impact, bounceDirection.Normalize()}
			indirectLight := Radiance(bounceRay, scene, diffuseMap /*causticsMap,*/, depth+1, alpha*0.9, rand)
			dot := outgoing.Dot(reverse)
			diffuseLight := geometry.Vec3{
				(shape.Colour.X * (directLight.X + indirectLight.X) /*+ causticLight.X*/) * dot,
				(shape.Colour.Y * (directLight.Y + indirectLight.Y) /*+ causticLight.Y*/) * dot,
				(shape.Colour.Z * (directLight.Z + indirectLight.Z) /*+ causticLight.Z*/) * dot,
			}

			return contribution.Add(diffuseLight)

		}
		if shape.Material == geometry.SPECULAR {
			reflectionDirection := ray.Direction.Sub(normal.Mult(2 * outgoing.Dot(ray.Direction)))
			reflectedRay := geometry.Ray{impact, reflectionDirection.Normalize()}
			incomingLight := Radiance(reflectedRay, scene, diffuseMap /*causticsMap,*/, depth+1, alpha*0.99, rand)
			return incomingLight.Mult(outgoing.Dot(reverse))
		}

		if shape.Material == geometry.REFRACTIVE {
			var n1, n2 float64
			if normal.Dot(outgoing) < 0 {
				// Leave the glass
				n1, n2 = GLASS, AIR
			} else {
				n1, n2 = AIR, GLASS
			}

			factor := n1 / n2
			cosTi := float64(normal.Dot(reverse))
			sinTi := math.Sqrt(1 - cosTi*cosTi) // sinĀ² + cosĀ² = 1
			sqrt := math.Sqrt(math.Max(1.0-math.Pow(factor*sinTi, 2), 0))
			// Rs
			top := n1*cosTi - n2*sqrt
			bottom := n1*cosTi + n2*sqrt
			Rs := math.Pow(top/bottom, 2)
			// Rp
			top = n1*sqrt - n2*cosTi
			bottom = n1*sqrt + n2*cosTi
			Rp := math.Pow(top/bottom, 2)

			R := (Rs*Rs + Rp*Rp) / 2.0

			// Approximate:
			R = math.Pow((n1-n2)/(n1+n2), 2)
			// SmallPT formula
			//R = R + (1 - R) * math.Pow(1 - cosTi, 5)
			T := 1.0 - R

			if math.IsNaN(R) {
				fmt.Printf("into: %v, sqrt: %v\n", n2 > n1, sqrt)
				fmt.Printf("cos: %v, sin: %v\n", cosTi, sinTi)
				fmt.Printf("n1: %v, n2: %v\n", n1, n2)
				fmt.Printf("Top: %v, Bottom: %v\n", top, bottom)
				fmt.Printf("Rs: %v, Rp: %v\n", Rs, Rp)
				fmt.Printf("R: %v, T: %v\n", R, T)
				panic("NAN!")
			}

			totalReflection := false
			if n1 > n2 {
				maxAngle := math.Asin(n2 / n1)
				actualAngle := math.Asin(sinTi)

				if actualAngle > maxAngle {
					totalReflection = true
				}
				totalReflection = totalReflection
			}

			if totalReflection {
				reflectionDirection := ray.Direction.Sub(outgoing.Mult(2 * outgoing.Dot(ray.Direction)))
				reflectedRay := geometry.Ray{impact, reflectionDirection.Normalize()}
				return Radiance(reflectedRay, scene, diffuseMap /*causticsMap,*/, depth+1, alpha*0.9, rand)
			} else {
				reflectionDirection := ray.Direction.Sub(outgoing.Mult(2 * outgoing.Dot(ray.Direction)))
				reflectedRay := geometry.Ray{impact, reflectionDirection.Normalize()}
				reflectedLight := Radiance(reflectedRay, scene, diffuseMap /*causticsMap,*/, depth+1, alpha*0.9, rand).Mult(geometry.Float(R))

				nDotI := float64(normal.Dot(ray.Direction))
				trasmittedDirection := ray.Direction.Mult(geometry.Float(factor))
				term2 := factor * nDotI
				term3 := math.Sqrt(1 - factor*factor*(1-nDotI*nDotI))

				trasmittedDirection = trasmittedDirection.Add(normal.Mult(geometry.Float(term2 - term3)))
				transmittedRay := geometry.Ray{impact, trasmittedDirection.Normalize()}
				transmittedLight := Radiance(transmittedRay, scene, diffuseMap /*causticsMap,*/, depth+1, alpha*0.9, rand).Mult(geometry.Float(T))
				return reflectedLight.Add(transmittedLight).Mult(outgoing.Dot(reverse))
			}
		}
		panic("Material without property encountered!")
	}

	return geometry.Vec3{0, 0, 0}
}