]> gitweb.factorcode.org Git - factor.git/blob - extra/game/debug/debug.factor
a4f489581269f24f830e008f5172b066f0969d2e
[factor.git] / extra / game / debug / debug.factor
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 ;
9 IN: game.debug
10
11 <PRIVATE
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;
17 varying   vec3 v_color;
18 void main()
19 {
20     gl_Position = u_mvp_matrix * vec4(a_position, 1.0);
21     gl_PointSize = 5.0;
22     v_color = a_color;
23 }
24 ;
25
26 GLSL-SHADER: debug-shapes-fragment-shader fragment-shader
27 varying vec3 v_color;
28 void main()
29 {
30     gl_FragColor = vec4(v_color, 1.0);
31 }
32 ;
33
34 VERTEX-FORMAT: debug-shapes-vertex-format
35     { "a_position" float-components 3 f }
36     { "a_color"    float-components 3 f } ;
37
38 UNIFORM-TUPLE: debug-shapes-uniforms
39     { "u_mvp_matrix" mat4-uniform f } ;
40
41 GLSL-PROGRAM: debug-shapes-program debug-shapes-vertex-shader
42 debug-shapes-fragment-shader debug-shapes-vertex-format ;
43
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;
49 void main()
50 {
51     gl_Position = vec4(a_position, 0.0, 1.0);
52     v_texcoord  = a_texcoord;
53 }
54 ;
55
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;
60 void main()
61 {
62     vec4 c = texture2D(u_text_map, v_texcoord);
63     if (c.xyz == u_background_color)
64         discard;
65     else
66         gl_FragColor = c;
67 }
68 ;
69
70 VERTEX-FORMAT: debug-text-vertex-format
71     { "a_position" float-components 2 f }
72     { "a_texcoord" float-components 2 f } ;
73
74 UNIFORM-TUPLE: debug-text-uniforms
75     { "u_text_map"         texture-uniform f }
76     { "u_background_color" vec3-uniform    f } ;
77
78 GLSL-PROGRAM: debug-text-program debug-text-vertex-shader
79 debug-text-fragment-shader debug-text-vertex-format ;
80
81 CONSTANT: debug-text-font
82     T{ font 
83        { name       "monospace"  }
84        { size       16           }
85        { bold?      f            }
86        { italic?    f            }
87        { foreground COLOR: white }
88        { background COLOR: black } }
89        
90 CONSTANT: debug-text-texture-parameters       
91     T{ texture-parameters
92        { wrap              repeat-texcoord }
93        { min-filter        filter-linear   }
94        { min-mipmap-filter f               } }
95        
96 : text>image ( string color -- image )      
97     debug-text-font clone swap >>foreground swap string>image drop ;
98
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 ;
103
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 )
108     image upside-down?>>
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 } ]
111     if >float-array ;
112
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 ;
117
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 ;
121  
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> ;
125
126 : debug-text-render ( uniforms vertex-array index-buffer -- )
127     [
128         {
129             { "primitive-mode" [ 3drop triangles-mode ] }
130             { "uniforms"       [ 2drop ] }
131             { "vertex-array"   [ drop nip ] }
132             { "indexes"        [ 2nip ] }
133         } 3<render-set> render
134     ] with-destructors ;
135
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 ;
139
140 : draw-debug-primitives ( mode primitives mvp-matrix -- )
141     f origin-upper-left 1.0 <point-state> set-gpu-state
142     {
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 ;
148
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 } } }
162       
163 CONSTANT: cylinder-vertices
164     $[ 12 iota [ 2pi 12 / * [ cos ] [ drop 0.0 ] [ sin ] tri 3array ] map ]
165     
166 :: scale-cylinder-vertices ( radius half-height verts -- bot-verts top-verts )
167     verts
168     [ [ radius v*n { 0 half-height 0 } v- ] map ]
169     [ [ radius v*n { 0 half-height 0 } v+ ] map ] bi ;
170 PRIVATE>
171
172 : debug-point ( pt color -- )
173     [ first3 [ , ] tri@ ]
174     [ [ red>> , ] [ green>> , ] [ blue>> , ] tri ]
175     bi* ; inline
176
177 : debug-line ( from to color -- )
178     dup swapd [ debug-point ] 2bi@ ; inline
179
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 ]
184     2tri ; inline
185         
186 :: debug-box ( pt half-widths color -- )
187     box-vertices [
188         first2 [ half-widths v* pt v+ ] bi@ color debug-line
189     ] each ; inline
190
191 :: debug-circle ( points color -- )
192     points dup <circular> [ 1 swap change-circular-start ] keep
193     [ color debug-line ] 2each ; inline
194
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
199
200 TYPED: draw-debug-lines ( lines: float-array mvp-matrix -- )
201     [ lines-mode -rot draw-debug-primitives ] with-destructors ; inline
202
203 TYPED: draw-debug-points ( points: float-array mvp-matrix -- )
204     [ points-mode -rot draw-debug-primitives ] with-destructors ; inline
205         
206 TYPED: draw-text ( string color: rgba pt dim -- )
207     [
208         [ debug-text-uniform-variables ] 2dip
209         debug-text-vertex-array
210         debug-text-index-buffer
211         debug-text-render
212     ] with-destructors ; inline