! Copyright (C) 2010 Erik Charlebois.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays classes.struct destructors game.loop
-game.worlds gpu gpu.buffers gpu.framebuffers gpu.render gpu.shaders
-gpu.state gpu.textures gpu.util images images.loader kernel literals
-locals make math math.rectangles math.vectors namespaces opengl.gl
-sequences specialized-arrays ui.gadgets.worlds images.ppm
-ui.gestures ui.pixel-formats images.pgm gpu.effects.blur
-gpu.effects.step ;
+game.worlds gpu gpu.buffers gpu.effects.blur gpu.framebuffers
+gpu.render gpu.shaders gpu.state gpu.textures gpu.util images
+images.loader kernel literals locals make math math.rectangles
+math.vectors namespaces opengl.gl sequences specialized-arrays
+ui.gadgets.worlds ui.gestures ui.pixel-formats gpu.effects.step
+images.pgm images.ppm ;
FROM: alien.c-types => float ;
SPECIALIZED-ARRAY: float
IN: fluids
[ verlet-integrate-particle ] curry map! ;
TUPLE: fluids-world < game-world
- particles texture framebuffer color-texture ramp { paused boolean initial: f } ;
+ particles texture ramp { paused boolean initial: f } ;
: make-texture ( pathname -- texture )
load-image
initial-particles clone >>particles
"resource:extra/fluids/particle2.pgm" make-texture >>texture
"resource:extra/fluids/colors.ppm" make-texture >>ramp
-
- RGB float-components T{ texture-parameters
- { wrap clamp-texcoord-to-edge }
- { min-filter filter-linear }
- { min-mipmap-filter f } }
- <texture-2d> >>color-texture
-
- dup color-texture>> 0 <texture-2d-attachment> 1array f f { 320 240 } <framebuffer> >>framebuffer
drop ;
M: fluids-world end-game-world
- framebuffer>> dispose ;
+ drop ;
M: fluids-world tick-game-world
dup paused>> [ drop ] [ integrate ] if ;
M:: fluids-world draw-world* ( world -- )
- world framebuffer>> { { default-attachment { 0 0 0 } } } clear-framebuffer
- system-framebuffer { { default-attachment { 0 0 0 } } } clear-framebuffer
-
- f eq-add func-one func-one <blend-mode> dup <blend-state> set-gpu-state
- f origin-upper-left 1.0 <point-state> set-gpu-state
world particles>> [
[ p>> [ x>> , ] [ y>> , ] bi ] each
] curry float-array{ } make :> verts
- { 0 0 } { 320 240 } <rect> <viewport-state> set-gpu-state
- GL_POINT_SPRITE glEnable
- world verts {
- { "primitive-mode" [ 2drop points-mode ] }
- { "uniforms" [ drop texture>> 50.0 window-point-uniforms boa ] }
- { "vertex-array" [ nip stream-upload draw-usage vertex-buffer byte-array>buffer &dispose window-point-program <program-instance> &dispose <vertex-array> &dispose ] }
- { "indexes" [ nip length 2 / 0 swap <index-range> ] }
- { "framebuffer" [ drop framebuffer>> ] }
- } 2<render-set> render
-
- world color-texture>> gaussian-blur
- { 0 0 } { 640 480 } <rect> <viewport-state> set-gpu-state
- world ramp>> {
- { "primitive-mode" [ 2drop triangle-strip-mode ] }
- { "uniforms" [ step-uniforms boa ] }
- { "vertex-array" [ 2drop <window-vertex-buffer> step-program <program-instance> <vertex-array> ] }
- { "indexes" [ 2drop T{ index-range f 0 4 } ] }
- } 2<render-set> render
+ [
+ verts world texture>> 50.0 { 320 240 } blended-point-sprite-batch &dispose
+
+ blend-state new set-gpu-state
+
+ gaussian-blur &dispose world ramp>> { 1024 768 } step-texture &dispose
+ { 1024 768 } draw-texture
+ ] with-destructors
;
GAME: fluids {
{ title "Fluids Test" }
{ pixel-format-attributes {
windowed double-buffered T{ depth-bits { value 24 } } } }
- { pref-dim { 640 480 } }
+ { pref-dim { 1024 768 } }
{ tick-interval-micros $[ 60 fps ] }
} ;
fluids-world H{
{ T{ button-down } [ [
- hand-loc get { 640 480 } v/ 2 v*n 1 v-n { 1 -1 } v* first2 float2_t <struct-boa>
+ hand-loc get { 1024 768 } v/ 2 v*n 1 v-n { 1 -1 } v* first2 float2_t <struct-boa>
dup 2.0 particle_t <struct-boa> suffix
] change-particles drop ] }
} set-gestures
! Copyright (C) 2010 Erik Charlebois.
! See http://factorcode.org/license.txt for BSD license.
-USING: gpu.render gpu.shaders gpu.util ;
+USING: destructors gpu.render gpu.shaders gpu.state gpu.textures
+gpu.util images kernel locals math.rectangles ;
IN: gpu.effects.step
GLSL-SHADER: step-fragment-shader fragment-shader
{ "ramp" texture-uniform f } ;
GLSL-PROGRAM: step-program window-vertex-shader step-fragment-shader window-vertex-format ;
+
+: (step-texture) ( texture ramp texture dim -- )
+ { 0 0 } swap <rect> <viewport-state> set-gpu-state
+ [ step-uniforms boa ] dip {
+ { "primitive-mode" [ 2drop triangle-strip-mode ] }
+ { "uniforms" [ drop ] }
+ { "vertex-array" [ 2drop <window-vertex-buffer> step-program <program-instance> <vertex-array> ] }
+ { "indexes" [ 2drop T{ index-range f 0 4 } ] }
+ { "framebuffer" [ nip ] }
+ } 2<render-set> render ;
+
+:: step-texture ( texture ramp dim -- texture )
+ dim RGB float-components <2d-render-texture> :> ( target-framebuffer target-texture )
+ texture ramp target-framebuffer dim (step-texture)
+ target-framebuffer dispose
+ target-texture ;
! (c)2009 Joe Groff bsd license
-USING: accessors arrays gpu.buffers gpu.framebuffers gpu.render
-gpu.shaders gpu.textures images kernel locals opengl.framebuffers
-specialized-arrays ;
+USING: arrays destructors gpu.buffers gpu.framebuffers gpu.render
+gpu.shaders gpu.state gpu.textures images kernel locals math
+math.rectangles opengl.gl sequences specialized-arrays ;
FROM: alien.c-types => float ;
SPECIALIZED-ARRAY: float
IN: gpu.util
dup { { default-attachment { 0 0 0 } } } clear-framebuffer
] keep ;
-: draw-texture ( texture -- )
+: draw-texture ( texture dim -- )
+ { 0 0 } swap <rect> <viewport-state> set-gpu-state
{
{ "primitive-mode" [ drop triangle-strip-mode ] }
{ "uniforms" [ window-uniforms boa ] }
- { "vertex-array" [ drop <window-vertex-buffer> window-program <program-instance> <vertex-array> ] }
+ { "vertex-array" [ drop window-program <program-instance> <window-vertex-array> &dispose ] }
{ "indexes" [ drop T{ index-range f 0 4 } ] }
} <render-set> render ;
+
+:: <streamed-vertex-array> ( verts program-instance -- vertex-array )
+ verts stream-upload draw-usage vertex-buffer byte-array>buffer &dispose
+ program-instance <vertex-array> &dispose ;
+
+: (blended-point-sprite-batch) ( verts framebuffer texture point-size dim -- )
+ f eq-add func-one func-one <blend-mode> dup <blend-state> set-gpu-state
+ f origin-upper-left 1.0 <point-state> set-gpu-state
+ GL_POINT_SPRITE glEnable
+ { 0 0 } swap <rect> <viewport-state> set-gpu-state
+ window-point-uniforms boa {
+ { "primitive-mode" [ 3drop points-mode ] }
+ { "uniforms" [ 2nip ] }
+ { "vertex-array" [ 2drop window-point-program <program-instance> <streamed-vertex-array> ] }
+ { "indexes" [ 2drop length 2 / 0 swap <index-range> ] }
+ { "framebuffer" [ drop nip ] }
+ } 3<render-set> render ;
+
+:: blended-point-sprite-batch ( verts texture point-size dim -- texture )
+ dim RGB float-components <2d-render-texture> :> ( target-framebuffer target-texture )
+ verts target-framebuffer texture point-size dim (blended-point-sprite-batch)
+ target-framebuffer dispose
+ target-texture ;