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 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
19 ! roll-until-horizontal
20 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
22 : V ( -- V ) { 0 1 0 } ;
24 : X ( -- 3array ) orientation> [ first ] map ;
25 : Y ( -- 3array ) orientation> [ second ] map ;
26 : Z ( -- 3array ) orientation> [ third ] map ;
28 : set-X ( seq -- ) orientation> [ 0 swap set-nth ] 2each ;
29 : set-Y ( seq -- ) orientation> [ 1 swap set-nth ] 2each ;
30 : set-Z ( seq -- ) orientation> [ 2 swap set-nth ] 2each ;
32 : roll-until-horizontal ( -- )
33 V Z cross normalize set-X
34 Z X cross normalize set-Y ;
36 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
38 : record-vertex ( -- ) position> gl-vertex ;
40 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
44 : draw-forward ( length -- )
45 GL_LINES glBegin record-vertex step-turtle record-vertex glEnd ;
47 : move-forward ( length -- ) step-turtle polygon-vertex ;
49 : sneak-forward ( length -- ) step-turtle ;
51 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
53 ! (v0 - v1) x (v1 - v2)
55 : polygon-normal ( {_v0_v1_v2_} -- normal )
56 0 over nth over 1 swap nth v- swap
57 1 over nth swap 2 swap nth v- cross ;
59 ! Test and replace with:
61 ! : v0-v1 ( { v0 v1 v2 } -- vec ) first2 v- ;
63 ! : v1-v2 ( { v0 v1 v2 } -- vec ) first3 v- nip ;
65 ! : polygon-normal ( { v0 v1 v2 } -- normal ) dup v0-v1 swap v1-v2 cross ;
67 : (polygon) ( vertices -- )
68 GL_POLYGON glBegin dup polygon-normal gl-normal [ gl-vertex ] each glEnd ;
70 : polygon ( vertices -- ) dup length 3 >= [ (polygon) ] [ drop ] if ;
72 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
74 ! Maybe use an array instead of a vector
78 : start-polygon ( -- ) 0 <vector> >vertices ;
80 : finish-polygon ( -- ) vertices> polygon ;
82 : polygon-vertex ( -- ) position> vertices> push ;
84 : reset-vertices start-polygon ;
86 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
87 ! Lindenmayer string rewriting
88 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
90 ! Maybe use an array instead of a quot in the work of segment
94 : segment ( str -- seq )
95 { { [ dup "" = ] [ drop [ ] ] }
96 { [ dup length 1 = ] [ unit ] }
97 { [ 1 over nth CHAR: ( = ]
98 [ CHAR: ) over index 1 + ! str i
99 2dup head ! str i head
100 -rot tail ! head tail
101 segment swap add* ] }
102 { [ t ] [ dup 1 head swap 1 tail segment swap add* ] } }
105 : lookup ( str -- str ) dup 1 head rules get hash dup [ nip ] [ drop ] if ;
107 : rewrite ( str -- str ) segment [ lookup ] map concat ;
109 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
110 ! Lindenmayer string interpretation
111 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
113 SYMBOL: command-table
115 : segment-command ( seg -- command ) 1 head ;
117 ! : segment-parameter ( seg -- parameter )
118 ! dup length 1 - 2 swap rot subseq parse call ;
120 : segment-parameter ( seg -- parameter )
121 dup length 1 - 2 swap rot subseq string>number ;
123 : segment-parts ( seg -- param command )
124 dup segment-parameter swap segment-command ;
126 : exec-command ( str -- ) command-table get hash dup [ call ] [ drop ] if ;
128 : exec-command-with-param ( param command -- )
129 command-table get hash dup [ peek unit call ] [ 2drop ] if ;
131 : (interpret) ( seg -- )
133 [ exec-command ] [ segment-parts exec-command-with-param ] if ;
135 : interpret ( str -- ) segment [ (interpret) ] each ;
137 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
139 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
146 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
149 DEFER: set-color-index
151 TUPLE: state position orientation angle len thickness color-index ;
155 : reset-state-stack ( -- ) V{ } clone >states ;
158 position> orientation> angle> len> thickness> color-index> <state>
161 : restore-state ( -- )
163 dup state-position >position
164 dup state-orientation >orientation
166 dup state-angle >angle
167 dup state-color-index set-color-index
168 dup state-thickness set-thickness
171 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
173 : scale-len ( m -- ) len> * >len ;
175 : scale-angle ( m -- ) angle> * >angle ;
177 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
181 : init-color-table ( -- )
183 { 0.5 0.5 0.5 } ! grey
187 { 0.25 0.88 0.82 } ! turquoise
189 { 0.63 0.13 0.94 } ! purple
190 { 0.00 0.50 0.00 } ! dark green
191 { 0.00 0.82 0.82 } ! dark turquoise
192 { 0.00 0.00 0.50 } ! dark blue
193 { 0.58 0.00 0.82 } ! dark purple
194 { 0.50 0.00 0.00 } ! dark red
195 { 0.25 0.25 0.25 } ! dark grey
196 { 0.75 0.75 0.75 } ! medium grey
198 } [ 1 set-color-alpha ] map color-table set ;
200 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
202 : material-color ( color -- )
203 GL_FRONT_AND_BACK GL_AMBIENT_AND_DIFFUSE rot gl-material-fv ;
205 : set-color-index ( i -- )
206 dup >color-index color-table> nth dup gl-color material-color ;
208 : inc-color-index ( -- ) color-index> 1 + set-color-index ;
210 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
212 : set-thickness ( i -- ) dup >thickness glLineWidth ;
214 : scale-thickness ( m -- ) thickness> * 0.5 max set-thickness ;
216 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
221 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
223 : lparser-dialect ( -- )
225 [ 1 >len 45 >angle 1 >thickness 2 >color-index ] >default-values
227 H{ { "+" [ angle get turn-left ] }
228 { "-" [ angle get turn-right ] }
229 { "&" [ angle get pitch-down ] }
230 { "^" [ angle get pitch-up ] }
231 { "<" [ angle get roll-left ] }
232 { ">" [ angle get roll-right ] }
234 { "|" [ 180.0 rotate-y ] }
235 { "%" [ 180.0 rotate-z ] }
236 { "$" [ roll-until-horizontal ] }
238 { "F" [ len get draw-forward ] }
239 { "Z" [ len get 2 / draw-forward ] }
240 { "f" [ len get move-forward ] }
241 { "z" [ len get 2 / move-forward ] }
242 { "g" [ len get sneak-forward ] }
243 { "." [ polygon-vertex ] }
245 { "[" [ save-state ] }
246 { "]" [ restore-state ] }
247 { "{" [ start-polygon ] }
248 { "}" [ finish-polygon ] }
250 { "/" [ 1.1 scale-len ] } ! double quote command in lparser
251 { "'" [ 0.9 scale-len ] }
252 { ";" [ 1.1 scale-angle ] }
253 { ":" [ 0.9 scale-angle ] }
254 { "?" [ 1.4 scale-thickness ] }
255 { "!" [ 0.7 scale-thickness ] }
257 { "c" [ color-index> 1 + color-table get length mod set-color-index ] }
259 } command-table set ;
261 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
266 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
268 : iterate ( -- ) result> rewrite >result ;
270 : iterations ( n -- ) [ iterate ] times ;
272 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
276 : init-model ( -- ) 1 glGenLists >model ;
278 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
286 GL_PROJECTION glMatrixMode
288 -1 1 -1 1 1.5 200 glFrustum
290 GL_MODELVIEW glMatrixMode
294 [ do-look-at ] camera> with-turtle
296 GL_COLOR_BUFFER_BIT glClear
298 GL_FRONT_AND_BACK GL_LINE glPolygonMode
301 GL_LINES glBegin { 0 0 0 } gl-vertex { 0 0 1 } gl-vertex glEnd
303 color-index> set-color-index
307 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
309 : init-turtle ( -- ) <turtle> >turtle ;
311 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
313 : init-camera ( -- ) <turtle> >camera ;
315 : reset-camera ( -- ) [
321 ] camera> with-turtle ;
323 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
326 <camera-slate> >slate
327 namespace slate> set-slate-ns
328 slate> "L-system" open-titled-window
329 [ display ] >action ;
331 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
336 init-camera reset-camera
344 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
346 : result>model ( -- )
347 [ model> GL_COMPILE glNewList result> interpret glEndList ] >action .slate ;
357 [ display ] >action .slate ;
359 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
361 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
363 : koch ( -- ) lparser-dialect [ 90 >angle ] >model-values
365 H{ { "K" "[[a|b] '(0.41)f'(2.439) |<(60) [a|b]]" }
366 { "k" "[ c'(0.5) K]" }
367 { "a" "[d <(120) d <(120) d ]" }
369 { "e" "[^ '(.2887)f'(3.4758) &(180) +z{.-(120)f-(120)f}]" }
370 { "d" "[^ '(.2887)f'(3.4758) &(109.5111) +zk{.-(120)f-(120)f}]" }
373 "K" >axiom axiom> >result ;
375 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
377 : spiral-0 ( -- ) lparser-dialect [ 10 >angle 5 >thickness ] >model-values
381 H{ { "P" "[A]>>>>>>>>>[cB]>>>>>>>>>[ccC]>>>>>>>>>[cccD]" }
385 { "D" "F!>^+F>^+;'D" }
390 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
392 : tree-5 ( -- ) lparser-dialect [ 5 >angle 1 >thickness ] >model-values
396 H{ { "S" "FFR>(60)R>(60)R>(60)R>(60)R>(60)R>(30)S" }
407 { "F" "'(1.25)F'(.8)" }
410 : tree-5-scene ( -- )
414 [ reset-turtle 90 pitch-down -70 step-turtle 50 strafe-up ] camera> with-turtle
417 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
419 : abop-1 ( -- ) lparser-dialect [ 45 >angle 5 >thickness ] >model-values
421 H{ { "A" "F[&'(.8)!BL]>(137)'!(.9)A" }
422 { "B" "F[-'(.8)!(.9)$CL]'!(.9)C" }
423 { "C" "F[+'(.8)!(.9)$BL]'!(.9)B" }
425 { "L" "~c(8){+(30)f-(120)f-(120)f}" }
428 "c(12)FFAL" >result ;
430 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
432 : abop-2 ( -- ) lparser-dialect [ 30 >angle 5 >thickness ] >model-values
434 H{ { "A" "F[&'(.7)!BL]>(137)[&'(.6)!BL]>(137)'(.9)!(.9)A" }
435 { "B" "F[-'(.7)!(.9)$CL]'(.9)!(.9)C" }
436 { "C" "F[+'(.7)!(.9)$BL]'(.9)!(.9)B" }
438 { "L" "~c(8){+(45)f(.1)-(45)f(.1)-(45)f(.1)+(45)|+(45)f(.1)-(45)f(.1)-(45)f(.1)}" }
444 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
446 : abop-3 ( -- ) lparser-dialect [ 30 >angle 5 >thickness ] >model-values
448 H{ { "A" "!(.9)t(.4)FB>(94)B>(132)B" }
449 { "B" "[&t(.4)F$A]" }
450 { "F" "'(1.25)F'(.8)" }
455 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
457 : abop-4 ( -- ) lparser-dialect [ 18 >angle 5 >thickness ] >model-values
459 H{ { "N" "FII[&(60)rY]>(90)[&(45)'(0.8)rA]>(90)[&(60)rY]>(90)[&(45)'(0.8)rD]!FIK" }
460 { "Y" "[c(4){++l.--l.--l.++|++l.--l.--l.}]" }
462 { "K" "[!c(2)FF>w>(72)w>(72)w>(72)w>(72)w]" }
463 { "w" "[c(2)^!F][c(5)&(72){-(54)f(3)+(54)f(3)|-(54)f(3)+(54)f(3)}]" }
490 "c(12)&(20)N" >result ;
492 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
494 : abop-5 ( -- ) lparser-dialect [ 5 >angle 5 >thickness ] >model-values
496 H{ { "a" "F[+(45)l][-(45)l]^;ca" }
505 { "F" "'(1.17)F'(.855)" }
508 "&(90)+(90)a" >result ;
510 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
512 : abop-6 ( -- ) lparser-dialect [ 5 >angle 5 >thickness ] >model-values
514 "&(90)+(90)FFF[-(120)'(.6)x][-(60)'(.8)x][+(120)'(.6)x][+(60)'(.8)x]x" >result
516 H{ { "a" "F[cdx][cex]F!(.9)a" }
522 { "F" "'(1.25)F'(.8)" }
525 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
527 : airhorse ( -- ) lparser-dialect [ 10 >angle 5 >thickness ] >model-values
533 { "B" "[[''aH]|[g]]" }
536 { "s" "[::cc!!!!&&[FFcccZ]^^^^FFcccZ]" }
537 { "t" "[c!!!!&[FF]^^FF]" }
547 { "V" "[cc!!!&(90)[Zp]|[Zp]]" }
548 { "p" "h>(120)h>(120)h" }
549 { "h" "[+(40)!F'''p]" }
551 { "H" "[cccci[>(50)dcFFF][<(50)ecFFF]]" }
556 { "W" "[%[!!cb][<<<!!cb][>>>!!cb]]" }
558 { "l" "[-cc{--z++z++z--|--z++z++z}]" }
561 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
563 ! These should be moved into a separate file. They are used to pretty
564 ! print matricies and vectors.
566 USING: styles prettyprint io ;
568 : decimal-places ( n d -- n )
569 10 swap ^ tuck * >fixnum swap /f ;
571 ! : .mat ( matrix -- ) [ [ 2 decimal-places ] map ] map . ;
574 H{ { table-gap 4 } { table-border 4 } }
575 [ 2 decimal-places pprint ]
578 : .vec ( vector -- ) [ 2 decimal-places ] map . ;
580 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
582 PROVIDE: lindenmayer ;
584 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!