1 ! (c)2009 Joe Groff bsd license
2 USING: arrays destructors gpu.buffers gpu.framebuffers gpu.render
3 gpu.shaders gpu.state gpu.textures images kernel locals math
4 math.rectangles opengl.gl sequences specialized-arrays ;
5 FROM: alien.c-types => float ;
6 SPECIALIZED-ARRAY: float
9 CONSTANT: environment-cube-map-mv-matrices
49 GLSL-SHADER: window-vertex-shader vertex-shader
50 attribute vec2 vertex;
51 varying vec2 texcoord;
54 texcoord = vertex * vec2(0.5) + vec2(0.5);
55 gl_Position = vec4(vertex, 0.0, 1.0);
59 GLSL-SHADER: window-fragment-shader fragment-shader
60 uniform sampler2D texture;
61 varying vec2 texcoord;
64 gl_FragColor = texture2D(texture, texcoord);
68 VERTEX-FORMAT: window-vertex-format
69 { "vertex" float-components 2 f } ;
71 UNIFORM-TUPLE: window-uniforms
72 { "texture" texture-uniform f } ;
74 GLSL-PROGRAM: window-program window-vertex-shader window-fragment-shader window-vertex-format ;
76 GLSL-SHADER: window-point-vertex-shader vertex-shader
77 uniform float point_size;
78 attribute vec2 vertex;
81 gl_Position = vec4(vertex, 0.0, 1.0);
82 gl_PointSize = point_size;
86 GLSL-SHADER: window-point-fragment-shader fragment-shader
88 uniform sampler2D texture;
91 gl_FragColor = texture2D(texture, gl_PointCoord);
95 UNIFORM-TUPLE: window-point-uniforms
96 { "texture" texture-uniform f }
97 { "point_size" float-uniform f } ;
99 GLSL-PROGRAM: window-point-program window-point-vertex-shader window-point-fragment-shader window-vertex-format ;
101 CONSTANT: window-vertexes
109 : <window-vertex-buffer> ( -- buffer )
111 static-upload draw-usage vertex-buffer
112 byte-array>buffer ; inline
114 : <window-vertex-array> ( program-instance -- vertex-array )
115 [ <window-vertex-buffer> ] dip window-vertex-format <vertex-array*> ; inline
117 :: <2d-render-texture> ( dim order type -- renderbuffer texture )
119 T{ texture-parameters
120 { wrap clamp-texcoord-to-edge }
121 { min-filter filter-linear }
122 { min-mipmap-filter f } }
124 0 <texture-2d-attachment> 1array f f dim <framebuffer>
125 dup { { default-attachment { 0 0 0 } } } clear-framebuffer
128 : draw-texture ( texture dim -- )
129 { 0 0 } swap <rect> <viewport-state> set-gpu-state
131 { "primitive-mode" [ drop triangle-strip-mode ] }
132 { "uniforms" [ window-uniforms boa ] }
133 { "vertex-array" [ drop window-program <program-instance> <window-vertex-array> &dispose ] }
134 { "indexes" [ drop T{ index-range f 0 4 } ] }
135 } <render-set> render ;
137 :: <streamed-vertex-array> ( verts program-instance -- vertex-array )
138 verts stream-upload draw-usage vertex-buffer byte-array>buffer &dispose
139 program-instance <vertex-array> &dispose ;
141 : (blended-point-sprite-batch) ( verts framebuffer texture point-size dim -- )
142 f eq-add func-one func-one <blend-mode> dup <blend-state> set-gpu-state
143 f origin-upper-left 1.0 <point-state> set-gpu-state
144 GL_POINT_SPRITE glEnable
145 { 0 0 } swap <rect> <viewport-state> set-gpu-state
146 window-point-uniforms boa {
147 { "primitive-mode" [ 3drop points-mode ] }
148 { "uniforms" [ 2nip ] }
149 { "vertex-array" [ 2drop window-point-program <program-instance> <streamed-vertex-array> ] }
150 { "indexes" [ 2drop length 2 / 0 swap <index-range> ] }
151 { "framebuffer" [ drop nip ] }
152 } 3<render-set> render ;
154 :: blended-point-sprite-batch ( verts texture point-size dim -- texture )
155 dim RGB float-components <2d-render-texture> :> ( target-framebuffer target-texture )
156 verts target-framebuffer texture point-size dim (blended-point-sprite-batch)
157 target-framebuffer dispose