gdritter repos chenoska / 2068804
Draw the upper and lower stuff in two VBOs Getty Ritter 5 years ago
5 changed file(s) with 130 addition(s) and 70 deletion(s). Collapse all Expand all
1414
1515 float l = max(dot(light_dir, normalize(normal)), 0.0);
1616 vec3 col = vec3(pixel) * pow(l, 4.0);
17 if (pixel.a < 0.1) {
18 discard;
19 }
1720 out_color = vec4(col, 1.0);
1821 }
2121 fn passable(&self) -> bool {
2222 true
2323 }
24 }
25
26 #[derive(PartialEq, Eq, Debug)]
27 pub struct Esse {
28 pub sprite: Coord,
2429 }
2530
2631 #[derive(Debug)]
7883
7984 /// Get the set of entities present at the board location that
8085 /// this entity is currently looking at
81 fn get_focus<'a>(&self, b: &'a Board) -> &'a Vec<Entity> {
86 fn get_focus<'a>(&self, b: &'a Board) -> &'a Vec<Esse> {
8287 b.get_entity(self.game_coords())
8388 }
8489
208213 #[derive(Debug)]
209214 pub struct Board {
210215 base: Grid<Tile>,
211 entity: Grid<Vec<Entity>>,
216 entity: Grid<Vec<Esse>>,
212217 }
213218
214219
221226 &mut self.base
222227 }
223228
224 pub fn entities(&mut self) -> &mut Grid<Vec<Entity>> {
229 pub fn esses(&mut self) -> &mut Grid<Vec<Esse>> {
225230 &mut self.entity
226231 }
227232
228 pub fn get_entity(&self, c: Coord) -> &Vec<Entity> {
233 pub fn get_entity(&self, c: Coord) -> &Vec<Esse> {
229234 &self.entity[c]
230235 }
231236
250255 });
251256
252257 let entity = Grid::new_with(w, h, |x, y| {
253 let _o1 = map.layers[1].tiles[y as usize][x as usize];
254 let _o2 = map.layers[1].tiles[y as usize][x as usize];
255 vec![
256 Entity::blank(),
257 Entity::blank(),
258 ]
258 let o1 = map.layers[1].tiles[y as usize][x as usize];
259 let o2 = map.layers[2].tiles[y as usize][x as usize];
260 let mut v = Vec::new();
261 if let Some(coord) = offset_from_gid(o1) {
262 v.push(Esse { sprite: coord });
263 }
264 if let Some(coord) = offset_from_gid(o2) {
265 v.push(Esse { sprite: coord });
266 }
267 v
259268 });
260269 Board { base, entity }
261270 }
269278 }
270279 }
271280 }
272 }
281
282 pub fn each_esse<F>(&self, mut f: F)
283 where F: FnMut(usize, usize, &Esse)
284 {
285 for x in 0..self.base.width {
286 for y in 0..self.base.height {
287 for e in self.entity[Pair::new(x, y)].iter() {
288 f(x as usize, y as usize, &e);
289 }
290 }
291 }
292 }
293 }
1616 /// Clear the screen to a generic color
1717 pub fn clear() {
1818 unsafe {
19 gl::Enable(gl::BLEND);
20 gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
1921 gl::ClearColor(0.3, 0.3, 0.3, 1.0);
2022 gl::Clear(gl::COLOR_BUFFER_BIT);
2123 }
4042 unsafe { gl_window.make_current() }?;
4143
4244 gl::load_with(|symbol| gl_window.get_proc_address(symbol) as *const _);
43
4445 Ok(Window { events, gl_window })
4546 }
4647
233234 &self,
234235 s: &str,
235236 tex: &Texture,
236 n: i32,
237 ) -> Result<(), Error>{
238 unsafe {
239 gl::ActiveTexture(match n {
240 0 => gl::TEXTURE0,
241 1 => gl::TEXTURE1,
242 2 => gl::TEXTURE2,
243 _ => panic!("too many textures, i guess"),
237 n: u32,
238 ) -> Result<(), Error> {
239 unsafe {
240 gl::ActiveTexture(if n < gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS {
241 gl::TEXTURE0 + n
242 } else {
243 panic!("Texture unit {} too high", n)
244244 });
245245 gl::BindTexture(gl::TEXTURE_2D, tex.idx);
246246 let t = gl::GetUniformLocation(
247247 self.p,
248248 ffi::CString::new(s)?.as_ptr(),
249249 );
250 gl::Uniform1i(t, n);
250 gl::Uniform1i(t, n as i32);
251251 }
252252 Ok(())
253253 }
119119 let y = h as f32 - y as f32 - 1.0;
120120 let tx = (t.sprite.x as f32) * o_stride;
121121 let ty = (t.sprite.y as f32) * o_stride;
122 vec.extend(vec![
123 V2D {
124 x: x_stride * (x + 1.0) - 1.0,
125 y: y_stride * y - 1.0,
126 u: tx + o_stride,
127 v: ty + o_stride,
128 },
129 V2D {
130 x: x_stride * x - 1.0,
131 y: y_stride * y - 1.0,
132 u: tx,
133 v: ty + o_stride,
134 },
135 V2D {
136 x: x_stride * (x + 1.0) - 1.0,
137 y: y_stride * (y + 1.0) - 1.0,
138 u: tx + o_stride,
139 v: ty,
140 },
141
142 V2D {
143 x: x_stride * x - 1.0,
144 y: y_stride * y - 1.0,
145 u: tx,
146 v: ty + o_stride,
147 },
148 V2D {
149 x: x_stride * (x + 1.0) - 1.0,
150 y: y_stride * (y + 1.0) - 1.0,
151 u: tx + o_stride,
152 v: ty,
153 },
154 V2D {
155 x: x_stride * x - 1.0,
156 y: y_stride * (y + 1.0) - 1.0,
157 u: tx,
158 v: ty,
159 },
160 ]);
122 vec.extend(mk_quad((x, y), (x_stride, y_stride), (tx, ty), o_stride));
161123 });
162124 vec
163125 }
126
127
128 pub fn make_upper_grid_with(w: usize, h: usize, b: &board::Board) -> Vec<V2D> {
129 let mut vec = Vec::with_capacity(w * h * 4);
130 let x_stride = 2.0 / w as f32;
131 let y_stride = 2.0 / h as f32;
132 let o_stride = 1.0 / 32.0;
133 b.each_esse(|x, y, t| {
134 let x = x as f32;
135 let y = h as f32 - y as f32 - 1.0;
136 let tx = (t.sprite.x as f32) * o_stride;
137 let ty = (t.sprite.y as f32) * o_stride;
138 vec.extend(mk_quad((x, y), (x_stride, y_stride), (tx, ty), o_stride));
139 });
140 vec
141 }
142
143
144 fn mk_quad(
145 (x, y): (f32, f32),
146 (x_stride, y_stride): (f32, f32),
147 (tx, ty): (f32, f32),
148 o_stride: f32,
149 ) -> Vec<V2D> {
150 vec![
151 V2D {
152 x: x_stride * (x + 1.0) - 1.0,
153 y: y_stride * y - 1.0,
154 u: tx + o_stride,
155 v: ty + o_stride,
156 },
157 V2D {
158 x: x_stride * x - 1.0,
159 y: y_stride * y - 1.0,
160 u: tx,
161 v: ty + o_stride,
162 },
163 V2D {
164 x: x_stride * (x + 1.0) - 1.0,
165 y: y_stride * (y + 1.0) - 1.0,
166 u: tx + o_stride,
167 v: ty,
168 },
169
170 V2D {
171 x: x_stride * x - 1.0,
172 y: y_stride * y - 1.0,
173 u: tx,
174 v: ty + o_stride,
175 },
176 V2D {
177 x: x_stride * (x + 1.0) - 1.0,
178 y: y_stride * (y + 1.0) - 1.0,
179 u: tx + o_stride,
180 v: ty,
181 },
182 V2D {
183 x: x_stride * x - 1.0,
184 y: y_stride * (y + 1.0) - 1.0,
185 u: tx,
186 v: ty,
187 },
188 ]
189 }
1414
1515 use graphics as gfx;
1616
17 // static TILE_DATA: [V2D; 4] = [
18 // V2D { x: 0.5, y: -0.5, u: 1.0, v: 1.0 },
19 // V2D { x: -0.5, y: -0.5, u: 0.0, v: 1.0 },
20 // V2D { x: 0.5, y: 0.5, u: 1.0, v: 0.0 },
21 // V2D { x: -0.5, y: 0.5, u: 0.0, v: 0.0 },
22 // ];
23
2417 // Shader sources
2518 static VS_SRC: &'static str = include_str!("../shaders/basic_vertex.glsl");
2619 static FS_SRC: &'static str = include_str!("../shaders/basic_fragment.glsl");
3023 /// event loop
3124 pub fn main_loop() -> Result<(), gfx::Error> {
3225 let t = tiled::parse_file(
33 std::path::Path::new("/home/gdritter/projects/animaltransiro/areas/west.tmx")
26 std::path::Path::new("/home/gdritter/projects/animaltransiro/areas/main.tmx")
3427 ).unwrap();
3528 let board = board::Board::from_tiled(&t);
36
37 println!("{:#?}", board);
3829
3930 // let audio = audio::Audio::init()?;
4031 let window = gfx::Window::create()?;
5647 gfx::VertexArray::new(&program),
5748 &tile_data,
5849 );
50 vbo.unbind();
51
52 let upper_tile_data = graphics::vertices::make_upper_grid_with(
53 board::BOARD_WIDTH as usize,
54 board::BOARD_HEIGHT as usize,
55 &board,
56 );
57 let upper_vbo = gfx::VertexBuffer::new_array_buffer(
58 gfx::VertexArray::new(&program),
59 &upper_tile_data,
60 );
61 upper_vbo.unbind();
5962
6063 program.use_program();
6164 program.bind_frag_data_location(0, "out_color")?;
6265
66 vbo.bind();
6367 vbo.bind_position("position")?;
6468 vbo.bind_uv("uv")?;
6569 vbo.unbind();
70
71 upper_vbo.bind();
72 upper_vbo.bind_position("position")?;
73 upper_vbo.bind_uv("uv")?;
74 upper_vbo.unbind();
6675
6776 program.set_texture("tex", &tex, 0).unwrap();
6877 program.set_texture("nrm", &nrm, 1).unwrap();
93102
94103 gfx::clear();
95104 vbo.draw();
105 upper_vbo.draw();
96106
97107 ControlFlow::Continue
98108 })?;