comparison src/th06/std_vm.rs @ 679:6020e33d4fc4

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