comparison examples/eclrenderer.rs @ 658:3a9d82a02c88

Add a contructor for enemy, and a new example.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 10 Aug 2019 12:48:01 +0200
parents
children 31fc0d881105
comparison
equal deleted inserted replaced
657:ff7b6355cdf1 658:3a9d82a02c88
1 use image::GenericImageView;
2 use luminance::context::GraphicsContext;
3 use luminance::framebuffer::Framebuffer;
4 use luminance::pipeline::BoundTexture;
5 use luminance::pixel::{NormRGB8UI, Floating};
6 use luminance::render_state::RenderState;
7 use luminance::shader::program::{Program, Uniform};
8 use luminance::tess::{Mode, TessBuilder};
9 use luminance::texture::{Dim2, Flat, Sampler, Texture, GenMipmaps};
10 use luminance_derive::{Semantics, Vertex, UniformInterface};
11 use luminance_glfw::event::{Action, Key, WindowEvent};
12 use luminance_glfw::surface::{GlfwSurface, Surface, WindowDim, WindowOpt};
13 use touhou::th06::anm0::Anm0;
14 use touhou::th06::anm0_vm::{Sprite, Vertex as FakeVertex};
15 use touhou::th06::ecl::Ecl;
16 use touhou::th06::enemy::{Enemy, Game, Position};
17 use touhou::util::math::{perspective, setup_camera};
18 use touhou::util::prng::Prng;
19 use std::cell::RefCell;
20 use std::fs::File;
21 use std::io::{BufReader, Read};
22 use std::rc::Rc;
23 use std::env;
24 use std::path::Path;
25
26 const VS: &str = r#"
27 in ivec3 in_position;
28 in vec2 in_texcoord;
29 in uvec4 in_color;
30
31 uniform mat4 mvp;
32
33 out vec2 texcoord;
34 out vec4 color;
35
36 void main()
37 {
38 gl_Position = mvp * vec4(vec3(in_position), 1.0);
39 texcoord = vec2(in_texcoord);
40
41 // Normalized from the u8 being passed.
42 color = vec4(in_color) / 255.;
43 }
44 "#;
45
46 const FS: &str = r#"
47 in vec2 texcoord;
48 in vec4 color;
49
50 uniform sampler2D color_map;
51
52 out vec4 frag_color;
53
54 void main()
55 {
56 frag_color = texture(color_map, texcoord) * color;
57 }
58 "#;
59
60 #[derive(Clone, Copy, Debug, Eq, PartialEq, Semantics)]
61 pub enum Semantics {
62 #[sem(name = "in_position", repr = "[i16; 3]", wrapper = "VertexPosition")]
63 Position,
64
65 #[sem(name = "in_texcoord", repr = "[f32; 2]", wrapper = "VertexTexcoord")]
66 Texcoord,
67
68 #[sem(name = "in_color", repr = "[u8; 4]", wrapper = "VertexColor")]
69 Color,
70 }
71
72 #[repr(C)]
73 #[derive(Clone, Copy, Debug, PartialEq, Vertex)]
74 #[vertex(sem = "Semantics")]
75 struct Vertex {
76 pos: VertexPosition,
77 uv: VertexTexcoord,
78 rgba: VertexColor,
79 }
80
81 #[derive(UniformInterface)]
82 struct ShaderInterface {
83 // the 'static lifetime acts as “anything” here
84 color_map: Uniform<&'static BoundTexture<'static, Flat, Dim2, Floating>>,
85
86 #[uniform(name = "mvp")]
87 mvp: Uniform<[[f32; 4]; 4]>,
88 }
89
90 fn main() {
91 // Parse arguments.
92 let args: Vec<_> = env::args().collect();
93 if args.len() != 5 {
94 eprintln!("Usage: {} <ECL file> <sub number> <ANM file> <PNG file>", args[0]);
95 return;
96 }
97 let ecl_filename = &args[1];
98 let sub = args[2].parse().expect("number");
99 let anm_filename = &args[3];
100 let png_filename = &args[4];
101
102 // Open the ECL file.
103 let file = File::open(ecl_filename).unwrap();
104 let mut file = BufReader::new(file);
105 let mut buf = vec![];
106 file.read_to_end(&mut buf).unwrap();
107 let (_, ecl) = Ecl::from_slice(&buf).unwrap();
108
109 // Open the ANM file.
110 let file = File::open(anm_filename).unwrap();
111 let mut file = BufReader::new(file);
112 let mut buf = vec![];
113 file.read_to_end(&mut buf).unwrap();
114 let anm0 = Anm0::from_slice(&buf).unwrap();
115 let anm0 = Rc::new(RefCell::new(anm0));
116
117 if ecl.subs.len() < sub {
118 eprintln!("This ecl doesn’t contain a sub named {}.", sub);
119 return;
120 }
121
122 // TODO: seed this PRNG with a valid seed.
123 let prng = Rc::new(RefCell::new(Prng::new(0)));
124
125 // Create the Game god object.
126 let game = Game::new(prng);
127 let game = Rc::new(RefCell::new(game));
128
129 // And the enemy object.
130 let mut enemy = Enemy::new(Position::new(0., 0.), 500, 0, 640, Rc::downgrade(&anm0), Rc::downgrade(&game));
131 enemy.set_anim(0);
132
133 assert_eq!(std::mem::size_of::<Vertex>(), std::mem::size_of::<FakeVertex>());
134 let vertices: [Vertex; 4] = unsafe { std::mem::uninitialized() };
135
136 let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap();
137
138 // Open the image atlas matching this ANM.
139 let tex = load_from_disk(&mut surface, Path::new(png_filename)).expect("texture loading");
140
141 // set the uniform interface to our type so that we can read textures from the shader
142 let (program, _) =
143 Program::<Semantics, (), ShaderInterface>::from_strings(None, VS, None, FS).expect("program creation");
144
145 let mut tess = TessBuilder::new(&mut surface)
146 .add_vertices(vertices)
147 .set_mode(Mode::TriangleFan)
148 .build()
149 .unwrap();
150
151 let mut back_buffer = Framebuffer::back_buffer(surface.size());
152
153 let mut frame = 0;
154 'app: loop {
155 for event in surface.poll_events() {
156 match event {
157 WindowEvent::Close | WindowEvent::Key(Key::Escape, _, Action::Release, _) => break 'app,
158
159 WindowEvent::FramebufferSize(width, height) => {
160 back_buffer = Framebuffer::back_buffer([width as u32, height as u32]);
161 }
162
163 _ => (),
164 }
165 }
166
167 if frame == 60 {
168 break;
169 }
170 frame += 1;
171
172 {
173 let mut slice = tess
174 .as_slice_mut()
175 .unwrap();
176
177 let mut game = game.borrow_mut();
178 game.run_frame();
179 let sprites = game.get_sprites();
180 fill_vertices_ptr(sprites, slice.as_mut_ptr());
181 }
182
183 // here, we need to bind the pipeline variable; it will enable us to bind the texture to the GPU
184 // and use it in the shader
185 surface
186 .pipeline_builder()
187 .pipeline(&back_buffer, [0., 0., 0., 0.], |pipeline, shd_gate| {
188 // bind our fancy texture to the GPU: it gives us a bound texture we can use with the shader
189 let bound_tex = pipeline.bind_texture(&tex);
190
191 shd_gate.shade(&program, |rdr_gate, iface| {
192 // update the texture; strictly speaking, this update doesn’t do much: it just tells the GPU
193 // to use the texture passed as argument (no allocation or copy is performed)
194 iface.color_map.update(&bound_tex);
195 //let mvp = ortho_2d(0., 384., 448., 0.);
196 let proj = perspective(0.5235987755982988, 384. / 448., 101010101./2010101., 101010101./10101.);
197 let view = setup_camera(0., 0., 1.);
198 let mvp = view * proj;
199 //println!("{:#?}", mvp);
200 // TODO: check how to pass by reference.
201 iface.mvp.update(*mvp.borrow_inner());
202
203 rdr_gate.render(RenderState::default(), |tess_gate| {
204 // render the tessellation to the surface the regular way and let the vertex shader’s
205 // magic do the rest!
206 tess_gate.render(&mut surface, (&tess).into());
207 });
208 });
209 });
210
211 surface.swap_buffers();
212 }
213 }
214
215 fn fill_vertices_ptr(sprites: Vec<Rc<RefCell<Sprite>>>, vertices: *mut Vertex) {
216 let mut fake_vertices = unsafe { std::mem::transmute::<*mut Vertex, &mut [FakeVertex; 4]>(vertices) };
217 for sprite in sprites {
218 let sprite = sprite.borrow();
219 sprite.fill_vertices(&mut fake_vertices);
220 }
221 }
222
223 fn load_from_disk(surface: &mut GlfwSurface, path: &Path) -> Option<Texture<Flat, Dim2, NormRGB8UI>> {
224 // load the texture into memory as a whole bloc (i.e. no streaming)
225 match image::open(&path) {
226 Ok(img) => {
227 let (width, height) = img.dimensions();
228 let texels = img
229 .pixels()
230 .map(|(x, y, rgb)| (rgb[0], rgb[1], rgb[2]))
231 .collect::<Vec<_>>();
232
233 // create the luminance texture; the third argument is the number of mipmaps we want (leave it
234 // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
235 // shader (we’ll just use the default one)
236 let tex =
237 Texture::new(surface, [width, height], 0, &Sampler::default()).expect("luminance texture creation");
238
239 // the first argument disables mipmap generation (we don’t care so far)
240 tex.upload(GenMipmaps::No, &texels);
241
242 Some(tex)
243 }
244
245 Err(e) => {
246 eprintln!("cannot open image {}: {}", path.display(), e);
247 None
248 }
249 }
250 }