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