Mercurial > touhou
comparison runners/src/bin/eclrenderer.rs @ 757:21b186be2590
Split the Rust version into multiple crates.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 05 Jan 2021 02:16:32 +0100 |
parents | examples/eclrenderer.rs@a662dddd4a2b |
children |
comparison
equal
deleted
inserted
replaced
756:4d91790cf8ab | 757:21b186be2590 |
---|---|
1 use luminance::blending::{Equation, Factor}; | |
2 use luminance::context::GraphicsContext; | |
3 use luminance::pipeline::{BoundTexture, PipelineState}; | |
4 use luminance::pixel::NormUnsigned; | |
5 use luminance::render_state::RenderState; | |
6 use luminance::shader::program::{Program, Uniform}; | |
7 use luminance::tess::{Mode, TessBuilder}; | |
8 use luminance::texture::Dim2; | |
9 use luminance_derive::{Semantics, Vertex, UniformInterface}; | |
10 use luminance_glfw::{Action, Key, WindowEvent, GlfwSurface, Surface, WindowDim, WindowOpt}; | |
11 use touhou_formats::th06::anm0::Anm0; | |
12 use touhou_formats::th06::ecl::{Ecl, Rank}; | |
13 use touhou_interpreters::th06::anm0::{Sprite, Vertex as FakeVertex}; | |
14 use touhou_interpreters::th06::ecl::EclRunner; | |
15 use touhou_interpreters::th06::enemy::{Enemy, Game, Position}; | |
16 use touhou_utils::math::{perspective, setup_camera}; | |
17 use touhou_utils::prng::Prng; | |
18 use std::cell::RefCell; | |
19 use std::rc::Rc; | |
20 use std::env; | |
21 use std::path::Path; | |
22 | |
23 use touhou_runners::common::{load_file_into_vec, load_anm_image, LoadedTexture}; | |
24 | |
25 const VS: &str = r#" | |
26 in ivec3 in_position; | |
27 in vec2 in_texcoord; | |
28 in uvec4 in_color; | |
29 | |
30 uniform mat4 mvp; | |
31 | |
32 out vec2 texcoord; | |
33 out vec4 color; | |
34 | |
35 void main() | |
36 { | |
37 gl_Position = mvp * vec4(vec3(in_position), 1.0); | |
38 texcoord = vec2(in_texcoord); | |
39 | |
40 // Normalized from the u8 being passed. | |
41 color = vec4(in_color) / 255.; | |
42 } | |
43 "#; | |
44 | |
45 const FS: &str = r#" | |
46 in vec2 texcoord; | |
47 in vec4 color; | |
48 | |
49 uniform sampler2D color_map; | |
50 | |
51 out vec4 frag_color; | |
52 | |
53 void main() | |
54 { | |
55 frag_color = texture(color_map, texcoord) * color; | |
56 } | |
57 "#; | |
58 | |
59 #[derive(Clone, Copy, Debug, Eq, PartialEq, Semantics)] | |
60 pub enum Semantics { | |
61 #[sem(name = "in_position", repr = "[i16; 3]", wrapper = "VertexPosition")] | |
62 Position, | |
63 | |
64 #[sem(name = "in_texcoord", repr = "[f32; 2]", wrapper = "VertexTexcoord")] | |
65 Texcoord, | |
66 | |
67 #[sem(name = "in_color", repr = "[u8; 4]", wrapper = "VertexColor")] | |
68 Color, | |
69 } | |
70 | |
71 #[repr(C)] | |
72 #[derive(Clone, Copy, Debug, PartialEq, Vertex)] | |
73 #[vertex(sem = "Semantics")] | |
74 struct Vertex { | |
75 pos: VertexPosition, | |
76 uv: VertexTexcoord, | |
77 rgba: VertexColor, | |
78 } | |
79 | |
80 #[derive(UniformInterface)] | |
81 struct ShaderInterface { | |
82 // the 'static lifetime acts as “anything” here | |
83 color_map: Uniform<&'static BoundTexture<'static, Dim2, NormUnsigned>>, | |
84 | |
85 #[uniform(name = "mvp")] | |
86 mvp: Uniform<[[f32; 4]; 4]>, | |
87 } | |
88 | |
89 fn main() { | |
90 // Parse arguments. | |
91 let args: Vec<_> = env::args().collect(); | |
92 if args.len() != 5 { | |
93 eprintln!("Usage: {} <ECL file> <ANM file> <easy|normal|hard|lunatic> <sub number>", args[0]); | |
94 return; | |
95 } | |
96 let ecl_filename = Path::new(&args[1]); | |
97 let anm_filename = Path::new(&args[2]); | |
98 let rank: Rank = args[3].parse().expect("rank"); | |
99 let sub: u16 = args[4].parse().expect("number"); | |
100 | |
101 // Open the ECL file. | |
102 let buf = load_file_into_vec(ecl_filename).unwrap(); | |
103 let (_, ecl) = Ecl::from_slice(&buf).unwrap(); | |
104 | |
105 // Open the ANM file. | |
106 let buf = load_file_into_vec(anm_filename).unwrap(); | |
107 let (_, mut anms) = Anm0::from_slice(&buf).unwrap(); | |
108 let anm0 = anms.pop().unwrap(); | |
109 let anm0 = Rc::new(RefCell::new([anm0.clone(), anm0])); | |
110 | |
111 if ecl.subs.len() < sub as usize { | |
112 eprintln!("This ecl doesn’t contain a sub named {}.", sub); | |
113 return; | |
114 } | |
115 | |
116 // Get the time since January 1970 as a seed for the PRNG. | |
117 let time = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap(); | |
118 let prng = Rc::new(RefCell::new(Prng::new(time.subsec_micros() as u16))); | |
119 | |
120 // Create the Game god object. | |
121 let game = Game::new(prng, rank); | |
122 let game = Rc::new(RefCell::new(game)); | |
123 | |
124 // And the enemy object. | |
125 let enemy = Enemy::new(Position::new(0., 0.), 500, 0, 640, false, Rc::downgrade(&anm0), Rc::downgrade(&game)); | |
126 let mut ecl_runner = EclRunner::new(&ecl, enemy.clone(), sub); | |
127 | |
128 assert_eq!(std::mem::size_of::<Vertex>(), std::mem::size_of::<FakeVertex>()); | |
129 let vertices: [Vertex; 4] = { | |
130 let data = std::mem::MaybeUninit::uninit(); | |
131 unsafe { data.assume_init() } | |
132 }; | |
133 | |
134 let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap(); | |
135 | |
136 // Open the image atlas matching this ANM. | |
137 let tex = load_anm_image(&mut surface, &anm0.borrow()[0], &anm_filename).expect("image loading"); | |
138 | |
139 // set the uniform interface to our type so that we can read textures from the shader | |
140 let program = | |
141 Program::<Semantics, (), ShaderInterface>::from_strings(None, VS, None, FS).expect("program creation").ignore_warnings(); | |
142 | |
143 let mut tess = TessBuilder::new(&mut surface) | |
144 .add_vertices(vertices) | |
145 .set_mode(Mode::TriangleFan) | |
146 .build() | |
147 .unwrap(); | |
148 | |
149 let mut back_buffer = surface.back_buffer().unwrap(); | |
150 let mut resize = false; | |
151 | |
152 'app: loop { | |
153 for event in surface.poll_events() { | |
154 match event { | |
155 WindowEvent::Close | WindowEvent::Key(Key::Escape, _, Action::Release, _) => break 'app, | |
156 | |
157 WindowEvent::FramebufferSize(..) => { | |
158 resize = true; | |
159 } | |
160 | |
161 _ => (), | |
162 } | |
163 } | |
164 | |
165 if resize { | |
166 back_buffer = surface.back_buffer().unwrap(); | |
167 resize = false; | |
168 } | |
169 | |
170 if ecl_runner.running == false { | |
171 break; | |
172 } | |
173 | |
174 { | |
175 let mut slice = tess | |
176 .as_slice_mut() | |
177 .unwrap(); | |
178 | |
179 ecl_runner.run_frame(); | |
180 { | |
181 let mut enemy = enemy.borrow_mut(); | |
182 enemy.update(); | |
183 } | |
184 let mut game = game.borrow_mut(); | |
185 game.run_frame(); | |
186 let sprites = game.get_sprites(); | |
187 fill_vertices_ptr(sprites, slice.as_mut_ptr()); | |
188 } | |
189 | |
190 // here, we need to bind the pipeline variable; it will enable us to bind the texture to the GPU | |
191 // and use it in the shader | |
192 surface | |
193 .pipeline_builder() | |
194 .pipeline(&back_buffer, &PipelineState::default(), |pipeline, mut shd_gate| { | |
195 // bind our fancy texture to the GPU: it gives us a bound texture we can use with the shader | |
196 let bound_tex = match &tex { | |
197 LoadedTexture::Rgb(tex) => pipeline.bind_texture(tex), | |
198 LoadedTexture::Rgba(tex) => pipeline.bind_texture(tex), | |
199 LoadedTexture::RgbaArray(tex) => unreachable!(), | |
200 }; | |
201 | |
202 shd_gate.shade(&program, |iface, mut rdr_gate| { | |
203 // update the texture; strictly speaking, this update doesn’t do much: it just tells the GPU | |
204 // to use the texture passed as argument (no allocation or copy is performed) | |
205 iface.color_map.update(&bound_tex); | |
206 //let mvp = ortho_2d(0., 384., 448., 0.); | |
207 let proj = perspective(0.5235987755982988, 384. / 448., 101010101./2010101., 101010101./10101.); | |
208 let view = setup_camera(0., 0., 1.); | |
209 let mvp = view * proj; | |
210 //println!("{:#?}", mvp); | |
211 // TODO: check how to pass by reference. | |
212 iface.mvp.update(*mvp.borrow_inner()); | |
213 | |
214 let render_state = RenderState::default() | |
215 .set_blending((Equation::Additive, Factor::SrcAlpha, Factor::SrcAlphaComplement)); | |
216 | |
217 rdr_gate.render(&render_state, |mut tess_gate| { | |
218 // render the tessellation to the surface the regular way and let the vertex shader’s | |
219 // magic do the rest! | |
220 tess_gate.render(&tess); | |
221 }); | |
222 }); | |
223 }); | |
224 | |
225 surface.swap_buffers(); | |
226 } | |
227 } | |
228 | |
229 fn fill_vertices_ptr(sprites: Vec<(f32, f32, f32, Rc<RefCell<Sprite>>)>, vertices: *mut Vertex) { | |
230 let mut fake_vertices = unsafe { std::mem::transmute::<*mut Vertex, &mut [FakeVertex; 4]>(vertices) }; | |
231 for (x, y, z, sprite) in sprites { | |
232 let sprite = sprite.borrow(); | |
233 sprite.fill_vertices(&mut fake_vertices, x, y, z); | |
234 } | |
235 } |