From 8a699ae4222fd201fcfff3d879820b25013cb0d5 Mon Sep 17 00:00:00 2001 From: Warwick Date: Fri, 12 Aug 2022 12:37:01 +0100 Subject: [PATCH] Fixed resizing of models. --- src/Chunk.cpp | 3 --- src/MarchingCubeChunkRenderer.cpp | 2 +- src/Model.cpp | 17 ++++++++++------- src/Terrain.cpp | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index bbb5874..7f7f766 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -22,9 +22,6 @@ Chunk::Chunk(FastNoise::SmartNode<> &noiseGenerator, TerrainInfo ti) { for (int z = 0; z < ti.zRange[1] - ti.zRange[0]; z++) { for (int y = 0; y < ti.yRange[1] - ti.yRange[0]; y++) { for (int x = 0; x < ti.xRange[1] - ti.xRange[0]; x++) { - error.log(std::to_string(x) + " " + std::to_string(y) + " " + - std::to_string(z) + ": " + - std::to_string(noiseOutput[index])); xArray.push_back(noiseOutput[index++]); } yArray.push_back(xArray); diff --git a/src/MarchingCubeChunkRenderer.cpp b/src/MarchingCubeChunkRenderer.cpp index 82716f5..34bd5af 100644 --- a/src/MarchingCubeChunkRenderer.cpp +++ b/src/MarchingCubeChunkRenderer.cpp @@ -15,7 +15,7 @@ void MarchingCubeChunkRenderer::draw(ShaderLoader &shader) { for (int x = ti.xRange[0]; x < ti.xRange[1]; x++) { for (int y = ti.yRange[0]; y < ti.yRange[1]; y++) { for (int z = ti.zRange[0]; z < ti.zRange[1]; z++) { - if (chunk->getNoise(x, y, z) > 0.5) { + if (chunk->getNoise(x, y, z) > 0.2f) { models.at(0)->setPosition(glm::vec3(x, y, z)); models.at(0)->draw(shader); } diff --git a/src/Model.cpp b/src/Model.cpp index ebc54c9..ef329b9 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -18,19 +18,22 @@ void Model::translate(glm::vec3 translation) { glm::vec3 position = trans * this->position; // set model transform - this->model = glm::translate(glm::mat4(1.0f), glm::vec3(position)); + this->model = trans * this->model; // set position based on the current model this->position = model * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); } void Model::resize(glm::vec3 scale) { - // set worldspace postition - glm::mat4 transMatrix = - glm::translate(glm::mat4(1.0f), glm::vec3(this->position)); - this->scale = scale; + // Store objects position. + glm::vec3 pos = this->position; + // Reset models position so scale scales the object not it's transformation + this->setPositionToOrigin(); + // Scale the model + this->model = glm::scale(this->model, scale); + // Return model to position. + this->setPosition(pos); - // set model transform - this->model = glm::scale(transMatrix, glm::vec3(this->scale)); + this->scale = scale; } void Model::rotate(float angle, glm::vec3 axis) { this->model = glm::rotate(this->model, angle, axis); diff --git a/src/Terrain.cpp b/src/Terrain.cpp index a0795df..bd9d45f 100644 --- a/src/Terrain.cpp +++ b/src/Terrain.cpp @@ -16,7 +16,7 @@ Terrain::Terrain() : renderer(nullptr) { // Create chunks renderer. models = {new Model(ROOT_DIR "data/game-models/12/MetalFloor.obj")}; - models.at(0)->resize(glm::vec3(0.1f, 0.1f, 0.1f)); + models.at(0)->resize(glm::vec3(0.5f)); renderer = new MarchingCubeChunkRenderer(this->chunks, models); }