Skip to content

Commit

Permalink
Implemented update scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
gue-ni committed Oct 18, 2023
1 parent 22bcfcf commit 148b770
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
55 changes: 27 additions & 28 deletions OpenGL_Flightsim/src/terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ TextureClipmap::TextureClipmap(int clipsize, int levels)
: m_levels{levels},
m_tilesize{256},
m_clipsize{clipsize},
m_center{0},
m_virtual_size{m_clipsize * pow2(levels - 1)},
m_virtual_size{m_clipsize << (levels - 1)},
m_centers(levels),
texture({.texture_size{clipsize, clipsize}, .array_size{levels}})
{
m_center = m_virtual_size / 2;
std::cout << "clipmap:" << std::endl;
std::cout << "levels = " << m_levels << std::endl;
std::cout << "clipsize = " << m_clipsize << std::endl;
std::cout << "virtual size = " << m_virtual_size << std::endl;
std::cout << "inital center = " << m_center << std::endl;
std::cout << "virtual_size = " << m_virtual_size << std::endl;

glm::ivec2 center = m_virtual_size / 2;

for (int level = 0; level < m_levels; level++) {
m_centers[level] = center / (m_tilesize << level);
// std::cout << m_centers[level] << std::endl;
}
}

int TextureClipmap::pow2(int n) { return 1 << n; }
Expand All @@ -28,35 +33,29 @@ void TextureClipmap::update(const glm::vec2& center)
{
glm::ivec2 new_center = center * glm::vec2(m_virtual_size);

// start at 1, as level 0 is never updated
for (int level = 1; level < m_levels; level++) {
// TODO: possibly load new tiles, update center

auto size = m_clipsize * pow2(level);

// the 256x256 tile we are currently in
glm::ivec2 tile_offset = new_center / m_tilesize;

// TODO: check if manhatten distance from old center (snapped to grid) is larger
// than some value, if so, move center and load tiles
// level 0 is the level with the highest resolution
for (int level = 0; level < m_levels; level++) {

glm::ivec2 clipped_center = tile_offset * m_tilesize;
// the tile on which to center the 4x4 tiles that are loaded into the texture
glm::ivec2 center_tile = new_center / (m_tilesize << level);

// shift tiles if difference between new_center and center is greater than one
// tile in any direction
// we need to clamp the coordinate so there is always space for the 4x4 tiles
int max_tile_num = 4 << (m_levels - 1);
center_tile = glm::clamp(center_tile, glm::ivec2(1), glm::ivec2((max_tile_num >> level) - 1));

glm::ivec2 difference = new_center - m_center;

auto shift = glm::greaterThan(glm::abs(difference), glm::ivec2(m_tilesize));

if (glm::any(shift)) {
m_center += m_tilesize * (glm::sign(difference) * glm::ivec2(shift));

// TODO:
if (center_tile != m_centers[level]) {
m_centers[level] = center_tile;
load_tiles(level, center_tile);
}
}
}

void TextureClipmap::load_tiles(int level, const glm::ivec2& center_tile)
{
std::cout << "update level = " << level << ", center = " << center_tile << std::endl;
// TODO: load images and update texture array with glSubTex...
}

void TextureClipmap::bind(GLuint texture_unit) { texture.bind(texture_unit); }

void TextureClipmap::unbind() { texture.unbind(); }
Expand Down Expand Up @@ -161,7 +160,7 @@ GeometryClipmap::GeometryClipmap(int levels_, int segments_)
vertical(1, 2 * segments + 2, segment_size),
center(2 * segments + 2, 2 * segments + 2, segment_size),
seam(2 * segments + 2, segment_size * 2),
m_texture_clipmap(1024, 2)
m_texture_clipmap(1024, 3)
{
std::cout << "terrain_size = " << terrain_size << " m" << std::endl;

Expand Down
10 changes: 8 additions & 2 deletions OpenGL_Flightsim/src/terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class Block {
};

// A TextureClipmap is used to represent a Texture of arbitrary 'virtual' size
// Level 0 is the largest, with the highest resolution
// https://www-f9.ijs.si/~matevz/docs/007-2392-003/sgi_html/ch08.html#LE62092-PARENT
class TextureClipmap
{
Expand All @@ -108,6 +109,10 @@ class TextureClipmap

// update center in texel coordinate space
void update(const glm::vec2& center);

// load grid of 4x4 tiles centered on 'center_tile'
void load_tiles(int level, const glm::ivec2& center_tile);

void bind(GLuint texture_unit);
void unbind();

Expand All @@ -117,9 +122,10 @@ class TextureClipmap
const int m_levels;
const int m_tilesize;
const glm::ivec2 m_clipsize;
const glm::ivec2 m_virtual_size;
const glm::ivec2 m_virtual_size;
std::vector<glm::ivec2> m_centers;


glm::ivec2 m_center;

static int pow2(int n);
// manhattan distance
Expand Down

0 comments on commit 148b770

Please sign in to comment.