Skip to content

Instantly share code, notes, and snippets.

@bolives-hax
Created May 27, 2024 01:27
Show Gist options
  • Save bolives-hax/27574241456dac66b6272f7d1974d3fd to your computer and use it in GitHub Desktop.
Save bolives-hax/27574241456dac66b6272f7d1974d3fd to your computer and use it in GitHub Desktop.
'get_res'⎕NA'U4 /home/flandre/apl-raymarcher/target/debug/libapl_window_draw_helper.so|get_res P >U4[2]'
'init_drawer'⎕NA'P /home/flandre/apl-raymarcher/target/debug/libapl_window_draw_helper.so|init_drawer'
'draw'⎕NA'U4 /home/flandre/apl-raymarcher/target/debug/libapl_window_draw_helper.so|draw P U4 U4 <F4[]'
'ddd'⎕NA'U4 /home/flandre/apl-raymarcher/target/debug/libapl_window_draw_helper.so|ddd U4 <U4[]'
⍝⎕SE.UCMD 'BOX on'
drawer←init_drawer
res←↑(get_res drawer ( 0 0 ))[2] ⋄ xres←(res)[1]
yres←(res)[2] ⋄ xy←{({⍵}¨((⍳(xres))-1))⍵}¨((⍳(yres))-1)
_uv←{(⊃⍵[1]÷xres) (⍵[2]÷yres)}¨xy
uv←_uv-0.5 ⋄ uv←{⊃((⍵[1] ÷ (xres ÷ yres)) ⍵[2])}¨uv ⋄ uv_vecs ← ⊃,/{y←⍵[2] ⋄ {⍵ (-y)}¨(⊃⍵[1]) }¨uv
epsi←0.0001
sqrt←{⍵*÷⍺}
length←{2 sqrt (+/{⍵*2}¨⍵)}
norm←{o ← ⍵ ⋄ v ← +/{⍵*2}¨o ⋄ w← 2 sqrt v ⋄ {⍵÷w}¨o}
cam_origin←(0 0 ¯1)
clamp←{(⍵[2]) ⌊ ( (⍵[1])⌈⍺ )}
lin_interpol←{le←⊃(⍵[1])⋄ue←⊃(⍵[2])⋄le × ( 1 - ⍺ ) + ue × ⍺}
blend_broken←{
d_a←⍵[1] ⋄d_b←⍵[2]
h← ( 0.5 + 0.5 × ((d_a - d_b)÷⍺)) clamp (0.0 1.0)
(h lin_interpol d_b (-d_a)) - ⍺ × h × ( 1.0 - h)
}
blend←{
d_a←⊃⊃⍵[1]⋄d_b←⊃⊃⍵[2]
h← ( 0.5 + 0.5 × ((d_b - d_a)÷⍺)) clamp (0.0 1.0)
(h lin_interpol d_b d_a) - ⍺ × h × ( 1.0 - h)
}
ball_sdf←{ (length ⍵) - ⍺ }
inf_plane_floor←{((norm ⍺) dot (⊃⍵[1]) ) + ⊃⍵[2] }
octahedron_sdf←{ 0.57735027 × ( {+/(|⍵)} ⍵) - ⍺ }
boxframe_sdf←{
p ← ( (|⍵) - (⊃⍺[1])) ⋄q ← ((|(p + (⊃⍺[2]))) - (⊃⍺[2]))
p1←p[1]
q2←q[2]
q3←q[3]
q1←q[1]
p2←p[2]
q3←q[3]
p3←p[3]
u1 ← length ((0 0 0) ⌈ (p1 q2 q3))
u2 ← length ((0 0 0) ⌈ (q1 p2 q3))
u3 ← length ((0 0 0) ⌈ (q1 q2 p3))
v1 ← ((0) ⌊ (p1 ⌈ (q2 ⌈ q3)))
v2 ← ((0) ⌊ (q1 ⌈ (p2 ⌈ q3)))
v3 ← ((0) ⌊ (q1 ⌈ (q2 ⌈ p3)))
( u3 + v3 ) ⌊ ((u2 + v2) ⌊ (u1 + v1))
}
torus_sdf←{q←((length ( (⍵[1]) (⍵[3]) )) - (⍺[1])) (⍵[2]) ⋄(length q) - (⍺[2])}
scene_obj1_ball←1
scene_obj2_octa←2
scene_obj3_melted_balls←3
scene_obj4_floor←4
scene_obj5_frame←5
scene_obj6_ball←6
scene_obj7_torus←7
sdf←{
ball1 ← 0.5 ball_sdf (⍵ - ( 0 ¯1 4) )
melted_balls2 ← {
b1←0.5 ball_sdf (⍵ - ( 0.3 1.0 4) ) ⋄ b2←0.5 ball_sdf (⍵ - ( 1.55 1.0 4.0) )
0.8 blend b1 b2
} ⍵
octa3 ← 0.5 octahedron_sdf ( ⍵ - ( ¯1 0.55 4))
floor4←⊃(( 0 1 0 ) inf_plane_floor ((⍵ - ( 0 ¯4 0)) 1))
frame5 ←(( ( 2.0 × 0.65) 0.65 0.65) 0.05 ) boxframe_sdf ( ⍵ - (¯0.65 ¯1 4))
ball6 ← 0.5 ball_sdf (⍵ - ( ¯1.15 ¯1 4) )
torus7 ← ( 2.0 0.12) torus_sdf ( ⍵ - ( 0 1.37 4))
geo←( ball1 octa3 melted_balls2 floor4 frame5 ball6 torus7)
closest←⊃⍋geo
(geo[closest]) closest
⍝⌊/(a b c)
}
march←{
total_dist ← ⍺[1] ⋄ro ← ⊃⍺[2] ⋄ rd ← ⊃⍺[3] ⋄ max_dest ← ⊃⍺[4] ⋄ r ← sdf ( ro + rd × total_dist )
dist ← r[1] ⋄ obj ← r[2] ⋄ ⍵<100 ^ dist < max_dest : {dist < epsi: (1 (dist + total_dist) obj ) ⋄ ( (dist + total_dist) ro rd max_dest) march ⍵ }⍵+1 ⋄ ( 0 0)
}
dot←{+/(⍵×⍺)}
reflect←{ ⍺ - 2.0 × ( ⍵ dot ⍺) × ⍵ }
estNormal←{
e←epsi ⋄ _sdf←{(sdf ⍵)[1]}
p1←(⍵[1] × 1.0) ⋄p2←(⍵[2] × 1.0) ⋄p3←(⍵[3] × 1.0)
a1←p1 p2 p3 + e 0 0 ⋄a2←p1 p2 p3 + 0 e 0 ⋄a3←p1 p2 p3 + 0 0 e
b1←p1 p2 p3 - e 0 0 ⋄b2←p1 p2 p3 - 0 e 0 ⋄b3←p1 p2 p3 - 0 0 e
s1←(_sdf a1) - (_sdf b1) ⋄s2←(_sdf a2) - (_sdf b2) ⋄s3←(_sdf a3) - (_sdf b3)
norm (s1 s2 s3)
}
phongLight←{
p←⊃⍵[1]⋄ro←⊃⍵[2]
ambient_color←(⊃⍺[1]) × 0.5 × (1.0, 1.0, 1.0) ⋄k_d←⊃⍺[2] ⋄k_s←⊃⍺[3]
alpha←⊃⍺[4]⋄light_intensity←⊃⍺[5] ⋄n← estNormal p ⋄light_pos←((0) (2) (0))
l← norm ( light_pos - p) ⋄v← norm ( ro - p) ⋄r← norm ( (-l) reflect n )
⋄dotln ← l dot n ⋄dotrv ← r dot v ⋄dotln < 0.0: 3 ⍴ 0.0 ⋄ambient_color + ({
⍵ < 0.0: light_intensity × k_d × dotln
light_intensity × (k_d × dotln + k_s ×( dotrv * alpha ) )
} dotrv)
}
checkers←{size←⍺ ⋄pos_x← ⌊ ( (⍵[1]) ÷ size) ⋄ pos_y← ⌊ ( (⍵[2]) ÷ size) ⋄ 3 ⍴ (2.0 | (pos_x + (2.0 | pos_y)))}
⍝ this is more of a grid tbh
checkers_ball←{ si←2 ⋄ox←⍵[1] ⋄oy←⍵[1] + ⍵[2]
x←( 2.0 | (ox×10) ) > 1.0 ⋄y←(( 2.0 | oy × 10) ) > 1.0⋄x ∨ y : (1 0 0) ⋄ (1 1 1)
}
phong←{
total_dist←⊃⍵[1]⋄ro←⊃⍵[2] ⋄rd←⊃⍵[3]⋄obj←⍵[4] ⋄p←ro + rd × total_dist
l1←{
ambient_color←checkers_ball p
diffuse_color←0.5 0.5 0.5
specular_color←0.1 0.1 0.1
alpha←1.0
light_intensity←⍵
ambient_color diffuse_color specular_color alpha light_intensity
} 0.5
l2←{
ambient_color←0.3 0.6 0
diffuse_color←0.5 0.5 0.5
specular_color←0.1 0.9 0.1
alpha←1.0
light_intensity←⍵
ambient_color diffuse_color specular_color alpha light_intensity
} 0.5
l3←{
ambient_color←(0.0 0.749 1.0) × ( 0.6)
diffuse_color←ambient_color × ( 0.4)
specular_color←0.0 0.0 1.0
alpha←1.0
light_intensity←⍵
ambient_color diffuse_color specular_color alpha light_intensity
} 1.0
l4←{
ambient_color←0.5 0.5 0.5
diffuse_color←0.5 0.5 0.5
specular_color←1.0 1.0 1.0
alpha←1.0
light_intensity←⍵
ambient_color diffuse_color specular_color alpha light_intensity
} 0.5
l5←{
ambient_color←0.57 0.164 0.96
diffuse_color←0.57 0.164 0.96
specular_color←0.0 1.0 0.0
alpha←1.0
light_intensity←⍵
ambient_color diffuse_color specular_color alpha light_intensity
} 1.0
l6←{
ambient_color←0.2 0.2 0.2
diffuse_color←0.4 0.4 0.4
specular_color←0.3 0.3 0.3
alpha←1.0
light_intensity←⍵
ambient_color diffuse_color specular_color alpha light_intensity
} 0.5 ⋄lightPos←3 3 0
{
⍵=scene_obj1_ball: l1 phongLight p ro
⍵=scene_obj2_octa: l2 phongLight p ro
⍵=scene_obj3_melted_balls: l3 phongLight p ro
⍵=scene_obj4_floor: (l4 phongLight p ro) + (4 checkers ((p[1]) (p[3])))
⍵=scene_obj5_frame: (l5 phongLight p ro)
⍵=scene_obj6_ball: (l6 phongLight p ro) + ( 0.5 × (0.22 checkers ((p[1]) (p[2]))))
⍵=scene_obj7_torus: (l6 phongLight p ro) + ( 0.5 × (0.22 checkers ((p[1]) (p[2]))))
} obj
}
sky←{ dawn←(0.4 (0.4 - (2.67 * (⍵[2] × ¯20.0 )) × 0.15) 0) × ( 2.67 * ( ⍵[2] × ¯9)) ⋄sky←(0.3 0.5 0.6) × ( 1.0 - (2.67 * (⍵[2] × ¯8.0))) × (2.67 * (⍵[2] × ¯0.9)) ⋄sky+dawn⍝+sun
}
rgba←{ cam_dir←⊃⍵[1] ⋄time←⊃⍵[2] ⋄ bg←⊃⍵[3] ⋄ d←(( 0 cam_origin cam_dir 35) march 0) ⋄( 0 0 0 0 ) {⍵=1: ⍺ {((⍺ phong (⍵ , d[3])) , 0)} d[2] cam_origin cam_dir ⋄ bg , 0 } d[1] }
⎕←'rendering @ resolution' xres 'x' yres
render_loop←{t←⍵ ⋄ pixbuf←⊃,/{y←⊃⍵[2] ⋄ x←{⍵=-0.5: {⎕←'rendered' (100 + (y+0.5) × -100) '%' ⋄ ⍵} ⍵ ⋄ ⍵}⊃⍵[1] ⋄ cam_dir←norm x y 1 ⋄ rgba cam_dir t (sky x (0 ⌈(y+0.12)))}¨uv_vecs ⋄ ⎕←'drawing' (⍴ pixbuf) 'pixel rgba values' ⋄ draw drawer xres yres pixbuf
}¨⍳1 ⍝ only run one loop as for now there is an OOM event
⎕←'press return to render next frame'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment