Skip to content

Commit

Permalink
Merge pull request #24 from gue-ni/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
gue-ni authored Feb 19, 2024
2 parents c6aad45 + f07e9df commit 84935b1
Show file tree
Hide file tree
Showing 23 changed files with 121 additions and 109 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
libraries/
x64/
build/
build2/
out/
tiles/
doc/
pre-commit.ps1
*.user
CMakeSettings.json
NOTES.txt
imgui.ini
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.21)
cmake_minimum_required(VERSION 3.18)
project(TerrainRenderer LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ A simple OpenGL Terrain Renderer. Orthophoto data is provided by [basemap.at](ht

## Build Instructions

This project depends on [SDL2](https://www.libsdl.org/), [GLM](https://github.com/g-truc/glm),
[GLEW](https://glew.sourceforge.net/) and [libcpr](https://github.com/libcpr/cpr). You should be able to
This project depends on [SDL2](https://www.libsdl.org/), [GLM](https://github.com/g-truc/glm),
[GLEW](https://glew.sourceforge.net/) and [libcpr](https://github.com/libcpr/cpr). You should be able to
build by following the steps in the [cmake.yml](./.github/workflows/cmake.yml).

```
git clone --recursive git@github.com:gue-ni/TerrainRenderer.git
git clone --recursive https://github.com/gue-ni/TerrainRenderer.git
cmake -DCMAKE_BUILD_TYPE=Release -B build
cmake --build build --config Release --parallel
```
6 changes: 4 additions & 2 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.21)
cmake_minimum_required(VERSION 3.18)

FetchContent_Declare(
cpr GIT_REPOSITORY https://github.com/libcpr/cpr.git
Expand Down Expand Up @@ -28,7 +28,9 @@ add_executable(app
${imgui_SOURCE}
)

target_link_libraries(app PRIVATE terrain)
target_link_libraries(app PRIVATE
terrain
)

target_include_directories(app PRIVATE
"${glm_SOURCE_DIR}"
Expand Down
2 changes: 1 addition & 1 deletion collision/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.21)
cmake_minimum_required(VERSION 3.18)

FetchContent_Declare(
glm
Expand Down
13 changes: 0 additions & 13 deletions collision/Collision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,6 @@ AABB AABB::from_center_and_size(const glm::vec3& center, const glm::vec3& size)
return AABB(center - half_size, center + half_size);
}

AABB AABB::from_points(const std::span<glm::vec3>& points)
{
constexpr float float_max = std::numeric_limits<float>::max();
glm::vec3 min(float_max), max(-float_max);

for (auto& point : points) {
min = glm::min(min, point);
max = glm::max(max, point);
}

return AABB(min, max);
}

bool AABB::contains(const AABB& other)
{
return glm::all(glm::lessThanEqual(min, other.min)) && glm::all(glm::lessThanEqual(other.max, max));
Expand Down
3 changes: 0 additions & 3 deletions collision/Collision.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <array>
#include <glm/glm.hpp>
#include <iostream>
#include <ranges>
#include <span>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/io.hpp>

Expand All @@ -19,7 +17,6 @@ struct AABB {
std::array<glm::vec3, 8> vertices() const;
bool contains(const AABB &);
static AABB from_center_and_size(const glm::vec3 &center, const glm::vec3 &size);
static AABB from_points(const std::span<glm::vec3> &points);

template <class It>
static AABB from_points(It begin, It end)
Expand Down
2 changes: 1 addition & 1 deletion gfx
Submodule gfx updated 2 files
+5 −1 CMakeLists.txt
+3 −3 gl.h
12 changes: 11 additions & 1 deletion terrain/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
cmake_minimum_required(VERSION 3.21)
cmake_minimum_required(VERSION 3.18)

FetchContent_Declare(fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG e69e5f977d458f2650bb346dadf2ad30c5320281
)
FetchContent_MakeAvailable(fmt)

find_package(Threads REQUIRED)

add_library(terrain STATIC
TileService.cpp TileService.h
Expand All @@ -14,6 +22,8 @@ target_link_libraries(terrain PUBLIC
collision
gfx
cpr::cpr
fmt::fmt
Threads::Threads
)

target_include_directories(terrain PUBLIC
Expand Down
4 changes: 2 additions & 2 deletions terrain/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ Chunk::Chunk(unsigned vertex_count, float size)
m_vao->bind();

m_vbo->bind();
m_vbo->buffer_data(std::span(vertices));
m_vbo->buffer_data(vertices.data(), Buffer::size_bytes(vertices));

m_ebo->bind();
m_ebo->buffer_data(std::span(indices));
m_ebo->buffer_data(indices.data(), Buffer::size_bytes(indices));

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(offsetof(Vertex, pos)));
glEnableVertexAttribArray(0);
Expand Down
2 changes: 1 addition & 1 deletion terrain/Cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Cube::Cube() : m_vao(std::make_unique<VertexArrayObject>()), m_vbo(std::make_uni
m_vao->bind();

m_vbo->bind();
m_vbo->buffer_data(std::span(vertices));
m_vbo->buffer_data(vertices.data(), Buffer::size_bytes(vertices));

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void*)0);
glEnableVertexAttribArray(0);
Expand Down
30 changes: 15 additions & 15 deletions terrain/TerrainRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ float altitude_from_color(vec4 color) {
return (color.r + color.g / 255.0);
}
float altitude_from_color_2(vec4 color) {
return color.r;
}
vec2 map_range(vec2 value, vec2 in_min, vec2 in_max, vec2 out_min, vec2 out_max) {
return out_min + (value - in_min) * (out_max - out_min) / (in_max - in_min);
}
Expand All @@ -47,7 +51,7 @@ void main() {
vec4 height_sample = texture(u_height_texture, scaled_uv);
float height = altitude_from_color(height_sample);
float height = altitude_from_color_2(height_sample);
world_pos.y = height * u_height_scaling_factor;
Expand Down Expand Up @@ -204,19 +208,9 @@ Bounds<glm::vec2> rescale_uv(const TileId& parent_tile_id, const TileId& tile_id
unsigned delta_y = tile_id.y - scaled_root_tile.y;

float factor = 1.0f / num_tiles;
#if 1

Bounds<glm::vec2> uv = {glm::vec2((delta_x + 0) * factor, (delta_y + 0) * factor),
glm::vec2((delta_x + 1) * factor, (delta_y + 1) * factor)};
#else
Bounds<glm::vec2> uv = {glm::vec2(0), glm::vec2(1)};
#endif

if ((tile_id.zoom == 12U || tile_id.zoom == 11U) && tile_id.x == scaled_root_tile.x) {
// std::cout << tile_id << ": " << uv << std::endl;

// uv.min = glm::vec2(0);
// uv.max = glm::vec2(1);
}

return uv;
}
Expand All @@ -238,10 +232,15 @@ TerrainRenderer::TerrainRenderer(const TileId& root_tile, unsigned max_zoom_leve
float tile_width = wms::tile_width(wms::tiley2lat(m_root_tile.y, m_root_tile.zoom), m_root_tile.zoom);
m_terrain_scaling_factor = width / tile_width;

// for decoding the height map
// for decoding the height map
#if 0
const float min_elevation = 0.0f, max_elevation = 8191.0f;
#else
const float min_elevation = 0.0f, max_elevation = 3795.0f;
#endif
m_height_scaling_factor = (max_elevation - min_elevation);

#if 0
(void)m_tile_cache.tile_texture_sync(m_root_tile, TileType::ORTHO);
(void)m_tile_cache.tile_texture_sync(m_root_tile, TileType::HEIGHT);

Expand All @@ -250,6 +249,7 @@ TerrainRenderer::TerrainRenderer(const TileId& root_tile, unsigned max_zoom_leve
(void)m_tile_cache.tile_texture_sync(child, TileType::ORTHO);
(void)m_tile_cache.tile_texture_sync(child, TileType::HEIGHT);
}
#endif
}

float TerrainRenderer::terrain_elevation(const glm::vec2& point)
Expand Down Expand Up @@ -355,8 +355,8 @@ Texture* TerrainRenderer::find_cached_lower_zoom_parent(Node* node, Bounds<glm::
#if 1
if (!parent_texture) {
parent_tile_id = m_root_tile;
parent_texture = m_tile_cache.tile_texture_cached(m_root_tile, type);
assert(parent_texture);
parent_texture = m_tile_cache.tile_texture(m_root_tile, type);
// assert(parent_texture);
}
#endif

Expand Down
8 changes: 4 additions & 4 deletions terrain/Threading.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include <condition_variable>
#include <deque>
#include <functional>
#include <mutex>
#include <queue>
#include <thread>

template <typename T>
Expand All @@ -27,7 +27,7 @@ class ThreadedQueue
{
{
std::unique_lock lock(m_mutex);
m_queue.push_front(std::move(item));
m_queue.push(std::move(item));
}
m_condition.notify_one();
}
Expand All @@ -42,7 +42,7 @@ class ThreadedQueue
}

item = std::move(m_queue.front());
m_queue.pop_front();
m_queue.pop();
return true;
}

Expand Down Expand Up @@ -76,7 +76,7 @@ class ThreadedQueue

private:
bool m_stop = false;
std::deque<T> m_queue;
std::queue<T> m_queue;
std::mutex m_mutex;
std::condition_variable m_condition;
};
Expand Down
4 changes: 1 addition & 3 deletions terrain/TileCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ TileCache::TileCache()
m_ortho_service("https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile",
UrlPattern::ZYX_Y_SOUTH, "", "tiles/ortho-2"),
#endif
m_height_service("https://alpinemaps.cg.tuwien.ac.at/tiles/alpine_png", UrlPattern::ZXY_Y_NORTH, ".png",
"tiles/height")

m_height_service("https://www.jakobmaier.at/blob/dem-tiles", UrlPattern::ZXY_Y_NORTH, ".png", "tiles/height-1")
{
}

Expand Down
2 changes: 0 additions & 2 deletions terrain/TileCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

#include <chrono>
#include <memory>
#include <numbers>
#include <string>
#include <tuple>
#include <unordered_map>

#include "../gfx/gfx.h"
Expand Down
21 changes: 15 additions & 6 deletions terrain/TileService.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "TileService.h"

#include <cpr/cpr.h>
#include <fmt/core.h>

#include <filesystem>
#include <format>

#define LOG_REQUESTS true
#define CACHE_ON_DISK true
Expand Down Expand Up @@ -31,16 +31,16 @@ std::string TileService::tile_url(const TileId& tile) const

switch (m_url_pattern) {
case ZXY_Y_NORTH: {
return std::format("{}/{}/{}/{}{}", m_url, tile.zoom, tile.x, (num_y_tiles - tile.y - 1), m_filetype);
return fmt::format("{}/{}/{}/{}{}", m_url, tile.zoom, tile.x, (num_y_tiles - tile.y - 1), m_filetype);
}
case ZYX_Y_NORTH: {
return std::format("{}/{}/{}/{}{}", m_url, tile.zoom, (num_y_tiles - tile.y - 1), tile.x, m_filetype);
return fmt::format("{}/{}/{}/{}{}", m_url, tile.zoom, (num_y_tiles - tile.y - 1), tile.x, m_filetype);
}
case ZYX_Y_SOUTH: {
return std::format("{}/{}/{}/{}{}", m_url, tile.zoom, tile.y, tile.x, m_filetype);
return fmt::format("{}/{}/{}/{}{}", m_url, tile.zoom, tile.y, tile.x, m_filetype);
}
case ZXY_Y_SOUTH: {
return std::format("{}/{}/{}/{}{}", m_url, tile.zoom, tile.x, tile.y, m_filetype);
return fmt::format("{}/{}/{}/{}{}", m_url, tile.zoom, tile.x, tile.y, m_filetype);
}
default:
assert(false);
Expand All @@ -50,7 +50,7 @@ std::string TileService::tile_url(const TileId& tile) const

std::string TileService::tile_filename(const TileId& tile) const
{
return std::format("{}/{}.png", m_cache_dir, tile.to_string());
return fmt::format("{}/{}.png", m_cache_dir, tile.to_string());
}

Image* TileService::get_tile(const TileId& tile)
Expand Down Expand Up @@ -81,6 +81,15 @@ Image* TileService::get_tile_sync(const TileId& tile)
return m_ram_cache[tile].get();
}

Image* TileService::get_tile_cached(const TileId& tile)
{
if (m_ram_cache.contains(tile)) {
return m_ram_cache[tile].get();
} else {
return nullptr;
}
}

void TileService::request_tile(const TileId& tile)
{
m_already_requested.insert(tile);
Expand Down
2 changes: 2 additions & 0 deletions terrain/TileService.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class TileService
// Download tile and return it.
Image* get_tile_sync(const TileId&);

Image* get_tile_cached(const TileId&);

private:
const UrlPattern m_url_pattern;
const std::string m_url, m_filetype, m_cache_dir;
Expand Down
5 changes: 3 additions & 2 deletions terrain/TileUtils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once

#include <fmt/core.h>

#include <array>
#include <format>
#include <numbers>
#include <string>

Expand Down Expand Up @@ -84,7 +85,7 @@ struct TileId {

auto operator<=>(const TileId&) const = default;

inline std::string to_string() const { return std::format("{}-{}-{}", zoom, x, y); }
inline std::string to_string() const { return fmt::format("{}-{}-{}", zoom, x, y); }

inline Bounds<Coordinate> bounds() const
{
Expand Down
13 changes: 9 additions & 4 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.21)
cmake_minimum_required(VERSION 3.18)

Include(FetchContent)

Expand All @@ -11,11 +11,16 @@ FetchContent_Declare(
FetchContent_MakeAvailable(Catch2)

add_executable(tests
test-collision.cpp
test-quadtree.cpp
test-terrain.cpp
test_collision.cpp
test_quadtree.cpp
test_terrain.cpp
)

if(CMAKE_COMPILER_IS_GNUCC)
target_link_options(tests PRIVATE -fsanitize=thread)
target_compile_options(tests PRIVATE -fsanitize=thread)
endif()

target_link_libraries(tests PRIVATE Catch2::Catch2WithMain collision terrain)

if(WIN32)
Expand Down
9 changes: 0 additions & 9 deletions test/test-terrain.cpp

This file was deleted.

Loading

0 comments on commit 84935b1

Please sign in to comment.