1 ! Eduardo Cavazos - wayo.cavazos@gmail.com
6 contrib/lindenmayer/opengl
7 contrib/lindenmayer/turtle
8 contrib/lindenmayer/camera
9 contrib/lindenmayer/camera-slate ;
11 USING: kernel alien namespaces arrays vectors math opengl sequences threads
12 hashtables strings gadgets
13 math-contrib vars slate turtle turtle-camera camera-slate
18 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
20 : record-vertex ( -- ) position> gl-vertex ;
22 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
26 : draw-forward ( length -- )
27 GL_LINES glBegin record-vertex step-turtle record-vertex glEnd ;
29 : move-forward ( length -- ) step-turtle polygon-vertex ;
31 : sneak-forward ( length -- ) step-turtle ;
33 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
35 ! (v0 - v1) x (v1 - v2)
37 : polygon-normal ( {_v0_v1_v2_} -- normal )
38 0 over nth over 1 swap nth v- swap
39 1 over nth swap 2 swap nth v- cross ;
41 ! Test and replace with:
43 ! : v0-v1 ( { v0 v1 v2 } -- vec ) first2 v- ;
45 ! : v1-v2 ( { v0 v1 v2 } -- vec ) first3 v- nip ;
47 ! : polygon-normal ( { v0 v1 v2 } -- normal ) dup v0-v1 swap v1-v2 cross ;
49 : (polygon) ( vertices -- )
50 GL_POLYGON glBegin dup polygon-normal gl-normal [ gl-vertex ] each glEnd ;
52 : polygon ( vertices -- ) dup length 3 >= [ (polygon) ] [ drop ] if ;
54 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
56 ! Maybe use an array instead of a vector
60 : start-polygon ( -- ) 0 <vector> >vertices ;
62 : finish-polygon ( -- ) vertices> polygon ;
64 : polygon-vertex ( -- ) position> vertices> push ;
66 : reset-vertices start-polygon ;
68 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
69 ! Lindenmayer string rewriting
70 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
72 ! Maybe use an array instead of a quot in the work of segment
76 : segment ( str -- seq )
77 { { [ dup "" = ] [ drop [ ] ] }
78 { [ dup length 1 = ] [ unit ] }
79 { [ 1 over nth CHAR: ( = ]
80 [ CHAR: ) over index 1 + ! str i
81 2dup head ! str i head
84 { [ t ] [ dup 1 head swap 1 tail segment swap add* ] } }
87 : lookup ( str -- str ) dup 1 head rules get hash dup [ nip ] [ drop ] if ;
89 : rewrite ( str -- str ) segment [ lookup ] map concat ;
91 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
92 ! Lindenmayer string interpretation
93 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
97 : segment-command ( seg -- command ) 1 head ;
99 ! : segment-parameter ( seg -- parameter )
100 ! dup length 1 - 2 swap rot subseq parse call ;
102 : segment-parameter ( seg -- parameter )
103 dup length 1 - 2 swap rot subseq string>number ;
105 : segment-parts ( seg -- param command )
106 dup segment-parameter swap segment-command ;
108 : exec-command ( str -- ) command-table get hash dup [ call ] [ drop ] if ;
110 : exec-command-with-param ( param command -- )
111 command-table get hash dup [ peek unit call ] [ 2drop ] if ;
113 : (interpret) ( seg -- )
115 [ exec-command ] [ segment-parts exec-command-with-param ] if ;
117 : interpret ( str -- ) segment [ (interpret) ] each ;
119 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
121 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
128 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
131 DEFER: set-color-index
133 TUPLE: state position orientation angle len thickness color-index ;
137 : reset-state-stack ( -- ) V{ } clone >states ;
140 position> orientation> angle> len> thickness> color-index> <state>
143 : restore-state ( -- )
145 dup state-position >position
146 dup state-orientation >orientation
148 dup state-angle >angle
149 dup state-color-index set-color-index
150 dup state-thickness set-thickness
153 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
155 : scale-len ( m -- ) len> * >len ;
157 : scale-angle ( m -- ) angle> * >angle ;
159 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
163 : init-color-table ( -- )
165 { 0.5 0.5 0.5 } ! grey
169 { 0.25 0.88 0.82 } ! turquoise
171 { 0.63 0.13 0.94 } ! purple
172 { 0.00 0.50 0.00 } ! dark green
173 { 0.00 0.82 0.82 } ! dark turquoise
174 { 0.00 0.00 0.50 } ! dark blue
175 { 0.58 0.00 0.82 } ! dark purple
176 { 0.50 0.00 0.00 } ! dark red
177 { 0.25 0.25 0.25 } ! dark grey
178 { 0.75 0.75 0.75 } ! medium grey
180 } [ 1 set-color-alpha ] map color-table set ;
182 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
184 : material-color ( color -- )
185 GL_FRONT_AND_BACK GL_AMBIENT_AND_DIFFUSE rot gl-material-fv ;
187 : set-color-index ( i -- )
188 dup >color-index color-table> nth dup gl-color material-color ;
190 : inc-color-index ( -- ) color-index> 1 + set-color-index ;
192 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
194 : set-thickness ( i -- ) dup >thickness glLineWidth ;
196 : scale-thickness ( m -- ) thickness> * 0.5 max set-thickness ;
198 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
203 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
205 : lparser-dialect ( -- )
207 [ 1 >len 45 >angle 1 >thickness 2 >color-index ] >default-values
209 H{ { "+" [ angle get turn-left ] }
210 { "-" [ angle get turn-right ] }
211 { "&" [ angle get pitch-down ] }
212 { "^" [ angle get pitch-up ] }
213 { "<" [ angle get roll-left ] }
214 { ">" [ angle get roll-right ] }
216 { "|" [ 180.0 rotate-y ] }
217 { "%" [ 180.0 rotate-z ] }
218 { "$" [ roll-until-horizontal ] }
220 { "F" [ len get draw-forward ] }
221 { "Z" [ len get 2 / draw-forward ] }
222 { "f" [ len get move-forward ] }
223 { "z" [ len get 2 / move-forward ] }
224 { "g" [ len get sneak-forward ] }
225 { "." [ polygon-vertex ] }
227 { "[" [ save-state ] }
228 { "]" [ restore-state ] }
229 { "{" [ start-polygon ] }
230 { "}" [ finish-polygon ] }
232 { "/" [ 1.1 scale-len ] } ! double quote command in lparser
233 { "'" [ 0.9 scale-len ] }
234 { ";" [ 1.1 scale-angle ] }
235 { ":" [ 0.9 scale-angle ] }
236 { "?" [ 1.4 scale-thickness ] }
237 { "!" [ 0.7 scale-thickness ] }
239 { "c" [ color-index> 1 + color-table get length mod set-color-index ] }
241 } command-table set ;
243 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
248 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
250 : iterate ( -- ) result> rewrite >result ;
252 : iterations ( n -- ) [ iterate ] times ;
254 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
258 : init-model ( -- ) 1 glGenLists >model ;
260 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
268 GL_PROJECTION glMatrixMode
270 -1 1 -1 1 1.5 200 glFrustum
272 GL_MODELVIEW glMatrixMode
276 [ do-look-at ] camera> with-turtle
278 GL_COLOR_BUFFER_BIT glClear
280 GL_FRONT_AND_BACK GL_LINE glPolygonMode
283 GL_LINES glBegin { 0 0 0 } gl-vertex { 0 0 1 } gl-vertex glEnd
285 color-index> set-color-index
289 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
291 : init-turtle ( -- ) <turtle> >turtle ;
293 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
295 : init-camera ( -- ) <turtle> >camera ;
297 : reset-camera ( -- ) [
303 ] camera> with-turtle ;
305 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
308 <camera-slate> >slate
309 namespace slate> set-slate-ns
310 slate> "L-system" open-titled-window
311 [ display ] >action ;
313 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
318 init-camera reset-camera
326 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
328 : result>model ( -- )
329 [ model> GL_COMPILE glNewList result> interpret glEndList ] >action .slate ;
339 [ display ] >action .slate ;
341 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
343 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
345 : koch ( -- ) lparser-dialect [ 90 >angle ] >model-values
347 H{ { "K" "[[a|b] '(0.41)f'(2.439) |<(60) [a|b]]" }
348 { "k" "[ c'(0.5) K]" }
349 { "a" "[d <(120) d <(120) d ]" }
351 { "e" "[^ '(.2887)f'(3.4758) &(180) +z{.-(120)f-(120)f}]" }
352 { "d" "[^ '(.2887)f'(3.4758) &(109.5111) +zk{.-(120)f-(120)f}]" }
355 "K" >axiom axiom> >result ;
357 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
359 : spiral-0 ( -- ) lparser-dialect [ 10 >angle 5 >thickness ] >model-values
363 H{ { "P" "[A]>>>>>>>>>[cB]>>>>>>>>>[ccC]>>>>>>>>>[cccD]" }
367 { "D" "F!>^+F>^+;'D" }
372 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
374 : tree-5 ( -- ) lparser-dialect [ 5 >angle 1 >thickness ] >model-values
378 H{ { "S" "FFR>(60)R>(60)R>(60)R>(60)R>(60)R>(30)S" }
389 { "F" "'(1.25)F'(.8)" }
392 : tree-5-scene ( -- )
396 [ reset-turtle 90 pitch-down -70 step-turtle 50 strafe-up ] camera> with-turtle
399 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
401 : abop-1 ( -- ) lparser-dialect [ 45 >angle 5 >thickness ] >model-values
403 H{ { "A" "F[&'(.8)!BL]>(137)'!(.9)A" }
404 { "B" "F[-'(.8)!(.9)$CL]'!(.9)C" }
405 { "C" "F[+'(.8)!(.9)$BL]'!(.9)B" }
407 { "L" "~c(8){+(30)f-(120)f-(120)f}" }
410 "c(12)FFAL" >result ;
412 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
414 : abop-2 ( -- ) lparser-dialect [ 30 >angle 5 >thickness ] >model-values
416 H{ { "A" "F[&'(.7)!BL]>(137)[&'(.6)!BL]>(137)'(.9)!(.9)A" }
417 { "B" "F[-'(.7)!(.9)$CL]'(.9)!(.9)C" }
418 { "C" "F[+'(.7)!(.9)$BL]'(.9)!(.9)B" }
420 { "L" "~c(8){+(45)f(.1)-(45)f(.1)-(45)f(.1)+(45)|+(45)f(.1)-(45)f(.1)-(45)f(.1)}" }
426 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
428 : abop-3 ( -- ) lparser-dialect [ 30 >angle 5 >thickness ] >model-values
430 H{ { "A" "!(.9)t(.4)FB>(94)B>(132)B" }
431 { "B" "[&t(.4)F$A]" }
432 { "F" "'(1.25)F'(.8)" }
437 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
439 : abop-4 ( -- ) lparser-dialect [ 18 >angle 5 >thickness ] >model-values
441 H{ { "N" "FII[&(60)rY]>(90)[&(45)'(0.8)rA]>(90)[&(60)rY]>(90)[&(45)'(0.8)rD]!FIK" }
442 { "Y" "[c(4){++l.--l.--l.++|++l.--l.--l.}]" }
444 { "K" "[!c(2)FF>w>(72)w>(72)w>(72)w>(72)w]" }
445 { "w" "[c(2)^!F][c(5)&(72){-(54)f(3)+(54)f(3)|-(54)f(3)+(54)f(3)}]" }
472 "c(12)&(20)N" >result ;
474 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
476 : abop-5 ( -- ) lparser-dialect [ 5 >angle 5 >thickness ] >model-values
478 H{ { "a" "F[+(45)l][-(45)l]^;ca" }
487 { "F" "'(1.17)F'(.855)" }
490 "&(90)+(90)a" >result ;
492 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
494 : abop-6 ( -- ) lparser-dialect [ 5 >angle 5 >thickness ] >model-values
496 "&(90)+(90)FFF[-(120)'(.6)x][-(60)'(.8)x][+(120)'(.6)x][+(60)'(.8)x]x" >result
498 H{ { "a" "F[cdx][cex]F!(.9)a" }
504 { "F" "'(1.25)F'(.8)" }
507 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
509 : airhorse ( -- ) lparser-dialect [ 10 >angle 5 >thickness ] >model-values
515 { "B" "[[''aH]|[g]]" }
518 { "s" "[::cc!!!!&&[FFcccZ]^^^^FFcccZ]" }
519 { "t" "[c!!!!&[FF]^^FF]" }
529 { "V" "[cc!!!&(90)[Zp]|[Zp]]" }
530 { "p" "h>(120)h>(120)h" }
531 { "h" "[+(40)!F'''p]" }
533 { "H" "[cccci[>(50)dcFFF][<(50)ecFFF]]" }
538 { "W" "[%[!!cb][<<<!!cb][>>>!!cb]]" }
540 { "l" "[-cc{--z++z++z--|--z++z++z}]" }
543 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
545 ! These should be moved into a separate file. They are used to pretty
546 ! print matricies and vectors.
548 USING: styles prettyprint io ;
550 : decimal-places ( n d -- n )
551 10 swap ^ tuck * >fixnum swap /f ;
553 ! : .mat ( matrix -- ) [ [ 2 decimal-places ] map ] map . ;
556 H{ { table-gap 4 } { table-border 4 } }
557 [ 2 decimal-places pprint ]
560 : .vec ( vector -- ) [ 2 decimal-places ] map . ;
562 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
564 PROVIDE: lindenmayer ;
566 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!