1 USING: accessors alien.c-types alien.data alien.data.map arrays
2 assocs byte-arrays colors combinators combinators.short-circuit
3 destructors euler.b-rep euler.b-rep.triangulation fry game.input
4 game.loop game.models.half-edge game.worlds gml.printer gpu
5 gpu.buffers gpu.framebuffers gpu.render gpu.shaders gpu.state
6 gpu.util.wasd growable images kernel literals locals math
7 math.order math.ranges math.vectors math.vectors.conversion
8 math.vectors.simd math.vectors.simd.cords method-chains models
9 namespaces sequences sets specialized-vectors typed ui
10 ui.gadgets ui.gadgets.worlds ui.gestures ui.pixel-formats
12 FROM: math.matrices => m.v ;
13 FROM: models => change-model ;
14 SPECIALIZED-VECTORS: ushort float-4 ;
17 CONSTANT: neutral-edge-color float-4{ 1 1 1 1 }
18 CONSTANT: neutral-face-color float-4{ 1 1 1 1 }
19 CONSTANT: selected-face-color float-4{ 1 0.9 0.8 1 }
21 : double-4>float-4 ( in: double-4 -- out: float-4 )
22 [ head>> ] [ tail>> ] bi double-2 float-4 vconvert ; inline
23 : rgba>float-4 ( in: rgba -- out: float-4 )
24 >rgba-components float-4-boa ; inline
26 : face-color ( edge -- color )
27 face-normal float-4{ 0 1 0.1 0 } v. 0.3 * 0.4 + dup dup 1.0 float-4-boa ; inline
30 { array byte-array read-only }
31 { face-vertex-count integer read-only }
32 { edge-vertex-count integer read-only }
33 { point-vertex-count integer read-only } ;
35 :: <b-rep-vertices> ( face-array face-count
37 point-array point-count -- vxs )
38 face-array edge-array point-array 3append
39 face-count edge-count point-count \ b-rep-vertices boa ; inline
41 : face-selected? ( face selected -- ? )
42 [ f ] 2dip [ edge>> ] dip '[ _ in? or ] each-face-edge ;
44 :: b-rep-face-vertices ( b-rep selected -- vertices count indices )
45 float-4-vector{ } clone :> vertices
46 ushort-vector{ } clone :> indices
48 0 b-rep faces>> [| count face |
49 face selected face-selected? :> selected?
50 face dup base-face>> eq? [
51 face edge>> face-color
52 selected? selected-face-color neutral-face-color ? v* :> color
53 face triangulate-face seq>> :> triangles
54 triangles members :> tri-vertices
55 tri-vertices >index-hash :> vx-indices
58 position>> double-4>float-4 vertices push
61 triangles [ vx-indices at count + indices push ] each
63 count tri-vertices length +
66 vertices float-4 >c-array underlying>>
68 indices ushort-array{ } like ;
70 : b-rep-edge-vertices ( b-rep -- vertices count )
73 position>> [ double-4>float-4 ] keep
74 [ drop neutral-edge-color ]
75 [ vertex-color rgba>float-4 ] 2bi
76 ] data-map( object -- float-4[4] )
77 ] [ length 2 * ] bi ; inline
79 GENERIC: selected-vectors ( object -- vectors )
80 M: object selected-vectors drop { } ;
81 M: double-4 selected-vectors 1array ;
82 M: sequence selected-vectors [ selected-vectors ] map concat ;
84 : selected-vertices ( selected -- vertices count )
86 [ [ double-4>float-4 ] [ vertex-color rgba>float-4 ] bi ]
87 data-map( object -- float-4[2] )
88 ] [ length ] bi ; inline
90 : edge-vertex-index ( e vertex-indices selected -- n selected? )
91 [ dup vertex>> ] [ at 2 * ] [ swapd in? [ [ 1 + ] when ] keep ] tri* ;
93 :: b-rep-edge-index-array ( b-rep selected offset -- edge-indices )
94 b-rep vertices>> >index-hash :> vertex-indices
95 b-rep edges>> length <ushort-vector> :> edge-indices
98 e opposite-edge>> :> o
99 e vertex-indices selected edge-vertex-index [ offset + ] dip :> ( from e-selected? )
100 o vertex-indices selected edge-vertex-index [ offset + ] dip :> ( to o-selected? )
102 from to < [ from edge-indices push to edge-indices push ] when
105 edge-indices ushort-array{ } like ;
107 :: make-b-rep-vertices ( b-rep selected -- vertices face-indices edge-indices point-indices )
108 b-rep selected b-rep-face-vertices :> ( face-vertices face-count face-indices )
109 b-rep b-rep-edge-vertices :> ( edge-vertices edge-count )
110 selected selected-vertices :> ( sel-vertices sel-count )
111 face-vertices face-count edge-vertices edge-count sel-vertices sel-count
112 <b-rep-vertices> :> vertices
118 b-rep selected vertices face-vertex-count>> b-rep-edge-index-array
121 [ face-vertex-count>> ]
122 [ edge-vertex-count>> + dup ]
123 [ point-vertex-count>> + ] tri
124 [a,b) ushort >c-array ;
126 VERTEX-FORMAT: wire-vertex-format
127 { "vertex" float-components 3 f }
128 { f float-components 1 f }
129 { "color" float-components 4 f } ;
131 GLSL-SHADER-FILE: gml-viewer-vertex-shader vertex-shader "viewer.v.glsl"
132 GLSL-SHADER-FILE: gml-viewer-fragment-shader fragment-shader "viewer.f.glsl"
133 GLSL-PROGRAM: gml-viewer-program
134 gml-viewer-vertex-shader gml-viewer-fragment-shader
137 TUPLE: gml-viewer-world < wasd-world
142 face-indices edge-indices point-indices
143 view-faces? view-edges?
146 TYPED: refresh-b-rep-view ( world: gml-viewer-world -- )
147 dup control-value >>b-rep
148 dup vertex-array>> [ vertex-array-buffer dispose ] when*
149 dup [ b-rep>> ] [ selected>> value>> ] bi make-b-rep-vertices {
151 static-upload draw-usage vertex-buffer byte-array>buffer
152 over program>> <vertex-array> >>vertex-array
160 : viewable? ( gml-viewer-world -- ? )
161 { [ b-rep>> ] [ program>> ] } 1&& ;
163 M: gml-viewer-world model-changed
167 [ dup viewable? [ refresh-b-rep-view ] [ drop ] if ] tri ;
169 : init-viewer-model ( gml-viewer-world -- )
170 [ dup model>> add-connection ]
171 [ dup selected>> add-connection ] bi ;
173 : reset-view ( gml-viewer-world -- )
174 { 0.0 0.0 5.0 } 0.0 0.0 set-wasd-view drop ;
176 M: gml-viewer-world begin-game-world
180 T{ point-state { size 5.0 } } set-gpu-state
182 gml-viewer-program <program-instance> >>program
183 dup init-viewer-model
186 M: gml-viewer-world end-game-world
187 [ dup selected>> remove-connection ]
188 [ dup model>> remove-connection ] bi ;
190 M: gml-viewer-world draw-world*
192 { default-attachment { 0.0 0.0 0.0 1.0 } }
193 { depth-attachment 1.0 }
198 T{ depth-state { comparison cmp-less } } set-gpu-state
200 { "primitive-mode" [ drop triangles-mode ] }
201 { "indexes" [ face-indices>> ] }
202 { "uniforms" [ <mvp-uniforms> ] }
203 { "vertex-array" [ vertex-array>> ] }
204 } <render-set> render
205 T{ depth-state { comparison f } } set-gpu-state
210 { "primitive-mode" [ drop lines-mode ] }
211 { "indexes" [ edge-indices>> ] }
212 { "uniforms" [ <mvp-uniforms> ] }
213 { "vertex-array" [ vertex-array>> ] }
214 } <render-set> render
218 { "primitive-mode" [ drop points-mode ] }
219 { "indexes" [ point-indices>> ] }
220 { "uniforms" [ <mvp-uniforms> ] }
221 { "vertex-array" [ vertex-array>> ] }
222 } <render-set> render
225 TYPED: rotate-view-mode ( world: gml-viewer-world -- )
229 [ f >>view-edges? t >>view-faces? ] if
230 ] [ t >>view-edges? ] if drop ;
232 CONSTANT: edge-hitbox-radius 0.05
234 :: line-nearest-t ( p0 u q0 v -- tp tq )
243 a c * b b * - :> denom
245 b e * c d * - denom /f
246 a e * b d * - denom /f ;
248 :: intersects-edge-node? ( source direction edge -- ? )
249 edge vertex>> position>> double-4>float-4 :> edge-source
250 edge opposite-edge>> vertex>> position>> double-4>float-4 edge-source v- :> edge-direction
252 source direction edge-source edge-direction line-nearest-t :> ( ray-t edge-t )
254 ray-t 0.0 >= edge-t 0.0 0.5 between? and [
255 source direction ray-t v*n v+
256 edge-source edge-direction edge-t v*n v+ v- norm
260 : intersecting-edge-node ( source direction b-rep -- edge/f )
261 edges>> [ intersects-edge-node? ] 2with find nip ;
263 : select-edge ( world -- )
264 [ [ location>> ] [ hand-loc get wasd-pixel-ray ] bi ]
265 [ b-rep>> intersecting-edge-node ]
266 [ '[ _ [ selected>> push-model ] [ refresh-b-rep-view ] bi ] when* ] tri ;
269 { T{ button-up f f 1 } [ dup drag?>> [ drop ] [ select-edge ] if ] }
270 { T{ drag f 1 } [ t >>drag? drop ] }
271 { T{ key-down f f "RET" } [ reset-view ] }
272 { T{ key-down f f "TAB" } [ rotate-view-mode ] }
275 AFTER: gml-viewer-world tick-game-world
278 ! FIXME: GTK Mouse buttons are an integer
279 ! MacOSX mouse buttons are an array of bools
280 dup integer? [ 0 bit? ] [ first ] if >>drag?
283 M: gml-viewer-world wasd-mouse-scale drag?>> -1/600. 0.0 ? ;
285 : wrap-in-model ( object -- model )
286 dup model? [ <model> ] unless ;
287 : wrap-in-growable-model ( object -- model )
289 dup growable? [ >vector ] unless
293 : gml-viewer ( b-rep selection -- )
294 [ wrap-in-model ] [ wrap-in-growable-model ] bi*
297 { world-class gml-viewer-world }
298 { title "GML wireframe viewer" }
299 { pixel-format-attributes {
305 { use-game-input? t }
306 { use-audio-engine? f }
307 { pref-dim { 1024 768 } }
308 { tick-interval-nanos $[ 30 fps ] }