2 USING: kernel math vectors sequences opengl.gl math.vectors math.order
3 math.matrices vars opengl self pos ori turtle lsys.tortoise
5 lsys.strings.interpret combinators.short-circuit ;
9 IN: lsys.tortoise.graphics
11 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
13 ! (v0 - v1) x (v1 - v2)
15 : polygon-normal ( {_v0_v1_v2_} -- normal ) first3 dupd v- -rot v- swap cross ;
17 : (polygon) ( vertices -- )
19 dup polygon-normal gl-normal [ gl-vertex ] each
22 : polygon ( vertices -- ) dup length 3 >= [ (polygon) ] [ drop ] if ;
24 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
28 ! : init-vertices ( -- ) 0 <vector> >vertices ;
30 : start-polygon ( -- ) vertices> delete-all ;
32 : finish-polygon ( -- ) vertices> polygon ;
34 : polygon-vertex ( -- ) pos> vertices> push ;
36 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
38 : record-vertex ( -- ) pos> gl-vertex ;
40 : draw-forward ( length -- )
41 GL_LINES glBegin record-vertex step-turtle record-vertex glEnd ;
43 : move-forward ( length -- ) step-turtle polygon-vertex ;
45 : sneak-forward ( length -- ) step-turtle ;
47 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
49 : scale-len ( m -- ) len> * >len ;
51 : scale-angle ( m -- ) angle> * >angle ;
53 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
55 : set-thickness ( i -- ) dup >thickness glLineWidth ;
57 : scale-thickness ( m -- ) thickness> * 0.5 max set-thickness ;
59 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
63 : init-color-table ( -- )
65 { 0.5 0.5 0.5 } ! grey
69 { 0.25 0.88 0.82 } ! turquoise
71 { 0.63 0.13 0.94 } ! purple
72 { 0.00 0.50 0.00 } ! dark green
73 { 0.00 0.82 0.82 } ! dark turquoise
74 { 0.00 0.00 0.50 } ! dark blue
75 { 0.58 0.00 0.82 } ! dark purple
76 { 0.50 0.00 0.00 } ! dark red
77 { 0.25 0.25 0.25 } ! dark grey
78 { 0.75 0.75 0.75 } ! medium grey
80 } [ 1 suffix ] map >color-table ;
82 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
84 : material-color ( color -- )
85 GL_FRONT_AND_BACK GL_AMBIENT_AND_DIFFUSE rot gl-material ;
88 dup >color color-table> nth dup gl-color material-color ;
90 : inc-color ( -- ) color> 1+ set-color ;
92 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
96 ! : init-tortoise-stack ( -- ) V{ } clone >tortoise-stack ;
98 ! : save-tortoise ( -- ) self> tortoise-stack> push ;
100 ! : save-tortoise ( -- ) self> tortoise-stack> push self> clone >self ;
102 : save-tortoise ( -- ) self> clone tortoise-stack> push ;
104 : restore-tortoise ( -- )
105 tortoise-stack> pop >self
107 thickness> set-thickness ;
109 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
114 : lparser-dialect ( -- )
116 [ 1 >len 45 >angle 1 >thickness 2 >color ] >default-values
118 H{ { "+" [ angle> turn-left ] }
119 { "-" [ angle> turn-right ] }
120 { "&" [ angle> pitch-down ] }
121 { "^" [ angle> pitch-up ] }
122 { "<" [ angle> roll-left ] }
123 { ">" [ angle> roll-right ] }
125 { "|" [ 180.0 rotate-y ] }
126 { "%" [ 180.0 rotate-z ] }
127 { "$" [ roll-until-horizontal ] }
129 { "F" [ len> draw-forward ] }
130 { "Z" [ len> 2 / draw-forward ] }
131 { "f" [ len> move-forward ] }
132 { "z" [ len> 2 / move-forward ] }
133 { "g" [ len> sneak-forward ] }
134 { "." [ polygon-vertex ] }
136 { "[" [ save-tortoise ] }
137 { "]" [ restore-tortoise ] }
138 { "{" [ start-polygon ] }
139 { "}" [ finish-polygon ] }
141 { "/" [ 1.1 scale-len ] } ! double quote command in lparser
142 { "'" [ 0.9 scale-len ] }
143 { ";" [ 1.1 scale-angle ] }
144 { ":" [ 0.9 scale-angle ] }
145 { "?" [ 1.4 scale-thickness ] }
146 { "!" [ 0.7 scale-thickness ] }
148 { "c" [ color> 1 + color-table> length mod set-color ] }
152 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!