1 ! Copyright (C) 2010 Erik Charlebois
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types arrays circular colors colors.constants
4 columns destructors fonts gpu.buffers gpu.render gpu.shaders gpu.state
5 gpu.textures images kernel literals locals make math math.constants
6 math.functions math.vectors sequences specialized-arrays typed ui.text fry ;
7 FROM: alien.c-types => float ;
8 SPECIALIZED-ARRAYS: float uint ;
12 ! Vertex shader for debug shapes
13 GLSL-SHADER: debug-shapes-vertex-shader vertex-shader
14 uniform mat4 u_mvp_matrix;
15 attribute vec3 a_position;
16 attribute vec3 a_color;
20 gl_Position = u_mvp_matrix * vec4(a_position, 1.0);
26 GLSL-SHADER: debug-shapes-fragment-shader fragment-shader
30 gl_FragColor = vec4(v_color, 1.0);
34 VERTEX-FORMAT: debug-shapes-vertex-format
35 { "a_position" float-components 3 f }
36 { "a_color" float-components 3 f } ;
38 UNIFORM-TUPLE: debug-shapes-uniforms
39 { "u_mvp_matrix" mat4-uniform f } ;
41 GLSL-PROGRAM: debug-shapes-program debug-shapes-vertex-shader
42 debug-shapes-fragment-shader debug-shapes-vertex-format ;
44 ! Vertex shader for debug text
45 GLSL-SHADER: debug-text-vertex-shader vertex-shader
46 attribute vec2 a_position;
47 attribute vec2 a_texcoord;
48 varying vec2 v_texcoord;
51 gl_Position = vec4(a_position, 0.0, 1.0);
52 v_texcoord = a_texcoord;
56 GLSL-SHADER: debug-text-fragment-shader fragment-shader
57 uniform sampler2D u_text_map;
58 uniform vec3 u_background_color;
59 varying vec2 v_texcoord;
62 vec4 c = texture2D(u_text_map, v_texcoord);
63 if (c.xyz == u_background_color)
70 VERTEX-FORMAT: debug-text-vertex-format
71 { "a_position" float-components 2 f }
72 { "a_texcoord" float-components 2 f } ;
74 UNIFORM-TUPLE: debug-text-uniforms
75 { "u_text_map" texture-uniform f }
76 { "u_background_color" vec3-uniform f } ;
78 GLSL-PROGRAM: debug-text-program debug-text-vertex-shader
79 debug-text-fragment-shader debug-text-vertex-format ;
81 CONSTANT: debug-text-font
87 { foreground COLOR: white }
88 { background COLOR: black } }
90 CONSTANT: debug-text-texture-parameters
92 { wrap repeat-texcoord }
93 { min-filter filter-linear }
94 { min-mipmap-filter f } }
96 : text>image ( string color -- image )
97 debug-text-font clone swap >>foreground swap string>image drop ;
99 :: image>texture ( image -- texture )
100 image [ component-order>> ] [ component-type>> ] bi
101 debug-text-texture-parameters <texture-2d> &dispose
102 [ 0 image allocate-texture-image ] keep ;
104 :: screen-quad ( image pt dim -- float-array )
105 pt dim v/ 2.0 v*n 1.0 v-n
106 dup image dim>> dim v/ 2.0 v*n v+
107 [ first2 ] bi@ :> ( x0 y0 x1 y1 )
109 [ { x0 y0 0 0 x1 y0 1 0 x1 y1 1 1 x0 y1 0 1 } ]
110 [ { x0 y0 0 1 x1 y0 1 1 x1 y1 1 0 x0 y1 0 0 } ]
113 : debug-text-uniform-variables ( string color -- image uniforms )
114 text>image dup image>texture
115 float-array{ 0.0 0.0 0.0 }
116 debug-text-uniforms boa swap ;
118 : debug-text-vertex-array ( image pt dim -- vertex-array )
119 screen-quad stream-upload draw-usage vertex-buffer byte-array>buffer &dispose
120 debug-text-program <program-instance> <vertex-array> &dispose ;
122 : debug-text-index-buffer ( -- index-buffer )
123 uint-array{ 0 1 2 2 3 0 } stream-upload draw-usage index-buffer
124 byte-array>buffer &dispose 0 <buffer-ptr> 6 uint-indexes <index-elements> ;
126 : debug-text-render ( uniforms vertex-array index-buffer -- )
129 { "primitive-mode" [ 3drop triangles-mode ] }
130 { "uniforms" [ 2drop ] }
131 { "vertex-array" [ drop nip ] }
132 { "indexes" [ 2nip ] }
133 } 3<render-set> render
136 : debug-shapes-vertex-array ( sequence -- vertex-array )
137 stream-upload draw-usage vertex-buffer byte-array>buffer &dispose
138 debug-shapes-program <program-instance> &dispose <vertex-array> &dispose ;
140 : draw-debug-primitives ( mode primitives mvp-matrix -- )
141 f origin-upper-left 1.0 <point-state> set-gpu-state
143 { "primitive-mode" [ 2drop ] }
144 { "uniforms" [ 2nip debug-shapes-uniforms boa ] }
145 { "vertex-array" [ drop nip debug-shapes-vertex-array ] }
146 { "indexes" [ drop nip length 0 swap <index-range> ] }
147 } 3<render-set> render ;
149 CONSTANT: box-vertices
150 { { { 1 1 1 } { 1 1 -1 } }
151 { { 1 1 1 } { 1 -1 1 } }
152 { { 1 1 1 } { -1 1 1 } }
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 } } }
163 CONSTANT: cylinder-vertices
164 $[ 12 iota [ 2pi 12 / * [ cos ] [ drop 0.0 ] [ sin ] tri 3array ] map ]
166 :: scale-cylinder-vertices ( radius half-height verts -- bot-verts top-verts )
168 [ [ radius v*n { 0 half-height 0 } v- ] map ]
169 [ [ radius v*n { 0 half-height 0 } v+ ] map ] bi ;
172 : debug-point ( pt color -- )
173 [ first3 [ , ] tri@ ]
174 [ [ red>> , ] [ green>> , ] [ blue>> , ] tri ]
177 : debug-line ( from to color -- )
178 dup swapd [ debug-point ] 2bi@ ; inline
180 : debug-axes ( pt mat -- )
181 [ 0 <column> normalize over v+ COLOR: red debug-line ]
182 [ 1 <column> normalize over v+ COLOR: green debug-line ]
183 [ 2 <column> normalize over v+ COLOR: blue debug-line ]
186 :: debug-box ( pt half-widths color -- )
188 first2 [ half-widths v* pt v+ ] bi@ color debug-line
191 :: debug-circle ( points color -- )
192 points dup <circular> [ 1 swap change-circular-start ] keep
193 [ color debug-line ] 2each ; inline
195 :: debug-cylinder ( pt half-height radius color -- )
196 radius half-height cylinder-vertices scale-cylinder-vertices
197 [ [ color debug-circle ] bi@ ]
198 [ color '[ _ debug-line ] 2each ] 2bi ; inline
200 TYPED: draw-debug-lines ( lines: float-array mvp-matrix -- )
201 [ lines-mode -rot draw-debug-primitives ] with-destructors ; inline
203 TYPED: draw-debug-points ( points: float-array mvp-matrix -- )
204 [ points-mode -rot draw-debug-primitives ] with-destructors ; inline
206 TYPED: draw-text ( string color: rgba pt dim -- )
208 [ debug-text-uniform-variables ] 2dip
209 debug-text-vertex-array
210 debug-text-index-buffer
212 ] with-destructors ; inline