! (c)2009 Joe Groff bsd license 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 CONSTANT: environment-cube-map-mv-matrices H{ { +X { { 0.0 0.0 -1.0 0.0 } { 0.0 -1.0 0.0 0.0 } { -1.0 0.0 0.0 0.0 } { 0.0 0.0 0.0 1.0 } } } { +Y { { 1.0 0.0 0.0 0.0 } { 0.0 0.0 1.0 0.0 } { 0.0 -1.0 0.0 0.0 } { 0.0 0.0 0.0 1.0 } } } { +Z { { 1.0 0.0 0.0 0.0 } { 0.0 -1.0 0.0 0.0 } { 0.0 0.0 -1.0 0.0 } { 0.0 0.0 0.0 1.0 } } } { -X { { 0.0 0.0 1.0 0.0 } { 0.0 -1.0 0.0 0.0 } { 1.0 0.0 0.0 0.0 } { 0.0 0.0 0.0 1.0 } } } { -Y { { 1.0 0.0 0.0 0.0 } { 0.0 0.0 -1.0 0.0 } { 0.0 1.0 0.0 0.0 } { 0.0 0.0 0.0 1.0 } } } { -Z { { -1.0 0.0 0.0 0.0 } { 0.0 -1.0 0.0 0.0 } { 0.0 0.0 1.0 0.0 } { 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); } ; 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 #version 120 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 -1.0 1.0 1.0 -1.0 1.0 1.0 } : ( -- buffer ) window-vertexes static-upload draw-usage vertex-buffer byte-array>buffer ; inline : ( program-instance -- vertex-array ) [ ] dip window-vertex-format ; 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 } } [ 0 1array f f dim dup { { default-attachment { 0 0 0 } } } clear-framebuffer ] keep ; : 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 &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 ;