2014-12-18 07:02:48 +01:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
|
|
|
* Filename: World.cpp
|
|
|
|
*
|
2018-06-05 01:24:54 +02:00
|
|
|
* Description:
|
2014-12-18 07:02:48 +01:00
|
|
|
*
|
|
|
|
* Created: 16/12/2014 15:28:19
|
|
|
|
*
|
2018-06-12 09:24:43 +02:00
|
|
|
* Author: Quentin Bazin, <quent42340@gmail.com>
|
2014-12-18 07:02:48 +01:00
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#define GLM_FORCE_RADIANS
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
|
2018-06-13 03:47:20 +02:00
|
|
|
#include "Camera.hpp"
|
|
|
|
#include "Config.hpp"
|
2014-12-18 07:02:48 +01:00
|
|
|
#include "World.hpp"
|
|
|
|
|
2018-06-14 01:42:54 +02:00
|
|
|
// Block *World::selectedBlock = nullptr;
|
|
|
|
// Chunk *World::selectedChunk = nullptr;
|
2018-06-13 03:47:20 +02:00
|
|
|
|
2014-12-18 07:02:48 +01:00
|
|
|
World::World() {
|
2015-02-06 01:44:16 +01:00
|
|
|
//m_texture.load("textures/cobblestone.bmp");
|
|
|
|
m_texture.load("textures/blocks.png");
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2014-12-20 15:13:51 +01:00
|
|
|
for(s32 z = -m_depth / 2 ; z < m_depth / 2 ; z++) {
|
|
|
|
for(s32 x = -m_width / 2 ; x < m_width / 2 ; x++) {
|
2015-02-06 01:44:16 +01:00
|
|
|
m_chunks.push_back(std::unique_ptr<Chunk>(new Chunk(x, 0, z, m_texture)));
|
2014-12-18 07:02:48 +01:00
|
|
|
}
|
|
|
|
}
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2014-12-20 15:13:51 +01:00
|
|
|
for(s32 z = -m_depth / 2 ; z < m_depth / 2 ; z++) {
|
|
|
|
for(s32 x = -m_width / 2 ; x < m_width / 2 ; x++) {
|
2018-06-14 01:42:54 +02:00
|
|
|
if(x > -m_width / 2) getChunk(x, z)->setLeft(getChunk(x - 1, z));
|
2014-12-20 15:13:51 +01:00
|
|
|
if(x < m_width / 2 - 1) getChunk(x, z)->setRight(getChunk(x + 1, z));
|
2018-06-14 01:42:54 +02:00
|
|
|
if(z > -m_depth / 2) getChunk(x, z)->setFront(getChunk(x, z - 1));
|
2014-12-20 15:13:51 +01:00
|
|
|
if(z < m_depth / 2 - 1) getChunk(x, z)->setBack(getChunk(x, z + 1));
|
2014-12-18 07:02:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 23:07:20 +02:00
|
|
|
void World::updateChunks() {
|
|
|
|
// FIXME
|
|
|
|
for (auto &it : m_chunks) {
|
|
|
|
it->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 22:11:25 +02:00
|
|
|
void World::draw(RenderTarget &target, RenderStates states) const {
|
2014-12-20 15:13:51 +01:00
|
|
|
float ud = 1000.0;
|
2018-06-14 04:19:16 +02:00
|
|
|
int ux = 0;
|
|
|
|
// int uy = 0;
|
|
|
|
int uz = 0;
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-14 22:11:25 +02:00
|
|
|
Shader::bind(states.shader);
|
|
|
|
states.shader->setUniform("u_renderDistance", renderDistance * Chunk::width);
|
|
|
|
Shader::bind(nullptr);
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2014-12-18 07:02:48 +01:00
|
|
|
for(auto &it : m_chunks) {
|
2018-06-18 12:24:46 +02:00
|
|
|
glm::mat4 modelMatrix{glm::translate(glm::mat4(1.0f),
|
|
|
|
glm::vec3(it->x() * Chunk::width,
|
|
|
|
it->y() * Chunk::height,
|
|
|
|
it->z() * Chunk::depth))};
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-14 22:11:25 +02:00
|
|
|
states.modelMatrix = &modelMatrix;
|
|
|
|
|
2018-06-14 04:19:16 +02:00
|
|
|
// Is the chunk close enough?
|
2018-06-14 22:11:25 +02:00
|
|
|
glm::vec4 center = *states.viewMatrix * *states.modelMatrix * glm::vec4(Chunk::width / 2,
|
|
|
|
Chunk::height / 2,
|
|
|
|
Chunk::depth / 2, 1);
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2014-12-29 03:14:09 +01:00
|
|
|
if(glm::length(center) > (renderDistance + 1) * Chunk::width) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2014-12-18 07:02:48 +01:00
|
|
|
// Is this chunk on the screen?
|
2018-06-14 22:11:25 +02:00
|
|
|
center = *states.projectionMatrix * center;
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2014-12-18 07:02:48 +01:00
|
|
|
float d = glm::length(center);
|
|
|
|
center.x /= center.w;
|
|
|
|
center.y /= center.w;
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2014-12-18 07:02:48 +01:00
|
|
|
// If it is behind the camera, don't bother drawing it
|
2014-12-20 01:46:31 +01:00
|
|
|
if(center.z < -Chunk::height / 2) {
|
2014-12-18 07:02:48 +01:00
|
|
|
continue;
|
2014-12-20 01:46:31 +01:00
|
|
|
}
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-14 22:11:25 +02:00
|
|
|
// If it is outside the screen, don't bother drawing it
|
2014-12-23 16:15:44 +01:00
|
|
|
if(fabsf(center.x) > 1 + fabsf(Chunk::height * 2 / center.w)
|
|
|
|
|| fabsf(center.y) > 1 + fabsf(Chunk::height * 2 / center.w)) {
|
2014-12-18 07:02:48 +01:00
|
|
|
continue;
|
2014-12-20 01:46:31 +01:00
|
|
|
}
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-14 04:19:16 +02:00
|
|
|
// If this chunk is not initialized, skip it
|
2018-06-12 23:08:42 +02:00
|
|
|
if(!it->isInitialized()) {
|
2018-06-14 04:19:16 +02:00
|
|
|
// But if it is the closest to the camera, mark it for initialization
|
2014-12-20 15:13:51 +01:00
|
|
|
if(d < ud) {
|
2014-12-18 07:02:48 +01:00
|
|
|
ud = d;
|
|
|
|
ux = it->x();
|
2018-06-14 04:19:16 +02:00
|
|
|
// ux = it->y();
|
2014-12-18 07:02:48 +01:00
|
|
|
uz = it->z();
|
|
|
|
}
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2014-12-18 07:02:48 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2018-06-14 22:11:25 +02:00
|
|
|
Shader::bind(states.shader);
|
2018-06-14 23:07:20 +02:00
|
|
|
// states.shader->setUniform("u_modelMatrix", modelMatrix);
|
|
|
|
target.draw(*it, states);
|
2018-06-14 22:11:25 +02:00
|
|
|
Shader::bind(nullptr);
|
2014-12-18 07:02:48 +01:00
|
|
|
}
|
2018-06-05 01:24:54 +02:00
|
|
|
|
2014-12-20 15:13:51 +01:00
|
|
|
if(ud < 1000) {
|
2018-06-12 23:08:42 +02:00
|
|
|
m_terrainGenerator.generate(*getChunk(ux, uz));
|
|
|
|
|
|
|
|
if(getChunk(ux, uz)->left()) m_terrainGenerator.generate(*getChunk(ux, uz)->left());
|
|
|
|
if(getChunk(ux, uz)->right()) m_terrainGenerator.generate(*getChunk(ux, uz)->right());
|
|
|
|
if(getChunk(ux, uz)->front()) m_terrainGenerator.generate(*getChunk(ux, uz)->front());
|
|
|
|
if(getChunk(ux, uz)->back()) m_terrainGenerator.generate(*getChunk(ux, uz)->back());
|
2018-06-14 04:19:16 +02:00
|
|
|
|
|
|
|
getChunk(ux, uz)->setInitialized(true);
|
2014-12-18 07:02:48 +01:00
|
|
|
}
|
2014-12-20 15:13:51 +01:00
|
|
|
}
|
|
|
|
|
2018-06-14 22:11:25 +02:00
|
|
|
Chunk *World::getChunk(int cx, int cz) const {
|
2018-06-14 01:42:54 +02:00
|
|
|
cx += m_width / 2;
|
|
|
|
cz += m_depth / 2;
|
2018-06-13 03:47:20 +02:00
|
|
|
|
2018-06-14 01:42:54 +02:00
|
|
|
if (cx < 0 || cx >= m_width /*|| cy < 0 || cy > m_height */ || cz < 0 || cz >= m_depth)
|
|
|
|
return nullptr;
|
2018-06-13 03:47:20 +02:00
|
|
|
|
2018-06-14 01:42:54 +02:00
|
|
|
return m_chunks.at(cx + cz * m_width).get();
|
2018-06-13 03:47:20 +02:00
|
|
|
}
|
|
|
|
|
2018-06-14 23:36:01 +02:00
|
|
|
Block *World::getBlock(int x, int y, int z) const {
|
2018-06-14 01:42:54 +02:00
|
|
|
s32 cx = (x + Chunk::width * (m_width / 2)) / Chunk::width;
|
|
|
|
s32 cy = (y + Chunk::height * (m_height / 2)) / Chunk::height;
|
|
|
|
s32 cz = (z + Chunk::depth * (m_depth / 2)) / Chunk::depth;
|
2018-06-13 03:47:20 +02:00
|
|
|
|
2018-06-14 01:42:54 +02:00
|
|
|
if (cx < 0 || cx >= m_width || cy < 0 || cy > m_height || cz < 0 || cz >= m_depth)
|
|
|
|
return nullptr;
|
2018-06-13 03:47:20 +02:00
|
|
|
|
2018-06-14 01:42:54 +02:00
|
|
|
Chunk *chunk = m_chunks.at(cx + cz * m_width).get();
|
|
|
|
if (chunk)
|
|
|
|
return chunk->getBlock(x & (Chunk::width - 1), y & (Chunk::height - 1), z & (Chunk::depth - 1));
|
|
|
|
return nullptr;
|
2018-06-13 03:47:20 +02:00
|
|
|
}
|
|
|
|
|
2018-06-14 01:42:54 +02:00
|
|
|
void World::setBlock(int x, int y, int z, u32 id) {
|
|
|
|
s32 cx = (x + Chunk::width * (m_width / 2)) / Chunk::width;
|
|
|
|
s32 cy = (y + Chunk::height * (m_height / 2)) / Chunk::height;
|
|
|
|
s32 cz = (z + Chunk::depth * (m_depth / 2)) / Chunk::depth;
|
2018-06-13 03:47:20 +02:00
|
|
|
|
2018-06-14 01:42:54 +02:00
|
|
|
if (cx < 0 || cx >= m_width || cy < 0 || cy > m_height || cz < 0 || cz >= m_depth)
|
|
|
|
return;
|
2018-06-13 21:48:35 +02:00
|
|
|
|
2018-06-14 01:42:54 +02:00
|
|
|
Chunk *chunk = m_chunks.at(cx + cz * m_width).get();
|
|
|
|
if (chunk)
|
|
|
|
chunk->setBlock(x & (Chunk::width - 1), y & (Chunk::height - 1), z & (Chunk::depth - 1), id);
|
2018-06-13 03:47:20 +02:00
|
|
|
}
|
|
|
|
|