Added terrain

This commit is contained in:
Anselme 2018-05-12 13:39:47 +02:00
parent 6c70b27d50
commit cd714f62e2
8 changed files with 80 additions and 14 deletions

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:0ef5565c529081f035c2a272b7e6f920e0b33ede3f0acf60c913f79cd7254c38
size 998404

3
deploy/data/ColorMap.png Normal file
View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:96f6f799bd788d60498f9dc7af5bbc0fbdbe50895f083fc152b26f521f1c6ba6
size 3669859

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:ae822dec7735ae6ac1f01fb8c176031d9868cae784d966a5ff5b554acd454c02
size 1863853

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:15f07a3b40708615c9e3f62161ae8fcfff54864d9d0d4f382528ccb76a679159
size 84255

View File

@ -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;
}

View File

@ -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);

View File

@ -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 <btBulletCollisionCommon.h>
#include <btBulletDynamicsCommon.h>
@ -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; x<heightMapImg->width; ++x)
{
for(int y=0; y<heightMapImg->height; ++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;
}

View File

@ -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