gdritter repos chenoska / 37f3283
Some probably bad refactoring Getty Ritter 6 years ago
3 changed file(s) with 124 addition(s) and 57 deletion(s). Collapse all Expand all
33 pub use gl::*;
44 }
55 use glutin;
6 mod errors;
6
7 pub mod errors;
8 pub mod vertices;
9
710 pub use self::errors::Error;
811 use std::{ffi,mem,ptr};
912 use imagefmt::{Image, ColFmt};
13 use std::marker::PhantomData;
14 use std::os::raw::c_void;
1015
1116 //
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 }
1224
1325 pub struct Window {
1426 events: glutin::EventsLoop,
209221 }
210222 }
211223
212 pub struct VertexBuffer {
224 pub struct VertexBuffer<Vtx> {
213225 idx: u32,
214226 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> {
218232 fn drop(&mut self) {
219233 unsafe { gl::DeleteBuffers(1, &self.idx); }
220234 }
244258 }
245259 }
246260
247 impl VertexBuffer {
248 pub fn new_array_buffer<Vtx>(
261 impl<Vtx: vertices::Vertex> VertexBuffer<Vtx> {
262 pub fn new_array_buffer(
249263 data: VertexArray,
250264 vertex_data: &[Vtx],
251 ) -> VertexBuffer {
265 ) -> VertexBuffer<Vtx> {
252266 let mut idx = 0;
253267 unsafe {
254268 gl::GenBuffers(1, &mut idx);
261275 );
262276 }
263277
264 VertexBuffer { idx, data }
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(())
265319 }
266320
267321 pub fn bind(&self) {
276330 gl::BindBuffer(gl::ARRAY_BUFFER, 0);
277331 self.data.unbind();
278332 }
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();
279341 }
280342 }
281343
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 }
55 }
66 extern crate glutin;
77 extern crate imagefmt;
8 use std::{mem,ptr};
9 use std::os::raw::c_void;
108
119 mod board;
1210 mod graphics;
1311 mod pan {
1412 pub use graphics::*;
1513 }
14 use graphics::vertices::V2D;
1615
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 },
3021 ];
3122
3223 // Shader sources
7566
7667 program.use_program();
7768 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")?;
8069
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")?;
10372 vbo.unbind();
10473
10574 window.run(|event| {
12493 _ => (),
12594 }
12695
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();
13699
137100 ControlFlow::Continue
138101 })?;