Techniques work

release
Erik Charlebois 2010-04-04 18:57:58 -07:00
parent 925d2c7e58
commit 964f45fc77
14 changed files with 16732 additions and 4 deletions

1
extra/fluids/authors.txt Normal file
View File

@ -0,0 +1 @@
Erik Charlebois

BIN
extra/fluids/colors.ppm Normal file

Binary file not shown.

164
extra/fluids/fluids.factor Normal file
View File

@ -0,0 +1,164 @@
! 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 ;
FROM: alien.c-types => float ;
SPECIALIZED-ARRAY: float
IN: fluids
STRUCT: float2_t
{ x float }
{ y float } ;
: f2+ ( lhs rhs -- res )
[ [ x>> ] bi@ + ]
[ [ y>> ] bi@ + ]
2bi float2_t <struct-boa> ; inline
: f2- ( lhs rhs -- res )
[ [ x>> ] bi@ - ]
[ [ y>> ] bi@ - ]
2bi float2_t <struct-boa> ; inline
: f2*n ( lhs rhs -- res )
[ [ x>> ] dip * ]
[ [ y>> ] dip * ]
2bi float2_t <struct-boa> ; inline
STRUCT: particle_t
{ p float2_t }
{ p' float2_t }
{ m float } ;
SPECIALIZED-ARRAY: particle_t
CONSTANT: gravity S{ float2_t f 0.0 -0.1 }
:: verlet-integrate-particle ( p dt -- p' )
p p>> 2.0 f2*n :> v1
p p'>> :> v2
gravity dt dt * 1.0 p m>> 2.0 * / * f2*n :> v3
v1 v2 f2- v3 f2+
p p m>> particle_t <struct-boa> ; inline
CONSTANT: initial-particles
particle_t-array{
S{ particle_t f S{ float2_t f 0.5 0.6 } S{ float2_t f 0.499 0.599 } 1.0 }
S{ particle_t f S{ float2_t f 0.5 0.6 } S{ float2_t f 0.501 0.599 } 3.0 }
S{ particle_t f S{ float2_t f 0.5 0.5 } S{ float2_t f 0.5 0.5 } 2.0 }
S{ particle_t f S{ float2_t f 0.5 0.6 } S{ float2_t f 0.5 0.599 } 1.0 }
S{ particle_t f S{ float2_t f 0.6 0.5 } S{ float2_t f 0.6 0.5 } 3.0 }
S{ particle_t f S{ float2_t f 0.7 0.5 } S{ float2_t f 0.7 0.5 } 1.0 }
S{ particle_t f S{ float2_t f 0.1 0.5 } S{ float2_t f 0.1 0.5 } 5.0 }
S{ particle_t f S{ float2_t f 0.2 0.5 } S{ float2_t f 0.2 0.5 } 1.0 }
S{ particle_t f S{ float2_t f 0.3 0.3 } S{ float2_t f 0.3 0.3 } 4.0 }
S{ particle_t f S{ float2_t f 0.5 0.15 } S{ float2_t f 0.5 0.15 } 1.0 }
S{ particle_t f S{ float2_t f 0.5 0.1 } S{ float2_t f 0.5 0.1 } 9.0 }
}
: integrate-particles! ( particles dt -- particles )
[ verlet-integrate-particle ] curry map! ;
TUPLE: fluids-world < game-world
particles texture framebuffer color-texture ramp { paused boolean initial: f } ;
: make-texture ( pathname -- texture )
load-image
[
[ component-order>> ]
[ component-type>> ] bi
T{ texture-parameters
{ wrap clamp-texcoord-to-edge }
{ min-filter filter-nearest }
{ mag-filter filter-nearest }
{ min-mipmap-filter f } }
<texture-2d>
]
[
0 swap [ allocate-texture-image ] 3keep 2drop
] bi ;
SYMBOL: fluid
: integrate ( world -- )
particles>> $[ 60 fps 1000000 /f ] integrate-particles! drop ;
: pause ( -- )
fluid get [ not ] change-paused drop ;
: step ( -- )
fluid get paused>> [ fluid get integrate ] when ;
M: fluids-world begin-game-world
dup fluid set
init-gpu
initial-particles clone >>particles
"C:/Users/erikc/Pictures/particle2.pgm" make-texture >>texture
"C:/Users/erikc/Pictures/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 ;
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
;
GAME: fluids {
{ world-class fluids-world }
{ title "Fluids Test" }
{ pixel-format-attributes {
windowed double-buffered T{ depth-bits { value 24 } } } }
{ pref-dim { 640 480 } }
{ tick-interval-micros $[ 60 fps ] }
} ;
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 <struct-boa>
dup 2.0 particle_t <struct-boa> suffix
] change-particles drop ] }
} set-gestures

16388
extra/fluids/particle2.pgm Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1 @@
Erik Charlebois

View File

@ -0,0 +1,81 @@
! Copyright (C) 2010 Erik Charlebois.
! See http://factorcode.org/license.txt for BSD license.
USING: destructors fry gpu.render gpu.shaders gpu.state gpu.textures
gpu.util images kernel locals math math.rectangles sequences ;
IN: gpu.effects.blur
GLSL-SHADER: blur-fragment-shader fragment-shader
uniform sampler2D texture;
uniform bool horizontal;
uniform float blurSize;
varying vec2 texcoord;
void main()
{
vec4 col = 0.16 * texture2D(texture, texcoord);
if (horizontal)
{
const vec2 blurX1 = vec2(blurSize, 0.0);
const vec2 blurX2 = vec2(blurSize * 2.0, 0.0);
const vec2 blurX3 = vec2(blurSize * 3.0, 0.0);
const vec2 blurX4 = vec2(blurSize * 4.0, 0.0);
col += 0.15 * ( texture2D(texture, texcoord - blurX1)
+ texture2D(texture, texcoord + blurX1));
col += 0.12 * ( texture2D(texture, texcoord - blurX2)
+ texture2D(texture, texcoord + blurX2));
col += 0.09 * ( texture2D(texture, texcoord - blurX3)
+ texture2D(texture, texcoord + blurX3));
col += 0.05 * ( texture2D(texture, texcoord - blurX4)
+ texture2D(texture, texcoord + blurX4));
}
else
{
const vec2 blurY1 = vec2(0.0, blurSize);
const vec2 blurY2 = vec2(0.0, blurSize * 2.0);
const vec2 blurY3 = vec2(0.0, blurSize * 3.0);
const vec2 blurY4 = vec2(0.0, blurSize * 4.0);
col += 0.15 * ( texture2D(texture, texcoord - blurY1)
+ texture2D(texture, texcoord + blurY1));
col += 0.12 * ( texture2D(texture, texcoord - blurY2)
+ texture2D(texture, texcoord + blurY2));
col += 0.09 * ( texture2D(texture, texcoord - blurY3)
+ texture2D(texture, texcoord + blurY3));
col += 0.05 * ( texture2D(texture, texcoord - blurY4)
+ texture2D(texture, texcoord + blurY4));
}
gl_FragColor = col ;
}
;
UNIFORM-TUPLE: blur-uniforms
{ "texture" texture-uniform f }
{ "horizontal" bool-uniform f }
{ "blurSize" float-uniform f } ;
GLSL-PROGRAM: blur-program window-vertex-shader blur-fragment-shader window-vertex-format ;
:: (blur) ( texture horizontal? framebuffer dim -- )
{ 0 0 } dim <rect> <viewport-state> set-gpu-state
texture horizontal? 1.0 dim horizontal? [ first ] [ second ] if / blur-uniforms boa framebuffer {
{ "primitive-mode" [ 2drop triangle-strip-mode ] }
{ "uniforms" [ drop ] }
{ "vertex-array" [ 2drop blur-program <program-instance> <window-vertex-array> &dispose ] }
{ "indexes" [ 2drop T{ index-range f 0 4 } ] }
{ "framebuffer" [ nip ] }
} 2<render-set> render ;
:: blur ( texture horizontal? -- texture )
texture 0 texture-dim :> dim
dim L ubyte-components <2d-render-texture> :> ( target-framebuffer target-texture )
texture horizontal? target-framebuffer dim (blur)
target-framebuffer dispose
target-texture ;
: horizontal-blur ( texture -- texture ) t blur ; inline
: vertical-blur ( texture -- texture ) f blur ; inline
: discompose ( quot1 quot2 -- compose )
'[ @ &dispose @ ] with-destructors ; inline
: gaussian-blur ( texture -- texture )
[ horizontal-blur ] [ vertical-blur ] discompose ;

View File

@ -0,0 +1 @@
Blur effects for textures.

View File

@ -0,0 +1 @@
Erik Charlebois

View File

@ -0,0 +1,4 @@
! Copyright (C) 2010 Erik Charlebois.
! See http://factorcode.org/license.txt for BSD license.
USING: ;
IN: gpu.effects.quad

View File

@ -0,0 +1 @@
Render a screen-aligned quad.

View File

@ -0,0 +1 @@
Erik Charlebois

View File

@ -0,0 +1,23 @@
! Copyright (C) 2010 Erik Charlebois.
! See http://factorcode.org/license.txt for BSD license.
USING: ;
IN: gpu.effects.step
GLSL-SHADER: step-fragment-shader fragment-shader
const vec4 luminance = vec4(0.3, 0.59, 0.11, 0.0);
uniform sampler2D texture;
uniform sampler2D ramp;
varying vec2 texcoord;
void main()
{
vec4 col = texture2D(texture, texcoord);
float l = dot(col, luminance);
gl_FragColor = texture2D(ramp, vec2(l, 0.0));
}
;
UNIFORM-TUPLE: step-uniforms
{ "texture" texture-uniform f }
{ "ramp" texture-uniform f } ;
GLSL-PROGRAM: step-program window-vertex-shader step-fragment-shader window-vertex-format ;

View File

@ -0,0 +1 @@
Render a quad with a step texture.

View File

@ -1,6 +1,6 @@
! (c)2009 Joe Groff bsd license
USING: gpu.buffers gpu.render gpu.shaders gpu.textures images kernel
specialized-arrays ;
USING: arrays gpu.buffers gpu.framebuffers gpu.render gpu.shaders
gpu.textures images kernel locals specialized-arrays ;
FROM: alien.c-types => float ;
SPECIALIZED-ARRAY: float
IN: gpu.util
@ -44,10 +44,58 @@ CONSTANT: environment-cube-map-mv-matrices
{ 0.0 0.0 0.0 1.0 }
} }
}
GLSL-SHADER: window-vertex-shader vertex-shader
attribute vec2 vertex;
varying vec2 texcoord;
void main()
{
texcoord = vertex * vec2(0.5) + vec2(0.5);
gl_Position = vec4(vertex, 0.0, 1.0);
}
;
VERTEX-FORMAT: window-vertex
GLSL-SHADER: window-fragment-shader fragment-shader
uniform sampler2D texture;
varying vec2 texcoord;
void main()
{
gl_FragColor = texture2D(texture, texcoord);
}
;
VERTEX-FORMAT: window-vertex-format
{ "vertex" float-components 2 f } ;
UNIFORM-TUPLE: window-uniforms
{ "texture" texture-uniform f } ;
GLSL-PROGRAM: window-program window-vertex-shader window-fragment-shader window-vertex-format ;
GLSL-SHADER: window-point-vertex-shader vertex-shader
uniform float point_size;
attribute vec2 vertex;
void main()
{
gl_Position = vec4(vertex, 0.0, 1.0);
gl_PointSize = point_size;
}
;
GLSL-SHADER: window-point-fragment-shader fragment-shader
uniform sampler2D texture;
void main()
{
gl_FragColor = texture2D(texture, gl_PointCoord);
}
;
UNIFORM-TUPLE: window-point-uniforms
{ "texture" texture-uniform f }
{ "point_size" float-uniform f } ;
GLSL-PROGRAM: window-point-program window-point-vertex-shader window-point-fragment-shader window-vertex-format ;
CONSTANT: window-vertexes
float-array{
-1.0 -1.0
@ -62,4 +110,17 @@ CONSTANT: window-vertexes
byte-array>buffer ; inline
: <window-vertex-array> ( program-instance -- vertex-array )
[ <window-vertex-buffer> ] dip window-vertex <vertex-array*> ; inline
[ <window-vertex-buffer> ] dip window-vertex-format <vertex-array*> ; inline
:: <2d-render-texture> ( dim order type -- renderbuffer texture )
order type T{ texture-parameters { wrap clamp-texcoord-to-edge }
{ min-filter filter-linear } { min-mipmap-filter f } } <texture-2d>
[ 0 <texture-2d-attachment> 1array f f dim <framebuffer> ] keep ;
: draw-texture ( texture -- )
{
{ "primitive-mode" [ drop triangle-strip-mode ] }
{ "uniforms" [ window-uniforms boa ] }
{ "vertex-array" [ drop <window-vertex-buffer> window-program <program-instance> <vertex-array> ] }
{ "indexes" [ drop T{ index-range f 0 4 } ] }
} <render-set> render ;