]> gitweb.factorcode.org Git - factor.git/blob - extra/gpu/demos/raytrace/raytrace.factor
Fixes #2966
[factor.git] / extra / gpu / demos / raytrace / raytrace.factor
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
6 ui.pixel-formats ;
7 IN: gpu.demos.raytrace
8
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 ;
13
14 UNIFORM-TUPLE: sphere-uniforms
15     { "center" vec3-uniform  f }
16     { "radius" float-uniform f }
17     { "color"  vec4-uniform  f } ;
18
19 UNIFORM-TUPLE: raytrace-uniforms
20     { "mv-inv-matrix"    mat4-uniform f }
21     { "fov"              vec2-uniform f }
22     
23     { "spheres"          sphere-uniforms 4 }
24
25     { "floor-height"     float-uniform f }
26     { "floor-color"      vec4-uniform 2 }
27     { "background-color" vec4-uniform f }
28     { "light-direction"  vec3-uniform f } ;
29
30 CONSTANT: reflection-color { 1.0 0.0 1.0 0.0 }
31
32 TUPLE: sphere
33     { axis array }
34     { home array }
35     { dtheta float }
36     { radius float }
37     { color array }
38     { theta float initial: 0.0 } ;
39
40 TUPLE: raytrace-world < wasd-world
41     fov
42     spheres
43     vertex-array ;
44
45 : tick-sphere ( sphere -- )
46     dup dtheta>> [ + ] curry change-theta drop ;
47
48 : sphere-center ( sphere -- center )
49     [ [ axis>> ] [ theta>> ] bi rotation-matrix4 ]
50     [ home>> ] bi m.v ;
51
52 : <sphere-uniforms> ( world -- uniforms )
53     [ wasd-mv-inv-matrix ]
54     [ fov>> ]
55     [
56         spheres>>
57         [ [ sphere-center ] [ radius>> ] [ color>> ] tri sphere-uniforms boa ] map
58     ] tri
59     -30.0 ! floor_height
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 ;
64
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 } }
70 }
71
72 BEFORE: raytrace-world begin-world
73     init-gpu
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
77     drop ;
78
79 CONSTANT: fov 0.7
80
81 AFTER: raytrace-world resize-world
82     dup dim>> dup first2 min >float v/n fov v*n >>fov drop ;
83
84 AFTER: raytrace-world tick*
85     spheres>> [ tick-sphere ] each ;
86
87 M: raytrace-world draw-world*
88     {
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 ;
94
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. ;
98
99 : raytrace-window ( -- )
100     [
101         f T{ world-attributes
102             { world-class raytrace-world }
103             { title "Raytracing" }
104             { pixel-format-attributes {
105                 windowed
106                 double-buffered
107             } }
108             { grab-input? t }
109         } open-window
110     ] with-ui ;
111
112 MAIN: raytrace-window