// Vec3Diff returns the rotation quaternion between two vectors. func Vec3Diff(a, b *vec3.T) T { cr := vec3.Cross(a, b) sr := math.Sqrt(2 * (1 + vec3.Dot(a, b))) oosr := 1 / sr q := T{cr[0] * oosr, cr[1] * oosr, cr[2] * oosr, sr * 0.5} return q.Normalized() }
func (c *FreeFlightCamera) Lift(dist float32) { position := vec3.T(c.Pos) forward := vec3.T(c.Forward()) right := vec3.T(c.Right()) up := vec3.Cross(&forward, &right) up.Scale(dist) c.Pos = Vec3(vec3.Add(&position, &up)) }
func (rt *Raytracer) calcIncVectors(camera Camera, size image.Point) (vec3.T, vec3.T, vec3.T) { width := float32(size.X) height := float32(size.Y) lookAtPoint := vec3.T(camera.LookAt()) eyePoint := vec3.T(camera.Position()) up := vec3.T(camera.Up()) viewDirection := vec3.Sub(&lookAtPoint, &eyePoint) u := vec3.Cross(&viewDirection, &up) v := vec3.Cross(&u, &viewDirection) u.Normalize() v.Normalize() viewPlaneHalfWidth := float32(math.Tan(float64(rt.cfg.FieldOfView / 2))) aspectRatio := height / width viewPlaneHalfHeight := aspectRatio * viewPlaneHalfWidth sV := v.Scaled(viewPlaneHalfHeight) sU := u.Scaled(viewPlaneHalfWidth) lookV := vec3.Sub(&lookAtPoint, &sV) viewPlaneBottomLeftPoint := vec3.Sub(&lookV, &sU) xIncVector := u.Scaled(2 * viewPlaneHalfWidth) yIncVector := v.Scaled(2 * viewPlaneHalfHeight) xIncVector[0] /= width xIncVector[1] /= width xIncVector[2] /= width yIncVector[0] /= height yIncVector[1] /= height yIncVector[2] /= height return xIncVector, yIncVector, viewPlaneBottomLeftPoint }
func (c *FreeFlightCamera) Right() Vec3 { up := vec3.T(c.Up()) forward := vec3.T(c.Forward()) return Vec3(vec3.Cross(&up, &forward)) }
// Cross returns the cross product of two vectors. func Cross(a, b *T) T { a3 := a.Vec3DividedByW() b3 := b.Vec3DividedByW() c3 := vec3.Cross(&a3, &b3) return T{c3[0], c3[1], c3[2], 1} }