1 ! Copyright (C) 2010 Erik Charlebois.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays classes.struct destructors game.loop
4 game.worlds gpu gpu.buffers gpu.effects.blur gpu.framebuffers
5 gpu.render gpu.shaders gpu.state gpu.textures gpu.util images
6 images.loader kernel literals locals make math math.rectangles
7 math.vectors namespaces opengl.gl sequences specialized-arrays
8 ui.gadgets.worlds ui.gestures ui.pixel-formats gpu.effects.step
9 images.pgm images.ppm ;
10 FROM: alien.c-types => float ;
11 SPECIALIZED-ARRAY: float
18 : f2+ ( lhs rhs -- res )
21 2bi float2_t <struct-boa> ; inline
23 : f2- ( lhs rhs -- res )
26 2bi float2_t <struct-boa> ; inline
28 : f2*n ( lhs rhs -- res )
31 2bi float2_t <struct-boa> ; inline
37 SPECIALIZED-ARRAY: particle_t
39 CONSTANT: gravity S{ float2_t f 0.0 -0.1 }
41 :: verlet-integrate-particle ( p dt -- p' )
44 gravity dt dt * 1.0 p m>> 2.0 * / * f2*n :> v3
46 p p m>> particle_t <struct-boa> ; inline
48 CONSTANT: initial-particles
50 S{ particle_t f S{ float2_t f 0.5 0.6 } S{ float2_t f 0.499 0.599 } 1.0 }
51 S{ particle_t f S{ float2_t f 0.5 0.6 } S{ float2_t f 0.501 0.599 } 3.0 }
53 S{ particle_t f S{ float2_t f 0.5 0.5 } S{ float2_t f 0.5 0.5 } 2.0 }
54 S{ particle_t f S{ float2_t f 0.5 0.6 } S{ float2_t f 0.5 0.599 } 1.0 }
55 S{ particle_t f S{ float2_t f 0.6 0.5 } S{ float2_t f 0.6 0.5 } 3.0 }
56 S{ particle_t f S{ float2_t f 0.7 0.5 } S{ float2_t f 0.7 0.5 } 1.0 }
57 S{ particle_t f S{ float2_t f 0.1 0.5 } S{ float2_t f 0.1 0.5 } 5.0 }
58 S{ particle_t f S{ float2_t f 0.2 0.5 } S{ float2_t f 0.2 0.5 } 1.0 }
59 S{ particle_t f S{ float2_t f 0.3 0.3 } S{ float2_t f 0.3 0.3 } 4.0 }
60 S{ particle_t f S{ float2_t f 0.5 0.15 } S{ float2_t f 0.5 0.15 } 1.0 }
61 S{ particle_t f S{ float2_t f 0.5 0.1 } S{ float2_t f 0.5 0.1 } 9.0 }
64 : integrate-particles! ( particles dt -- particles )
65 [ verlet-integrate-particle ] curry map! ;
67 TUPLE: fluids-world < game-world
68 particles texture ramp { paused boolean initial: f } ;
70 : make-texture ( pathname -- texture )
74 [ component-type>> ] bi
76 { wrap clamp-texcoord-to-edge }
77 { min-filter filter-nearest }
78 { mag-filter filter-nearest }
79 { min-mipmap-filter f } }
83 0 swap [ allocate-texture-image ] 3keep 2drop
88 : integrate ( world -- )
89 particles>> $[ 60 fps 1000000 /f ] integrate-particles! drop ;
92 fluid get [ not ] change-paused drop ;
95 fluid get paused>> [ fluid get integrate ] when ;
97 M: fluids-world begin-game-world
100 initial-particles clone >>particles
101 "resource:extra/fluids/particle2.pgm" make-texture >>texture
102 "resource:extra/fluids/colors.ppm" make-texture >>ramp
105 M: fluids-world end-game-world
108 M: fluids-world tick-game-world
109 dup paused>> [ drop ] [ integrate ] if ;
111 M:: fluids-world draw-world* ( world -- )
113 [ p>> [ x>> , ] [ y>> , ] bi ] each
114 ] curry float-array{ } make :> verts
117 verts world texture>> 50.0 { 320 240 } blended-point-sprite-batch &dispose
119 blend-state new set-gpu-state
121 gaussian-blur &dispose world ramp>> { 1024 768 } step-texture &dispose
122 { 1024 768 } draw-texture
127 { world-class fluids-world }
128 { title "Fluids Test" }
129 { pixel-format-attributes {
130 windowed double-buffered T{ depth-bits { value 24 } } } }
131 { pref-dim { 1024 768 } }
132 { tick-interval-micros $[ 60 fps ] }
138 { T{ button-down } [ [
139 hand-loc get { 1024 768 } v/ 2 v*n 1 v-n { 1 -1 } v* first2 float2_t <struct-boa>
140 dup 2.0 particle_t <struct-boa> suffix
141 ] change-particles drop ] }