11using Raycore, GeometryBasics, LinearAlgebra
22using Colors, ImageShow
33import KernelAbstractions as KA
4+ using KernelAbstractions: @kernel , @index
45function compute_normal (triangle, bary_coords)
56 v0, v1, v2 = Raycore. normals (triangle)
67 u, v, w = bary_coords[1 ], bary_coords[2 ], bary_coords[3 ]
@@ -29,8 +30,13 @@ struct Material
2930 base_color:: RGB{Float32}
3031 metallic:: Float32
3132 roughness:: Float32
33+ ior:: Float32 # Index of refraction (1.0 = opaque, 1.5 = glass)
34+ transmission:: Float32 # 0 = opaque, 1 = fully transparent
3235end
3336
37+ # Convenience constructor for backward compatibility
38+ Material (base_color, metallic, roughness) = Material (base_color, metallic, roughness, 1.0f0 , 0.0f0 )
39+
3440struct RenderContext{L<: AbstractVector{PointLight} ,M<: AbstractVector{Material} }
3541 lights:: L
3642 materials:: M
@@ -41,21 +47,30 @@ function Raycore.to_gpu(Arr, ctx::RenderContext)
4147 return RenderContext (to_gpu (Arr, ctx. lights), to_gpu (Arr, ctx. materials), ctx. ambient)
4248end
4349
44- function render_context ()
50+ function render_context (; glass_cat = false )
4551 # Create lights and materials
4652 lights = [
4753 PointLight (Point3f (3 , 4 , - 2 ), 50.0f0 , RGB (1.0f0 , 0.9f0 , 0.8f0 )),
4854 PointLight (Point3f (- 3 , 2 , 0 ), 20.0f0 , RGB (0.7f0 , 0.8f0 , 1.0f0 )),
4955 PointLight (Point3f (0 , 5 , 5 ), 15.0f0 , RGB (1.0f0 , 1.0f0 , 1.0f0 ))
5056 ]
5157
58+ # Material: base_color, metallic, roughness, ior, transmission
59+ cat_material = if glass_cat
60+ # Glass cat: clear glass with slight green tint, ior=1.5
61+ Material (RGB (0.95f0 , 1.0f0 , 0.95f0 ), 0.0f0 , 0.0f0 , 1.5f0 , 1.0f0 )
62+ else
63+ # Original diffuse cat
64+ Material (RGB (0.8f0 , 0.6f0 , 0.4f0 ), 0.0f0 , 0.8f0 , 1.0f0 , 0.0f0 )
65+ end
66+
5267 materials = [
53- Material ( RGB ( 0.8f0 , 0.6f0 , 0.4f0 ), 0.0f0 , 0.8f0 ), # cat
54- Material (RGB (0.3f0 , 0.5f0 , 0.3f0 ), 0.0f0 , 0.9f0 ), # floor
55- Material (RGB (0.8f0 , 0.6f0 , 0.5f0 ), 0.8f0 , 0.05f0 ), # back wall
56- Material (RGB (0.7f0 , 0.7f0 , 0.8f0 ), 0.0f0 , 0.8f0 ), # left wall
57- Material (RGB (0.9f0 , 0.9f0 , 0.9f0 ), 0.8f0 , 0.02f0 ), # sphere1 - metallic
58- Material (RGB (0.3f0 , 0.6f0 , 0.9f0 ), 0.5f0 , 0.3f0 ), # sphere2 - semi-metallic
68+ cat_material, # cat (index 1)
69+ Material (RGB (0.3f0 , 0.5f0 , 0.3f0 ), 0.0f0 , 0.9f0 , 1.0f0 , 0.0f0 ), # floor - diffuse
70+ Material (RGB (0.8f0 , 0.6f0 , 0.5f0 ), 0.8f0 , 0.05f0 , 1.0f0 , 0.0f0 ), # back wall - metallic
71+ Material (RGB (0.7f0 , 0.7f0 , 0.8f0 ), 0.0f0 , 0.8f0 , 1.0f0 , 0.0f0 ), # left wall - diffuse
72+ Material (RGB (0.9f0 , 0.9f0 , 0.9f0 ), 0.8f0 , 0.02f0 , 1.0f0 , 0.0f0 ), # sphere1 - metallic
73+ Material (RGB (0.3f0 , 0.6f0 , 0.9f0 ), 0.5f0 , 0.3f0 , 1.0f0 , 0.0f0 ), # sphere2 - semi-metallic
5974 ]
6075
6176 return RenderContext (lights, materials, 0.1f0 )
@@ -171,7 +186,7 @@ function reflective_kernel(bvh, ctx, tri, dist, bary, ray, sky_color, shadow_sam
171186 return to_rgb (direct_color)
172187end
173188
174- function example_scene ()
189+ function example_scene (; glass_cat = false )
175190 cat_mesh = Makie. loadasset (" cat.obj" )
176191 angle = deg2rad (150f0 )
177192 rotation = Makie. Quaternionf (0 , sin (angle/ 2 ), 0 , cos (angle/ 2 ))
@@ -198,8 +213,11 @@ function example_scene()
198213
199214 # Build our BVH acceleration structure
200215 scene_geometry = [cat_mesh, floor, back_wall, left_wall, sphere1, sphere2]
201- bvh = Raycore. BVH (scene_geometry)
202- return bvh, render_context ()
216+ return scene_geometry, render_context (glass_cat= glass_cat)
217+ end
218+
219+ function example_scene_glass_cat ()
220+ example_scene (glass_cat= true )
203221end
204222
205223function sample_light (bvh, ctx, width, height, camera_pos, focal_length, aspect, x, y, sky_color)
0 commit comments