diff --git a/extra/fluids/fluids.factor b/extra/fluids/fluids.factor index 1b4f1524de..1fff38b079 100644 --- a/extra/fluids/fluids.factor +++ b/extra/fluids/fluids.factor @@ -1,12 +1,12 @@ ! 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 @@ -65,7 +65,7 @@ particle_t-array{ [ 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 @@ -100,50 +100,27 @@ M: fluids-world begin-game-world 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 } } - >>color-texture - - dup color-texture>> 0 1array f f { 320 240 } >>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 dup set-gpu-state - f origin-upper-left 1.0 set-gpu-state world particles>> [ [ p>> [ x>> , ] [ y>> , ] bi ] each ] curry float-array{ } make :> verts - { 0 0 } { 320 240 } 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 &dispose &dispose ] } - { "indexes" [ nip length 2 / 0 swap ] } - { "framebuffer" [ drop framebuffer>> ] } - } 2 render - - world color-texture>> gaussian-blur - { 0 0 } { 640 480 } set-gpu-state - world ramp>> { - { "primitive-mode" [ 2drop triangle-strip-mode ] } - { "uniforms" [ step-uniforms boa ] } - { "vertex-array" [ 2drop step-program ] } - { "indexes" [ 2drop T{ index-range f 0 4 } ] } - } 2 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 { @@ -151,7 +128,7 @@ 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 ] } } ; @@ -159,7 +136,7 @@ MAIN: fluids fluids-world H{ { T{ button-down } [ [ - hand-loc get { 640 480 } v/ 2 v*n 1 v-n { 1 -1 } v* first2 float2_t + hand-loc get { 1024 768 } v/ 2 v*n 1 v-n { 1 -1 } v* first2 float2_t dup 2.0 particle_t suffix ] change-particles drop ] } } set-gestures diff --git a/extra/gpu/effects/step/step.factor b/extra/gpu/effects/step/step.factor index f50c0c3cc1..bd3b013b4a 100644 --- a/extra/gpu/effects/step/step.factor +++ b/extra/gpu/effects/step/step.factor @@ -1,6 +1,7 @@ ! 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 @@ -21,3 +22,19 @@ UNIFORM-TUPLE: step-uniforms { "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 set-gpu-state + [ step-uniforms boa ] dip { + { "primitive-mode" [ 2drop triangle-strip-mode ] } + { "uniforms" [ drop ] } + { "vertex-array" [ 2drop step-program ] } + { "indexes" [ 2drop T{ index-range f 0 4 } ] } + { "framebuffer" [ nip ] } + } 2 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 ; diff --git a/extra/gpu/util/util.factor b/extra/gpu/util/util.factor index 29fe5f1314..2678f0452c 100644 --- a/extra/gpu/util/util.factor +++ b/extra/gpu/util/util.factor @@ -1,7 +1,7 @@ ! (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 @@ -125,10 +125,34 @@ CONSTANT: window-vertexes dup { { default-attachment { 0 0 0 } } } clear-framebuffer ] keep ; -: draw-texture ( texture -- ) +: draw-texture ( texture dim -- ) + { 0 0 } swap set-gpu-state { { "primitive-mode" [ drop triangle-strip-mode ] } { "uniforms" [ window-uniforms boa ] } - { "vertex-array" [ drop window-program ] } + { "vertex-array" [ drop window-program &dispose ] } { "indexes" [ drop T{ index-range f 0 4 } ] } } render ; + +:: ( verts program-instance -- vertex-array ) + verts stream-upload draw-usage vertex-buffer byte-array>buffer &dispose + program-instance &dispose ; + +: (blended-point-sprite-batch) ( verts framebuffer texture point-size dim -- ) + f eq-add func-one func-one dup set-gpu-state + f origin-upper-left 1.0 set-gpu-state + GL_POINT_SPRITE glEnable + { 0 0 } swap set-gpu-state + window-point-uniforms boa { + { "primitive-mode" [ 3drop points-mode ] } + { "uniforms" [ 2nip ] } + { "vertex-array" [ 2drop window-point-program ] } + { "indexes" [ 2drop length 2 / 0 swap ] } + { "framebuffer" [ drop nip ] } + } 3 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 ;