summaryrefslogtreecommitdiff
path: root/game/terrain.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'game/terrain.cpp')
-rw-r--r--game/terrain.cpp26
1 files changed, 10 insertions, 16 deletions
diff --git a/game/terrain.cpp b/game/terrain.cpp
index 3e5c187..092374c 100644
--- a/game/terrain.cpp
+++ b/game/terrain.cpp
@@ -6,14 +6,14 @@
#include <cstddef>
#include <gfx/gl/shader.h>
#include <gfx/gl/transform.h>
+#include <gfx/image.h>
#include <gfx/models/vertex.hpp>
#include <glm/glm.hpp>
#include <random>
#include <stb_image.h>
-#include <stdexcept>
Terrain::Terrain() :
- m_vertexArrayObject {}, m_vertexArrayBuffers {}, texture {Texture::cachedTexture.get("res/terrain.png")}
+ m_vertexArrayObject {}, m_vertexArrayBuffers {}, texture {Texture::cachedTexture.get("terrain.png")}
{
constexpr auto size {241}; // Vertices
constexpr auto offset {(size - 1) / 2};
@@ -65,33 +65,27 @@ Terrain::Terrain() :
}
Terrain::Terrain(const std::string & fileName) :
- m_vertexArrayObject {}, m_vertexArrayBuffers {}, texture {Texture::cachedTexture.get("res/terrain.png")}
+ m_vertexArrayObject {}, m_vertexArrayBuffers {}, texture {Texture::cachedTexture.get("terrain.png")}
{
constexpr auto resolution {100};
- int width, height, numComponents;
- unsigned char * data = stbi_load((fileName).c_str(), &width, &height, &numComponents, STBI_grey);
-
- if (!data) {
- throw std::runtime_error {"Unable to load heightmap: " + fileName};
- }
+ const Image map {fileName.c_str(), STBI_grey};
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- vertices.reserve((width * height) + 4);
+ vertices.reserve((map.width * map.height) + 4);
- for (auto z = 0; z < height; z += 1) {
- for (auto x = 0; x < width; x += 1) {
+ for (auto z = 0; z < map.height; z += 1) {
+ for (auto x = 0; x < map.width; x += 1) {
vertices.emplace_back(
- glm::vec3 {resolution * (x - (width / 2)), ((float)data[x + (z * width)] * 0.1F) - 1.5F,
- resolution * (z - (height / 2))},
+ glm::vec3 {resolution * (x - (map.width / 2)), ((float)map.data[x + (z * map.width)] * 0.1F) - 1.5F,
+ resolution * (z - (map.height / 2))},
glm::vec2 {(x % 2) / 2.01, (z % 2) / 2.01}, glm::vec3 {0, 1, 0});
}
}
- stbi_image_free(data);
- finish(width, height, resolution);
+ finish(map.width, map.height, resolution);
}
void