@@ -9,12 +9,12 @@ use winit::{
99 window:: WindowId ,
1010} ;
1111
12- // TODO: this could probably be calculated based on time since last frame instead
1312const DELTA_TIME : f32 = 0.1 ;
1413
14+ use crate :: common:: ModelSelection ;
1515use crate :: gui:: LSystemConfig ;
1616use crate :: lsystem:: LSystem ;
17- use crate :: model_loader:: { Model3D , load_cylinder , load_floor, load_monkey } ;
17+ use crate :: model_loader:: { load_floor, load_model } ;
1818use crate :: scene:: Scene ;
1919use crate :: turtle:: TurtleInterpreter ;
2020use crate :: {
@@ -36,7 +36,7 @@ pub struct App {
3636 pressed_keys : HashSet < KeyCode > ,
3737 interaction_mode : AppInteractionMode ,
3838 lsystem_config : Option < LSystemConfig > ,
39- base_models : Vec < Model3D > ,
39+ model_selection : Option < ModelSelection > ,
4040 scene : Option < Scene > ,
4141}
4242
@@ -51,10 +51,9 @@ impl ApplicationHandler for App {
5151 glm:: vec3 ( 0.0 , 1.0 , 5.0 ) ,
5252 self . renderer . as_ref ( ) . unwrap ( ) . get_aspect_ratio ( ) ,
5353 ) ) ;
54- self . base_models = vec ! [ load_cylinder( ) , load_monkey( ) ] ;
5554 self . scene = Some ( Scene :: new (
5655 load_floor ( ) ,
57- self . base_models [ 0 ] . clone ( ) ,
56+ load_model ( ModelSelection :: default ( ) ) ,
5857 Vec :: new ( ) ,
5958 3.0 ,
6059 [ 10.0 , 10.0 , 10.0 ] ,
@@ -94,12 +93,7 @@ impl ApplicationHandler for App {
9493 return ;
9594 }
9695
97- let new_lsystem_config = self . get_current_lsystem_config ( ) ;
98- if new_lsystem_config != self . lsystem_config . as_ref ( ) . unwrap ( ) {
99- log:: info!( "L-System config changed to {new_lsystem_config:?}" ) ;
100- self . lsystem_config = Some ( new_lsystem_config. clone ( ) ) ;
101- self . calculate_transformations ( ) ;
102- }
96+ self . update_fractal ( ) ;
10397
10498 self . render_scene ( ) ;
10599 self . handle_movement ( ) ;
@@ -197,17 +191,6 @@ impl App {
197191
198192 fn render_scene ( & mut self ) {
199193 let renderer = self . renderer . as_mut ( ) . unwrap ( ) ;
200-
201- // TODO some reasonable base models for L-systems
202- let model = match renderer. get_gui_controller ( ) . get_model_selection ( ) {
203- crate :: gui:: ModelSelection :: Monkey => & self . base_models [ 1 ] ,
204- crate :: gui:: ModelSelection :: Cylinder => & self . base_models [ 0 ] ,
205- } ;
206-
207- if model. geometry . name != self . scene . as_ref ( ) . unwrap ( ) . fractal_base ( ) . geometry . name {
208- self . scene . as_mut ( ) . unwrap ( ) . set_fractal_base ( model. clone ( ) ) ;
209- }
210-
211194 let camera = self . camera . as_ref ( ) . unwrap ( ) ;
212195
213196 renderer. render_scene (
@@ -217,6 +200,55 @@ impl App {
217200 ) ;
218201 }
219202
203+ fn update_fractal ( & mut self ) {
204+ let Some ( renderer) = & self . renderer else {
205+ return ;
206+ } ;
207+
208+ let gui = renderer. get_gui_controller ( ) ;
209+ let config = gui. get_lsystem_config ( ) ;
210+ let model_selection = gui. get_model_selection ( ) ;
211+
212+ let config_changed = self . lsystem_config . as_ref ( ) != Some ( config) ;
213+ let model_changed = self . model_selection . as_ref ( ) != Some ( model_selection) ;
214+
215+ if !config_changed && !model_changed {
216+ return ;
217+ }
218+
219+ log:: info!(
220+ "Updating fractal - config_changed: {config_changed}, model_changed: {model_changed}"
221+ ) ;
222+
223+ if config_changed {
224+ log:: info!( "L-System config changed to {config:?}" ) ;
225+ self . lsystem_config = Some ( config. clone ( ) ) ;
226+ }
227+
228+ if model_changed {
229+ log:: info!( "Model selection changed to {model_selection:?}" ) ;
230+ self . model_selection = Some ( * model_selection) ;
231+
232+ let new_base = load_model ( * model_selection) ;
233+
234+ if let Some ( scene) = & mut self . scene {
235+ scene. set_fractal_base ( new_base) ;
236+ }
237+ }
238+
239+ let production_rules: HashMap < char , String > =
240+ config. production_rules . iter ( ) . cloned ( ) . collect ( ) ;
241+ let lsystem = LSystem :: new ( & config. axiom , production_rules) ;
242+ let generated = lsystem. generate ( config. n_iterations ) ;
243+ let transformations = TurtleInterpreter :: interpret ( & generated, config. angle ) ;
244+
245+ if let Some ( scene) = & mut self . scene {
246+ scene. update_transformations ( transformations, config. fractal_height ) ;
247+ }
248+
249+ renderer. request_redraw ( ) ;
250+ }
251+
220252 fn calculate_transformations ( & mut self ) {
221253 let lsystem_config = self . get_current_lsystem_config ( ) ;
222254 let target_height = lsystem_config. fractal_height ;
0 commit comments