annotate examples/anmrenderer.rs @ 704:84af5bedbde4

anmrenderer: also load the alpha PNG.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 23 Aug 2019 19:09:37 +0200
parents b6c351ca0a35
children ed65f9412bc0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
1 use image::{GenericImageView, DynamicImage};
675
6be3320a1cb3 Use the same RenderState as PyTouhou in anmrenderer, and normalize color, thanks phaazon!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 656
diff changeset
2 use luminance::blending::{Equation, Factor};
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 use luminance::context::GraphicsContext;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 use luminance::framebuffer::Framebuffer;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 use luminance::pipeline::BoundTexture;
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
6 use luminance::pixel::{NormRGB8UI, NormRGBA8UI, Floating};
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 use luminance::render_state::RenderState;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 use luminance::shader::program::{Program, Uniform};
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 use luminance::tess::{Mode, TessBuilder};
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 use luminance::texture::{Dim2, Flat, Sampler, Texture, GenMipmaps};
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 use luminance_derive::{Semantics, Vertex, UniformInterface};
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 use luminance_glfw::event::{Action, Key, WindowEvent};
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 use luminance_glfw::surface::{GlfwSurface, Surface, WindowDim, WindowOpt};
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 use touhou::th06::anm0::Anm0;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 use touhou::th06::anm0_vm::{AnmRunner, Sprite, Vertex as FakeVertex};
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 use touhou::util::math::{perspective, setup_camera};
646
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 644
diff changeset
17 use touhou::util::prng::Prng;
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 use std::cell::RefCell;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 use std::fs::File;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 use std::io::{BufReader, Read};
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 use std::rc::Rc;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 use std::env;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 use std::path::Path;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 const VS: &str = r#"
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 in ivec3 in_position;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 in vec2 in_texcoord;
675
6be3320a1cb3 Use the same RenderState as PyTouhou in anmrenderer, and normalize color, thanks phaazon!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 656
diff changeset
28 in vec4 in_color;
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 uniform mat4 mvp;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 out vec2 texcoord;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 out vec4 color;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 void main()
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 gl_Position = mvp * vec4(vec3(in_position), 1.0);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 texcoord = vec2(in_texcoord);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39
675
6be3320a1cb3 Use the same RenderState as PyTouhou in anmrenderer, and normalize color, thanks phaazon!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 656
diff changeset
40 // It’s already normalized from the u8 being passed.
6be3320a1cb3 Use the same RenderState as PyTouhou in anmrenderer, and normalize color, thanks phaazon!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 656
diff changeset
41 color = in_color;
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 "#;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 const FS: &str = r#"
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 in vec2 texcoord;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 in vec4 color;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 uniform sampler2D color_map;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 out vec4 frag_color;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 void main()
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 frag_color = texture(color_map, texcoord) * color;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 "#;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 #[derive(Clone, Copy, Debug, Eq, PartialEq, Semantics)]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 pub enum Semantics {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 #[sem(name = "in_position", repr = "[i16; 3]", wrapper = "VertexPosition")]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 Position,
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 #[sem(name = "in_texcoord", repr = "[f32; 2]", wrapper = "VertexTexcoord")]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 Texcoord,
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 #[sem(name = "in_color", repr = "[u8; 4]", wrapper = "VertexColor")]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 Color,
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 #[repr(C)]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 #[derive(Clone, Copy, Debug, PartialEq, Vertex)]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 #[vertex(sem = "Semantics")]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 struct Vertex {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 pos: VertexPosition,
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 uv: VertexTexcoord,
675
6be3320a1cb3 Use the same RenderState as PyTouhou in anmrenderer, and normalize color, thanks phaazon!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 656
diff changeset
77 #[vertex(normalized = "true")]
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 rgba: VertexColor,
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 #[derive(UniformInterface)]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 struct ShaderInterface {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 // the 'static lifetime acts as “anything” here
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 color_map: Uniform<&'static BoundTexture<'static, Flat, Dim2, Floating>>,
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 #[uniform(name = "mvp")]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 mvp: Uniform<[[f32; 4]; 4]>,
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
90 fn load_file_into_vec(filename: &Path) -> Vec<u8> {
700
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 675
diff changeset
91 let file = File::open(filename).unwrap();
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 675
diff changeset
92 let mut file = BufReader::new(file);
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 675
diff changeset
93 let mut buf = vec![];
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 675
diff changeset
94 file.read_to_end(&mut buf).unwrap();
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 675
diff changeset
95 buf
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 675
diff changeset
96 }
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 675
diff changeset
97
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
98 enum LoadedTexture {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
99 Rgba(Texture<Flat, Dim2, NormRGBA8UI>),
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
100 Rgb(Texture<Flat, Dim2, NormRGB8UI>),
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
101 }
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
102
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 fn main() {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 // Parse arguments.
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 let args: Vec<_> = env::args().collect();
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
106 if args.len() != 3 {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
107 eprintln!("Usage: {} <ANM file> <script number>", args[0]);
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 return;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 }
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
110 let anm_filename = Path::new(&args[1]);
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
111 let script: u8 = args[2].parse().expect("number");
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 // Open the ANM file.
700
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 675
diff changeset
114 let buf = load_file_into_vec(anm_filename);
701
b6c351ca0a35 anm0: return the nom IResult and the list of Anm0s.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 700
diff changeset
115 let (_, mut anms) = Anm0::from_slice(&buf).unwrap();
b6c351ca0a35 anm0: return the nom IResult and the list of Anm0s.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 700
diff changeset
116 let anm0 = anms.pop().unwrap();
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 if !anm0.scripts.contains_key(&script) {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 eprintln!("This anm0 doesn’t contain a script named {}.", script);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 return;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 // Create the sprite.
656
988e5130fb00 Add a simpler Sprite::new() which doesn’t override width/height.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 649
diff changeset
124 let sprite = Rc::new(RefCell::new(Sprite::new()));
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125
646
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 644
diff changeset
126 // TODO: seed this PRNG with a valid seed.
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 644
diff changeset
127 let prng = Rc::new(RefCell::new(Prng::new(0)));
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 644
diff changeset
128
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 // Create the AnmRunner from the ANM and the sprite.
646
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 644
diff changeset
130 let mut anm_runner = AnmRunner::new(&anm0, script, sprite.clone(), Rc::downgrade(&prng), 0);
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 assert_eq!(std::mem::size_of::<Vertex>(), std::mem::size_of::<FakeVertex>());
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 let mut vertices: [Vertex; 4] = unsafe { std::mem::uninitialized() };
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134 fill_vertices(sprite.clone(), &mut vertices);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136 let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap();
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138 // Open the image atlas matching this ANM.
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
139 let png_filename = anm_filename.with_file_name(Path::new(&anm0.png_filename).file_name().unwrap());
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
140 let tex = match anm0.alpha_filename {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
141 Some(ref filename) => {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
142 let alpha_filename = anm_filename.with_file_name(Path::new(filename).file_name().unwrap());
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
143 LoadedTexture::Rgba(png_alpha(&mut surface, &png_filename, &alpha_filename).expect("texture loading"))
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
144 },
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
145 None => {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
146 LoadedTexture::Rgb(load_from_disk(&mut surface, &png_filename).expect("texture loading"))
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
147 }
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
148 };
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 // set the uniform interface to our type so that we can read textures from the shader
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151 let (program, _) =
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 Program::<Semantics, (), ShaderInterface>::from_strings(None, VS, None, FS).expect("program creation");
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 let mut tess = TessBuilder::new(&mut surface)
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 .add_vertices(vertices)
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156 .set_mode(Mode::TriangleFan)
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 .build()
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158 .unwrap();
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160 let mut back_buffer = Framebuffer::back_buffer(surface.size());
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162 'app: loop {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 for event in surface.poll_events() {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164 match event {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165 WindowEvent::Close | WindowEvent::Key(Key::Escape, _, Action::Release, _) => break 'app,
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
166
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 WindowEvent::FramebufferSize(width, height) => {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 back_buffer = Framebuffer::back_buffer([width as u32, height as u32]);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 _ => (),
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
174
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175 {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176 let mut slice = tess
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 .as_slice_mut()
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178 .unwrap();
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
180 anm_runner.run_frame();
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
181 fill_vertices_ptr(sprite.clone(), slice.as_mut_ptr());
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
182 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
183
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
184 // here, we need to bind the pipeline variable; it will enable us to bind the texture to the GPU
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
185 // and use it in the shader
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
186 surface
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
187 .pipeline_builder()
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
188 .pipeline(&back_buffer, [0., 0., 0., 0.], |pipeline, shd_gate| {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
189 // bind our fancy texture to the GPU: it gives us a bound texture we can use with the shader
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
190 let bound_tex = match &tex {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
191 LoadedTexture::Rgb(tex) => pipeline.bind_texture(tex),
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
192 LoadedTexture::Rgba(tex) => pipeline.bind_texture(tex),
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
193 };
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
194
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
195 shd_gate.shade(&program, |rdr_gate, iface| {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
196 // update the texture; strictly speaking, this update doesn’t do much: it just tells the GPU
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
197 // to use the texture passed as argument (no allocation or copy is performed)
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
198 iface.color_map.update(&bound_tex);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
199 //let mvp = ortho_2d(0., 384., 448., 0.);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
200 let proj = perspective(0.5235987755982988, 384. / 448., 101010101./2010101., 101010101./10101.);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
201 let view = setup_camera(0., 0., 1.);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
202 let mvp = view * proj;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
203 //println!("{:#?}", mvp);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
204 // TODO: check how to pass by reference.
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
205 iface.mvp.update(*mvp.borrow_inner());
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
206
675
6be3320a1cb3 Use the same RenderState as PyTouhou in anmrenderer, and normalize color, thanks phaazon!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 656
diff changeset
207 let render_state = RenderState::default()
6be3320a1cb3 Use the same RenderState as PyTouhou in anmrenderer, and normalize color, thanks phaazon!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 656
diff changeset
208 .set_blending((Equation::Additive, Factor::SrcAlpha, Factor::SrcAlphaComplement));
6be3320a1cb3 Use the same RenderState as PyTouhou in anmrenderer, and normalize color, thanks phaazon!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 656
diff changeset
209
6be3320a1cb3 Use the same RenderState as PyTouhou in anmrenderer, and normalize color, thanks phaazon!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 656
diff changeset
210 rdr_gate.render(render_state, |tess_gate| {
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
211 // render the tessellation to the surface the regular way and let the vertex shader’s
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
212 // magic do the rest!
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
213 tess_gate.render(&mut surface, (&tess).into());
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
214 });
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
215 });
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
216 });
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
217
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
218 surface.swap_buffers();
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
219 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
220 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
221
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
222 fn fill_vertices_ptr(sprite: Rc<RefCell<Sprite>>, vertices: *mut Vertex) {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
223 let mut fake_vertices = unsafe { std::mem::transmute::<*mut Vertex, &mut [FakeVertex; 4]>(vertices) };
675
6be3320a1cb3 Use the same RenderState as PyTouhou in anmrenderer, and normalize color, thanks phaazon!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 656
diff changeset
224 sprite.borrow().fill_vertices(&mut fake_vertices, 0., 0., 0.);
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
225 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
226
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
227 fn fill_vertices(sprite: Rc<RefCell<Sprite>>, vertices: &mut [Vertex; 4]) {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
228 let mut fake_vertices = unsafe { std::mem::transmute::<&mut [Vertex; 4], &mut [FakeVertex; 4]>(vertices) };
675
6be3320a1cb3 Use the same RenderState as PyTouhou in anmrenderer, and normalize color, thanks phaazon!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 656
diff changeset
229 sprite.borrow().fill_vertices(&mut fake_vertices, 0., 0., 0.);
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
230 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
231
649
967627181a76 Use the merged name of RGB in AnmRenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 646
diff changeset
232 fn load_from_disk(surface: &mut GlfwSurface, path: &Path) -> Option<Texture<Flat, Dim2, NormRGB8UI>> {
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
233 // load the texture into memory as a whole bloc (i.e. no streaming)
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
234 match image::open(&path) {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
235 Ok(img) => {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
236 let (width, height) = img.dimensions();
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
237 let texels = img
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
238 .pixels()
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
239 .map(|(_x, _y, rgb)| (rgb[0], rgb[1], rgb[2]))
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
240 .collect::<Vec<_>>();
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
241
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
242 // create the luminance texture; the third argument is the number of mipmaps we want (leave it
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
243 // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
244 // shader (we’ll just use the default one)
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
245 let tex =
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
246 Texture::new(surface, [width, height], 0, &Sampler::default()).expect("luminance texture creation");
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
247
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
248 // the first argument disables mipmap generation (we don’t care so far)
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
249 tex.upload(GenMipmaps::No, &texels);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
250
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
251 Some(tex)
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
252 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
253
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
254 Err(e) => {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
255 eprintln!("cannot open image {}: {}", path.display(), e);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
256 None
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
257 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
258 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
259 }
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
260
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
261 fn png_alpha(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Option<Texture<Flat, Dim2, NormRGBA8UI>> {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
262 // load the texture into memory as a whole bloc (i.e. no streaming)
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
263 match image::open(&alpha) {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
264 Ok(img) => {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
265 let (width, height) = img.dimensions();
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
266 let alpha = match img.grayscale() {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
267 DynamicImage::ImageLuma8(img) => img,
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
268 _ => {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
269 eprintln!("cannot convert alpha image {} to grayscale", alpha.display());
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
270 return None;
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
271 }
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
272 };
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
273 let img = match image::open(&rgb) {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
274 Ok(img) => img,
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
275 Err(e) => {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
276 eprintln!("cannot open rgb image {}: {}", rgb.display(), e);
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
277 return None;
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
278 },
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
279 };
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
280 let texels = img
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
281 .pixels()
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
282 .zip(alpha.pixels())
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
283 .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[1], luma[0]))
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
284 .collect::<Vec<_>>();
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
285
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
286 // create the luminance texture; the third argument is the number of mipmaps we want (leave it
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
287 // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
288 // shader (we’ll just use the default one)
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
289 let tex =
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
290 Texture::new(surface, [width, height], 0, &Sampler::default()).expect("luminance texture creation");
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
291
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
292 // the first argument disables mipmap generation (we don’t care so far)
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
293 tex.upload(GenMipmaps::No, &texels);
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
294
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
295 Some(tex)
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
296 }
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
297
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
298 Err(e) => {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
299 eprintln!("cannot open alpha image {}: {}", alpha.display(), e);
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
300 None
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
301 }
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
302 }
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
303 }