1 ! Copyright (C) 2010 Erik Charlebois
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors alien.c-types alien.data arrays circular colors
5 columns destructors fonts gpu.buffers gpu.render gpu.shaders
6 gpu.state gpu.textures images kernel literals locals make math
7 math.constants math.functions math.vectors sequences
8 specialized-arrays typed ui.text ;
10 FROM: alien.c-types => float ;
11 SPECIALIZED-ARRAYS: float uint ;
15 ! Vertex shader for debug shapes
16 GLSL-SHADER: debug-shapes-vertex-shader vertex-shader
17 uniform mat4 u_mvp_matrix;
18 attribute vec3 a_position;
19 attribute vec3 a_color;
23 gl_Position = u_mvp_matrix * vec4(a_position, 1.0);
29 GLSL-SHADER: debug-shapes-fragment-shader fragment-shader
33 gl_FragColor = vec4(v_color, 1.0);
37 VERTEX-FORMAT: debug-shapes-vertex-format
38 { "a_position" float-components 3 f }
39 { "a_color" float-components 3 f } ;
41 UNIFORM-TUPLE: debug-shapes-uniforms
42 { "u_mvp_matrix" mat4-uniform f } ;
44 GLSL-PROGRAM: debug-shapes-program debug-shapes-vertex-shader
45 debug-shapes-fragment-shader debug-shapes-vertex-format ;
47 ! Vertex shader for debug text
48 GLSL-SHADER: debug-text-vertex-shader vertex-shader
49 attribute vec2 a_position;
50 attribute vec2 a_texcoord;
51 varying vec2 v_texcoord;
54 gl_Position = vec4(a_position, 0.0, 1.0);
55 v_texcoord = a_texcoord;
59 GLSL-SHADER: debug-text-fragment-shader fragment-shader
60 uniform sampler2D u_text_map;
61 uniform vec3 u_background_color;
62 varying vec2 v_texcoord;
65 vec4 c = texture2D(u_text_map, v_texcoord);
66 if (c.xyz == u_background_color)
73 VERTEX-FORMAT: debug-text-vertex-format
74 { "a_position" float-components 2 f }
75 { "a_texcoord" float-components 2 f } ;
77 UNIFORM-TUPLE: debug-text-uniforms
78 { "u_text_map" texture-uniform f }
79 { "u_background_color" vec3-uniform f } ;
81 GLSL-PROGRAM: debug-text-program debug-text-vertex-shader
82 debug-text-fragment-shader debug-text-vertex-format ;
84 CONSTANT: debug-text-font
90 { foreground COLOR: white }
91 { background COLOR: black } }
93 CONSTANT: debug-text-texture-parameters
95 { wrap repeat-texcoord }
96 { min-filter filter-linear }
97 { min-mipmap-filter f } }
99 : text>image ( string color -- image )
100 debug-text-font clone swap >>foreground swap string>image drop ;
102 :: image>texture ( image -- texture )
103 image [ component-order>> ] [ component-type>> ] bi
104 debug-text-texture-parameters <texture-2d> &dispose
105 [ 0 image allocate-texture-image ] keep ;
107 :: screen-quad ( image pt dim -- float-array )
108 pt dim v/ 2.0 v*n 1.0 v-n
109 dup image dim>> dim v/ 2.0 v*n v+
110 [ first2 ] bi@ :> ( x0 y0 x1 y1 )
112 [ { x0 y0 0 0 x1 y0 1 0 x1 y1 1 1 x0 y1 0 1 } ]
113 [ { x0 y0 0 1 x1 y0 1 1 x1 y1 1 0 x0 y1 0 0 } ]
116 : debug-text-uniform-variables ( string color -- image uniforms )
117 text>image dup image>texture
118 float-array{ 0.0 0.0 0.0 }
119 debug-text-uniforms boa swap ;
121 : debug-text-vertex-array ( image pt dim -- vertex-array )
122 screen-quad stream-upload draw-usage vertex-buffer byte-array>buffer &dispose
123 debug-text-program <program-instance> <vertex-array> &dispose ;
125 : debug-text-index-buffer ( -- index-buffer )
126 uint-array{ 0 1 2 2 3 0 } stream-upload draw-usage index-buffer
127 byte-array>buffer &dispose 0 <buffer-ptr> 6 uint-indexes <index-elements> ;
129 : debug-text-render ( uniforms vertex-array index-buffer -- )
132 { "primitive-mode" [ 3drop triangles-mode ] }
133 { "uniforms" [ 2drop ] }
134 { "vertex-array" [ drop nip ] }
135 { "indexes" [ 2nip ] }
136 } 3<render-set> render
139 : debug-shapes-vertex-array ( sequence -- vertex-array )
140 stream-upload draw-usage vertex-buffer byte-array>buffer &dispose
141 debug-shapes-program <program-instance> &dispose <vertex-array> &dispose ;
143 : draw-debug-primitives ( mode primitives mvp-matrix -- )
144 f origin-upper-left 1.0 <point-state> set-gpu-state
146 { "primitive-mode" [ 2drop ] }
147 { "uniforms" [ 2nip debug-shapes-uniforms boa ] }
148 { "vertex-array" [ drop nip debug-shapes-vertex-array ] }
149 { "indexes" [ drop nip length 0 swap <index-range> ] }
150 } 3<render-set> render ;
152 CONSTANT: box-vertices
153 { { { 1 1 1 } { 1 1 -1 } }
154 { { 1 1 1 } { 1 -1 1 } }
155 { { 1 1 1 } { -1 1 1 } }
156 { { -1 -1 -1 } { -1 -1 1 } }
157 { { -1 -1 -1 } { -1 1 -1 } }
158 { { -1 -1 -1 } { 1 -1 -1 } }
159 { { -1 -1 1 } { -1 1 1 } }
160 { { -1 -1 1 } { 1 -1 1 } }
161 { { -1 1 -1 } { -1 1 1 } }
162 { { -1 1 -1 } { 1 1 -1 } }
163 { { 1 -1 -1 } { 1 -1 1 } }
164 { { 1 -1 -1 } { 1 1 -1 } } }
166 CONSTANT: cylinder-vertices
167 $[ 12 <iota> [ 2pi 12 / * [ cos ] [ drop 0.0 ] [ sin ] tri 3array ] map ]
169 :: scale-cylinder-vertices ( radius half-height verts -- bot-verts top-verts )
171 [ [ radius v*n { 0 half-height 0 } v- ] map ]
172 [ [ radius v*n { 0 half-height 0 } v+ ] map ] bi ;
175 : debug-point ( pt color -- )
176 [ first3 [ , ] tri@ ]
177 [ [ red>> , ] [ green>> , ] [ blue>> , ] tri ]
180 : debug-line ( from to color -- )
181 dup swapd [ debug-point ] 2bi@ ; inline
183 : debug-axes ( pt mat -- )
184 [ 0 <column> normalize over v+ COLOR: red debug-line ]
185 [ 1 <column> normalize over v+ COLOR: green debug-line ]
186 [ 2 <column> normalize over v+ COLOR: blue debug-line ]
189 :: debug-box ( pt half-widths color -- )
191 first2 [ half-widths v* pt v+ ] bi@ color debug-line
194 :: debug-circle ( points color -- )
195 points dup <circular> [ 1 swap change-circular-start ] keep
196 [ color debug-line ] 2each ; inline
198 :: debug-cylinder ( pt half-height radius color -- )
199 radius half-height cylinder-vertices scale-cylinder-vertices
200 [ [ color debug-circle ] bi@ ]
201 [ color '[ _ debug-line ] 2each ] 2bi ; inline
203 TYPED: draw-debug-lines ( lines: float-array mvp-matrix -- )
204 [ lines-mode -rot draw-debug-primitives ] with-destructors ; inline
206 TYPED: draw-debug-points ( points: float-array mvp-matrix -- )
207 [ points-mode -rot draw-debug-primitives ] with-destructors ; inline
209 TYPED: draw-text ( string color: rgba pt dim -- )
211 [ debug-text-uniform-variables ] 2dip
212 debug-text-vertex-array
213 debug-text-index-buffer
215 ] with-destructors ; inline