Files
ngzz-mc/internal/camera/camera_test.go

46 lines
1.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package camera
import (
"math"
"testing"
)
// nearEq 近似相等。
func nearEq(a, b, eps float64) bool { return math.Abs(a-b) < eps }
// TestForward 前方向:yaw=0 朝向 -Z,pitch>0 抬头。
func TestForward(t *testing.T) {
c := New([3]float64{0, 0, 0}, 0, 0, 70)
f := c.Forward()
if !nearEq(f[0], 0, 1e-6) || !nearEq(f[1], 0, 1e-6) || !nearEq(f[2], -1, 1e-6) {
t.Fatalf("前方向异常: %+v", f)
}
// 单位向量
l := math.Sqrt(f[0]*f[0] + f[1]*f[1] + f[2]*f[2])
if !nearEq(l, 1, 1e-6) {
t.Fatalf("前方向非单位向量: %v", l)
}
}
// TestYaw 转向 90°:前方向朝 -X 或 +X(右方向指向 -Z 转向后)。
func TestYaw(t *testing.T) {
c := New([3]float64{0, 0, 0}, math.Pi/2, 0, 70)
f := c.Forward()
if !nearEq(f[0], -1, 1e-6) || !nearEq(f[2], 0, 1e-6) {
t.Fatalf("yaw=90° 前方向异常: %+v", f)
}
}
// TestViewTranslate 视图矩阵平移:相机原点位置应映射到视图空间原点附近。
func TestViewTranslate(t *testing.T) {
c := New([3]float64{10, 64, -20}, 0, 0, 70)
m := c.View()
// 世界点 (10,64,-20) → 视图空间 ≈ (0,0,0,1)
vx := float64(m[0])*10 + float64(m[4])*64 + float64(m[8])*(-20) + float64(m[12])
vy := float64(m[1])*10 + float64(m[5])*64 + float64(m[9])*(-20) + float64(m[13])
vz := float64(m[2])*10 + float64(m[6])*64 + float64(m[10])*(-20) + float64(m[14])
if !nearEq(vx, 0, 1e-3) || !nearEq(vy, 0, 1e-3) || !nearEq(vz, 0, 1e-3) {
t.Fatalf("相机位置未映射到原点: %v %v %v", vx, vy, vz)
}
}