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