płaszczyzna dodana

This commit is contained in:
zymon 2024-01-23 22:51:29 +01:00
parent 7f8d412d8b
commit b6909997cd
2 changed files with 29 additions and 8 deletions

View file

@ -94,14 +94,26 @@ function check_hit(ray::Ray{T}, sphere::Sphere{T})::Tuple{Bool, T} where {T <: A
return t < eps() ? (false, -1.0) : (true, t)
end
struct Plane{T<:AbstractFloat} <: AbstractObject
point::Vec3{T}
normal::Vec3{T}
color::RGB
end
function check_hit(ray::Ray{T}, plane::Plane{T})::Tuple{Bool, T} where {T <: AbstractFloat}
t = (plane.point - ray.origin) plane.normal / (ray.direction plane.normal)
return t > eps() ? (true, t) : (false, -1.0)
end
function trace_ray(world::World, ray::Ray)::HitInfo
mindist = Float64 |> typemax |> prevfloat
maxdist = Float64 |> typemax |> prevfloat
hit = false
color = world.background
for obj in world.objects
hit, dist = check_hit(ray, obj)
if hit && dist < mindist
mindist = dist
if hit && dist < maxdist
maxdist = dist
hit = true
color = obj.color
end

19
rt.jl
View file

@ -11,10 +11,20 @@ using GLMakie
include("SimpleRayTracer.jl")
world = SimpleRayTracer.World(RGB(.1,.5,.5))
push!(
world,
SimpleRayTracer.Plane(
Vec3(0.0, -2.0, 0.0),
Vec3(0.0, 1.0, 0.0),
RGB(.5, .5, .5)
)
)
push!(world, SimpleRayTracer.Sphere(Vec3(-4.0, 0.0, 0.0), 2.0, RGB(1, 0, 0)))
push!(world, SimpleRayTracer.Sphere(Vec3( 4.0, 0.0, 0.0), 2.0, RGB(0, 1, 0)))
push!(world, SimpleRayTracer.Sphere(Vec3( 0.0, 0.0, 3.0), 2.0, RGB(0, 0, 1)))
camera = SimpleRayTracer.OrthogonalCamera(
Vec3(0.0, 0.0, -5.0),
(20.0, 10.0),
@ -28,13 +38,12 @@ camera = SimpleRayTracer.PinHoleCamera(
1.0,
(4.0, 2.0),
)
camera.base
resolution = (512, 256);
img = SimpleRayTracer.render(world, camera, resolution);
# img |> size
fig = Figure(size=resolution)
ax = GLMakie.Axis(fig[1,1], aspect=2)
image!(ax, img |> rotr90)
# fig = Figure(size=resolution)
# ax = GLMakie.Axis(fig[1,1], aspect=2)
# image!(ax, img |> rotr90)
Images.save("test.png", img)