Some probably bad refactoring
Getty Ritter
6 years ago
3 | 3 | pub use gl::*; |
4 | 4 | } |
5 | 5 | use glutin; |
6 | mod errors; | |
6 | ||
7 | pub mod errors; | |
8 | pub mod vertices; | |
9 | ||
7 | 10 | pub use self::errors::Error; |
8 | 11 | use std::{ffi,mem,ptr}; |
9 | 12 | use imagefmt::{Image, ColFmt}; |
13 | use std::marker::PhantomData; | |
14 | use std::os::raw::c_void; | |
10 | 15 | |
11 | 16 | // |
17 | ||
18 | pub fn clear() { | |
19 | unsafe { | |
20 | gl::ClearColor(0.3, 0.3, 0.3, 1.0); | |
21 | gl::Clear(gl::COLOR_BUFFER_BIT); | |
22 | } | |
23 | } | |
12 | 24 | |
13 | 25 | pub struct Window { |
14 | 26 | events: glutin::EventsLoop, |
209 | 221 | } |
210 | 222 | } |
211 | 223 | |
212 |
pub struct VertexBuffer |
|
224 | pub struct VertexBuffer<Vtx> { | |
213 | 225 | idx: u32, |
214 | 226 | data: VertexArray, |
215 | } | |
216 | ||
217 | impl Drop for VertexBuffer { | |
227 | phantom: PhantomData<Vtx>, | |
228 | len: usize, | |
229 | } | |
230 | ||
231 | impl<Vtx> Drop for VertexBuffer<Vtx> { | |
218 | 232 | fn drop(&mut self) { |
219 | 233 | unsafe { gl::DeleteBuffers(1, &self.idx); } |
220 | 234 | } |
244 | 258 | } |
245 | 259 | } |
246 | 260 | |
247 | impl VertexBuffer { | |
248 | pub fn new_array_buffer<Vtx>( | |
261 | impl<Vtx: vertices::Vertex> VertexBuffer<Vtx> { | |
262 | pub fn new_array_buffer( | |
249 | 263 | data: VertexArray, |
250 | 264 | vertex_data: &[Vtx], |
251 |
) -> VertexBuffer |
|
265 | ) -> VertexBuffer<Vtx> { | |
252 | 266 | let mut idx = 0; |
253 | 267 | unsafe { |
254 | 268 | gl::GenBuffers(1, &mut idx); |
261 | 275 | ); |
262 | 276 | } |
263 | 277 | |
264 |
|
|
278 | let phantom = PhantomData; | |
279 | let len = vertex_data.len(); | |
280 | VertexBuffer { idx, data, phantom, len } | |
281 | } | |
282 | ||
283 | pub fn bind_position(&self, p: &Program, name: &str) -> Result<(), Error> { | |
284 | let attr = p.get_attrib_location(name)?; | |
285 | let (offset, num) = Vtx::pos_location(); | |
286 | unsafe { | |
287 | let off = ptr::null::<u8>().offset( | |
288 | (mem::size_of::<gl::GLfloat>() * offset as usize) as isize); | |
289 | gl::EnableVertexAttribArray(attr as gl::GLuint); | |
290 | gl::VertexAttribPointer( | |
291 | attr as gl::GLuint, | |
292 | num, | |
293 | gl::FLOAT, | |
294 | gl::FALSE as gl::GLboolean, | |
295 | Vtx::size() as i32, | |
296 | off as *const c_void, | |
297 | ) | |
298 | } | |
299 | Ok(()) | |
300 | } | |
301 | ||
302 | pub fn bind_uv(&self, p: &Program, name: &str) -> Result<(), Error> { | |
303 | let attr = p.get_attrib_location(name)?; | |
304 | let (offset, num) = Vtx::uv_location(); | |
305 | unsafe { | |
306 | let off = ptr::null::<u8>().offset( | |
307 | (mem::size_of::<gl::GLfloat>() * offset as usize) as isize); | |
308 | gl::EnableVertexAttribArray(attr as gl::GLuint); | |
309 | gl::VertexAttribPointer( | |
310 | attr as gl::GLuint, | |
311 | num, | |
312 | gl::FLOAT, | |
313 | gl::FALSE as gl::GLboolean, | |
314 | Vtx::size() as i32, | |
315 | off as *const c_void, | |
316 | ) | |
317 | } | |
318 | Ok(()) | |
265 | 319 | } |
266 | 320 | |
267 | 321 | pub fn bind(&self) { |
276 | 330 | gl::BindBuffer(gl::ARRAY_BUFFER, 0); |
277 | 331 | self.data.unbind(); |
278 | 332 | } |
333 | } | |
334 | ||
335 | pub fn draw(&self) { | |
336 | self.bind(); | |
337 | unsafe { | |
338 | gl::DrawArrays(gl::TRIANGLE_STRIP, 0, self.len as i32); | |
339 | } | |
340 | self.unbind(); | |
279 | 341 | } |
280 | 342 | } |
281 | 343 |
1 | #![allow(dead_code)] | |
2 | use gl; | |
3 | use std::mem; | |
4 | ||
5 | pub struct V2D { | |
6 | pub x: gl::GLfloat, | |
7 | pub y: gl::GLfloat, | |
8 | pub u: gl::GLfloat, | |
9 | pub v: gl::GLfloat, | |
10 | } | |
11 | ||
12 | impl V2D { | |
13 | fn new( | |
14 | x: gl::GLfloat, | |
15 | y: gl::GLfloat, | |
16 | u: gl::GLfloat, | |
17 | v: gl::GLfloat, | |
18 | ) -> V2D { | |
19 | V2D { x, y, u, v } | |
20 | } | |
21 | } | |
22 | ||
23 | ||
24 | pub trait Vertex { | |
25 | fn size() -> i32; | |
26 | fn pos_location() -> (i32, i32); | |
27 | fn uv_location() -> (i32, i32); | |
28 | } | |
29 | ||
30 | impl Vertex for V2D { | |
31 | fn size() -> i32 { | |
32 | mem::size_of::<V2D>() as i32 | |
33 | } | |
34 | ||
35 | fn pos_location() -> (i32, i32) { | |
36 | (0, 2) | |
37 | } | |
38 | ||
39 | fn uv_location() -> (i32, i32) { | |
40 | (2, 2) | |
41 | } | |
42 | } |
5 | 5 | } |
6 | 6 | extern crate glutin; |
7 | 7 | extern crate imagefmt; |
8 | use std::{mem,ptr}; | |
9 | use std::os::raw::c_void; | |
10 | 8 | |
11 | 9 | mod board; |
12 | 10 | mod graphics; |
13 | 11 | mod pan { |
14 | 12 | pub use graphics::*; |
15 | 13 | } |
14 | use graphics::vertices::V2D; | |
16 | 15 | |
17 | #[allow(dead_code)] | |
18 | struct Vertex { | |
19 | x: gl::GLfloat, | |
20 | y: gl::GLfloat, | |
21 | u: gl::GLfloat, | |
22 | v: gl::GLfloat, | |
23 | } | |
24 | ||
25 | static TILE_DATA: [Vertex; 4] = [ | |
26 | Vertex { x: 0.5, y: -0.5, u: 1.0, v: 0.0 }, | |
27 | Vertex { x: -0.5, y: -0.5, u: 0.0, v: 0.0 }, | |
28 | Vertex { x: 0.5, y: 0.5, u: 1.0, v: 1.0 }, | |
29 | Vertex { x: -0.5, y: 0.5, u: 0.0, v: 1.0 }, | |
16 | static TILE_DATA: [V2D; 4] = [ | |
17 | V2D { x: 0.5, y: -0.5, u: 1.0, v: 0.0 }, | |
18 | V2D { x: -0.5, y: -0.5, u: 0.0, v: 0.0 }, | |
19 | V2D { x: 0.5, y: 0.5, u: 1.0, v: 1.0 }, | |
20 | V2D { x: -0.5, y: 0.5, u: 0.0, v: 1.0 }, | |
30 | 21 | ]; |
31 | 22 | |
32 | 23 | // Shader sources |
75 | 66 | |
76 | 67 | program.use_program(); |
77 | 68 | program.bind_frag_data_location(0, "out_color")?; |
78 | let pos_attr = program.get_attrib_location("position")?; | |
79 | let uv_attr = program.get_attrib_location("uv")?; | |
80 | 69 | |
81 | unsafe { | |
82 | gl::EnableVertexAttribArray(pos_attr as gl::GLuint); | |
83 | gl::VertexAttribPointer( | |
84 | pos_attr as gl::GLuint, | |
85 | 2, | |
86 | gl::FLOAT, | |
87 | gl::FALSE as gl::GLboolean, | |
88 | 4 * mem::size_of::<gl::GLfloat>() as i32, | |
89 | ptr::null(), | |
90 | ); | |
91 | let off = ptr::null::<u8>().offset((mem::size_of::<gl::GLfloat>() * 2) as isize); | |
92 | gl::EnableVertexAttribArray(uv_attr as gl::GLuint); | |
93 | gl::VertexAttribPointer( | |
94 | uv_attr as gl::GLuint, | |
95 | 2, | |
96 | gl::FLOAT, | |
97 | gl::FALSE as gl::GLboolean, | |
98 | 4 * mem::size_of::<gl::GLfloat>() as i32, | |
99 | off as *const c_void, | |
100 | ); | |
101 | } | |
102 | ||
70 | vbo.bind_position(&program, "position")?; | |
71 | vbo.bind_uv(&program, "uv")?; | |
103 | 72 | vbo.unbind(); |
104 | 73 | |
105 | 74 | window.run(|event| { |
124 | 93 | _ => (), |
125 | 94 | } |
126 | 95 | |
127 | unsafe { | |
128 | gl::ClearColor(0.3, 0.3, 0.3, 1.0); | |
129 | gl::Clear(gl::COLOR_BUFFER_BIT); | |
130 | ||
131 | vbo.bind(); | |
132 | program.set_texture("tex", &tex).unwrap(); | |
133 | ||
134 | gl::DrawArrays(gl::TRIANGLE_STRIP, 0, 4); | |
135 |
|
|
96 | pan::clear(); | |
97 | program.set_texture("tex", &tex).unwrap(); | |
98 | vbo.draw(); | |
136 | 99 | |
137 | 100 | ControlFlow::Continue |
138 | 101 | })?; |