1 ! Eduardo Cavazos - wayo.cavazos@gmail.com
6 apps/lindenmayer/opengl
7 apps/lindenmayer/turtle
8 apps/lindenmayer/camera
9 apps/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 ) first3 dupd v- -rot v- swap cross ;
39 : (polygon) ( vertices -- )
40 GL_POLYGON glBegin dup polygon-normal gl-normal [ gl-vertex ] each glEnd ;
42 : polygon ( vertices -- ) dup length 3 >= [ (polygon) ] [ drop ] if ;
44 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
46 ! Maybe use an array instead of a vector
50 : start-polygon ( -- ) 0 <vector> >vertices ;
52 : finish-polygon ( -- ) vertices> polygon ;
54 : polygon-vertex ( -- ) position> vertices> push ;
56 : reset-vertices start-polygon ;
58 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
59 ! Lindenmayer string rewriting
60 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
62 ! Maybe use an array instead of a quot in the work of segment
66 : segment ( str -- seq )
67 { { [ dup "" = ] [ drop [ ] ] }
68 { [ dup length 1 = ] [ unit ] }
69 { [ 1 over nth CHAR: ( = ]
70 [ CHAR: ) over index 1 + ! str i
71 2dup head ! str i head
74 { [ t ] [ dup 1 head swap 1 tail segment swap add* ] } }
77 : lookup ( str -- str ) dup 1 head rules get hash dup [ nip ] [ drop ] if ;
79 : rewrite ( str -- str ) segment [ lookup ] map concat ;
81 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
82 ! Lindenmayer string interpretation
83 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
87 : segment-command ( seg -- command ) 1 head ;
89 : segment-parameter ( seg -- parameter )
90 dup length 1 - 2 swap rot subseq string>number ;
92 : segment-parts ( seg -- param command )
93 dup segment-parameter swap segment-command ;
95 : exec-command ( str -- ) command-table get hash dup [ call ] [ drop ] if ;
97 : exec-command-with-param ( param command -- )
98 command-table get hash dup [ peek unit call ] [ 2drop ] if ;
100 : (interpret) ( seg -- )
102 [ exec-command ] [ segment-parts exec-command-with-param ] if ;
104 : interpret ( str -- ) segment [ (interpret) ] each ;
106 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
108 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
115 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
118 DEFER: set-color-index
120 TUPLE: state position orientation angle len thickness color-index ;
124 : reset-state-stack ( -- ) V{ } clone >states ;
127 position> orientation> angle> len> thickness> color-index> <state>
130 : restore-state ( -- )
132 dup state-position >position
133 dup state-orientation >orientation
135 dup state-angle >angle
136 dup state-color-index set-color-index
137 dup state-thickness set-thickness
140 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
142 : scale-len ( m -- ) len> * >len ;
144 : scale-angle ( m -- ) angle> * >angle ;
146 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
150 : init-color-table ( -- )
152 { 0.5 0.5 0.5 } ! grey
156 { 0.25 0.88 0.82 } ! turquoise
158 { 0.63 0.13 0.94 } ! purple
159 { 0.00 0.50 0.00 } ! dark green
160 { 0.00 0.82 0.82 } ! dark turquoise
161 { 0.00 0.00 0.50 } ! dark blue
162 { 0.58 0.00 0.82 } ! dark purple
163 { 0.50 0.00 0.00 } ! dark red
164 { 0.25 0.25 0.25 } ! dark grey
165 { 0.75 0.75 0.75 } ! medium grey
167 } [ 1 set-color-alpha ] map color-table set ;
169 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
171 : material-color ( color -- )
172 GL_FRONT_AND_BACK GL_AMBIENT_AND_DIFFUSE rot gl-material-fv ;
174 : set-color-index ( i -- )
175 dup >color-index color-table> nth dup gl-color material-color ;
177 : inc-color-index ( -- ) color-index> 1 + set-color-index ;
179 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
181 : set-thickness ( i -- ) dup >thickness glLineWidth ;
183 : scale-thickness ( m -- ) thickness> * 0.5 max set-thickness ;
185 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
190 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
192 : lparser-dialect ( -- )
194 [ 1 >len 45 >angle 1 >thickness 2 >color-index ] >default-values
196 H{ { "+" [ angle get turn-left ] }
197 { "-" [ angle get turn-right ] }
198 { "&" [ angle get pitch-down ] }
199 { "^" [ angle get pitch-up ] }
200 { "<" [ angle get roll-left ] }
201 { ">" [ angle get roll-right ] }
203 { "|" [ 180.0 rotate-y ] }
204 { "%" [ 180.0 rotate-z ] }
205 { "$" [ roll-until-horizontal ] }
207 { "F" [ len get draw-forward ] }
208 { "Z" [ len get 2 / draw-forward ] }
209 { "f" [ len get move-forward ] }
210 { "z" [ len get 2 / move-forward ] }
211 { "g" [ len get sneak-forward ] }
212 { "." [ polygon-vertex ] }
214 { "[" [ save-state ] }
215 { "]" [ restore-state ] }
216 { "{" [ start-polygon ] }
217 { "}" [ finish-polygon ] }
219 { "/" [ 1.1 scale-len ] } ! double quote command in lparser
220 { "'" [ 0.9 scale-len ] }
221 { ";" [ 1.1 scale-angle ] }
222 { ":" [ 0.9 scale-angle ] }
223 { "?" [ 1.4 scale-thickness ] }
224 { "!" [ 0.7 scale-thickness ] }
226 { "c" [ color-index> 1 + color-table get length mod set-color-index ] }
228 } command-table set ;
230 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
235 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
237 : iterate ( -- ) result> rewrite >result ;
239 : iterations ( n -- ) [ iterate ] times ;
241 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
245 : init-model ( -- ) 1 glGenLists >model ;
247 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
255 GL_PROJECTION glMatrixMode
257 -1 1 -1 1 1.5 200 glFrustum
259 GL_MODELVIEW glMatrixMode
263 [ do-look-at ] camera> with-turtle
265 GL_COLOR_BUFFER_BIT glClear
267 GL_FRONT_AND_BACK GL_LINE glPolygonMode
270 GL_LINES glBegin { 0 0 0 } gl-vertex { 0 0 1 } gl-vertex glEnd
272 color-index> set-color-index
276 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
278 : init-turtle ( -- ) <turtle> >turtle ;
280 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
282 : init-camera ( -- ) <turtle> >camera ;
284 : reset-camera ( -- ) [
290 ] camera> with-turtle ;
292 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
295 <camera-slate> >slate
296 namespace slate> set-slate-ns
297 slate> "L-system" open-titled-window
298 [ display ] >action ;
300 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
305 init-camera reset-camera
313 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
315 : result>model ( -- )
316 [ model> GL_COMPILE glNewList result> interpret glEndList ] >action .slate ;
326 [ display ] >action .slate ;
328 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
330 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
332 : koch ( -- ) lparser-dialect [ 90 >angle ] >model-values
334 H{ { "K" "[[a|b] '(0.41)f'(2.439) |<(60) [a|b]]" }
335 { "k" "[ c'(0.5) K]" }
336 { "a" "[d <(120) d <(120) d ]" }
338 { "e" "[^ '(.2887)f'(3.4758) &(180) +z{.-(120)f-(120)f}]" }
339 { "d" "[^ '(.2887)f'(3.4758) &(109.5111) +zk{.-(120)f-(120)f}]" }
344 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
346 : spiral-0 ( -- ) lparser-dialect [ 10 >angle 5 >thickness ] >model-values
350 H{ { "P" "[A]>>>>>>>>>[cB]>>>>>>>>>[ccC]>>>>>>>>>[cccD]" }
354 { "D" "F!>^+F>^+;'D" }
357 : spiral-0-scene ( -- )
361 [ reset-turtle 90 turn-left 16 step-turtle 180 turn-left ]
362 camera> with-turtle .slate ;
364 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
366 : tree-5 ( -- ) lparser-dialect [ 5 >angle 1 >thickness ] >model-values
370 H{ { "S" "FFR>(60)R>(60)R>(60)R>(60)R>(60)R>(30)S" }
381 { "F" "'(1.25)F'(.8)" }
384 : tree-5-scene ( -- )
388 [ reset-turtle 90 pitch-down -70 step-turtle 50 strafe-up ] camera> with-turtle
391 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
393 : abop-1 ( -- ) lparser-dialect [ 45 >angle 5 >thickness ] >model-values
395 H{ { "A" "F[&'(.8)!BL]>(137)'!(.9)A" }
396 { "B" "F[-'(.8)!(.9)$CL]'!(.9)C" }
397 { "C" "F[+'(.8)!(.9)$BL]'!(.9)B" }
399 { "L" "~c(8){+(30)f-(120)f-(120)f}" }
402 "c(12)FFAL" >result ;
404 : abop-1-scene ( -- )
409 90 pitch-up 7 step-turtle 90 pitch-down 4 step-turtle 90 pitch-down ]
410 camera> with-turtle .slate ;
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 : abop-2-scene ( -- )
430 [ reset-turtle { 0 4 4 } >position 90 pitch-down ]
431 camera> with-turtle .slate ;
433 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
435 : abop-3 ( -- ) lparser-dialect [ 30 >angle 5 >thickness ] >model-values
437 H{ { "A" "!(.9)t(.4)FB>(94)B>(132)B" }
438 { "B" "[&t(.4)F$A]" }
439 { "F" "'(1.25)F'(.8)" }
444 : abop-3-scene ( -- )
445 abop-3 11 iterations build-model
446 [ reset-turtle { 0 47 29 } >position 90 pitch-down ] camera> with-turtle
449 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
451 : abop-4 ( -- ) lparser-dialect [ 18 >angle 5 >thickness ] >model-values
453 H{ { "N" "FII[&(60)rY]>(90)[&(45)'(0.8)rA]>(90)[&(60)rY]>(90)[&(45)'(0.8)rD]!FIK" }
454 { "Y" "[c(4){++l.--l.--l.++|++l.--l.--l.}]" }
456 { "K" "[!c(2)FF>w>(72)w>(72)w>(72)w>(72)w]" }
457 { "w" "[c(2)^!F][c(5)&(72){-(54)f(3)+(54)f(3)|-(54)f(3)+(54)f(3)}]" }
484 "c(12)&(20)N" >result ;
486 : abop-4-scene ( -- )
487 abop-4 21 iterations build-model
489 { 53 25 36 } >position
490 { { 0.57 -0.14 -0.80 } { -0.81 -0.18 -0.54 } { -0.07 0.97 -0.22 } }
492 ] camera> with-turtle .slate ;
494 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
496 : abop-5 ( -- ) lparser-dialect [ 5 >angle 5 >thickness ] >model-values
498 H{ { "a" "F[+(45)l][-(45)l]^;ca" }
507 { "F" "'(1.17)F'(.855)" }
510 "&(90)+(90)a" >result ;
512 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
514 : abop-6 ( -- ) lparser-dialect [ 5 >angle 5 >thickness ] >model-values
516 "&(90)+(90)FFF[-(120)'(.6)x][-(60)'(.8)x][+(120)'(.6)x][+(60)'(.8)x]x" >result
518 H{ { "a" "F[cdx][cex]F!(.9)a" }
524 { "F" "'(1.25)F'(.8)" }
527 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
529 : airhorse ( -- ) lparser-dialect [ 10 >angle 5 >thickness ] >model-values
535 { "B" "[[''aH]|[g]]" }
538 { "s" "[::cc!!!!&&[FFcccZ]^^^^FFcccZ]" }
539 { "t" "[c!!!!&[FF]^^FF]" }
549 { "V" "[cc!!!&(90)[Zp]|[Zp]]" }
550 { "p" "h>(120)h>(120)h" }
551 { "h" "[+(40)!F'''p]" }
553 { "H" "[cccci[>(50)dcFFF][<(50)ecFFF]]" }
558 { "W" "[%[!!cb][<<<!!cb][>>>!!cb]]" }
560 { "l" "[-cc{--z++z++z--|--z++z++z}]" }
563 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
565 ! These should be moved into a separate file. They are used to pretty
566 ! print matricies and vectors.
568 USING: styles prettyprint io ;
570 : decimal-places ( n d -- n )
571 10 swap ^ tuck * >fixnum swap /f ;
573 ! : .mat ( matrix -- ) [ [ 2 decimal-places ] map ] map . ;
576 H{ { table-gap 4 } { table-border 4 } }
577 [ 2 decimal-places pprint ]
580 : .vec ( vector -- ) [ 2 decimal-places ] map . ;
582 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!