14702
|
1 |
use integral_geometry::{Point, Rect, Size};
|
14705
|
2 |
use land2d::Land2D;
|
|
3 |
use vec2d::Vec2D;
|
14702
|
4 |
|
|
5 |
use super::gl::{
|
14705
|
6 |
Buffer, InputElement, InputFormat, InputLayout, Shader, Texture2D, VariableBinding,
|
14702
|
7 |
};
|
|
8 |
|
|
9 |
// TODO: temp
|
|
10 |
const VERTEX_SHADER: &'static str = r#"
|
|
11 |
#version 150
|
|
12 |
|
|
13 |
in vec2 Position;
|
|
14 |
in vec3 Uv;
|
|
15 |
|
|
16 |
out vec3 a_Uv;
|
|
17 |
|
|
18 |
//uniform Common {
|
|
19 |
uniform mat4 Projection;
|
|
20 |
//};
|
|
21 |
|
|
22 |
void main()
|
|
23 |
{
|
|
24 |
a_Uv = Uv;
|
|
25 |
gl_Position = Projection * vec4(Position, 0.0, 1.0);
|
|
26 |
}
|
|
27 |
"#;
|
|
28 |
|
|
29 |
const PIXEL_SHADER: &'static str = r#"
|
|
30 |
#version 150
|
|
31 |
|
|
32 |
in vec3 a_Uv;
|
|
33 |
|
|
34 |
uniform sampler2D Texture;
|
|
35 |
|
|
36 |
out vec4 Target;
|
|
37 |
|
|
38 |
void main()
|
|
39 |
{
|
|
40 |
Target = texture2D(Texture, a_Uv.xy);
|
|
41 |
}
|
|
42 |
"#;
|
|
43 |
|
|
44 |
pub struct MapTile {
|
|
45 |
// either index into GL texture array or emulated [Texture; N]
|
|
46 |
texture_index: u32,
|
|
47 |
|
|
48 |
width: u32,
|
|
49 |
height: u32,
|
|
50 |
}
|
|
51 |
|
|
52 |
#[repr(C)]
|
|
53 |
#[derive(Copy, Clone)]
|
|
54 |
pub struct TileVertex {
|
|
55 |
pos: [f32; 2],
|
|
56 |
// doesn't hurt to include another float, just in case..
|
|
57 |
uv: [f32; 3],
|
|
58 |
}
|
|
59 |
|
|
60 |
pub struct DrawTile {
|
|
61 |
texture_index: u32,
|
|
62 |
index_len: u32,
|
|
63 |
}
|
|
64 |
|
|
65 |
pub struct MapRenderer {
|
|
66 |
tiles: Vec<MapTile>,
|
|
67 |
textures: Vec<Texture2D>,
|
|
68 |
|
|
69 |
tile_vertex_buffer: Buffer,
|
|
70 |
tile_index_buffer: Buffer,
|
|
71 |
tile_vertices: Vec<TileVertex>,
|
|
72 |
tile_indices: Vec<u16>,
|
|
73 |
tile_draw_calls: Vec<DrawTile>,
|
|
74 |
index_offset: u16,
|
|
75 |
tile_shader: Shader,
|
|
76 |
tile_layout: InputLayout,
|
14705
|
77 |
|
14702
|
78 |
tile_width: u32,
|
|
79 |
tile_height: u32,
|
|
80 |
num_tile_x: i32,
|
|
81 |
}
|
|
82 |
|
|
83 |
impl MapRenderer {
|
|
84 |
pub fn new(tile_width: u32, tile_height: u32) -> Self {
|
|
85 |
let tile_shader = Shader::new(
|
|
86 |
VERTEX_SHADER,
|
|
87 |
Some(PIXEL_SHADER),
|
|
88 |
&[
|
|
89 |
VariableBinding::Attribute("Position", 0),
|
|
90 |
VariableBinding::Attribute("Uv", 1),
|
|
91 |
VariableBinding::Sampler("Texture", 0),
|
14705
|
92 |
],
|
|
93 |
)
|
|
94 |
.unwrap();
|
14702
|
95 |
|
|
96 |
let tile_layout = InputLayout::new(vec![
|
|
97 |
// position
|
|
98 |
InputElement {
|
|
99 |
shader_slot: 0,
|
|
100 |
buffer_slot: 0,
|
|
101 |
format: InputFormat::Float(gl::FLOAT, false),
|
|
102 |
components: 2,
|
|
103 |
stride: 20,
|
14705
|
104 |
offset: 0,
|
14702
|
105 |
},
|
|
106 |
// uv
|
|
107 |
InputElement {
|
|
108 |
shader_slot: 1,
|
|
109 |
buffer_slot: 0,
|
|
110 |
format: InputFormat::Float(gl::FLOAT, false),
|
|
111 |
components: 3,
|
|
112 |
stride: 20,
|
14705
|
113 |
offset: 8,
|
14702
|
114 |
},
|
|
115 |
]);
|
14705
|
116 |
|
14702
|
117 |
MapRenderer {
|
|
118 |
tiles: Vec::new(),
|
|
119 |
textures: Vec::new(),
|
14705
|
120 |
|
14702
|
121 |
tile_vertex_buffer: Buffer::empty(gl::ARRAY_BUFFER, gl::DYNAMIC_DRAW),
|
|
122 |
tile_index_buffer: Buffer::empty(gl::ELEMENT_ARRAY_BUFFER, gl::DYNAMIC_DRAW),
|
|
123 |
tile_vertices: Vec::new(),
|
|
124 |
tile_indices: Vec::new(),
|
|
125 |
index_offset: 0,
|
|
126 |
|
|
127 |
tile_draw_calls: Vec::new(),
|
|
128 |
tile_shader,
|
|
129 |
tile_layout,
|
|
130 |
|
|
131 |
tile_width,
|
|
132 |
tile_height,
|
|
133 |
num_tile_x: 0,
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
pub fn init(&mut self, land: &Vec2D<u32>) {
|
|
138 |
// clear tiles, but keep our textures for potential re-use
|
|
139 |
self.tiles.clear();
|
|
140 |
|
|
141 |
let tw = self.tile_width as usize;
|
|
142 |
let th = self.tile_height as usize;
|
|
143 |
let lw = land.width();
|
|
144 |
let lh = land.height();
|
|
145 |
let num_tile_x = lw / tw + if lw % tw != 0 { 1 } else { 0 };
|
|
146 |
let num_tile_y = lh / th + if lh % th != 0 { 1 } else { 0 };
|
|
147 |
|
|
148 |
self.num_tile_x = num_tile_x as i32;
|
|
149 |
|
|
150 |
for y in 0..num_tile_y {
|
|
151 |
for x in 0..num_tile_x {
|
|
152 |
let idx = x + y * num_tile_x;
|
|
153 |
|
|
154 |
let (data, stride) = {
|
|
155 |
let bpp = 4;
|
|
156 |
|
|
157 |
let offset = x * tw * bpp + y * th * lw * bpp;
|
|
158 |
|
|
159 |
let data = unsafe { &land.as_bytes()[offset..] };
|
|
160 |
let stride = land.width();
|
|
161 |
|
|
162 |
(data, stride as u32)
|
|
163 |
};
|
14705
|
164 |
|
14702
|
165 |
let texture_index = if idx >= self.textures.len() {
|
|
166 |
let texture = Texture2D::with_data(
|
|
167 |
data,
|
|
168 |
stride,
|
|
169 |
self.tile_width,
|
|
170 |
self.tile_height,
|
|
171 |
gl::RGBA8,
|
|
172 |
gl::RGBA,
|
|
173 |
gl::UNSIGNED_BYTE,
|
14705
|
174 |
gl::NEAREST,
|
14702
|
175 |
);
|
|
176 |
|
|
177 |
let texture_index = self.textures.len();
|
|
178 |
self.textures.push(texture);
|
|
179 |
|
|
180 |
texture_index
|
|
181 |
} else {
|
|
182 |
let texture_region = Rect::new(
|
|
183 |
Point::new(0, 0),
|
14705
|
184 |
Point::new(self.tile_width as i32, self.tile_height as i32),
|
14702
|
185 |
);
|
|
186 |
|
14705
|
187 |
self.textures[idx].update(
|
|
188 |
texture_region,
|
|
189 |
data,
|
|
190 |
stride,
|
|
191 |
gl::RGBA,
|
|
192 |
gl::UNSIGNED_BYTE,
|
|
193 |
);
|
14702
|
194 |
idx
|
|
195 |
};
|
|
196 |
|
|
197 |
let tile = MapTile {
|
|
198 |
texture_index: texture_index as u32,
|
14705
|
199 |
|
14702
|
200 |
// TODO: are there ever non-power of two textures?
|
|
201 |
width: self.tile_width,
|
|
202 |
height: self.tile_height,
|
|
203 |
};
|
|
204 |
self.tiles.push(tile);
|
|
205 |
}
|
|
206 |
}
|
|
207 |
}
|
|
208 |
|
14705
|
209 |
pub fn update(&mut self, land: &Land2D<u32>, region: Rect) {}
|
14702
|
210 |
|
|
211 |
pub fn render(&mut self, viewport: Rect) {
|
|
212 |
self.tile_vertices.clear();
|
|
213 |
self.tile_indices.clear();
|
|
214 |
self.tile_draw_calls.clear();
|
|
215 |
self.index_offset = 0;
|
14705
|
216 |
|
14702
|
217 |
for (idx, tile) in self.tiles.iter().enumerate() {
|
|
218 |
let tile_x = idx as i32 % self.num_tile_x;
|
|
219 |
let tile_y = idx as i32 / self.num_tile_x;
|
|
220 |
let tile_w = self.tile_width as i32;
|
|
221 |
let tile_h = self.tile_height as i32;
|
|
222 |
|
|
223 |
let origin = Point::new(tile_x * tile_w, tile_y * tile_h);
|
|
224 |
let tile_rect = Rect::new(origin, origin + Point::new(tile_w, tile_h));
|
|
225 |
|
|
226 |
if viewport.intersects(&tile_rect) {
|
|
227 |
// lazy
|
|
228 |
//dbg!(origin);
|
|
229 |
let tile_x = origin.x as f32;
|
|
230 |
let tile_y = origin.y as f32;
|
|
231 |
let tile_w = tile_x + tile_w as f32;
|
|
232 |
let tile_h = tile_y + tile_h as f32;
|
|
233 |
let uv_depth = tile.texture_index as f32;
|
|
234 |
|
|
235 |
//dbg!(tile_x);
|
14705
|
236 |
let tl = TileVertex {
|
|
237 |
pos: [tile_x, tile_y],
|
|
238 |
uv: [0f32, 0f32, uv_depth],
|
|
239 |
};
|
|
240 |
let bl = TileVertex {
|
|
241 |
pos: [tile_x, tile_h],
|
|
242 |
uv: [0f32, 1f32, uv_depth],
|
|
243 |
};
|
|
244 |
let br = TileVertex {
|
|
245 |
pos: [tile_w, tile_h],
|
|
246 |
uv: [1f32, 1f32, uv_depth],
|
|
247 |
};
|
|
248 |
let tr = TileVertex {
|
|
249 |
pos: [tile_w, tile_y],
|
|
250 |
uv: [1f32, 0f32, uv_depth],
|
|
251 |
};
|
14702
|
252 |
|
|
253 |
self.tile_vertices.extend(&[tl, bl, br, tr]);
|
|
254 |
|
|
255 |
let i = self.index_offset;
|
14705
|
256 |
self.tile_indices
|
|
257 |
.extend(&[i + 0, i + 1, i + 2, i + 2, i + 3, i + 0]);
|
14702
|
258 |
self.index_offset += 4;
|
|
259 |
|
|
260 |
self.tile_draw_calls.push(DrawTile {
|
|
261 |
texture_index: tile.texture_index,
|
14705
|
262 |
index_len: 6,
|
14702
|
263 |
});
|
|
264 |
}
|
|
265 |
}
|
|
266 |
|
|
267 |
self.tile_vertex_buffer.write_typed(&self.tile_vertices);
|
|
268 |
self.tile_index_buffer.write_typed(&self.tile_indices);
|
|
269 |
|
14705
|
270 |
let _g = self.tile_layout.bind(
|
|
271 |
&[(0, &self.tile_vertex_buffer)],
|
|
272 |
Some(&self.tile_index_buffer),
|
|
273 |
);
|
14702
|
274 |
|
|
275 |
let ortho = {
|
|
276 |
let l = viewport.left() as f32;
|
|
277 |
let r = viewport.right() as f32;
|
|
278 |
let b = viewport.bottom() as f32;
|
|
279 |
let t = viewport.top() as f32;
|
|
280 |
|
|
281 |
[
|
14705
|
282 |
2f32 / (r - l),
|
|
283 |
0f32,
|
|
284 |
0f32,
|
|
285 |
0f32,
|
|
286 |
0f32,
|
|
287 |
2f32 / (t - b),
|
|
288 |
0f32,
|
|
289 |
0f32,
|
|
290 |
0f32,
|
|
291 |
0f32,
|
|
292 |
0.5f32,
|
|
293 |
0f32,
|
|
294 |
(r + l) / (l - r),
|
|
295 |
(t + b) / (b - t),
|
|
296 |
0.5f32,
|
|
297 |
1f32,
|
14702
|
298 |
]
|
|
299 |
};
|
|
300 |
|
|
301 |
self.tile_shader.bind();
|
|
302 |
self.tile_shader.set_matrix("Projection", ortho.as_ptr());
|
14705
|
303 |
|
14702
|
304 |
let mut draw_offset = 0;
|
|
305 |
for draw_call in &self.tile_draw_calls {
|
|
306 |
unsafe {
|
14705
|
307 |
self.tile_shader
|
|
308 |
.bind_texture_2d(0, &self.textures[draw_call.texture_index as usize]);
|
|
309 |
|
14702
|
310 |
gl::DrawElements(
|
|
311 |
gl::TRIANGLES,
|
|
312 |
draw_call.index_len as i32,
|
|
313 |
gl::UNSIGNED_SHORT,
|
14705
|
314 |
draw_offset as *const _,
|
14702
|
315 |
);
|
|
316 |
}
|
|
317 |
|
|
318 |
draw_offset += draw_call.index_len * 2;
|
|
319 |
}
|
|
320 |
}
|
|
321 |
}
|