Skip to content

Commit

Permalink
merge develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Lensimax committed Jan 13, 2020
2 parents 9a2e8c8 + 0f4cf9c commit 016219e
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 63 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ add_definitions(-DGLFW_INCLUDE_NONE)


if("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
add_definitions(-O3)
add_definitions(-O3 -DNDEBUG)
endif()

# si le compilateur est Visual Studio
Expand Down
Binary file removed documentation.pdf
Binary file not shown.
18 changes: 14 additions & 4 deletions src/components/terrainModificator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ TerrainModificator::TerrainModificator(){
setName("Terrain Modificator");
}

void TerrainModificator::destroy(size_t radius) {
assert(m_terrain != nullptr);
glm::vec3 world_coord = m_gameobject->getTransform()->getPosition();
void TerrainModificator::destroy(size_t radius)
{
setSphere(m_gameobject->getTransform()->getPosition(), radius, Voxel::Empty);
}

void TerrainModificator::setSphere(glm::vec3 world_coord, size_t radius, Voxel v) {
glm::vec3 min_coord = world_coord - glm::vec3(radius);

for(size_t i = 0 ; i < m_terrain->getChunkSize() ; i++) {
Expand All @@ -26,13 +28,21 @@ void TerrainModificator::destroy(size_t radius) {
glm::vec3 current_coord = min_coord + glm::vec3(i, j, k);

if (glm::distance(world_coord, current_coord) <= radius)
m_terrain->setVoxelAt(min_coord + glm::vec3(i, j, k), Voxel::Empty);
m_terrain->setVoxelAt(min_coord + glm::vec3(i, j, k), v);
}
}
}
}

void TerrainModificator::inputUpdate(){

glm::vec3 pos = m_gameobject->getTransform()->getPosition();

if(ImGui::IsKeyPressed('O'))
setSphere(pos, 10, Voxel::Empty);
else if (ImGui::IsKeyPressed('I'))
setSphere(pos, 10, Voxel::Full);
}

void TerrainModificator::createUI(){
// ImGui::Text("Height Offset : ");
Expand Down
3 changes: 2 additions & 1 deletion src/components/terrainModificator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ class TerrainModificator : public Component {

TerrainModificator();

void inputUpdate() override;
void createUI() override;

void destroy(size_t radius);

void setSphere(glm::vec3 position, size_t radius, Voxel v);

void setTerrain(TerrainManager *terrain){m_terrain = terrain;}

Expand Down
3 changes: 1 addition & 2 deletions src/engineClass/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,8 @@ void Scene::loadExplorationScene(){
objectsEngine.push_back(player);

GameObject *terrain = new GameObject(addNewId(), "Terrain");
terrain->addComponent<TerrainManager*>(new TerrainManager(32, 12, player->getTransform()));
terrain->addComponent<TerrainManager*>(new TerrainManager(32, 5, player->getTransform()));
objectsEngine.push_back(terrain);


GameObject *camera = new GameObject(addNewId(), "Camera", new Transform(glm::vec3(0,164, 0), glm::vec3(M_PI / 2 - 0.3, M_PI, 0)));
camera->addComponent<CameraProjective*>(new CameraProjective());
Expand Down
80 changes: 58 additions & 22 deletions src/terrain/terrainChunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,38 @@ void TerrainChunk::generate() {
glm::vec3 position = m_gameobject->getTransform()->getPosition();
// std::cerr << position.x << ','<< position.y << ',' << position.z << '\n';

for(size_t i = 0 ; i < voxels.width() ; ++i) {
for(size_t k = 0 ; k < voxels.depth() ; ++k) {

// perlin_value in [0, 1]
// float perlin_value = (snoise.fractal(octaves,
// position.x + i,
// position.z + k) + 1.0) / 2.0;
//
// size_t max_y = std::round(perlin_value * voxels.cubic_size());
size_t max_y = getHeightAt(voxels.cubic_size(), position.x + i, position.z + k);
//
// voxels(i, max_y, k) = Voxel::Full;

// if (max_y > 0)
// voxels(i, max_y - 1, k) = Voxel::Full;

for (size_t j = 0 ; j < max_y ; ++j) // => active les voxel de 0 à Y
{
voxels(i, j, k) = Voxel::Full;
if (position.y >= 0 && position.y < voxels.height()) // Affichage de la surface du terrain
{
for(size_t i = 0 ; i < voxels.width() ; ++i) {
for(size_t k = 0 ; k < voxels.depth() ; ++k) {

// perlin_value in [0, 1]
// float perlin_value = (snoise.fractal(octaves,
// position.x + i,
// position.z + k) + 1.0) / 2.0;
//
// size_t max_y = std::round(perlin_value * voxels.cubic_size());
size_t max_y = getHeightAt(voxels.cubic_size(), position.x + i, position.z + k);
//
// voxels(i, max_y, k) = Voxel::Full;

// if (max_y > 0)
// voxels(i, max_y - 1, k) = Voxel::Full;

for (size_t j = 0 ; j < max_y ; ++j) // => active les voxel de 0 à Y
{
voxels(i, j, k) = Voxel::Full;
}
}
}
}
else if (position.y < 0) { // Affiche le sous-sol de la map
for(size_t i = 0 ; i < voxels.width() ; ++i) {
for (size_t j = 0 ; j < voxels.height() ; ++j) // => active les voxel de 0 à Y
for(size_t k = 0 ; k < voxels.depth() ; ++k) {
{
voxels(i, j, k) = getGroundAt(voxels.cubic_size(), position.x + i, position.y + j, position.z + k);
}
}
}
}
Expand Down Expand Up @@ -99,6 +112,17 @@ std::array<bool, 6> TerrainChunk::surrounding(size_t x, size_t y, size_t z) cons
// if ( !(z == (voxels.depth() - 1)) && (voxels(x , y , z + 1) != Voxel::Empty) ) activated_neighbors[4] = true;
// if ( !(z == 0) && (voxels(x , y , z - 1) != Voxel::Empty) ) activated_neighbors[5] = true;

// assert(x != 0 && x != voxels.width() - 1);
// assert(y != 0 && y != voxels.height() - 1);
// assert(z != 0 && z != voxels.depth() - 1);

// if (voxels(x + 1, y , z ) != Voxel::Empty) activated_neighbors[0] = true;
// if (voxels(x - 1, y , z ) != Voxel::Empty) activated_neighbors[1] = true;
// if (voxels(x , y + 1, z ) != Voxel::Empty) activated_neighbors[2] = true;
// if (voxels(x , y - 1, z ) != Voxel::Empty) activated_neighbors[3] = true;
// if (voxels(x , y , z + 1) != Voxel::Empty) activated_neighbors[4] = true;
// if (voxels(x , y , z - 1) != Voxel::Empty) activated_neighbors[5] = true;

return activated_neighbors;
}

Expand Down Expand Up @@ -199,9 +223,9 @@ void TerrainChunk::addCubeFaces(const std::array<bool, 6>& surrounding, size_t x

void TerrainChunk::calculateMesh()
{
for(size_t i = 0 ; i< voxels.width() ; i++) {
for(size_t j = 0 ; j < voxels.height() ; j++) {
for(size_t k = 0 ; k < voxels.depth() ; k++) {
for(size_t i = 0 ; i< voxels.width(); i++) {
for(size_t j = 0 ; j < voxels.height(); j++) {
for(size_t k = 0 ; k < voxels.depth(); k++) {
if (voxels(i, j, k) != Voxel::Empty) // si le voxel est activé
{
this->addCubeFaces(this->surrounding(i, j, k), i, j, k);
Expand All @@ -219,3 +243,15 @@ size_t TerrainChunk::getHeightAt(size_t chunk_size, float x, float z) {

return std::round(perlin_value * chunk_size);
}

Voxel TerrainChunk::getGroundAt(size_t chunk_size, float x, float y, float z) {
float scale = 50.f;
size_t octaves = 3;

SimplexNoise snoise(1.0f / scale);

float perlin_value = (snoise.fractal(octaves, x, y, z) + 1.0) / 2.0;

return perlin_value < 0.5 ? Voxel::Empty : Voxel::Full;
}

1 change: 1 addition & 0 deletions src/terrain/terrainChunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TerrainChunk : public Component {
/// Statics

static size_t getHeightAt(size_t chunk_size, float x, float z);
static Voxel getGroundAt(size_t chunk_size, float x, float y, float z);
// private:
static float m_frequency;
static size_t nbOctaves;
Expand Down
66 changes: 33 additions & 33 deletions src/terrain/terrainManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,12 @@ void TerrainManager::inputUpdate() {
// return;
}

glm::vec3 player_coord = getPlayerCoord(); player_coord.y = 0;
glm::vec3 player_coord = getPlayerCoord();// player_coord.y = 0;

glm::ivec3 cg_coord = toChunkGridCoord(player_coord);

if (m_oldChunkGridCoord != cg_coord)
{

manageChunksAround(player_coord);

m_oldChunkGridCoord = cg_coord;
Expand Down Expand Up @@ -126,7 +125,7 @@ void TerrainManager::createChunksAround(glm::vec3 position) {
void TerrainManager::manageChunksAround(glm::vec3 world_coord) {

glm::ivec3 current_grid_coord(toChunkGridCoord(world_coord));
glm::ivec3 grid_offset(getTerrainSize() / 2, 0, getTerrainSize() / 2);
glm::ivec3 grid_offset(getTerrainSize() / 2, getTerrainSize() / 2, getTerrainSize() / 2);
glm::ivec3 min_grid_coord = current_grid_coord - grid_offset;


Expand All @@ -136,29 +135,32 @@ void TerrainManager::manageChunksAround(glm::vec3 world_coord) {

for (size_t i = 0 ; i < getTerrainSize() ; ++i)
{
for (size_t k = 0 ; k < getTerrainSize() ; ++k)
{
glm::ivec3 chunk_grid_coord = min_grid_coord + glm::ivec3(i, 0, k);

auto res = m_grid_to_chunk_map.find(chunk_grid_coord);

if (res != m_grid_to_chunk_map.end()) // Trouvé
{
TerrainChunk* terrain_chunk = res->second;

m_grid_to_chunk_map[chunk_grid_coord] = terrain_chunk;
to_keep.push_back(terrain_chunk->getGameObject());
}
else
{
// Si le chunk n'est pas dans la map il faut le creer
GameObject* chunk = createTerrainChunk(toWorldGridCoord(chunk_grid_coord));
m_gameobject->addChild(chunk);

m_grid_to_chunk_map[chunk_grid_coord] = chunk->getComponent<TerrainChunk*>();
to_keep.push_back(chunk);
}
}
for (size_t j = 0 ; j < getTerrainSize() ; ++j)
{
for (size_t k = 0 ; k < getTerrainSize() ; ++k)
{
glm::ivec3 chunk_grid_coord = min_grid_coord + glm::ivec3(i, j, k);

auto res = m_grid_to_chunk_map.find(chunk_grid_coord);

if (res != m_grid_to_chunk_map.end()) // Trouvé
{
TerrainChunk* terrain_chunk = res->second;

m_grid_to_chunk_map[chunk_grid_coord] = terrain_chunk;
to_keep.push_back(terrain_chunk->getGameObject());
}
else
{
// Si le chunk n'est pas dans la map il faut le creer
GameObject* chunk = createTerrainChunk(toWorldGridCoord(chunk_grid_coord));
m_gameobject->addChild(chunk);

m_grid_to_chunk_map[chunk_grid_coord] = chunk->getComponent<TerrainChunk*>();
to_keep.push_back(chunk);
}
}
}
}

// Efface les Chunks trop loin de la position world_coord
Expand Down Expand Up @@ -249,12 +251,11 @@ TerrainChunk* TerrainManager::getPlayerChunk() {
return getChunkAt(getPlayerCoord());
}

TerrainChunk* TerrainManager::getChunkAt(glm::vec3 world_coord) {
glm::vec3 chunk_coord = toChunkGridCoord(world_coord) * glm::ivec3(getChunkSize());

auto res = std::find_if(std::begin(m_gameobject->m_listOfChildren), std::end(m_gameobject->m_listOfChildren), [&](GameObject* go){return go->getTransform()->getPosition() == chunk_coord;});
if (res != std::end(m_gameobject->m_listOfChildren))
return (*res)->getComponent<TerrainChunk*>();
TerrainChunk* TerrainManager::getChunkAt(glm::vec3 world_coord) {
auto res = m_grid_to_chunk_map.find(toChunkGridCoord(world_coord));

if (res != m_grid_to_chunk_map.end())
return res->second;
else
return nullptr;
}
Expand All @@ -280,7 +281,6 @@ void TerrainManager::setVoxelAt(glm::vec3 world_coord, Voxel v) {
glm::uvec3 voxel = toVoxelCoord(world_coord);
chunk->voxels(voxel.x, voxel.y, voxel.z) = v;
chunk->needUpdate = true;
// std::cerr << "set\n";
}
}

Expand Down

0 comments on commit 016219e

Please sign in to comment.