Mercurial > touhou
diff examples/stagerunner.rs @ 753:a662dddd4a2b
examples: Use array textures for enemy PNGs
This requires luminance 0.39.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 25 Feb 2020 21:03:44 +0100 |
parents | 5e5e7136ac92 |
children |
line wrap: on
line diff
--- a/examples/stagerunner.rs +++ b/examples/stagerunner.rs @@ -5,7 +5,7 @@ use luminance::pixel::NormUnsigned; use luminance::render_state::RenderState; use luminance::shader::program::{Program, Uniform}; use luminance::tess::{Mode, TessBuilder}; -use luminance::texture::{Dim2, Flat}; +use luminance::texture::Dim2Array; use luminance_derive::{Semantics, Vertex, UniformInterface}; use luminance_glfw::{Action, Key, WindowEvent, GlfwSurface, Surface, WindowDim, WindowOpt}; use touhou::th06::anm0::Anm0; @@ -22,15 +22,17 @@ use std::path::Path; #[path = "common.rs"] mod common; -use common::{load_file_into_vec, load_anm_image, LoadedTexture}; +use common::{load_file_into_vec, load_multiple_anm_images, LoadedTexture}; const VS: &str = r#" in ivec3 in_position; +in uint in_layer; in vec2 in_texcoord; in uvec4 in_color; uniform mat4 mvp; +flat out uint layer; out vec2 texcoord; out vec4 color; @@ -41,20 +43,23 @@ void main() // Normalized from the u8 being passed. color = vec4(in_color) / 255.; + + layer = in_layer; } "#; const FS: &str = r#" +flat in uint layer; in vec2 texcoord; in vec4 color; -uniform sampler2D color_map; +uniform sampler2DArray color_map; out vec4 frag_color; void main() { - frag_color = texture(color_map, texcoord) * color; + frag_color = texture(color_map, vec3(texcoord, layer)) * color; } "#; @@ -63,6 +68,9 @@ pub enum Semantics { #[sem(name = "in_position", repr = "[i16; 3]", wrapper = "VertexPosition")] Position, + #[sem(name = "in_layer", repr = "u16", wrapper = "VertexLayer")] + Layer, + #[sem(name = "in_texcoord", repr = "[f32; 2]", wrapper = "VertexTexcoord")] Texcoord, @@ -75,6 +83,7 @@ pub enum Semantics { #[vertex(sem = "Semantics")] struct Vertex { pos: VertexPosition, + layer: VertexLayer, uv: VertexTexcoord, rgba: VertexColor, } @@ -82,7 +91,7 @@ struct Vertex { #[derive(UniformInterface)] struct ShaderInterface { // the 'static lifetime acts as “anything” here - color_map: Uniform<&'static BoundTexture<'static, Flat, Dim2, NormUnsigned>>, + color_map: Uniform<&'static BoundTexture<'static, Dim2Array, NormUnsigned>>, #[uniform(name = "mvp")] mvp: Uniform<[[f32; 4]; 4]>, @@ -136,15 +145,8 @@ fn main() { let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap(); // Open the image atlas matching this ANM. - let mut textures = vec![]; - for anm0 in anms.iter() { - let tex = load_anm_image(&mut surface, &anm0, &anm_filename).expect("image loading"); - textures.push(tex); - } - + let tex = load_multiple_anm_images(&mut surface, &anms, &anm_filename).expect("image loading"); let anms = Rc::new(RefCell::new(anms)); - let tex = textures.pop().unwrap(); - let tex = textures.pop().unwrap(); // set the uniform interface to our type so that we can read textures from the shader let program = @@ -209,8 +211,9 @@ fn main() { .pipeline(&back_buffer, &PipelineState::default(), |pipeline, mut shd_gate| { // bind our fancy texture to the GPU: it gives us a bound texture we can use with the shader let bound_tex = match &tex { - LoadedTexture::Rgb(tex) => pipeline.bind_texture(tex), - LoadedTexture::Rgba(tex) => pipeline.bind_texture(tex), + LoadedTexture::Rgb(tex) => unreachable!(), + LoadedTexture::Rgba(tex) => unreachable!(), + LoadedTexture::RgbaArray(tex) => pipeline.bind_texture(tex), }; shd_gate.shade(&program, |iface, mut rdr_gate| { @@ -226,6 +229,7 @@ fn main() { iface.mvp.update(*mvp.borrow_inner()); let render_state = RenderState::default() + .set_depth_test(None) .set_blending((Equation::Additive, Factor::SrcAlpha, Factor::SrcAlphaComplement)); rdr_gate.render(&render_state, |mut tess_gate| {