28 using namespace MaliSDK;
32 : size(size), levels(levels)
57 for (
unsigned int i = 0; i < 2; i++)
62 GL_CHECK(glBufferData(GL_PIXEL_UNPACK_BUFFER, pixel_buffer_size, NULL, GL_STREAM_DRAW));
64 GL_CHECK(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
73 for (
unsigned int i = 0; i <
levels; i++)
90 return -((-x + m - 1) / m);
100 int ret = m - (-x %
m);
101 return ret == m ? 0 : ret;
107 if (fabs(v) < 0.0001)
110 return sin(
PI * v) / (
PI *
v);
121 vector<float> orig(heightmap_size * heightmap_size);
122 vector<float> horiz(heightmap_size * heightmap_size);
125 #define FILTER_LEN 65
126 #define FILTER_CENTER ((FILTER_LEN - 1) / 2)
136 static const Filter bands[] = {
137 { 8.0, 0.0075, 0.0 },
140 { 0.0025, 0.4, 0.4 },
143 for (
unsigned int f = 0;
f <
sizeof(bands) /
sizeof(bands[0]);
f++)
151 orig[
y * heightmap_size +
x] = 50.0
f * (
float(rand()) / RAND_MAX - 0.5
f);
162 sum += src[(
x - i) & (heightmap_size - 1)] * filter[i];
170 const float *
src = &horiz[
x];
176 sum += src[((
y - i) & (heightmap_size - 1)) *
heightmap_size] * filter[i];
201 for (
int j = 0; j < 2; j++)
202 for (
int i = 0; i < 2; i++)
203 heights[j][i] =
sample_heightmap(((x + i) & ~1) << level, ((y + j) & ~1) << level);
207 (heights[0][0] + heights[0][1] + heights[1][0] + heights[1][1]) * 0.25
f);
214 int start_x,
int start_y,
217 if (width == 0 || height == 0)
222 buffer += pixel_offset;
236 pixel_offset += width *
height;
247 if (start_x == info.
x && start_y == info.
y && !info.
cleared)
259 if (abs(delta_x) >=
int(
size) || abs(delta_y) >=
int(
size) || info.
cleared)
261 int wrapped_x = start_x - base_x;
262 int wrapped_y = start_y - base_y;
265 0, 0, wrapped_x, wrapped_y,
266 base_x +
size, base_y +
size, level);
269 wrapped_x, 0,
size - wrapped_x, wrapped_y,
270 start_x, base_y +
size, level);
273 0, wrapped_y, wrapped_x,
size - wrapped_y,
274 base_x +
size, start_y, level);
277 wrapped_x, wrapped_y,
size - wrapped_x,
size - wrapped_y,
278 start_x, start_y, level);
284 int old_wrapped_x = info.
x - old_base_x;
285 int old_wrapped_y = info.
y - old_base_y;
286 int wrapped_x = start_x - base_x;
287 int wrapped_y = start_y - base_y;
289 int wrap_delta_x = wrapped_x - old_wrapped_x;
290 int wrap_delta_y = wrapped_y - old_wrapped_y;
296 if (wrap_delta_x >= 0 && delta_x >= 0)
299 old_wrapped_x, 0, wrap_delta_x, old_wrapped_y,
300 info.
x +
size, old_base_y +
size, level);
303 old_wrapped_x, old_wrapped_y, wrap_delta_x,
size - old_wrapped_y,
304 info.
x +
size, info.
y, level);
306 else if (wrap_delta_x < 0 && delta_x < 0)
309 wrapped_x, 0, -wrap_delta_x, old_wrapped_y,
310 start_x, old_base_y +
size, level);
313 wrapped_x, old_wrapped_y, -wrap_delta_x,
size - old_wrapped_y,
314 start_x, info.
y, level);
316 else if (wrap_delta_x < 0 && delta_x >= 0)
319 0, 0, wrapped_x, old_wrapped_y,
320 base_x +
size, old_base_y +
size, level);
323 old_wrapped_x, 0,
size - old_wrapped_x, old_wrapped_y,
324 base_x + old_wrapped_x, old_base_y +
size, level);
327 0, old_wrapped_y, wrapped_x,
size - old_wrapped_y,
328 base_x +
size, info.
y, level);
331 old_wrapped_x, old_wrapped_y,
size - old_wrapped_x,
size - old_wrapped_y,
332 base_x + old_wrapped_x, info.
y, level);
334 else if (wrap_delta_x >= 0 && delta_x < 0)
337 0, 0, old_wrapped_x, old_wrapped_y,
338 base_x +
size, old_base_y +
size, level);
341 wrapped_x, 0,
size - wrapped_x, old_wrapped_y,
342 start_x, old_base_y +
size, level);
345 0, old_wrapped_y, old_wrapped_x,
size - old_wrapped_y,
346 base_x +
size, info.
y, level);
349 wrapped_x, old_wrapped_y,
size - wrapped_x,
size - old_wrapped_y,
350 start_x, info.
y, level);
353 if (wrap_delta_y >= 0 && delta_y >= 0)
356 0, old_wrapped_y, wrapped_x, wrap_delta_y,
360 wrapped_x, old_wrapped_y,
size - wrapped_x, wrap_delta_y,
361 start_x, info.
y +
size, level);
363 else if (wrap_delta_y < 0 && delta_y < 0)
366 0, wrapped_y, wrapped_x, -wrap_delta_y,
367 base_x +
size, start_y, level);
370 wrapped_x, wrapped_y,
size - wrapped_x, -wrap_delta_y,
371 start_x, start_y, level);
373 else if (wrap_delta_y < 0 && delta_y >= 0)
376 0, 0, wrapped_x, wrapped_y,
377 base_x +
size, base_y +
size, level);
380 0, old_wrapped_y, wrapped_x,
size - old_wrapped_y,
381 base_x +
size, base_y + old_wrapped_y, level);
384 wrapped_x, 0,
size - wrapped_x, wrapped_y,
385 start_x, base_y +
size, level);
388 wrapped_x, old_wrapped_y,
size - wrapped_x,
size - old_wrapped_y,
389 start_x, base_y + old_wrapped_y, level);
391 else if (wrap_delta_y >= 0 && delta_y < 0)
394 0, 0, wrapped_x, old_wrapped_y,
395 base_x +
size, base_y +
size, level);
398 0, wrapped_y, wrapped_x,
size - wrapped_y,
399 base_x +
size, start_y, level);
402 wrapped_x, 0,
size - wrapped_x, old_wrapped_y,
403 start_x, base_y +
size, level);
406 wrapped_x, wrapped_y,
size - wrapped_x,
size - wrapped_y,
407 start_x, start_y, level);
420 pixel_buffer_index ^= 1;
421 GL_CHECK(
vec2 *
buffer = reinterpret_cast<vec2*>(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0,
425 LOGE(
"Failed to map heightmap PBO.\n");
429 unsigned int pixel_offset = 0;
430 for (
unsigned int i = 0; i <
levels; i++)
433 GL_CHECK(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER));
439 itr->x, itr->y, itr->level,
440 itr->width, itr->height, 1,
441 GL_RG, GL_FLOAT, reinterpret_cast<const GLvoid*>(itr->offset)));
444 GL_CHECK(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
Heightmap(unsigned int size, unsigned int levels)
void update_level(vec2 *buffer, unsigned int &pixel_offset, const vec2 &level_offset, unsigned level)
[Update region]
static double sinc(double v)
void update_heightmap(const std::vector< vec2 > &level_offsets)
GLint GLsizei GLsizei height
static int idiv(int x, int m)
unsigned int pixel_buffer_size
GLenum GLuint GLintptr offset
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
std::vector< UploadInfo > upload_info
static int imod(int x, int m)
vec2 compute_heightmap(int x, int y, int level)
[Compute heightmap]
#define GL_TEXTURE_2D_ARRAY
GLenum GLuint GLintptr GLsizeiptr size
void update_region(vec2 *buffer, unsigned int &pixel_offset, int x, int y, int width, int height, int start_x, int start_y, int level)
[Compute heightmap]
GLint GLint GLint GLint GLint x
float sample_heightmap(int x, int y)
GLenum GLuint GLint level
std::vector< LevelInfo > level_info
unsigned int heightmap_size
std::vector< float > heightmap
unsigned int pixel_buffer_index