OpenMiner/source/world/World.cpp

152 lines
4.6 KiB
C++
Raw Normal View History

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
*
* Author: Quentin Bazin, <quent42340@gmail.com>
2014-12-18 07:02:48 +01:00
*
* =====================================================================================
*/
#define GLM_FORCE_RADIANS
#include <glm/gtc/matrix_transform.hpp>
#include "Camera.hpp"
#include "Config.hpp"
2014-12-18 07:02:48 +01:00
#include "World.hpp"
// Block *World::selectedBlock = nullptr;
// Chunk *World::selectedChunk = nullptr;
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++) {
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));
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
}
}
}
void World::draw(Shader &shader, const glm::mat4 &projectionMatrix, const glm::mat4 &viewMatrix) {
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
2014-12-25 23:37:32 +01:00
shader.setUniform("u_renderDistance", renderDistance * Chunk::width);
2018-06-05 01:24:54 +02:00
2014-12-18 07:02:48 +01:00
for(auto &it : m_chunks) {
2014-12-29 03:14:09 +01: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 04:19:16 +02:00
// Is the chunk close enough?
2014-12-29 03:14:09 +01:00
glm::vec4 center = viewMatrix * 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?
2014-12-29 03:14:09 +01:00
center = 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
2014-12-18 07:02:48 +01:00
// It 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
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
2014-12-29 03:14:09 +01:00
shader.setUniform("u_modelMatrix", modelMatrix);
2018-06-05 01:24:54 +02:00
2014-12-18 07:02:48 +01:00
it->draw(shader);
}
2018-06-05 01:24:54 +02:00
2014-12-20 15:13:51 +01:00
if(ud < 1000) {
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
}
Chunk *World::getChunk(int cx, int cz) {
cx += m_width / 2;
cz += m_depth / 2;
if (cx < 0 || cx >= m_width /*|| cy < 0 || cy > m_height */ || cz < 0 || cz >= m_depth)
return nullptr;
return m_chunks.at(cx + cz * m_width).get();
}
Block *World::getBlock(int x, int y, int z) {
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;
if (cx < 0 || cx >= m_width || cy < 0 || cy > m_height || cz < 0 || cz >= m_depth)
return nullptr;
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;
}
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;
if (cx < 0 || cx >= m_width || cy < 0 || cy > m_height || cz < 0 || cz >= m_depth)
return;
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);
}