Małe zmiany

This commit is contained in:
zymon 2024-01-21 14:34:28 +01:00
parent a42a7ac1a7
commit cedcc35eca

View file

@ -18,17 +18,14 @@ struct OrthogonalCamera{T<:AbstractFloat} <: AbstractCamera
origin::Vec3{T} origin::Vec3{T}
size::Tuple{T, T} size::Tuple{T, T}
rotation::RotXYZ{T} rotation::RotXYZ{T}
# function OrthogonalCamera(o, θ, s)
# new(o, RotXYZ(0.0, 0.0, 0.0), s)
# end
end end
function get_rays(camera::OrthogonalCamera, resolution::Tuple{I, I}) where {I<:Integer} function get_rays(camera::OrthogonalCamera, resolution::Tuple{I, I}) where {I<:Integer}
R = camera.rotation R = camera.rotation
X, Y = camera.size X, Y = camera.size
Nx, Ny = resolution Nx, Ny = resolution
screen_pixel_position = [Vec3(x, y, 0.0) + camera.origin for y = LinRange(Y/2, -Y/2, Ny), x = LinRange(-X/2, X/2, Nx)] screen_pixel_position = [R*Vec3(x, y, 0.0) + camera.origin for y = LinRange(Y/2, -Y/2, Ny), x = LinRange(-X/2, X/2, Nx)]
[Ray(p, Vec3(0.0, 0.0, 1.0)) for p in screen_pixel_position] [Ray(p, R*Vec3(0.0, 0.0, 1.0)) for p in screen_pixel_position]
end end
struct PinHoleCamera{T<:AbstractFloat} <: AbstractCamera struct PinHoleCamera{T<:AbstractFloat} <: AbstractCamera
@ -40,8 +37,8 @@ end
function PinHoleCamera(origin, lookAt, up, distance, size) function PinHoleCamera(origin, lookAt, up, distance, size)
w = origin - lookAt |> normalize w = origin - lookAt |> normalize
u = cross(up, w) |> normalize u = up × w |> normalize
v = cross(w, u) v = w × u
B = [u v w] B = [u v w]
PinHoleCamera(origin, distance, B, size) PinHoleCamera(origin, distance, B, size)
end end
@ -52,17 +49,15 @@ function get_rays(camera::PinHoleCamera, resolution::Tuple{I, I}) where {I<:Inte
X, Y = camera.size X, Y = camera.size
B = camera.base B = camera.base
Nx, Ny = resolution Nx, Ny = resolution
[Ray(origin, normalize(B*Vec3(x, y, -d))) for y = LinRange(Y/2, -Y/2, Ny), x = LinRange(-X/2, X/2, Nx)] [Ray(origin, B*Vec3(x, y, -d)) for y = LinRange(Y/2, -Y/2, Ny), x = LinRange(-X/2, X/2, Nx)]
end end
struct Ray{T<:AbstractFloat} struct Ray{T<:AbstractFloat}
origin::Vec3{T} origin::Vec3{T}
direction::Vec3{T} direction::Vec3{T}
# function Ray{T}(o, d) where {T<:AbstractFloat} function Ray(o::Vec3{T}, d::Vec3{T}) where {T<:AbstractFloat}
# new(o, d |> normalize) new{T}(o, d |> normalize)
# end end
end end
struct HitInfo struct HitInfo
@ -88,9 +83,9 @@ end
function check_hit(ray::Ray{T}, sphere::Sphere{T})::Tuple{Bool, T} where {T <: AbstractFloat} function check_hit(ray::Ray{T}, sphere::Sphere{T})::Tuple{Bool, T} where {T <: AbstractFloat}
p = ray.origin - sphere.origin p = ray.origin - sphere.origin
a = dot(ray.direction, ray.direction) a = ray.direction ray.direction
b = 2 * dot(p, ray.direction) b = 2 * (p ray.direction)
c = dot(p, p) - sphere.radius^2 c = p p - sphere.radius^2
Δ = b^2 - 4a*c Δ = b^2 - 4a*c
Δ < 0.0 && return false, -1.0 Δ < 0.0 && return false, -1.0
x, z = -b/(2a), Δ/(2a) x, z = -b/(2a), Δ/(2a)