Skip to content

Commit 5c8ba9d

Browse files
committed
Add ray box intersection
1 parent 03eb2a9 commit 5c8ba9d

1 file changed

Lines changed: 49 additions & 2 deletions

File tree

src/volumetric_clouds/main.clj

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,6 @@ void main()
595595

596596
(defn float-buffer->array
597597
"Convert float buffer to flaot array"
598-
{:malli/schema [:=> [:cat :some] seqable?]}
599598
[buffer]
600599
(let [result (float-array (.limit buffer))]
601600
(.get buffer result)
@@ -616,7 +615,7 @@ void main()
616615
(let [buffer (BufferUtils/createFloatBuffer (* height width 4))]
617616
(GL11/glBindTexture GL11/GL_TEXTURE_2D texture)
618617
(GL11/glGetTexImage GL11/GL_TEXTURE_2D 0 GL12/GL_RGBA GL11/GL_FLOAT buffer)
619-
(seq (float-buffer->array buffer))))
618+
(vec (float-buffer->array buffer))))
620619

621620

622621
(defmacro framebuffer-render
@@ -737,6 +736,54 @@ void main()
737736
1 0 0 [1.0 0.0] 1.0)
738737

739738

739+
(def ray-box
740+
"#version 130
741+
vec2 ray_box(vec3 box_min, vec3 box_max, vec3 origin, vec3 direction)
742+
{
743+
vec3 inv_dir = 1.0 / direction;
744+
vec3 smin = (box_min - origin) * inv_dir;
745+
vec3 smax = (box_max - origin) * inv_dir;
746+
vec3 s1 = min(smin, smax);
747+
vec3 s2 = max(smin, smax);
748+
float s_near = max(max(s1.x, s1.y), s1.z);
749+
float s_far = min(min(s2.x, s2.y), s2.z);
750+
if (isinf(s_near) || isinf(s_far))
751+
return vec2(0.0, 0.0);
752+
else
753+
return vec2(s_near, s_far);
754+
}")
755+
756+
757+
(def ray-box-probe
758+
(template/fn [ox oy oz dx dy dz]
759+
"#version 130
760+
out vec4 fragColor;
761+
vec2 ray_box(vec3 box_min, vec3 box_max, vec3 origin, vec3 direction);
762+
void main()
763+
{
764+
vec3 box_min = vec3(-1, -1, -1);
765+
vec3 box_max = vec3(1, 1, 1);
766+
vec3 origin = vec3(<%= ox %>, <%= oy %>, <%= oz %>);
767+
vec3 direction = vec3(<%= dx %>, <%= dy %>, <%= dz %>);
768+
fragColor = vec4(ray_box(box_min, box_max, origin, direction), 0, 0);
769+
}"))
770+
771+
772+
(tabular "Test intersection of ray with box"
773+
(fact ((juxt first second) (render-pixels [vertex-test] [ray-box (ray-box-probe ?ox ?oy ?oz ?dx ?dy ?dz)] 1 1))
774+
=> ?result)
775+
?ox ?oy ?oz ?dx ?dy ?dz ?result
776+
-2 0 0 1 0 0 [1.0 3.0]
777+
-2 0 0 2 0 0 [0.5 1.5]
778+
-2 2 2 1 0 0 [0.0 0.0]
779+
0 -2 0 0 1 0 [1.0 3.0]
780+
0 -2 0 0 2 0 [0.5 1.5]
781+
2 -2 2 0 1 0 [0.0 0.0]
782+
0 0 -2 0 0 1 [1.0 3.0]
783+
0 0 -2 0 0 2 [0.5 1.5]
784+
2 2 -2 0 0 1 [0.0 0.0])
785+
786+
740787
(GLFW/glfwDestroyWindow window)
741788

742789
(GLFW/glfwTerminate)

0 commit comments

Comments
 (0)