14714
|
1 |
use integral_geometry::{GridIndex, Point, Rect, Size};
|
14705
|
2 |
use land2d::Land2D;
|
|
3 |
use vec2d::Vec2D;
|
14702
|
4 |
|
14718
|
5 |
use super::{
|
|
6 |
camera::Camera,
|
|
7 |
gl::{
|
14719
|
8 |
Buffer, InputElement, InputFormat, InputLayout, PipelineState, Shader, Texture2D,
|
|
9 |
VariableBinding,
|
|
10 |
},
|
14702
|
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,
|
14705
|
81 |
|
14714
|
82 |
tile_size: Size,
|
|
83 |
num_tile_x: usize,
|
14702
|
84 |
}
|
|
85 |
|
|
86 |
impl MapRenderer {
|
14714
|
87 |
pub fn new(tile_size: Size) -> Self {
|
|
88 |
debug_assert!(tile_size.is_power_of_two());
|
|
89 |
|
14702
|
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),
|
14705
|
97 |
],
|
|
98 |
)
|
|
99 |
.unwrap();
|
14702
|
100 |
|
14714
|
101 |
let vertex_size = std::mem::size_of::<TileVertex>() as u32;
|
14702
|
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,
|
14714
|
109 |
stride: vertex_size,
|
14705
|
110 |
offset: 0,
|
14702
|
111 |
},
|
|
112 |
// uv
|
|
113 |
InputElement {
|
|
114 |
shader_slot: 1,
|
|
115 |
buffer_slot: 0,
|
|
116 |
format: InputFormat::Float(gl::FLOAT, false),
|
|
117 |
components: 3,
|
14714
|
118 |
stride: vertex_size,
|
14705
|
119 |
offset: 8,
|
14702
|
120 |
},
|
|
121 |
]);
|
14705
|
122 |
|
14702
|
123 |
MapRenderer {
|
|
124 |
tiles: Vec::new(),
|
|
125 |
textures: Vec::new(),
|
14705
|
126 |
|
14702
|
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 |
|
14714
|
137 |
tile_size,
|
14702
|
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 |
|
14714
|
146 |
let tw = self.tile_size.width;
|
|
147 |
let th = self.tile_size.height;
|
14702
|
148 |
let lw = land.width();
|
|
149 |
let lh = land.height();
|
14714
|
150 |
let num_tile_x = lw / tw;
|
|
151 |
let num_tile_y = lh / th;
|
14702
|
152 |
|
14714
|
153 |
self.num_tile_x = num_tile_x;
|
14702
|
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 |
};
|
14705
|
169 |
|
14702
|
170 |
let texture_index = if idx >= self.textures.len() {
|
|
171 |
let texture = Texture2D::with_data(
|
|
172 |
data,
|
|
173 |
stride,
|
14714
|
174 |
self.tile_size.width as u32,
|
|
175 |
self.tile_size.height as u32,
|
14702
|
176 |
gl::RGBA8,
|
|
177 |
gl::RGBA,
|
|
178 |
gl::UNSIGNED_BYTE,
|
14705
|
179 |
gl::NEAREST,
|
14702
|
180 |
);
|
|
181 |
|
|
182 |
let texture_index = self.textures.len();
|
|
183 |
self.textures.push(texture);
|
|
184 |
|
|
185 |
texture_index
|
|
186 |
} else {
|
14714
|
187 |
let texture_region = Rect::at_origin(self.tile_size);
|
14702
|
188 |
|
14705
|
189 |
self.textures[idx].update(
|
|
190 |
texture_region,
|
|
191 |
data,
|
|
192 |
stride,
|
|
193 |
gl::RGBA,
|
|
194 |
gl::UNSIGNED_BYTE,
|
|
195 |
);
|
14702
|
196 |
idx
|
|
197 |
};
|
|
198 |
|
|
199 |
let tile = MapTile {
|
|
200 |
texture_index: texture_index as u32,
|
14705
|
201 |
|
14702
|
202 |
// TODO: are there ever non-power of two textures?
|
14714
|
203 |
width: self.tile_size.width as u32,
|
|
204 |
height: self.tile_size.height as u32,
|
14702
|
205 |
};
|
|
206 |
self.tiles.push(tile);
|
|
207 |
}
|
|
208 |
}
|
|
209 |
}
|
|
210 |
|
14705
|
211 |
pub fn update(&mut self, land: &Land2D<u32>, region: Rect) {}
|
14702
|
212 |
|
14718
|
213 |
pub fn render(&mut self, camera: &Camera) {
|
|
214 |
let viewport = camera.viewport();
|
14702
|
215 |
self.tile_vertices.clear();
|
|
216 |
self.tile_indices.clear();
|
|
217 |
self.tile_draw_calls.clear();
|
|
218 |
self.index_offset = 0;
|
14705
|
219 |
|
14702
|
220 |
for (idx, tile) in self.tiles.iter().enumerate() {
|
14714
|
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;
|
14702
|
225 |
|
14714
|
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);
|
14702
|
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);
|
14705
|
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 |
};
|
14702
|
255 |
|
|
256 |
self.tile_vertices.extend(&[tl, bl, br, tr]);
|
|
257 |
|
|
258 |
let i = self.index_offset;
|
14705
|
259 |
self.tile_indices
|
|
260 |
.extend(&[i + 0, i + 1, i + 2, i + 2, i + 3, i + 0]);
|
14702
|
261 |
self.index_offset += 4;
|
|
262 |
|
|
263 |
self.tile_draw_calls.push(DrawTile {
|
|
264 |
texture_index: tile.texture_index,
|
14705
|
265 |
index_len: 6,
|
14702
|
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 |
|
14705
|
273 |
let _g = self.tile_layout.bind(
|
|
274 |
&[(0, &self.tile_vertex_buffer)],
|
|
275 |
Some(&self.tile_index_buffer),
|
|
276 |
);
|
14702
|
277 |
|
14718
|
278 |
let projection = camera.projection();
|
14702
|
279 |
|
|
280 |
self.tile_shader.bind();
|
14719
|
281 |
self.tile_shader
|
|
282 |
.set_matrix("Projection", projection.as_ptr());
|
|
283 |
|
|
284 |
let _state = PipelineState::new().with_blend();
|
14705
|
285 |
|
14702
|
286 |
let mut draw_offset = 0;
|
|
287 |
for draw_call in &self.tile_draw_calls {
|
|
288 |
unsafe {
|
14705
|
289 |
self.tile_shader
|
|
290 |
.bind_texture_2d(0, &self.textures[draw_call.texture_index as usize]);
|
|
291 |
|
14702
|
292 |
gl::DrawElements(
|
|
293 |
gl::TRIANGLES,
|
|
294 |
draw_call.index_len as i32,
|
|
295 |
gl::UNSIGNED_SHORT,
|
14705
|
296 |
draw_offset as *const _,
|
14702
|
297 |
);
|
|
298 |
}
|
|
299 |
|
|
300 |
draw_offset += draw_call.index_len * 2;
|
|
301 |
}
|
|
302 |
}
|
|
303 |
}
|