comparison examples/stdrenderer.rs @ 742:0a250ddfae79

examples: Update luminance.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 07 Jan 2020 00:23:15 +0100
parents d5d5496e4e53
children 0fed350d1778
comparison
equal deleted inserted replaced
741:3555845f8cf4 742:0a250ddfae79
1 use luminance::blending::{Equation, Factor}; 1 use luminance::blending::{Equation, Factor};
2 use luminance::context::GraphicsContext; 2 use luminance::context::GraphicsContext;
3 use luminance::pipeline::BoundTexture; 3 use luminance::pipeline::{BoundTexture, PipelineState};
4 use luminance::pixel::NormUnsigned; 4 use luminance::pixel::NormUnsigned;
5 use luminance::render_state::RenderState; 5 use luminance::render_state::RenderState;
6 use luminance::shader::program::{Program, Uniform}; 6 use luminance::shader::program::{Program, Uniform};
7 use luminance::tess::{Mode, TessBuilder, TessSliceIndex}; 7 use luminance::tess::{Mode, TessBuilder, TessSliceIndex};
8 use luminance::texture::{Dim2, Flat}; 8 use luminance::texture::{Dim2, Flat};
127 let anm0 = anms.pop().unwrap(); 127 let anm0 = anms.pop().unwrap();
128 128
129 // TODO: seed this PRNG with a valid seed. 129 // TODO: seed this PRNG with a valid seed.
130 let prng = Rc::new(RefCell::new(Prng::new(0))); 130 let prng = Rc::new(RefCell::new(Prng::new(0)));
131 131
132 let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap();
133
134 // Open the image atlas matching this ANM.
135 let tex = load_anm_image(&mut surface, &anm0, anm_filename).expect("image loading");
136
132 assert_eq!(std::mem::size_of::<Vertex>(), std::mem::size_of::<FakeVertex>()); 137 assert_eq!(std::mem::size_of::<Vertex>(), std::mem::size_of::<FakeVertex>());
133 let mut vertices: Vec<Vertex> = vec![]; 138 let mut vertices: Vec<Vertex> = vec![];
134 let mut indices = vec![]; 139 let mut indices = vec![];
135 140
136 { 141 {
142 let anms = Rc::new(RefCell::new([anm0]));
137 for model in stage.models.iter() { 143 for model in stage.models.iter() {
138 let begin = vertices.len(); 144 let begin = vertices.len();
139 for quad in model.quads.iter() { 145 for quad in model.quads.iter() {
140 let Position { x, y, z } = quad.pos; 146 let Position { x, y, z } = quad.pos;
141 let Box2D { width, height } = quad.size_override; 147 let Box2D { width, height } = quad.size_override;
142 148
143 // Create the AnmRunner from the ANM and the sprite. 149 // Create the AnmRunner from the ANM and the sprite.
144 let sprite = Rc::new(RefCell::new(Sprite::with_size(width, height))); 150 let sprite = Rc::new(RefCell::new(Sprite::with_size(width, height)));
145 let _anm_runner = AnmRunner::new(&anm0, quad.anm_script as u8, sprite.clone(), Rc::downgrade(&prng), 0); 151 let _anm_runner = AnmRunner::new(anms.clone(), quad.anm_script as u8, sprite.clone(), Rc::downgrade(&prng), 0);
146 let mut new_vertices: [Vertex; 6] = unsafe { std::mem::uninitialized() }; 152 let mut new_vertices: [Vertex; 6] = unsafe { std::mem::uninitialized() };
147 fill_vertices(sprite.clone(), &mut new_vertices, x, y, z); 153 fill_vertices(sprite.clone(), &mut new_vertices, x, y, z);
148 new_vertices[4] = new_vertices[0]; 154 new_vertices[4] = new_vertices[0];
149 new_vertices[5] = new_vertices[2]; 155 new_vertices[5] = new_vertices[2];
150 vertices.extend(&new_vertices); 156 vertices.extend(&new_vertices);
154 } 160 }
155 } 161 }
156 162
157 let mut stage_runner = StageRunner::new(Rc::new(RefCell::new(stage))); 163 let mut stage_runner = StageRunner::new(Rc::new(RefCell::new(stage)));
158 164
159 let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap();
160
161 // Open the image atlas matching this ANM.
162 let tex = load_anm_image(&mut surface, &anm0, anm_filename).expect("image loading");
163
164 // set the uniform interface to our type so that we can read textures from the shader 165 // set the uniform interface to our type so that we can read textures from the shader
165 let program = 166 let program =
166 Program::<Semantics, (), ShaderInterface>::from_strings(None, VS, None, FS).expect("program creation").ignore_warnings(); 167 Program::<Semantics, (), ShaderInterface>::from_strings(None, VS, None, FS).expect("program creation").ignore_warnings();
167 168
168 let tess = TessBuilder::new(&mut surface) 169 let tess = TessBuilder::new(&mut surface)
200 201
201 // here, we need to bind the pipeline variable; it will enable us to bind the texture to the GPU 202 // here, we need to bind the pipeline variable; it will enable us to bind the texture to the GPU
202 // and use it in the shader 203 // and use it in the shader
203 surface 204 surface
204 .pipeline_builder() 205 .pipeline_builder()
205 .pipeline(&back_buffer, [0., 0., 0., 0.], |pipeline, mut shd_gate| { 206 .pipeline(&back_buffer, &PipelineState::default(), |pipeline, mut shd_gate| {
206 // bind our fancy texture to the GPU: it gives us a bound texture we can use with the shader 207 // bind our fancy texture to the GPU: it gives us a bound texture we can use with the shader
207 let bound_tex = match &tex { 208 let bound_tex = match &tex {
208 LoadedTexture::Rgb(tex) => pipeline.bind_texture(tex), 209 LoadedTexture::Rgb(tex) => pipeline.bind_texture(tex),
209 LoadedTexture::Rgba(tex) => pipeline.bind_texture(tex), 210 LoadedTexture::Rgba(tex) => pipeline.bind_texture(tex),
210 }; 211 };
231 232
232 let stage = stage_runner.stage.borrow(); 233 let stage = stage_runner.stage.borrow();
233 for instance in stage.instances.iter() { 234 for instance in stage.instances.iter() {
234 iface.instance_position.update([instance.pos.x, instance.pos.y, instance.pos.z]); 235 iface.instance_position.update([instance.pos.x, instance.pos.y, instance.pos.z]);
235 236
236 rdr_gate.render(render_state, |mut tess_gate| { 237 rdr_gate.render(&render_state, |mut tess_gate| {
237 let (begin, end) = indices[instance.id as usize]; 238 let (begin, end) = indices[instance.id as usize];
238 tess_gate.render(tess.slice(begin..end)); 239 tess_gate.render(tess.slice(begin..end));
239 }); 240 });
240 } 241 }
241 }); 242 });