|
| 1 | +using Rotations |
| 2 | +using Quaternions |
| 3 | + |
| 4 | +coordinateTransform = [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1] .|> Float32 |
| 5 | + |
| 6 | +mutable struct Camera |
| 7 | + fx |
| 8 | + fy |
| 9 | + far |
| 10 | + near |
| 11 | + eye |
| 12 | + lookat |
| 13 | + up |
| 14 | + scale |
| 15 | + aspectRatio |
| 16 | + id |
| 17 | +end |
| 18 | + |
| 19 | +function defaultCamera(;id=0) |
| 20 | + eye = [0.0, 0.0, 4.0] .|> Float32 |
| 21 | + lookat = [0, 0, 0] .|> Float32 |
| 22 | + up = [0, 1, 0] .|> Float32 |
| 23 | + scale = [1, 1, 1] .|> Float32 |
| 24 | + fx = 100.0f0 |
| 25 | + fy = 100.0f0 |
| 26 | + aspectRatio = 1.0 |> Float32 |
| 27 | + nearPlane = 0.1 |> Float32 |
| 28 | + farPlane = 100.0 |> Float32 |
| 29 | + return Camera( |
| 30 | + fx, |
| 31 | + fy, |
| 32 | + farPlane, |
| 33 | + nearPlane, |
| 34 | + eye, |
| 35 | + lookat, |
| 36 | + up, |
| 37 | + scale, |
| 38 | + aspectRatio, |
| 39 | + id |
| 40 | + ) |
| 41 | +end |
| 42 | + |
| 43 | +function rotateTransform(q::Quaternion) |
| 44 | + rotMat = coordinateTransform[1:3, 1:3]*rotmatrix_from_quat(q) |
| 45 | + mat = Matrix{Float32}(I, (4, 4)) |
| 46 | + mat[1:3, 1:3] .= rotMat |
| 47 | + return LinearMap( |
| 48 | + SMatrix{4, 4}( |
| 49 | + mat |
| 50 | + ) |
| 51 | + ) |
| 52 | +end |
| 53 | + |
| 54 | +function scaleTransform(loc) |
| 55 | + (x, y, z) = coordinateTransform[1:3, 1:3]*loc |
| 56 | + return LinearMap( |
| 57 | + @SMatrix( |
| 58 | + [ |
| 59 | + x 0 0 0; |
| 60 | + 0 y 0 0; |
| 61 | + 0 0 z 0; |
| 62 | + 0 0 0 1; |
| 63 | + ] |
| 64 | + ) .|> Float32 |
| 65 | + ) |
| 66 | +end |
| 67 | + |
| 68 | +function translateCamera(camera::Camera) |
| 69 | + (x, y, z) = coordinateTransform[1:3, 1:3]*camera.eye |
| 70 | + return LinearMap( |
| 71 | + @SMatrix( |
| 72 | + [ |
| 73 | + 1 0 0 x; |
| 74 | + 0 1 0 y; |
| 75 | + 0 0 1 z; |
| 76 | + 0 0 0 1; |
| 77 | + ] |
| 78 | + ) .|> Float32 |
| 79 | + ) |> inv |
| 80 | +end |
| 81 | + |
| 82 | +function computeTransform(camera::Camera) |
| 83 | + eye = camera.eye |
| 84 | + lookat = camera.lookat |
| 85 | + up = camera.up |
| 86 | + w = -(lookat .- eye) |> normalize |
| 87 | + u = cross(up, w) |> normalize |
| 88 | + v = cross(w, u) |
| 89 | + m = MMatrix{4, 4, Float32}(I) |
| 90 | + m[1:3, 1:3] .= (cat([u, v, w]..., dims=2) |> adjoint .|> Float32 |> collect) |
| 91 | + m = SMatrix(m) |
| 92 | + return LinearMap(m) ∘ translateCamera(camera) |
| 93 | +end |
| 94 | + |
| 95 | +function computeProjection(renderer::AbstractGaussianRenderer, camera::Camera) |
| 96 | + (w, h) = size(renderer.imageData)[1:2] |
| 97 | + p = MArray{Tuple{4, 4}, Float32}(undef) |
| 98 | + p .= 0.0f0 |
| 99 | + p[1, 1] = 2.0f0*(camera.fx)/w |
| 100 | + p[2, 2] = 2.0f0*(camera.fy)/h |
| 101 | + p[3, 3] = (camera.far + camera.near)/(camera.far - camera.near) |
| 102 | + p[3, 4] = -2.0f0*(camera.far*camera.near)/(camera.far - camera.near) |
| 103 | + p[4, 3] = 1 |
| 104 | + return LinearMap(p) |
| 105 | +end |
| 106 | + |
| 107 | +function computeJacobian() |
| 108 | + |
| 109 | +end |
0 commit comments