annotate examples/common.rs @ 706:bca515da9047

examples: use common module.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 23 Aug 2019 19:46:47 +0200
parents ed65f9412bc0
children 987409d48991
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 use image::{GenericImageView, DynamicImage};
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 use luminance::pixel::{NormRGB8UI, NormRGBA8UI};
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 use luminance::texture::{Dim2, Flat, Sampler, Texture, GenMipmaps};
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 use luminance_glfw::surface::GlfwSurface;
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
5 use touhou::th06::anm0::Anm0;
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 use std::fs::File;
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 use std::io::{BufReader, Read};
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 use std::path::Path;
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 pub fn load_file_into_vec(filename: &Path) -> Vec<u8> {
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 let file = File::open(filename).unwrap();
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 let mut file = BufReader::new(file);
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 let mut buf = vec![];
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 file.read_to_end(&mut buf).unwrap();
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 buf
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 }
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 pub enum LoadedTexture {
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 Rgba(Texture<Flat, Dim2, NormRGBA8UI>),
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 Rgb(Texture<Flat, Dim2, NormRGB8UI>),
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 }
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
23 fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Option<LoadedTexture> {
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 // load the texture into memory as a whole bloc (i.e. no streaming)
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 match image::open(&path) {
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 Ok(img) => {
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 let (width, height) = img.dimensions();
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 let texels = img
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 .pixels()
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 .map(|(_x, _y, rgb)| (rgb[0], rgb[1], rgb[2]))
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 .collect::<Vec<_>>();
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 // create the luminance texture; the third argument is the number of mipmaps we want (leave it
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 // shader (we’ll just use the default one)
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 let tex =
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 Texture::new(surface, [width, height], 0, &Sampler::default()).expect("luminance texture creation");
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 // the first argument disables mipmap generation (we don’t care so far)
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 tex.upload(GenMipmaps::No, &texels);
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 Some(LoadedTexture::Rgb(tex))
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 }
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 Err(e) => {
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 eprintln!("cannot open image {}: {}", path.display(), e);
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 None
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 }
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 }
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 }
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
52 fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Option<LoadedTexture> {
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 // load the texture into memory as a whole bloc (i.e. no streaming)
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 match image::open(&alpha) {
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 Ok(img) => {
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 let (width, height) = img.dimensions();
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 let alpha = match img.grayscale() {
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 DynamicImage::ImageLuma8(img) => img,
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 _ => {
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 eprintln!("cannot convert alpha image {} to grayscale", alpha.display());
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 return None;
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 }
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 };
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 let img = match image::open(&rgb) {
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 Ok(img) => img,
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 Err(e) => {
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 eprintln!("cannot open rgb image {}: {}", rgb.display(), e);
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 return None;
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 },
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 };
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 let texels = img
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 .pixels()
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 .zip(alpha.pixels())
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
74 .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[2], luma[0]))
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 .collect::<Vec<_>>();
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 // create the luminance texture; the third argument is the number of mipmaps we want (leave it
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 // shader (we’ll just use the default one)
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 let tex =
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 Texture::new(surface, [width, height], 0, &Sampler::default()).expect("luminance texture creation");
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 // the first argument disables mipmap generation (we don’t care so far)
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 tex.upload(GenMipmaps::No, &texels);
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 Some(LoadedTexture::Rgba(tex))
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 }
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 Err(e) => {
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 eprintln!("cannot open alpha image {}: {}", alpha.display(), e);
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 None
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 }
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 }
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 }
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
95
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
96 pub fn load_anm_image(mut surface: &mut GlfwSurface, anm0: &Anm0, anm_filename: &Path) -> LoadedTexture {
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
97 let png_filename = anm_filename.with_file_name(Path::new(&anm0.png_filename).file_name().unwrap());
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
98 match anm0.alpha_filename {
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
99 Some(ref filename) => {
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
100 let alpha_filename = anm_filename.with_file_name(Path::new(filename).file_name().unwrap());
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
101 println!("alpha {:?}", alpha_filename);
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
102 load_rgb_a_pngs(&mut surface, &png_filename, &alpha_filename).expect("texture loading")
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
103 },
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
104 None => {
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
105 load_rgb_png(&mut surface, &png_filename).expect("texture loading")
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
106 }
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
107 }
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 705
diff changeset
108 }