annotate examples/stdrenderer.rs @ 702:718348c7608e

anm0: simplify parsing with more combinators.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 23 Aug 2019 17:22:38 +0200
parents b6c351ca0a35
children bca515da9047
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
679
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 use image::GenericImageView;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 use luminance::context::GraphicsContext;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 use luminance::framebuffer::Framebuffer;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 use luminance::pipeline::BoundTexture;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 use luminance::pixel::{NormRGB8UI, Floating};
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 use luminance::render_state::RenderState;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 use luminance::shader::program::{Program, Uniform};
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 use luminance::tess::{Mode, TessBuilder, TessSliceIndex};
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 use luminance::texture::{Dim2, Flat, Sampler, Texture, GenMipmaps};
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 use luminance_derive::{Semantics, Vertex, UniformInterface};
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 use luminance_glfw::event::{Action, Key, WindowEvent};
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 use luminance_glfw::surface::{GlfwSurface, Surface, WindowDim, WindowOpt};
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 use touhou::th06::anm0::Anm0;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 use touhou::th06::anm0_vm::{AnmRunner, Sprite, Vertex as FakeVertex};
680
ef2dbd676a91 Add missing quad size override to stdrenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 679
diff changeset
15 use touhou::th06::std::{Stage, Position, Box2D};
679
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 use touhou::th06::std_vm::StageRunner;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 use touhou::util::prng::Prng;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 use touhou::util::math::perspective;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 use std::cell::RefCell;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 use std::fs::File;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 use std::io::{BufReader, Read};
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 use std::rc::Rc;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 use std::env;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 use std::path::Path;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 const VS: &str = r#"
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 in ivec3 in_position;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 in vec2 in_texcoord;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 in uvec4 in_color;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 uniform mat4 mvp;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 uniform vec3 instance_position;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 out vec2 texcoord;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 out vec4 color;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 void main()
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 vec3 position = vec3(in_position) + instance_position;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 gl_Position = mvp * vec4(position, 1.0);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 texcoord = vec2(in_texcoord);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 // Normalized from the u8 being passed.
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 color = vec4(in_color) / 255.;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 "#;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 const FS: &str = r#"
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 in vec2 texcoord;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 in vec4 color;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 uniform sampler2D color_map;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 uniform float fog_scale;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 uniform float fog_end;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 uniform vec4 fog_color;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 out vec4 frag_color;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 void main()
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 vec4 temp_color = texture(color_map, texcoord) * color;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 float depth = gl_FragCoord.z / gl_FragCoord.w;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 float fog_density = clamp((fog_end - depth) * fog_scale, 0.0, 1.0);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 frag_color = vec4(mix(fog_color, temp_color, fog_density).rgb, temp_color.a);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 "#;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 #[derive(Clone, Copy, Debug, Eq, PartialEq, Semantics)]
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 pub enum Semantics {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 #[sem(name = "in_position", repr = "[i16; 3]", wrapper = "VertexPosition")]
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 Position,
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 #[sem(name = "in_texcoord", repr = "[f32; 2]", wrapper = "VertexTexcoord")]
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 Texcoord,
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 #[sem(name = "in_color", repr = "[u8; 4]", wrapper = "VertexColor")]
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 Color,
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 #[repr(C)]
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 #[derive(Clone, Copy, Debug, PartialEq, Vertex)]
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 #[vertex(sem = "Semantics")]
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 struct Vertex {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 pos: VertexPosition,
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 uv: VertexTexcoord,
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 rgba: VertexColor,
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 #[derive(UniformInterface)]
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 struct ShaderInterface {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 // the 'static lifetime acts as “anything” here
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 color_map: Uniform<&'static BoundTexture<'static, Flat, Dim2, Floating>>,
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 #[uniform(name = "mvp")]
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 mvp: Uniform<[[f32; 4]; 4]>,
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 #[uniform(name = "instance_position")]
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 instance_position: Uniform<[f32; 3]>,
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 #[uniform(name = "fog_scale")]
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 fog_scale: Uniform<f32>,
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 #[uniform(name = "fog_end")]
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 fog_end: Uniform<f32>,
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 #[uniform(name = "fog_color")]
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 fog_color: Uniform<[f32; 4]>,
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109
700
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 680
diff changeset
110 fn load_file_into_vec(filename: &str) -> Vec<u8> {
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 680
diff changeset
111 let file = File::open(filename).unwrap();
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 680
diff changeset
112 let mut file = BufReader::new(file);
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 680
diff changeset
113 let mut buf = vec![];
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 680
diff changeset
114 file.read_to_end(&mut buf).unwrap();
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 680
diff changeset
115 buf
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 680
diff changeset
116 }
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 680
diff changeset
117
679
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 fn main() {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 // Parse arguments.
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 let args: Vec<_> = env::args().collect();
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 if args.len() != 4 {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122 eprintln!("Usage: {} <STD file> <ANM file> <PNG file>", args[0]);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 return;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125 let std_filename = &args[1];
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 let anm_filename = &args[2];
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 let png_filename = &args[3];
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 // Open the STD file.
700
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 680
diff changeset
130 let buf = load_file_into_vec(std_filename);
679
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 let (_, stage) = Stage::from_slice(&buf).unwrap();
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 // Open the ANM file.
700
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 680
diff changeset
134 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
135 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
136 let anm0 = anms.pop().unwrap();
679
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138 // TODO: seed this PRNG with a valid seed.
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139 let prng = Rc::new(RefCell::new(Prng::new(0)));
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141 assert_eq!(std::mem::size_of::<Vertex>(), std::mem::size_of::<FakeVertex>());
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142 let mut vertices: Vec<Vertex> = vec![];
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143 let mut indices = vec![];
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 for model in stage.models.iter() {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 let begin = vertices.len();
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 for quad in model.quads.iter() {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149 let Position { x, y, z } = quad.pos;
680
ef2dbd676a91 Add missing quad size override to stdrenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 679
diff changeset
150 let Box2D { width, height } = quad.size_override;
679
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 // Create the AnmRunner from the ANM and the sprite.
680
ef2dbd676a91 Add missing quad size override to stdrenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 679
diff changeset
153 let sprite = Rc::new(RefCell::new(Sprite::with_size(width, height)));
679
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 let _anm_runner = AnmRunner::new(&anm0, quad.anm_script as u8, sprite.clone(), Rc::downgrade(&prng), 0);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 let mut new_vertices: [Vertex; 6] = unsafe { std::mem::uninitialized() };
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156 fill_vertices(sprite.clone(), &mut new_vertices, x, y, z);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 new_vertices[4] = new_vertices[0];
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158 new_vertices[5] = new_vertices[2];
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159 vertices.extend(&new_vertices);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 let end = vertices.len();
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162 indices.push((begin, end));
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
166 let mut stage_runner = StageRunner::new(Rc::new(RefCell::new(stage)));
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap();
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170 // Open the image atlas matching this ANM.
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 let tex = load_from_disk(&mut surface, Path::new(png_filename)).expect("texture loading");
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173 // set the uniform interface to our type so that we can read textures from the shader
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
174 let (program, _) =
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175 Program::<Semantics, (), ShaderInterface>::from_strings(None, VS, None, FS).expect("program creation");
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 let tess = TessBuilder::new(&mut surface)
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178 .add_vertices(vertices)
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179 .set_mode(Mode::Triangle)
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
180 .build()
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
181 .unwrap();
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
182
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
183 let mut back_buffer = Framebuffer::back_buffer(surface.size());
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
184
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
185 'app: loop {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
186 for event in surface.poll_events() {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
187 match event {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
188 WindowEvent::Close | WindowEvent::Key(Key::Escape, _, Action::Release, _) => break 'app,
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
189
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
190 WindowEvent::FramebufferSize(width, height) => {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
191 back_buffer = Framebuffer::back_buffer([width as u32, height as u32]);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
192 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
193
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
194 _ => (),
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
195 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
196 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
197
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
198 {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
199 stage_runner.run_frame();
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
200 //let sprites = stage.get_sprites();
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
201 //fill_vertices_ptr(sprites, slice.as_mut_ptr());
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
202 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
203
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
204 // here, we need to bind the pipeline variable; it will enable us to bind the texture to the GPU
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
205 // and use it in the shader
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
206 surface
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
207 .pipeline_builder()
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
208 .pipeline(&back_buffer, [0., 0., 0., 0.], |pipeline, shd_gate| {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
209 // bind our fancy texture to the GPU: it gives us a bound texture we can use with the shader
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
210 let bound_tex = pipeline.bind_texture(&tex);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
211
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
212 shd_gate.shade(&program, |rdr_gate, iface| {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
213 // update the texture; strictly speaking, this update doesn’t do much: it just tells the GPU
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
214 // to use the texture passed as argument (no allocation or copy is performed)
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
215 iface.color_map.update(&bound_tex);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
216
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
217 let proj = perspective(0.5235987755982988, 384. / 448., 101010101./2010101., 101010101./10101.);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
218 let model_view = stage_runner.get_model_view();
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
219 let mvp = model_view * proj;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
220 // TODO: check how to pass by reference.
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
221 iface.mvp.update(*mvp.borrow_inner());
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
222
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
223 let near = stage_runner.fog_near - 101010101. / 2010101.;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
224 let far = stage_runner.fog_far - 101010101. / 2010101.;
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
225 iface.fog_color.update(stage_runner.fog_color);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
226 iface.fog_scale.update(1. / (far - near));
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
227 iface.fog_end.update(far);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
228
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
229 let stage = stage_runner.stage.borrow();
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
230 for instance in stage.instances.iter() {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
231 iface.instance_position.update([instance.pos.x, instance.pos.y, instance.pos.z]);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
232
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
233 rdr_gate.render(RenderState::default(), |tess_gate| {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
234 let (begin, end) = indices[instance.id as usize];
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
235 tess_gate.render(&mut surface, tess.slice(begin..end));
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
236 });
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
237 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
238 });
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
239 });
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
240
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
241 surface.swap_buffers();
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
242 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
243 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
244
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
245 fn fill_vertices(sprite: Rc<RefCell<Sprite>>, vertices: &mut [Vertex; 6], x: f32, y: f32, z: f32) {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
246 let mut fake_vertices = unsafe { std::mem::transmute::<&mut [Vertex; 6], &mut [FakeVertex; 4]>(vertices) };
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
247 let sprite = sprite.borrow();
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
248 sprite.fill_vertices(&mut fake_vertices, x, y, z);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
249 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
250
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
251 fn load_from_disk(surface: &mut GlfwSurface, path: &Path) -> Option<Texture<Flat, Dim2, NormRGB8UI>> {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
252 // load the texture into memory as a whole bloc (i.e. no streaming)
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
253 match image::open(&path) {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
254 Ok(img) => {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
255 let (width, height) = img.dimensions();
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
256 let texels = img
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
257 .pixels()
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
258 .map(|(_x, _y, rgb)| (rgb[0], rgb[1], rgb[2]))
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
259 .collect::<Vec<_>>();
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
260
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
261 // create the luminance texture; the third argument is the number of mipmaps we want (leave it
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
262 // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
263 // shader (we’ll just use the default one)
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
264 let tex =
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
265 Texture::new(surface, [width, height], 0, &Sampler::default()).expect("luminance texture creation");
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
266
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
267 // the first argument disables mipmap generation (we don’t care so far)
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
268 tex.upload(GenMipmaps::No, &texels);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
269
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
270 Some(tex)
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
271 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
272
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
273 Err(e) => {
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
274 eprintln!("cannot open image {}: {}", path.display(), e);
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
275 None
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
276 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
277 }
6020e33d4fc4 Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
278 }