From bde4a9f40bb4af39270a124d5ef9571a83305d0a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 30 Jan 2021 20:36:42 +0000 Subject: Restructure to allow a resource path and testing --- game/terrain.cpp | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) (limited to 'game') 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 #include #include +#include #include #include #include #include -#include 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 -- cgit v1.2.3