aboutsummaryrefslogtreecommitdiff
path: root/camera.go
blob: 2992b021e6ab6ae0aa4d55a6592506d234cf4f8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

package main

import "math"

// Camera handle view position and screen
type Camera struct {
	lowerLeftCorner              Vec3D
	origin, horizontal, vertical Vec3D
}

// NewCamera creates a new camera
func NewCamera(vfov float64, aspect float64) Camera {
	origin := Vec3D{0.0, 0.0, 0.0}
	theta := DegreesToRadians(vfov)
	halfHeight := math.Tan(theta / 2.0)
	halfWidth := aspect * halfHeight
	lowerLeftCorner := Vec3D{-halfWidth, -halfHeight, -1.0}
	horizontal := Vec3D{2.0 * halfWidth, 0.0, 0.0}
	vertical := Vec3D{0.0, 2.0 * halfHeight, 0.0}
	return Camera{lowerLeftCorner, origin, horizontal, vertical}
}

// GetRay computes Ray given screen params
func (camera Camera) GetRay(u float64, v float64) Ray {
	return Ray{
		camera.origin,
		AddVec3D(AddVec3D(
			camera.lowerLeftCorner,
			ScalarProduct(u, camera.horizontal)),
			ScalarProduct(v, camera.vertical),
		),
	}
}