1 ! (c)2009 Joe Groff bsd license
2 USING: accessors arrays combinators.tuple game-loop game-worlds
3 generalizations gpu gpu.render gpu.shaders gpu.util gpu.util.wasd
4 kernel literals math math.matrices math.order math.vectors
5 method-chains sequences ui ui.gadgets ui.gadgets.worlds
9 GLSL-SHADER-FILE: raytrace-vertex-shader vertex-shader "raytrace.v.glsl"
10 GLSL-SHADER-FILE: raytrace-fragment-shader fragment-shader "raytrace.f.glsl"
11 GLSL-PROGRAM: raytrace-program
12 raytrace-vertex-shader raytrace-fragment-shader ;
14 UNIFORM-TUPLE: sphere-uniforms
15 { "center" vec3-uniform f }
16 { "radius" float-uniform f }
17 { "color" vec4-uniform f } ;
19 UNIFORM-TUPLE: raytrace-uniforms
20 { "mv-inv-matrix" mat4-uniform f }
21 { "fov" vec2-uniform f }
23 { "spheres" sphere-uniforms 4 }
25 { "floor-height" float-uniform f }
26 { "floor-color" vec4-uniform 2 }
27 { "background-color" vec4-uniform f }
28 { "light-direction" vec3-uniform f } ;
30 CONSTANT: reflection-color { 1.0 0.0 1.0 0.0 }
38 { theta float initial: 0.0 } ;
40 TUPLE: raytrace-world < wasd-world
45 : tick-sphere ( sphere -- )
46 dup dtheta>> [ + ] curry change-theta drop ;
48 : sphere-center ( sphere -- center )
49 [ [ axis>> ] [ theta>> ] bi rotation-matrix4 ]
52 : <sphere-uniforms> ( world -- uniforms )
53 [ wasd-mv-inv-matrix ]
57 [ [ sphere-center ] [ radius>> ] [ color>> ] tri sphere-uniforms boa ] map
60 { { 1.0 0.0 0.0 1.0 } { 1.0 1.0 1.0 1.0 } } ! floor_color
61 { 0.15 0.15 1.0 1.0 } ! background_color
62 { 0.0 -1.0 -0.1 } ! light_direction
63 raytrace-uniforms boa ;
65 CONSTANT: initial-spheres {
66 T{ sphere f { 0.0 1.0 0.0 } { 0.0 0.0 0.0 } 0.0 4.0 $ reflection-color }
67 T{ sphere f { 0.0 1.0 0.0 } { 7.0 0.0 0.0 } 0.02 1.0 { 1.0 0.0 0.0 1.0 } }
68 T{ sphere f { 0.0 0.0 -1.0 } { -9.0 0.0 0.0 } 0.03 1.0 { 0.0 1.0 0.0 1.0 } }
69 T{ sphere f { 1.0 0.0 0.0 } { 0.0 5.0 0.0 } 0.025 1.0 { 1.0 1.0 0.0 1.0 } }
72 BEFORE: raytrace-world begin-world
74 { -2.0 6.25 10.0 } 0.19 0.55 set-wasd-view
75 initial-spheres [ clone ] map >>spheres
76 raytrace-program <program-instance> <window-vertex-array> >>vertex-array
81 AFTER: raytrace-world resize-world
82 dup dim>> dup first2 min >float v/n fov v*n >>fov drop ;
84 AFTER: raytrace-world tick*
85 spheres>> [ tick-sphere ] each ;
87 M: raytrace-world draw-world*
89 { "primitive-mode" [ drop triangle-strip-mode ] }
90 { "indexes" [ drop T{ index-range f 0 4 } ] }
91 { "uniforms" [ <sphere-uniforms> ] }
92 { "vertex-array" [ vertex-array>> ] }
93 } <render-set> render ;
95 M: raytrace-world pref-dim* drop { 1024 768 } ;
96 M: raytrace-world tick-length drop 1000 30 /i ;
97 M: raytrace-world wasd-movement-speed drop 1/4. ;
99 : raytrace-window ( -- )
101 f T{ world-attributes
102 { world-class raytrace-world }
103 { title "Raytracing" }
104 { pixel-format-attributes {
112 MAIN: raytrace-window