]> gitweb.factorcode.org Git - factor.git/blob - extra/fluids/fluids.factor
factor: trim using lists
[factor.git] / extra / fluids / fluids.factor
1 ! Copyright (C) 2010 Erik Charlebois.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.data classes.struct destructors game.loop
4 game.worlds gpu gpu.effects.blur gpu.effects.step gpu.state
5 gpu.textures gpu.util images.loader kernel literals make math
6 math.vectors namespaces sequences specialized-arrays
7 ui.gadgets.worlds ui.gestures ui.pixel-formats ;
8 FROM: alien.c-types => float ;
9 SPECIALIZED-ARRAY: float
10 IN: fluids
11
12 STRUCT: particle_t
13     { p float[2] }
14     { v float[2] }
15     { m float    } ;
16 SPECIALIZED-ARRAY: particle_t
17
18 CONSTANT: gravity { 0.0 -0.1 }
19
20 :: verlet-integrate-particle ( particle dt -- particle' )
21     particle [ p>> ] [ v>> ] bi dt v*n v+
22     gravity dt dt * particle m>> 2 * / v*n v+ :> p'
23     p' particle p>> v- dt v/n :> v'
24     p' v' particle m>> particle_t boa ; inline
25
26 CONSTANT: initial-particles
27 particle_t-array{
28     S{ particle_t f float-array{ 0.5 0.6 } float-array{ 0 0.1 } 1.0 }
29     S{ particle_t f float-array{ 0.5 0.6 } float-array{ 0.1 0 } 3.0 }
30
31     S{ particle_t f float-array{ 0.5 0.5 } float-array{ 0.1 0.1 } 2.0 }
32     S{ particle_t f float-array{ 0.5 0.6 } float-array{ -0.1 0 } 1.0 }
33     S{ particle_t f float-array{ 0.6 0.5 } float-array{ 0 -0.1 } 3.0 }
34     S{ particle_t f float-array{ 0.7 0.5 } float-array{ 0.1 0.1 } 1.0 }
35     S{ particle_t f float-array{ 0.1 0.5 } float-array{ -0.1 -0.1 } 5.0 }
36     S{ particle_t f float-array{ 0.2 0.5 } float-array{ 0 0 } 1.0 }
37     S{ particle_t f float-array{ 0.3 0.3 } float-array{ 0 0 } 4.0 }
38     S{ particle_t f float-array{ 0.5 0.15 } float-array{ 0 0 } 1.0 }
39     S{ particle_t f float-array{ 0.5 0.1 } float-array{ 0 0 } 9.0 }
40 }
41
42 : integrate-particles! ( particles dt -- particles )
43     [ verlet-integrate-particle ] curry map! ;
44
45 TUPLE: fluids-world < game-world
46     particles texture ramp { paused boolean initial: f } ;
47
48 : make-texture ( pathname -- texture )
49     load-image
50     [
51         [ component-order>> ]
52         [ component-type>> ] bi
53         T{ texture-parameters
54            { wrap clamp-texcoord-to-edge }
55            { min-filter filter-nearest }
56            { mag-filter filter-nearest }
57            { min-mipmap-filter f } }
58         <texture-2d>
59     ]
60     [
61         0 swap [ allocate-texture-image ] keepdd
62     ] bi ;
63
64 SYMBOL: fluid
65
66 : integrate ( world -- )
67     particles>> 1/60 integrate-particles! drop ;
68
69 : pause ( -- )
70     fluid get [ not ] change-paused drop ;
71
72 : step ( -- )
73     fluid get paused>> [ fluid get integrate ] when ;
74
75 M: fluids-world begin-game-world
76     dup fluid set
77     init-gpu
78     initial-particles clone >>particles
79     "vocab:fluids/particle2.pgm" make-texture >>texture
80     "vocab:fluids/colors.ppm" make-texture >>ramp
81     drop ;
82
83 M: fluids-world end-game-world
84     drop ;
85
86 M: fluids-world tick-game-world
87     dup paused>> [ drop ] [ integrate ] if ;
88
89 M:: fluids-world draw-world* ( world -- )
90     world particles>> [
91         [ p>> [ first , ] [ second , ] bi ] each
92     ] curry float-array{ } make :> verts
93
94     [
95         verts world texture>> 30.0 world dim>> { 4 4 } v/
96         blended-point-sprite-batch &dispose
97         blend-state new set-gpu-state
98         gaussian-blur &dispose
99         world ramp>> world dim>> step-texture &dispose
100         world dim>> draw-texture
101     ] with-destructors ;
102
103 GAME: fluids {
104     { world-class fluids-world }
105     { title "Fluids Test" }
106     { pixel-format-attributes {
107         windowed double-buffered T{ depth-bits { value 24 } } } }
108     { pref-dim { 1024 768 } }
109     { tick-interval-nanos $[ 60 fps ] }
110 } ;
111
112 fluids-world H{
113     { T{ button-down } [ [
114         hand-loc get float >c-array
115         world get dim>> float >c-array v/ 2 v*n 1 v-n { 1 -1 } v*
116         float-array{ 0 0.2 } 2.0 particle_t boa suffix
117     ] change-particles drop ] }
118 } set-gestures