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