annotate runners/src/bin/anmrenderer.rs @ 760:eba9a3d0c484

formats: Fix the tests.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 05 Jan 2021 04:28:45 +0100
parents 21b186be2590
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
1 use luminance::blending::{Equation, Factor};
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 use luminance::context::GraphicsContext;
742
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
3 use luminance::pipeline::{BoundTexture, PipelineState};
708
3954801b6299 examples: Update to luminance 0.32.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 707
diff changeset
4 use luminance::pixel::NormUnsigned;
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 use luminance::render_state::RenderState;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 use luminance::shader::program::{Program, Uniform};
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 use luminance::tess::{Mode, TessBuilder};
753
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
8 use luminance::texture::Dim2;
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 use luminance_derive::{Semantics, Vertex, UniformInterface};
707
987409d48991 Switch to versioned luminance now that it got released, not hardcoded paths.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 706
diff changeset
10 use luminance_glfw::{Action, Key, WindowEvent, GlfwSurface, Surface, WindowDim, WindowOpt};
757
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
11 use touhou_formats::th06::anm0::Anm0;
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
12 use touhou_interpreters::th06::anm0::{AnmRunner, Sprite, Vertex as FakeVertex};
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
13 use touhou_utils::math::{perspective, setup_camera};
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
14 use touhou_utils::prng::Prng;
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 use std::cell::RefCell;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 use std::rc::Rc;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 use std::env;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 use std::path::Path;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19
757
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
20 use touhou_runners::common::{load_file_into_vec, load_anm_image, LoadedTexture};
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
21
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 const VS: &str = r#"
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 in ivec3 in_position;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 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
25 in vec4 in_color;
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 uniform mat4 mvp;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 out vec2 texcoord;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 out vec4 color;
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 void main()
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 gl_Position = mvp * vec4(vec3(in_position), 1.0);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 texcoord = vec2(in_texcoord);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36
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
37 // 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
38 color = in_color;
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 "#;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 const FS: &str = r#"
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 in vec2 texcoord;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 in vec4 color;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 uniform sampler2D color_map;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 out vec4 frag_color;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 void main()
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 frag_color = texture(color_map, texcoord) * color;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 }
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
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 #[derive(Clone, Copy, Debug, Eq, PartialEq, Semantics)]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 pub enum Semantics {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 #[sem(name = "in_position", repr = "[i16; 3]", wrapper = "VertexPosition")]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 Position,
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 #[sem(name = "in_texcoord", repr = "[f32; 2]", wrapper = "VertexTexcoord")]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 Texcoord,
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_color", repr = "[u8; 4]", wrapper = "VertexColor")]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 Color,
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
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 #[repr(C)]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 #[derive(Clone, Copy, Debug, PartialEq, Vertex)]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 #[vertex(sem = "Semantics")]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 struct Vertex {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 pos: VertexPosition,
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 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
74 #[vertex(normalized = "true")]
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 rgba: VertexColor,
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 #[derive(UniformInterface)]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 struct ShaderInterface {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 // the 'static lifetime acts as “anything” here
753
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
81 color_map: Uniform<&'static BoundTexture<'static, Dim2, NormUnsigned>>,
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 #[uniform(name = "mvp")]
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 mvp: Uniform<[[f32; 4]; 4]>,
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
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
87 fn fill_vertices_ptr(sprite: Rc<RefCell<Sprite>>, vertices: *mut Vertex) {
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
88 let mut fake_vertices = unsafe { std::mem::transmute::<*mut Vertex, &mut [FakeVertex; 4]>(vertices) };
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
89 sprite.borrow().fill_vertices(&mut fake_vertices, 0., 0., 0.);
700
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 675
diff changeset
90 }
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 675
diff changeset
91
705
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
92 fn fill_vertices(sprite: Rc<RefCell<Sprite>>, vertices: &mut [Vertex; 4]) {
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
93 let mut fake_vertices = unsafe { std::mem::transmute::<&mut [Vertex; 4], &mut [FakeVertex; 4]>(vertices) };
ed65f9412bc0 anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
94 sprite.borrow().fill_vertices(&mut fake_vertices, 0., 0., 0.);
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
95 }
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
96
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 fn main() {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 // Parse arguments.
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 let args: Vec<_> = env::args().collect();
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
100 if args.len() != 3 {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
101 eprintln!("Usage: {} <ANM file> <script number>", args[0]);
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 return;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 }
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
104 let anm_filename = Path::new(&args[1]);
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
105 let script: u8 = args[2].parse().expect("number");
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 // Open the ANM file.
752
5e5e7136ac92 examples: Return a Result for load_file_into_vec()
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 743
diff changeset
108 let buf = load_file_into_vec(anm_filename).unwrap();
701
b6c351ca0a35 anm0: return the nom IResult and the list of Anm0s.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 700
diff changeset
109 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
110 let anm0 = anms.pop().unwrap();
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 if !anm0.scripts.contains_key(&script) {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 eprintln!("This anm0 doesn’t contain a script named {}.", script);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 return;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117 // 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
118 let sprite = Rc::new(RefCell::new(Sprite::new()));
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119
646
7d92730bf543 Add a PRNG and use it for anm0 instruction 16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 644
diff changeset
120 // 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
121 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
122
742
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
123 let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap();
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
124
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
125 // Open the image atlas matching this ANM.
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
126 let tex = load_anm_image(&mut surface, &anm0, anm_filename).expect("image loading");
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
127
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 // Create the AnmRunner from the ANM and the sprite.
742
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
129 let anms = Rc::new(RefCell::new([anm0]));
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
130 let mut anm_runner = AnmRunner::new(anms, 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>());
743
0fed350d1778 examples: Remove deprecated std::mem::uninitialized().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 742
diff changeset
133 let mut vertices: [Vertex; 4] = {
0fed350d1778 examples: Remove deprecated std::mem::uninitialized().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 742
diff changeset
134 let data = std::mem::MaybeUninit::uninit();
0fed350d1778 examples: Remove deprecated std::mem::uninitialized().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 742
diff changeset
135 unsafe { data.assume_init() }
0fed350d1778 examples: Remove deprecated std::mem::uninitialized().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 742
diff changeset
136 };
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137 fill_vertices(sprite.clone(), &mut vertices);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139 // set the uniform interface to our type so that we can read textures from the shader
715
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
140 let program =
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
141 Program::<Semantics, (), ShaderInterface>::from_strings(None, VS, None, FS).expect("program creation").ignore_warnings();
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143 let mut tess = TessBuilder::new(&mut surface)
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 .add_vertices(vertices)
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 .set_mode(Mode::TriangleFan)
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 .build()
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 .unwrap();
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148
715
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
149 let mut back_buffer = surface.back_buffer().unwrap();
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
150 let mut resize = false;
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 'app: loop {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 for event in surface.poll_events() {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 match event {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 WindowEvent::Close | WindowEvent::Key(Key::Escape, _, Action::Release, _) => break 'app,
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156
715
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
157 WindowEvent::FramebufferSize(..) => {
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
158 resize = true;
643
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
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 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164
715
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
165 if resize {
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
166 back_buffer = surface.back_buffer().unwrap();
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
167 resize = false;
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
168 }
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
169
643
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 let mut slice = tess
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 .as_slice_mut()
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173 .unwrap();
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 anm_runner.run_frame();
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176 fill_vertices_ptr(sprite.clone(), slice.as_mut_ptr());
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179 // 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
180 // and use it in the shader
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
181 surface
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
182 .pipeline_builder()
742
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
183 .pipeline(&back_buffer, &PipelineState::default(), |pipeline, mut shd_gate| {
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
184 // 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
185 let bound_tex = match &tex {
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
186 LoadedTexture::Rgb(tex) => pipeline.bind_texture(tex),
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
187 LoadedTexture::Rgba(tex) => pipeline.bind_texture(tex),
753
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
188 LoadedTexture::RgbaArray(tex) => unreachable!(),
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
189 };
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
190
715
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
191 shd_gate.shade(&program, |iface, mut rdr_gate| {
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
192 // 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
193 // 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
194 iface.color_map.update(&bound_tex);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
195 //let mvp = ortho_2d(0., 384., 448., 0.);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
196 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
197 let view = setup_camera(0., 0., 1.);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
198 let mvp = view * proj;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
199 //println!("{:#?}", mvp);
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
200 // TODO: check how to pass by reference.
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
201 iface.mvp.update(*mvp.borrow_inner());
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
202
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
203 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
204 .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
205
742
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
206 rdr_gate.render(&render_state, |mut tess_gate| {
715
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
207 tess_gate.render(&tess);
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
208 });
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
209 });
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
210 });
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
211
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
212 surface.swap_buffers();
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
213 }
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
214 }