From cd714f62e222d1b3379375b9a2a57fdac80c7414 Mon Sep 17 00:00:00 2001 From: Anselme Date: Sat, 12 May 2018 13:39:47 +0200 Subject: [PATCH] Added terrain --- deploy/data/ColorMap.jpg | 3 --- deploy/data/ColorMap.png | 3 +++ deploy/data/HeightMap.jpg | 3 --- deploy/data/HeightMap.png | 3 +++ src/scene/scenenode.cpp | 5 ++++- src/test/main.cpp | 36 +++++++++++++++++++++++++++++------- src/tools/utils.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/tools/utils.h | 3 +++ 8 files changed, 80 insertions(+), 14 deletions(-) delete mode 100644 deploy/data/ColorMap.jpg create mode 100644 deploy/data/ColorMap.png delete mode 100644 deploy/data/HeightMap.jpg create mode 100644 deploy/data/HeightMap.png diff --git a/deploy/data/ColorMap.jpg b/deploy/data/ColorMap.jpg deleted file mode 100644 index 72bbf58..0000000 --- a/deploy/data/ColorMap.jpg +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:0ef5565c529081f035c2a272b7e6f920e0b33ede3f0acf60c913f79cd7254c38 -size 998404 diff --git a/deploy/data/ColorMap.png b/deploy/data/ColorMap.png new file mode 100644 index 0000000..a4be763 --- /dev/null +++ b/deploy/data/ColorMap.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:96f6f799bd788d60498f9dc7af5bbc0fbdbe50895f083fc152b26f521f1c6ba6 +size 3669859 diff --git a/deploy/data/HeightMap.jpg b/deploy/data/HeightMap.jpg deleted file mode 100644 index cccd93b..0000000 --- a/deploy/data/HeightMap.jpg +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:ae822dec7735ae6ac1f01fb8c176031d9868cae784d966a5ff5b554acd454c02 -size 1863853 diff --git a/deploy/data/HeightMap.png b/deploy/data/HeightMap.png new file mode 100644 index 0000000..a1c3517 --- /dev/null +++ b/deploy/data/HeightMap.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:15f07a3b40708615c9e3f62161ae8fcfff54864d9d0d4f382528ccb76a679159 +size 84255 diff --git a/src/scene/scenenode.cpp b/src/scene/scenenode.cpp index 92e3be2..d82bf79 100644 --- a/src/scene/scenenode.cpp +++ b/src/scene/scenenode.cpp @@ -281,8 +281,11 @@ SceneNode* SceneNode::clone() node->setMesh(getMesh()); node->setTransform(getTransform()); node->setLight(getLight()); - node->setRigidBody(new btRigidBody(m_rigidBody->getInvMass(), node->getMotionState(), m_rigidBody->getCollisionShape())); + if(m_rigidBody != nullptr) + node->setRigidBody(new btRigidBody(m_rigidBody->getInvMass(), node->getMotionState(), m_rigidBody->getCollisionShape())); for(SceneNode* child : m_children) node->addChild(child->clone()); + for(SceneNode* child : m_nodesToAdd) + node->addChild(child->clone()); return node; } diff --git a/src/test/main.cpp b/src/test/main.cpp index d053486..b73c100 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -259,18 +259,40 @@ public: } else if(m_config->scene == "sandbox") { - sun->initShadowMap(4096); + // load terrain + + Loader::setTexDirectory("data/"); + SceneNode* terrain = utils::createTerrain("HeightMap.png", "ColorMap.png", 100); + terrain->translate(glm::vec3(353.96, -71.51, -60.79)); + //sandbox->scale(glm::vec3(0.5)); + terrain->getGeometryNode()->modelMatrix = terrain->getTransform(); + btRigidBody* terrainBody = utils::buildStaticCollider(terrain->getGeometryNode()); + terrain->setRigidBody(terrainBody); + scene->getPhysics()->addRigidBody(terrainBody); + scene->getRootObject()->addChild(terrain); + + // load sandbox + SceneNode* sandbox = Loader::loadMesh("sandbox.obj"); - btRigidBody* body = utils::buildStaticCollider(sandbox->getGeometryNode()); - sandbox->setRigidBody(body); - scene->getPhysics()->addRigidBody(body); + btRigidBody* sandboxBody = utils::buildStaticCollider(sandbox->getGeometryNode()); + sandbox->setRigidBody(sandboxBody); + scene->getPhysics()->addRigidBody(sandboxBody); scene->getRootObject()->addChild(sandbox); - m_player->setPosition(0.f, 1.4f, 0.f); - sun->setShadowView(glm::vec3(80)); // adding tree SceneNode* tree = Loader::loadMesh("Tree01.obj"); - scene->getRootObject()->addChild(tree); + for(int i=0; i<8; ++i) + { + tree->moveTo(glm::vec3(-(10 + rand()%20), 0, rand()%40 - 5)); + sandbox->addChild(tree->clone()); + } + delete tree; + + // init player and shadows + + sun->initShadowMap(4096); + m_player->setPosition(0.f, 1.4f, 0.f); + sun->setShadowView(glm::vec3(80)); } scene->getRootObject()->addChild(sunLight); diff --git a/src/tools/utils.cpp b/src/tools/utils.cpp index 6307b1f..f96c58e 100644 --- a/src/tools/utils.cpp +++ b/src/tools/utils.cpp @@ -4,7 +4,12 @@ #include "iostream" #include "sparrowshell/scriptnode.h" #include "transform.h" +#include "loader.h" #include "SparrowRenderer/mesh.h" +#include "SparrowRenderer/parametricmesh.h" +#include "SparrowRenderer/pbrmaterial.h" +#include "SparrowRenderer/image.h" +#include "SparrowRenderer/texture.h" #include #include @@ -116,3 +121,36 @@ btRigidBody* utils::buildStaticCollider(GeometryNode* node) rigidBody->setWorldTransform(transform); return rigidBody; } + +SceneNode* utils::createTerrain(const std::string& heightMap, const std::string& colorMap, float maxHeight) +{ + GridGenerator grid; + PBRMaterial* mat = new PBRMaterial(); + Image* heightMapImg = Loader::loadImage(heightMap, 24); + if(heightMapImg == nullptr) + return nullptr; + Image* colorMapImg = Loader::loadImage(colorMap, 24); + if(colorMapImg == nullptr) + return nullptr; + mat->setTexture(PBRMaterial::ALBEDO_SLOT, new Texture(colorMapImg)); + mat->roughness = 0.9f; + mat->metallic = 0.1f; + int size = std::min(heightMapImg->width, heightMapImg->height); + Mesh* mesh = grid.generateParametricMesh(mat, size-1, size-1, size); + for(int x=0; xwidth; ++x) + { + for(int y=0; yheight; ++y) + { + int height = heightMapImg->pixels[(y*heightMapImg->width + x)*3]; + mesh->m_positions3D[x*heightMapImg->width + y].y = height*maxHeight/255; + } + } + delete heightMapImg; + delete colorMapImg; + SceneNode* node = new SceneNode(); + mesh->computeNormals(); + mesh->mergeVertices(); + mesh->initGL(); + node->setMesh(mesh); + return node; +} diff --git a/src/tools/utils.h b/src/tools/utils.h index 8eb0602..cd40691 100644 --- a/src/tools/utils.h +++ b/src/tools/utils.h @@ -8,6 +8,7 @@ class ScriptNode; class btRigidBody; class GeometryNode; +class SceneNode; namespace utils { @@ -24,6 +25,8 @@ void initStandardScene(); * @warning collisions on static meshes does not work if the transform is modified after the rigidbody construction */ btRigidBody* buildStaticCollider(GeometryNode* node); + +SceneNode* createTerrain(const std::string &heightMap, const std::string &colorMap, float maxHeight = 100); } #endif // UTILS_H