]> gitweb.factorcode.org Git - factor.git/blob - extra/game/debug/debug.factor
colors: merge colors.constants and colors.hex.
[factor.git] / extra / game / debug / debug.factor
1 ! Copyright (C) 2010 Erik Charlebois
2 ! See http://factorcode.org/license.txt for BSD license.
3
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 ;
9
10 FROM: alien.c-types => float ;
11 SPECIALIZED-ARRAYS: float uint ;
12 IN: game.debug
13
14 <PRIVATE
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;
20 varying   vec3 v_color;
21 void main()
22 {
23     gl_Position = u_mvp_matrix * vec4(a_position, 1.0);
24     gl_PointSize = 5.0;
25     v_color = a_color;
26 }
27 ;
28
29 GLSL-SHADER: debug-shapes-fragment-shader fragment-shader
30 varying vec3 v_color;
31 void main()
32 {
33     gl_FragColor = vec4(v_color, 1.0);
34 }
35 ;
36
37 VERTEX-FORMAT: debug-shapes-vertex-format
38     { "a_position" float-components 3 f }
39     { "a_color"    float-components 3 f } ;
40
41 UNIFORM-TUPLE: debug-shapes-uniforms
42     { "u_mvp_matrix" mat4-uniform f } ;
43
44 GLSL-PROGRAM: debug-shapes-program debug-shapes-vertex-shader
45 debug-shapes-fragment-shader debug-shapes-vertex-format ;
46
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;
52 void main()
53 {
54     gl_Position = vec4(a_position, 0.0, 1.0);
55     v_texcoord  = a_texcoord;
56 }
57 ;
58
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;
63 void main()
64 {
65     vec4 c = texture2D(u_text_map, v_texcoord);
66     if (c.xyz == u_background_color)
67         discard;
68     else
69         gl_FragColor = c;
70 }
71 ;
72
73 VERTEX-FORMAT: debug-text-vertex-format
74     { "a_position" float-components 2 f }
75     { "a_texcoord" float-components 2 f } ;
76
77 UNIFORM-TUPLE: debug-text-uniforms
78     { "u_text_map"         texture-uniform f }
79     { "u_background_color" vec3-uniform    f } ;
80
81 GLSL-PROGRAM: debug-text-program debug-text-vertex-shader
82 debug-text-fragment-shader debug-text-vertex-format ;
83
84 CONSTANT: debug-text-font
85     T{ font
86        { name       "monospace"  }
87        { size       16           }
88        { bold?      f            }
89        { italic?    f            }
90        { foreground COLOR: white }
91        { background COLOR: black } }
92
93 CONSTANT: debug-text-texture-parameters
94     T{ texture-parameters
95        { wrap              repeat-texcoord }
96        { min-filter        filter-linear   }
97        { min-mipmap-filter f               } }
98
99 : text>image ( string color -- image )
100     debug-text-font clone swap >>foreground swap string>image drop ;
101
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 ;
106
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 )
111     image upside-down?>>
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 } ]
114     if float >c-array ;
115
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 ;
120
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 ;
124
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> ;
128
129 : debug-text-render ( uniforms vertex-array index-buffer -- )
130     [
131         {
132             { "primitive-mode" [ 3drop triangles-mode ] }
133             { "uniforms"       [ 2drop ] }
134             { "vertex-array"   [ drop nip ] }
135             { "indexes"        [ 2nip ] }
136         } 3<render-set> render
137     ] with-destructors ;
138
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 ;
142
143 : draw-debug-primitives ( mode primitives mvp-matrix -- )
144     f origin-upper-left 1.0 <point-state> set-gpu-state
145     {
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 ;
151
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 } } }
165
166 CONSTANT: cylinder-vertices
167     $[ 12 <iota> [ 2pi 12 / * [ cos ] [ drop 0.0 ] [ sin ] tri 3array ] map ]
168
169 :: scale-cylinder-vertices ( radius half-height verts -- bot-verts top-verts )
170     verts
171     [ [ radius v*n { 0 half-height 0 } v- ] map ]
172     [ [ radius v*n { 0 half-height 0 } v+ ] map ] bi ;
173 PRIVATE>
174
175 : debug-point ( pt color -- )
176     [ first3 [ , ] tri@ ]
177     [ [ red>> , ] [ green>> , ] [ blue>> , ] tri ]
178     bi* ; inline
179
180 : debug-line ( from to color -- )
181     dup swapd [ debug-point ] 2bi@ ; inline
182
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 ]
187     2tri ; inline
188
189 :: debug-box ( pt half-widths color -- )
190     box-vertices [
191         first2 [ half-widths v* pt v+ ] bi@ color debug-line
192     ] each ; inline
193
194 :: debug-circle ( points color -- )
195     points dup <circular> [ 1 swap change-circular-start ] keep
196     [ color debug-line ] 2each ; inline
197
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
202
203 TYPED: draw-debug-lines ( lines: float-array mvp-matrix -- )
204     [ lines-mode -rot draw-debug-primitives ] with-destructors ; inline
205
206 TYPED: draw-debug-points ( points: float-array mvp-matrix -- )
207     [ points-mode -rot draw-debug-primitives ] with-destructors ; inline
208
209 TYPED: draw-text ( string color: rgba pt dim -- )
210     [
211         [ debug-text-uniform-variables ] 2dip
212         debug-text-vertex-array
213         debug-text-index-buffer
214         debug-text-render
215     ] with-destructors ; inline