gdritter repos bouncy / 51108cc
Here is a ball that bounces Getty Ritter 8 years ago
6 changed file(s) with 172 addition(s) and 0 deletion(s). Collapse all Expand all
1 target
2 *~
1 [package]
2 name = "bouncy"
3 version = "0.1.0"
4 authors = ["Getty Ritter <gdritter@galois.com>"]
5
6 [dependencies]
7 image = "*"
8 glium = "*"
1 #version 140
2
3 in vec2 o_tex;
4
5 uniform sampler2D texmap;
6
7 out vec4 color;
8
9 void main() {
10 color = texture(texmap, o_tex);
11 }
Binary diff not shown
1 #version 140
2
3 in vec2 pos;
4 in vec2 tex;
5
6 uniform float ratio;
7
8 out vec2 o_tex;
9
10 void main() {
11 o_tex = tex;
12 gl_Position = vec4(0.1 * pos * vec2(ratio, 1.0), 0.0, 1.0) ;
13 }
1 #[macro_use]
2 extern crate glium;
3 extern crate image;
4
5 use glium::{DisplayBuild,Display,IndexBuffer,Program,Surface,VertexBuffer};
6 use glium::glutin::{Event,WindowBuilder};
7 use glium::index::PrimitiveType;
8 use glium::texture::Texture2d;
9
10 struct BallState {
11 pos: (f32, f32),
12 dpos: (f32, f32),
13 }
14
15 #[derive(Debug,Clone,Copy)]
16 struct V2D {
17 pos: [f32;2],
18 tex: [f32;2],
19 }
20
21 implement_vertex!(V2D, pos, tex);
22
23 fn load_texture(d: &Display) -> Texture2d {
24 use std::io::Cursor;
25 let img = image::load(
26 Cursor::new(&include_bytes!("../assets/texture.png")[..]),
27 image::PNG).unwrap().to_rgba();
28 let dims = img.dimensions();
29 let img = glium::texture::RawImage2d::from_raw_rgba_reversed(
30 img.into_raw(), dims);
31 Texture2d::new(d, img).unwrap()
32 }
33
34 fn walls(d: &Display) -> (VertexBuffer<V2D>, IndexBuffer<u16>) {
35 let mut vertices = vec![];
36 let mut indices = vec![];
37 let mut i = 0;
38 for x in -9..10i32 {
39 for y in -9..10i32 {
40 if x.abs() == 9 || y.abs() == 9 {
41 vertices.extend_from_slice(&square_at((x as f32, y as f32), (0.5, 0.0)));
42 indices.extend_from_slice(
43 &[i + 2, i + 1, i, i + 3, i + 2, i]);
44 i += 4;
45 }
46 }
47 }
48 let vbuf = VertexBuffer::new(d, vertices.as_slice()).unwrap();
49 let ibuf = IndexBuffer::new(d, PrimitiveType::TrianglesList, indices.as_slice()).unwrap();
50 (vbuf, ibuf)
51 }
52
53 fn square_at((x, y): (f32, f32), (bu, bv): (f32, f32)) -> [V2D;4] {
54 [ V2D { pos: [ x - 0.5, y - 0.5 ], tex: [bu,bv] },
55 V2D { pos: [ x + 0.5, y - 0.5 ], tex: [bu,bv+0.5] },
56 V2D { pos: [ x + 0.5, y + 0.5 ], tex: [bu+0.5,bv+0.5] },
57 V2D { pos: [ x - 0.5, y + 0.5 ], tex: [bu,bv+0.5] },
58 ]
59 }
60
61 fn ball(d: &Display, pos: (f32, f32)) -> (VertexBuffer<V2D>, IndexBuffer<u16>) {
62 let vertices = square_at(pos, (0.0, 0.0));
63 let indices = [ 2, 1, 0, 3, 2, 0 ];
64 let vbuf = VertexBuffer::new(d, &vertices).unwrap();
65 let ibuf = IndexBuffer::new(d, PrimitiveType::TrianglesList, &indices).unwrap();
66 (vbuf, ibuf)
67 }
68
69 fn main() {
70 let display = WindowBuilder::new()
71 .with_title(format!("bouncy"))
72 .build_glium()
73 .unwrap();
74 let program = Program::from_source(
75 &display,
76 include_str!("../assets/vertex.glsl"),
77 include_str!("../assets/fragment.glsl"),
78 None).unwrap();
79 let texture = load_texture(&display);
80
81 let mut st = BallState {
82 pos: (0.0, 0.0),
83 dpos: (0.1, 0.1),
84 };
85
86 let (w_vbuf, w_ibuf) = walls(&display);
87
88 loop {
89 let mut tgt = display.draw();
90
91 let params = Default::default();
92 tgt.clear_color(1.0, 1.0, 1.0, 1.0);
93
94 let (width, height) = tgt.get_dimensions();
95 let aspect_ratio = height as f32 / width as f32;
96 let (vbuf, ibuf) = ball(&display, st.pos);
97 tgt.draw(&vbuf, &ibuf, &program,
98 &uniform! {
99 texmap: &texture,
100 ratio: aspect_ratio,
101 },
102 &params).unwrap();
103 tgt.draw(&w_vbuf, &w_ibuf, &program,
104 &uniform! {
105 texmap: &texture,
106 ratio: aspect_ratio,
107 },
108 &params).unwrap();
109
110 tgt.finish().unwrap();
111
112 for ev in display.poll_events() {
113 match ev {
114 Event::Closed => return,
115 Event::KeyboardInput(_, n, _) =>
116 match n {
117 9 => return,
118 24 => return,
119 _ => {},
120 },
121 _ => {},
122 }
123 }
124 {
125 let (x, y) = st.pos;
126 let (mut dx, mut dy) = st.dpos;
127 if x < -8.0 || x > 8.0 {
128 dx = -dx;
129 }
130 if y < -8.0 || y > 8.0 {
131 dy = -dy;
132 }
133 dy -= 0.01;
134 st.pos = (x + dx, y + dy);
135 st.dpos = (dx, dy);
136 }
137 }
138 }