1 ! Copyright (C) 2009 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays destructors gpu.buffers gpu.framebuffers gpu.render
4 gpu.shaders gpu.state gpu.textures images kernel locals math
5 math.rectangles opengl.gl sequences specialized-arrays ;
6 FROM: alien.c-types => float ;
7 SPECIALIZED-ARRAY: float
10 CONSTANT: environment-cube-map-mv-matrices
50 GLSL-SHADER: window-vertex-shader vertex-shader
51 attribute vec2 vertex;
52 varying vec2 texcoord;
55 texcoord = vertex * vec2(0.5) + vec2(0.5);
56 gl_Position = vec4(vertex, 0.0, 1.0);
60 GLSL-SHADER: window-fragment-shader fragment-shader
61 uniform sampler2D texture;
62 varying vec2 texcoord;
65 gl_FragColor = texture2D(texture, texcoord);
69 VERTEX-FORMAT: window-vertex-format
70 { "vertex" float-components 2 f } ;
72 UNIFORM-TUPLE: window-uniforms
73 { "texture" texture-uniform f } ;
75 GLSL-PROGRAM: window-program window-vertex-shader window-fragment-shader window-vertex-format ;
77 GLSL-SHADER: window-point-vertex-shader vertex-shader
78 uniform float point_size;
79 attribute vec2 vertex;
82 gl_Position = vec4(vertex, 0.0, 1.0);
83 gl_PointSize = point_size;
87 GLSL-SHADER: window-point-fragment-shader fragment-shader
89 uniform sampler2D texture;
92 gl_FragColor = texture2D(texture, gl_PointCoord);
96 UNIFORM-TUPLE: window-point-uniforms
97 { "texture" texture-uniform f }
98 { "point_size" float-uniform f } ;
100 GLSL-PROGRAM: window-point-program window-point-vertex-shader window-point-fragment-shader window-vertex-format ;
102 CONSTANT: window-vertexes
110 : <window-vertex-buffer> ( -- buffer )
112 static-upload draw-usage vertex-buffer
113 byte-array>buffer ; inline
115 : <window-vertex-array> ( program-instance -- vertex-array )
116 [ <window-vertex-buffer> ] dip window-vertex-format <vertex-array*> ; inline
118 :: <2d-render-texture> ( dim order type -- renderbuffer texture )
120 T{ texture-parameters
121 { wrap clamp-texcoord-to-edge }
122 { min-filter filter-linear }
123 { min-mipmap-filter f } }
125 0 <texture-2d-attachment> 1array f f dim <framebuffer>
126 dup { { default-attachment { 0 0 0 } } } clear-framebuffer
129 : draw-texture ( texture dim -- )
130 { 0 0 } swap <rect> <viewport-state> set-gpu-state
132 { "primitive-mode" [ drop triangle-strip-mode ] }
133 { "uniforms" [ window-uniforms boa ] }
134 { "vertex-array" [ drop window-program <program-instance> <window-vertex-array> &dispose ] }
135 { "indexes" [ drop T{ index-range f 0 4 } ] }
136 } <render-set> render ;
138 :: <streamed-vertex-array> ( verts program-instance -- vertex-array )
139 verts stream-upload draw-usage vertex-buffer byte-array>buffer &dispose
140 program-instance <vertex-array> &dispose ;
142 : (blended-point-sprite-batch) ( verts framebuffer texture point-size dim -- )
143 f eq-add func-one func-one <blend-mode> dup <blend-state> set-gpu-state
144 f origin-upper-left 1.0 <point-state> set-gpu-state
145 GL_POINT_SPRITE glEnable
146 { 0 0 } swap <rect> <viewport-state> set-gpu-state
147 window-point-uniforms boa {
148 { "primitive-mode" [ 3drop points-mode ] }
149 { "uniforms" [ 2nip ] }
150 { "vertex-array" [ 2drop window-point-program <program-instance> <streamed-vertex-array> ] }
151 { "indexes" [ 2drop length 2 / 0 swap <index-range> ] }
152 { "framebuffer" [ drop nip ] }
153 } 3<render-set> render ;
155 :: blended-point-sprite-batch ( verts texture point-size dim -- texture )
156 dim RGB float-components <2d-render-texture> :> ( target-framebuffer target-texture )
157 verts target-framebuffer texture point-size dim (blended-point-sprite-batch)
158 target-framebuffer dispose