Mercurial > touhou
annotate src/th06/std_vm.rs @ 703:81232dac8136
ecl: simplify parsing with more combinators.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 23 Aug 2019 17:37:22 +0200 |
parents | 6020e33d4fc4 |
children | 377c241be559 |
rev | line source |
---|---|
679
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 //! Interpreter of STD files. |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 use crate::th06::std::{Stage, Position, Call, Instruction}; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 use crate::th06::interpolator::{Interpolator1, Interpolator4}; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
5 use crate::util::math::{Mat4, perspective, setup_camera}; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 use std::cell::RefCell; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 use std::rc::Rc; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 /// Interpreter for Stage. |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 pub struct StageRunner { |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 /// XXX: no pub. |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 pub stage: Rc<RefCell<Stage>>, |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 frame: u32, |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 // TODO: use interpolators. |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 position: [f32; 3], |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 direction: [f32; 3], |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 /// XXX: no pub. |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 pub fog_color: [f32; 4], |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 /// XXX: no pub. |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
22 pub fog_near: f32, |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
23 /// XXX: no pub. |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 pub fog_far: f32, |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 } |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
27 impl StageRunner { |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
28 /// Create a new StageRunner attached to a Stage. |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 pub fn new(stage: Rc<RefCell<Stage>>) -> StageRunner { |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
30 StageRunner { |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
31 stage, |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 frame: 0, |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 position: [0.; 3], |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
34 direction: [0.; 3], |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 fog_color: [1.; 4], |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 fog_near: 0., |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 fog_far: 1000., |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 } |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 } |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 /// Advance the simulation one frame. |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 pub fn run_frame(&mut self) { |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 let stage = self.stage.borrow(); |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 for Call { time, instr } in stage.script.iter() { |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 if *time != self.frame { |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 continue; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
48 } |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
50 println!("{} {:?}", time, instr); |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 match *instr { |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
53 Instruction::SetViewpos(x, y, z) => { |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 self.position[0] = x; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 self.position[1] = y; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
56 self.position[2] = z; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
57 } |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
58 Instruction::SetFog(b, g, r, a, near, far) => { |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
59 self.fog_color = [r as f32 / 255., g as f32 / 255., b as f32 / 255., a as f32 / 255.]; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
60 self.fog_near = near; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
61 self.fog_far = far; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
62 } |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
63 Instruction::SetViewpos2(dx, dy, dz) => { |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
64 self.direction[0] = dx; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
65 self.direction[1] = dy; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
66 self.direction[2] = dz; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
67 } |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
68 Instruction::StartInterpolatingViewpos2(frame, _, _) => { |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
69 } |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
70 Instruction::StartInterpolatingFog(frame, _, _) => { |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
71 } |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
72 Instruction::Unknown(_, _, _) => { |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
73 } |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 } |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
75 } |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
77 self.frame += 1; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
78 } |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
79 |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
80 /// Generate the model-view matrix for the current frame. |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
81 pub fn get_model_view(&self) -> Mat4 { |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
82 let [x, y, z] = self.position; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
83 |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
84 let [dx, dy, dz] = self.direction; |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
85 |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
86 let view = setup_camera(dx, dy, dz); |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
87 |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
88 let model = Mat4::new([[1., 0., 0., 0.], |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
89 [0., 1., 0., 0.], |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
90 [0., 0., 1., 0.], |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
91 [-x, -y, -z, 1.]]); |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
92 model * view |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
93 } |
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
94 } |