From 9cc74b8e88d0a8c6a92d5fc870730f91b1df3eb7 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 17 Feb 2021 01:36:03 +0000 Subject: Split landmass and water textures --- game/terrain.cpp | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'game/terrain.cpp') diff --git a/game/terrain.cpp b/game/terrain.cpp index 9a7d243..1ec5074 100644 --- a/game/terrain.cpp +++ b/game/terrain.cpp @@ -16,16 +16,13 @@ template class TerrainComp : public Mesh { using Mesh::Mesh; }; -Terrain::Terrain() : texture {Texture::cachedTexture.get("terrain.png")} +Terrain::Terrain() : grass {Texture::cachedTexture.get("grass.png")}, water {Texture::cachedTexture.get("water.png")} { constexpr auto size {241}; // Vertices constexpr auto offset {(size - 1) / 2}; constexpr auto verticesCount = size * size; constexpr auto resolution = 10; // Grid size - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - std::vector vertices; vertices.reserve(verticesCount + 4); vertices.resize(verticesCount, {{}, {}, {}}); @@ -36,7 +33,7 @@ Terrain::Terrain() : texture {Texture::cachedTexture.get("terrain.png")} auto & vertex = vertices[x + (z * size)]; vertex.pos = {resolution * (x - offset), -1.5, resolution * (z - offset)}; vertex.normal = {0, 1, 0}; - vertex.texCoord = {(x % 2) / 2.01, (z % 2) / 2.01}; + vertex.texCoord = {x, z}; } } // Add hills @@ -69,15 +66,13 @@ Terrain::Terrain() : texture {Texture::cachedTexture.get("terrain.png")} addWater(size, size, resolution); } -Terrain::Terrain(const std::string & fileName) : texture {Texture::cachedTexture.get("terrain.png")} +Terrain::Terrain(const std::string & fileName) : + grass {Texture::cachedTexture.get("grass.png")}, water {Texture::cachedTexture.get("water.png")} { constexpr auto resolution {100}; 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); - std::vector vertices; vertices.reserve((map.width * map.height) + 4); @@ -139,10 +134,10 @@ Terrain::addWater(unsigned int width, unsigned int height, unsigned int resoluti // Add water const auto extentx {(int)((width - 1) * resolution / 2)}; const auto extentz {(int)((height - 1) * resolution / 2)}; - vertices.emplace_back(glm::vec3 {-extentx, 0, -extentz}, glm::vec2 {0.5, 0.0}, glm::vec3 {0, 1, 0}); - vertices.emplace_back(glm::vec3 {-extentx, 0, extentz}, glm::vec2 {0.5, 0.5}, glm::vec3 {0, 1, 0}); - vertices.emplace_back(glm::vec3 {extentx, 0, extentz}, glm::vec2 {1, 0.5}, glm::vec3 {0, 1, 0}); - vertices.emplace_back(glm::vec3 {extentx, 0, -extentz}, glm::vec2 {1, 0.0}, glm::vec3 {0, 1, 0}); + vertices.emplace_back(glm::vec3 {-extentx, 0, -extentz}, glm::vec2 {0, 0}, glm::vec3 {0, 1, 0}); + vertices.emplace_back(glm::vec3 {-extentx, 0, extentz}, glm::vec2 {0, height}, glm::vec3 {0, 1, 0}); + vertices.emplace_back(glm::vec3 {extentx, 0, extentz}, glm::vec2 {width, height}, glm::vec3 {0, 1, 0}); + vertices.emplace_back(glm::vec3 {extentx, 0, -extentz}, glm::vec2 {width, 0}, glm::vec3 {0, 1, 0}); indices.push_back(verticesCount); indices.push_back(verticesCount + 1); indices.push_back(verticesCount + 2); @@ -159,7 +154,8 @@ void Terrain::render(const Shader & shader) const { shader.setModel(identityModel); - texture->Bind(); + grass->Bind(); meshes.apply>(&Mesh::Draw); + water->Bind(); meshes.apply>(&Mesh::Draw); } -- cgit v1.2.3