-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_test.go
186 lines (163 loc) · 6.19 KB
/
camera_test.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright © 2014-2018 Galvanized Logic Inc.
// Use is governed by a BSD-style license found in the LICENSE file.
package vu
import (
"math"
"testing"
"github.com/gazed/vu/math/lin"
"github.com/gazed/vu/physics"
)
// Test a ray cast with simple perspective and view inverses.
// The ray from center screen mouse coordinates should be directly
// along the -Z axis.
func TestRay(t *testing.T) {
cam, ww, wh := initScene()
cam.Move(0, 0, 15, cam.Lookat())
rx, ry, rz := cam.Ray(ww/2, wh/2, ww, wh) // center of screen.
ex, ey, ez := 0.0, 0.0, -1.0
if rx != ex || ry != ey || rz != ez {
t.Errorf("Expected %f %f %f got %f %f %f", ex, ey, ez, rx, ry, rz)
}
}
// Test a ray cast with perspective inverse and angled view inverse.
func TestAngledRay(t *testing.T) {
cam, ww, wh := initScene()
cam.SetPitch(cam.Pitch + 45)
cam.SetAt(0, -15, 15)
cam.vt(cam.at, cam.q0, cam.vm) // view transform
cam.it(cam.at, cam.q0, cam.ivm) // inverse view transform.
rx, ry, rz := cam.Ray(ww/2, wh/2, ww, wh) // center of screen.
ex, ey, ez := 0.0, 0.7071068, -0.7071068
if !lin.Aeq(rx, ex) || !lin.Aeq(ry, ey) || !lin.Aeq(rz, ez) {
t.Errorf("Expected %f %f %f got %f %f %f", ex, ey, ez, rx, ry, rz)
}
}
// Test that the ratio of the rays matches the ratio of the screen.
func TestRayRatios(t *testing.T) {
cam, ww, wh := initScene()
cam.Move(0, 0, 15, cam.Lookat())
// shoot and check opposing corner rays.
blx, bly, _ := cam.Ray(0, 0, ww, wh)
trx, try, _ := cam.Ray(ww, wh, ww, wh)
gotRatio := (try - bly) / (trx - blx)
expectedRatio := float64(wh) / float64(ww)
if expectedRatio != gotRatio {
t.Errorf("Expected %f got %f", expectedRatio, gotRatio)
}
}
// Test perspective and view inverses.
func TestInverses(t *testing.T) {
cam, _, _ := initScene()
cam.SetPitch(cam.Pitch + 45)
cam.Move(0, -15, 15, cam.Lookat())
cam.vt(cam.at, cam.q0, cam.vm) // view transform
cam.it(cam.at, cam.q0, cam.ivm) // inverse view transform.
// the inverses multiplied with non-inverses should be the identity matrix.
if !lin.NewM4().Mult(cam.pm, cam.ipm).Aeq(lin.M4I) {
t.Error("Invalid inverse projection matrix")
}
if !lin.NewM4().Mult(cam.vm, cam.ivm).Aeq(lin.M4I) {
t.Error("Invalid inverse view matrix")
}
}
// Check that the inverse of a perspective view is correct.
func TestInverseVp(t *testing.T) {
v := newCamera()
v.at.Loc.SetS(10, 10, 10)
v.at.Rot.SetAa(1, 0, 0, -lin.Rad(90))
vm, ivm := &lin.M4{}, &lin.M4{}
vp(v.at, lin.NewQ(), vm)
ivp(v.at, lin.NewQ(), ivm)
if !vm.Mult(vm, ivm).Aeq(lin.M4I) {
t.Errorf("Matrix times inverse should be identity")
}
}
func TestRoundTrip(t *testing.T) {
cam, _, _ := initScene()
cx, cy, cz := 0.0, 0.0, 14.0 // camera location to
cam.SetAt(cx, cy, cz) // ...point directly at 0, 0, 0
cam.vt(cam.at, cam.q0, cam.vm) // view transform
cam.it(cam.at, cam.q0, cam.ivm) // inverse view transform.
// Create the matricies to go between clip and world space.
toClip := lin.NewM4().Mult(cam.vm, cam.pm)
toWorld := lin.NewM4().Mult(cam.ipm, cam.ivm)
if !lin.NewM4().Mult(toClip, toWorld).Aeq(lin.M4I) {
t.Errorf("Invalid world<->clip matricies")
}
// start with world coordinates carefully chosen to give x=1, y=1 clip values
px, py := 6.002062, 3.751289
pnt := lin.NewV4().SetS(px, py, 0, 1)
pnt.MultMv(toClip, pnt)
if !lin.Aeq(pnt.X/pnt.W, 1) || !lin.Aeq(pnt.Y/pnt.W, 1) {
t.Errorf("%f %f gave clip %f %f %f, expected (1 1 -0.071429)", px, py, pnt.X, pnt.Y, pnt.Z)
}
// now reverse back to world coordinates.
pnt.MultMv(toWorld, pnt)
if !lin.Aeq(pnt.X, px) || !lin.Aeq(pnt.Y, py) {
t.Errorf("got point %f %f %f, expected x=%f y=%f", pnt.X, pnt.Y, pnt.Z, px, py)
}
}
func TestRayWithSpin(t *testing.T) {
cam, _, _ := initScene()
cx, cy, cz := 0.0, -10.0, 14.0 // camera location to
cam.SetAt(cx, cy, cz) // ...point directly at 0, 0, 0
cam.SetPitch(lin.Deg(math.Atan(-cy / cz))) // 35.53768 degrees
plane := Plane(0, 0, -1)
cam.vt(cam.at, cam.q0, cam.vm) // view transform
cam.it(cam.at, cam.q0, cam.ivm) // inverse view transform.
ww, wh := 1280, 800
rx, ry, rz := cam.Ray(0, 0, ww, wh)
ray := Ray(rx, ry, rz)
ray.World().SetLoc(cx, cy, cz)
hit, hx, hy, hz := physics.Cast(ray, plane)
ex, ey, ez := -6.191039, -4.755119, 0.0
if !hit || !lin.Aeq(hx, ex) || !lin.Aeq(hx, ex) || !lin.Aeq(hx, ex) {
t.Errorf("Hit %t %f %f %f, expected %f %f %f", hit, hx, hy, hz, ex, ey, ez)
}
rx, ry, rz = cam.Ray(0, wh, ww, wh)
ray = Ray(rx, ry, rz)
ray.World().SetLoc(cx, cy, cz)
hit, hx, hy, hz = physics.Cast(ray, plane)
ex, ey, ez = -9.121797, 7.006131, 0.0
if !hit || !lin.Aeq(hx, ex) || !lin.Aeq(hx, ex) || !lin.Aeq(hx, ex) {
t.Errorf("Hit %t %f %f %f, expected %f %f %f", hit, hx, hy, hz, ex, ey, ez)
}
rx, ry, rz = cam.Ray(ww, 0, ww, wh)
ray = Ray(rx, ry, rz)
ray.World().SetLoc(cx, cy, cz)
hit, hx, hy, hz = physics.Cast(ray, plane)
ex, ey, ez = 6.191039, -4.755119, 0.0
if !hit || !lin.Aeq(hx, ex) || !lin.Aeq(hx, ex) || !lin.Aeq(hx, ex) {
t.Errorf("Hit %t %f %f %f, expected %f %f %f", hit, hx, hy, hz, ex, ey, ez)
}
rx, ry, rz = cam.Ray(ww, wh, ww, wh)
ray = Ray(rx, ry, rz)
ray.World().SetLoc(cx, cy, cz)
hit, hx, hy, hz = physics.Cast(ray, plane)
ex, ey, ez = 9.121797, 7.006131, 0.0
if !hit || !lin.Aeq(hx, ex) || !lin.Aeq(hx, ex) || !lin.Aeq(hx, ex) {
t.Errorf("Hit %t %f %f %f, expected %f %f %f", hit, hx, hy, hz, ex, ey, ez)
}
}
func TestScreen(t *testing.T) {
cam, _, _ := initScene()
cx, cy, cz := 0.0, 0.0, 14.0 // camera location to
cam.SetAt(cx, cy, cz) // ...point directly at 0, 0, 0
cam.vt(cam.at, cam.q0, cam.vm) // view transform
cam.it(cam.at, cam.q0, cam.ivm) // inverse view transform.
// center of the world should give the center of the screen.
px, py, pz := 0.0, 0.0, 0.0
if x, y := cam.Screen(px, py, pz, 1280, 800); x != 640 || y != 400 {
t.Errorf("got point %d %d, expected 640, 400", x, y)
}
}
// =============================================================================
// test utility methods.
// initScene creats a scene with an initialized perspective matrix.
func initScene() (c *Camera, ww, wh int) {
c = newCamera()
ww, wh = 1280, 800
fov, ratio, near, far := 30.0, float64(ww)/float64(wh), 0.1, 500.0
c.setPerspective(fov, ratio, near, far)
return
}