Compare commits
38 Commits
fix-multit
...
main
Author | SHA1 | Date |
---|---|---|
EmaMaker | fa3d36ec58 | |
EmaMaker | ebef608000 | |
EmaMaker | 0b294bb35b | |
EmaMaker | f3d8ffed54 | |
EmaMaker | 0560fd43c6 | |
EmaMaker | 62212a22ab | |
EmaMaker | 357f67aac1 | |
EmaMaker | 1b4cef8958 | |
EmaMaker | e4c6eb50f3 | |
EmaMaker | 9b5939d256 | |
EmaMaker | 8401a3dca7 | |
EmaMaker | 255460892d | |
EmaMaker | afdd622ec2 | |
EmaMaker | 93bc0e7066 | |
EmaMaker | 8544620899 | |
EmaMaker | a4f1e5fc1f | |
EmaMaker | 78b65894b7 | |
EmaMaker | 355da726f6 | |
EmaMaker | ba95db4678 | |
EmaMaker | 7c82a71dd0 | |
EmaMaker | 7786d4f04d | |
EmaMaker | 1822911845 | |
EmaMaker | d0ddf2256f | |
EmaMaker | 88abf21502 | |
EmaMaker | f4947d5f70 | |
EmaMaker | 4e7fadd2b9 | |
EmaMaker | d1b151f92f | |
EmaMaker | 44edf3e53a | |
EmaMaker | 3adb061057 | |
EmaMaker | 1a50d1fb84 | |
EmaMaker | 60bbc85682 | |
EmaMaker | 490f207e39 | |
EmaMaker | c6d00c4200 | |
EmaMaker | 2a57796ed2 | |
EmaMaker | ca043bac68 | |
EmaMaker | f6f4057109 | |
EmaMaker | 353ef37186 | |
EmaMaker | 880c634be0 |
|
@ -18,6 +18,10 @@ public:
|
|||
|
||||
// This matrix needs to be also updated in viewPortCallback whenever it is changed
|
||||
projection = glm::perspective(glm::radians(90.0f), 800.0f / 600.0f, 0.1f, 1200.0f);
|
||||
|
||||
posX = cameraPos.x;
|
||||
posY = cameraPos.y;
|
||||
posZ = cameraPos.z;
|
||||
}
|
||||
|
||||
void update(GLFWwindow *window, float deltaTime)
|
||||
|
@ -112,7 +116,7 @@ public:
|
|||
|
||||
|
||||
private:
|
||||
glm::vec3 cameraPos = glm::vec3(256.0, 80.0f, 256.0f);
|
||||
glm::vec3 cameraPos = glm::vec3(512.0, 80.0f, 512.0f);
|
||||
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||
glm::vec3 direction = glm::vec3(0.0f);
|
||||
|
|
|
@ -20,16 +20,27 @@
|
|||
#define CHUNK_VOLUME (CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE)
|
||||
#define CHUNK_MAX_INDEX (CHUNK_VOLUME - 1)
|
||||
|
||||
// int32_t is fine, since i'm limiting the coordinate to only use up to ten bits (1023). There's actually two spare bits
|
||||
typedef int32_t chunk_index_t;
|
||||
typedef int16_t chunk_intcoord_t;
|
||||
typedef uint16_t chunk_state_t;
|
||||
|
||||
namespace Chunk
|
||||
{
|
||||
|
||||
constexpr uint8_t CHUNK_STATE_GENERATED = 1;
|
||||
constexpr uint8_t CHUNK_STATE_MESHED = 2;
|
||||
constexpr uint8_t CHUNK_STATE_MESH_LOADED = 4;
|
||||
constexpr uint8_t CHUNK_STATE_LOADED = 8;
|
||||
constexpr uint8_t CHUNK_STATE_OUTOFVISION = 16;
|
||||
constexpr uint8_t CHUNK_STATE_UNLOADED = 32;
|
||||
constexpr uint8_t CHUNK_STATE_EMPTY = 64;
|
||||
chunk_index_t calculateIndex(chunk_intcoord_t i, chunk_intcoord_t j, chunk_intcoord_t k);
|
||||
chunk_index_t calculateIndex(glm::vec3 pos);
|
||||
|
||||
constexpr chunk_state_t CHUNK_STATE_GENERATED = 1;
|
||||
constexpr chunk_state_t CHUNK_STATE_MESHED = 2;
|
||||
constexpr chunk_state_t CHUNK_STATE_MESH_LOADED = 4;
|
||||
constexpr chunk_state_t CHUNK_STATE_LOADED = 8;
|
||||
constexpr chunk_state_t CHUNK_STATE_OUTOFVISION = 16;
|
||||
constexpr chunk_state_t CHUNK_STATE_UNLOADED = 32;
|
||||
constexpr chunk_state_t CHUNK_STATE_EMPTY = 64;
|
||||
constexpr chunk_state_t CHUNK_STATE_IN_GENERATION_QUEUE = 128;
|
||||
constexpr chunk_state_t CHUNK_STATE_IN_MESHING_QUEUE = 256;
|
||||
constexpr chunk_state_t CHUNK_STATE_IN_DELETING_QUEUE = 512;
|
||||
|
||||
int coord3DTo1D(int x, int y, int z);
|
||||
|
||||
|
@ -41,13 +52,15 @@ namespace Chunk
|
|||
~Chunk();
|
||||
|
||||
public:
|
||||
void createBuffers();
|
||||
void deleteBuffers();
|
||||
|
||||
glm::vec3 getPosition() { return this->position; }
|
||||
uint8_t getTotalState() { return this->state; }
|
||||
bool getState(uint8_t n) { return (this->state & n) == n; }
|
||||
void setState(uint8_t nstate, bool value);
|
||||
void setState(chunk_state_t nstate, bool value);
|
||||
bool getState(chunk_state_t n) { return (this->state & n) == n; }
|
||||
bool isFree(){ return !(
|
||||
this->getState(CHUNK_STATE_IN_GENERATION_QUEUE) ||
|
||||
this->getState(CHUNK_STATE_IN_MESHING_QUEUE) ||
|
||||
this->getState(CHUNK_STATE_IN_DELETING_QUEUE)
|
||||
); }
|
||||
chunk_state_t getTotalState() { return this->state; }
|
||||
|
||||
void setBlock(Block b, int x, int y, int z);
|
||||
void setBlocks(int start, int end, Block b);
|
||||
|
@ -56,14 +69,15 @@ namespace Chunk
|
|||
std::unique_ptr<Block[]> getBlocksArray(int* len) { return (this->blocks.toArray(len)); }
|
||||
|
||||
public:
|
||||
GLuint VAO{0}, VBO{0}, extentsBuffer{0}, texinfoBuffer{0}, numVertices{0};
|
||||
std::atomic<float> unload_timer{0};
|
||||
chunk_index_t getIndex(){ return this->index; }
|
||||
|
||||
private:
|
||||
glm::vec3 position{};
|
||||
IntervalMap<Block> blocks{};
|
||||
|
||||
std::atomic_uint8_t state{0};
|
||||
std::atomic<chunk_state_t> state{0};
|
||||
chunk_index_t index;
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
#ifndef CHUNKMANAGER_H
|
||||
#define CHUNKMANAGER_H
|
||||
|
||||
#include "chunk.hpp"
|
||||
|
||||
#include <oneapi/tbb/concurrent_hash_map.h>
|
||||
#include <oneapi/tbb/concurrent_queue.h>
|
||||
#include <oneapi/tbb/concurrent_priority_queue.h>
|
||||
#include <thread>
|
||||
|
||||
#include "chunk.hpp"
|
||||
#include "globals.hpp"
|
||||
#include "worldupdatemessage.h"
|
||||
|
||||
// Seconds to be passed outside of render distance for a chunk to be destroyed
|
||||
#define UNLOAD_TIMEOUT 10
|
||||
|
@ -19,7 +19,7 @@
|
|||
|
||||
namespace chunkmanager
|
||||
{
|
||||
typedef oneapi::tbb::concurrent_hash_map<uint32_t, Chunk::Chunk*> ChunkTable;
|
||||
typedef oneapi::tbb::concurrent_hash_map<chunk_index_t, Chunk::Chunk*> ChunkTable;
|
||||
typedef std::pair<Chunk::Chunk*, uint8_t> ChunkPQEntry;
|
||||
// The comparing function to use
|
||||
struct compare_f {
|
||||
|
@ -30,15 +30,12 @@ namespace chunkmanager
|
|||
typedef oneapi::tbb::concurrent_priority_queue<ChunkPQEntry, compare_f> ChunkPriorityQueue;
|
||||
|
||||
void init();
|
||||
void blockpick(bool place);
|
||||
uint32_t calculateIndex(uint16_t i, uint16_t j, uint16_t k);
|
||||
|
||||
void update();
|
||||
void stop();
|
||||
void destroy();
|
||||
oneapi::tbb::concurrent_queue<Chunk::Chunk*>& getDeleteVector();
|
||||
std::array<std::array<int, 3>, chunks_volume>& getChunksIndices();
|
||||
WorldUpdateMsgQueue& getWorldUpdateQueue();
|
||||
std::array<std::array<chunk_intcoord_t, 3>, chunks_volume>& getChunksIndices();
|
||||
Block getBlockAtPos(int x, int y, int z);
|
||||
void update();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
#ifndef CHUNK_MESH_DATA_H
|
||||
#define CHUNK_MESH_DATA_H
|
||||
|
||||
#include <oneapi/tbb/concurrent_queue.h>
|
||||
#include "chunk.hpp"
|
||||
|
||||
enum class ChunkMeshDataType{
|
||||
MESH_UPDATE
|
||||
};
|
||||
|
||||
typedef struct ChunkMeshData{
|
||||
chunk_index_t index;
|
||||
glm::vec3 position;
|
||||
int num_vertices = 0;
|
||||
|
||||
std::vector<GLfloat> vertices;
|
||||
std::vector<GLfloat> extents;
|
||||
std::vector<GLfloat> texinfo;
|
||||
|
||||
ChunkMeshDataType message_type;
|
||||
|
||||
void clear(){
|
||||
vertices.clear();
|
||||
texinfo.clear();
|
||||
extents.clear();
|
||||
index = 0;
|
||||
position = glm::vec3(0);
|
||||
num_vertices = 0;
|
||||
}
|
||||
|
||||
}ChunkMeshData;
|
||||
typedef oneapi::tbb::concurrent_queue<ChunkMeshData*> ChunkMeshDataQueue;
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -5,10 +5,12 @@
|
|||
#include <vector>
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <oneapi/tbb/concurrent_queue.h>
|
||||
|
||||
#include "chunk.hpp"
|
||||
#include "chunkmeshdata.hpp"
|
||||
#include "globals.hpp"
|
||||
#include "shader.hpp"
|
||||
|
||||
|
@ -21,13 +23,12 @@ namespace chunkmesher{
|
|||
std::vector<GLfloat> extents;
|
||||
std::vector<GLfloat> texinfo;
|
||||
};
|
||||
oneapi::tbb::concurrent_queue<MeshData*>& getMeshDataQueue();
|
||||
|
||||
ChunkMeshDataQueue& getMeshDataQueue();
|
||||
void init();
|
||||
void mesh(Chunk::Chunk* chunk);
|
||||
void sendtogpu(MeshData* mesh_data);
|
||||
void quad(MeshData* mesh_data, glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight,
|
||||
glm::vec3 bottomRight, glm::vec3 normal, Block block, int dim, bool backFace);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef CONTROLS_H
|
||||
#define CONTROLS_H
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#define BLOCKPICK_TIMEOUT 0.1f
|
||||
|
||||
namespace controls{
|
||||
void init();
|
||||
void update(GLFWwindow* window);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -13,7 +13,8 @@
|
|||
#define RENDER_DISTANCE 16
|
||||
|
||||
extr Camera theCamera;
|
||||
constexpr int chunks_volume = static_cast<int>(1.333333333333*M_PI*(RENDER_DISTANCE*RENDER_DISTANCE*RENDER_DISTANCE));
|
||||
// the cube spans in both directions, to each axis has to be multiplied by 2. 2^3=8
|
||||
constexpr int chunks_volume = 8*(RENDER_DISTANCE*RENDER_DISTANCE*RENDER_DISTANCE);
|
||||
extr bool wireframe;
|
||||
|
||||
extr float sines[360];
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow *, int, int);
|
||||
void mouse_callback(GLFWwindow *window, double xpos, double ypos);
|
||||
void processInput(GLFWwindow *);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,12 +6,43 @@
|
|||
|
||||
#include "chunk.hpp"
|
||||
#include "chunkmesher.hpp"
|
||||
#include "chunkmeshdata.hpp"
|
||||
#include "shader.hpp"
|
||||
|
||||
namespace renderer{
|
||||
typedef oneapi::tbb::concurrent_unordered_set<Chunk::Chunk*> RenderSet;
|
||||
typedef struct RenderInfo {
|
||||
chunk_index_t index;
|
||||
int num_vertices;
|
||||
glm::vec3 position;
|
||||
bool buffers_allocated=false;
|
||||
|
||||
GLuint VAO, VBO, extentsBuffer, texinfoBuffer;
|
||||
|
||||
void allocateBuffers(){
|
||||
// Allocate buffers
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glGenBuffers(1, &extentsBuffer);
|
||||
glGenBuffers(1, &texinfoBuffer);
|
||||
|
||||
buffers_allocated=true;
|
||||
}
|
||||
|
||||
void deallocateBuffers(){
|
||||
// Allocate buffers
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glDeleteBuffers(1, &extentsBuffer);
|
||||
glDeleteBuffers(1, &texinfoBuffer);
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
|
||||
buffers_allocated=false;
|
||||
}
|
||||
} RenderInfo;
|
||||
|
||||
typedef oneapi::tbb::concurrent_queue<int32_t> IndexQueue;
|
||||
|
||||
void init(GLFWwindow* window);
|
||||
void send_chunk_to_gpu(ChunkMeshData* mesh_data, RenderInfo* render_info);
|
||||
void render();
|
||||
void resize_framebuffer(int width, int height);
|
||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
||||
|
@ -20,9 +51,11 @@ namespace renderer{
|
|||
void saveScreenshot(bool forceFullHD=false);
|
||||
|
||||
Shader* getRenderShader();
|
||||
RenderSet& getChunksToRender();
|
||||
oneapi::tbb::concurrent_queue<chunkmesher::MeshData*>& getMeshDataQueue();
|
||||
ChunkMeshDataQueue& getMeshDataQueue();
|
||||
IndexQueue& getDeleteIndexQueue();
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef WORLD_UPDATE_MSG_H
|
||||
#define WORLD_UPDATE_MSG_H
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <oneapi/tbb/concurrent_queue.h>
|
||||
|
||||
#include "block.hpp"
|
||||
|
||||
enum class WorldUpdateMsgType{
|
||||
BLOCKPICK_PLACE,
|
||||
BLOCKPICK_BREAK
|
||||
};
|
||||
|
||||
typedef struct WorldUpdateMsg{
|
||||
WorldUpdateMsgType msg_type;
|
||||
glm::vec3 cameraPos;
|
||||
glm::vec3 cameraFront;
|
||||
float time;
|
||||
Block block;
|
||||
} WorldUpdateMsg;
|
||||
|
||||
typedef oneapi::tbb::concurrent_queue<WorldUpdateMsg> WorldUpdateMsgQueue;
|
||||
|
||||
#endif
|
143
intervalmap.hpp
143
intervalmap.hpp
|
@ -1,143 +0,0 @@
|
|||
#ifndef INTERVALMAP_H
|
||||
#define INTERVALMAP_H
|
||||
|
||||
#include <iostream>
|
||||
#include <iterator> //std::prev
|
||||
#include <limits> // std::numeric_limits
|
||||
#include <memory> //std::shared_ptr
|
||||
#include <map>
|
||||
|
||||
template <typename K, typename V>
|
||||
class IntervalMap
|
||||
{
|
||||
|
||||
public:
|
||||
~IntervalMap(){
|
||||
treemap.clear();
|
||||
}
|
||||
|
||||
void insert(K start, K end, V value)
|
||||
{
|
||||
if (start >= end)
|
||||
return;
|
||||
|
||||
// The entry just before the end index
|
||||
auto tmp = treemap.upper_bound(end);
|
||||
auto end_prev_entry = tmp == treemap.end() ? tmp : --tmp;
|
||||
auto added_end = treemap.end();
|
||||
|
||||
// If it doesn't exist (empty map)
|
||||
if(end_prev_entry == treemap.end()){
|
||||
V v{};
|
||||
added_end = treemap.insert_or_assign(treemap.begin(), end, v);
|
||||
}
|
||||
// Or if it has value different from the insertion
|
||||
else if(end_prev_entry->second != value)
|
||||
// Add it back at the end
|
||||
added_end = treemap.insert_or_assign(end_prev_entry, end, end_prev_entry->second);
|
||||
|
||||
// The entry just before the start index
|
||||
tmp = treemap.upper_bound(start);
|
||||
auto start_prev_entry = tmp == treemap.end() ? tmp : --tmp;
|
||||
auto added_start = treemap.end();
|
||||
// If it has value different from the insertion
|
||||
if(start_prev_entry == treemap.end() || start_prev_entry->second != value)
|
||||
// Add the start node of the insertion
|
||||
added_start = treemap.insert_or_assign(start_prev_entry, start, value);
|
||||
|
||||
// Delete everything else inside
|
||||
// map.erase(start, end) deletes every node with key in the range [start, end)
|
||||
|
||||
// The key to start deleting from is the key after the start node we added
|
||||
// (We don't want to delete a node we just added)
|
||||
auto del_start = added_start == treemap.end() ? std::next(start_prev_entry) : std::next(added_start);
|
||||
auto del_end = added_end == treemap.end() ? end_prev_entry : added_end;
|
||||
auto del_end_next = std::next(del_end);
|
||||
|
||||
// If the node after the end is of the same type of the end, delete it
|
||||
// We cannot just expand del_end (++del_end) otherwise interval limits get messed up
|
||||
if(del_end != treemap.end() && del_end_next != treemap.end() && del_end->second ==
|
||||
del_end_next->second) treemap.erase(del_end_next);
|
||||
|
||||
// Delete everything in between
|
||||
if(del_start != treemap.end() && (del_end==treemap.end() || del_start->first <
|
||||
del_end->first)) treemap.erase(del_start, del_end);
|
||||
}
|
||||
|
||||
void remove(K at)
|
||||
{
|
||||
treemap.erase(at);
|
||||
}
|
||||
|
||||
auto at(K index)
|
||||
{
|
||||
const auto tmp = treemap.lower_bound(index);
|
||||
const auto r = tmp != treemap.begin() && tmp->first!=index ? std::prev(tmp) : tmp;
|
||||
return r;
|
||||
}
|
||||
|
||||
void print()
|
||||
{
|
||||
for (auto i = treemap.begin(); i != treemap.end(); i++)
|
||||
std::cout << i->first << ": " << (int)(i->second) << "\n";
|
||||
if(!treemap.empty()) std::cout << "end key: " << std::prev(treemap.end())->first << "\n";
|
||||
}
|
||||
|
||||
std::unique_ptr<V[]> toArray(int *length)
|
||||
{
|
||||
if (treemap.empty())
|
||||
{
|
||||
*length = 0;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const auto &end = std::prev(treemap.end());
|
||||
*length = end->first;
|
||||
if(*length == 0) return nullptr;
|
||||
|
||||
std::unique_ptr<V[]> arr(new V[*length]);
|
||||
auto start = treemap.begin();
|
||||
for (auto i = std::next(treemap.begin()); i != treemap.end(); i++)
|
||||
{
|
||||
for (int k = start->first; k < i->first; k++)
|
||||
arr[k] = start->second;
|
||||
start = i;
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
void fromArray(V *arr, int length)
|
||||
{
|
||||
treemap.clear();
|
||||
|
||||
if (length == 0)
|
||||
return;
|
||||
|
||||
V prev = arr[0];
|
||||
unsigned int prev_start = 0;
|
||||
for (unsigned int i = 1; i < length; i++)
|
||||
{
|
||||
if (prev != arr[i])
|
||||
{
|
||||
insert(prev_start, i, prev);
|
||||
prev_start = i;
|
||||
}
|
||||
prev = arr[i];
|
||||
}
|
||||
insert(prev_start, length, prev);
|
||||
}
|
||||
|
||||
auto begin(){
|
||||
return treemap.begin();
|
||||
}
|
||||
|
||||
auto end(){
|
||||
return treemap.end();
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<K, V> treemap{};
|
||||
};
|
||||
|
||||
#endif
|
|
@ -26,29 +26,30 @@ void main(){
|
|||
EmitVertex();
|
||||
|
||||
if(gs_in[0].Extents.x == 0){
|
||||
TexCoord = vec3(0.0, gs_in[0].Extents.z, gs_in[0].BlockType);
|
||||
|
||||
TexCoord = vec3(gs_in[0].Extents.z, 0.0, gs_in[0].BlockType);
|
||||
gl_Position = gl_in[0].gl_Position + vec4(0.0, 0.0, gs_in[0].Extents.z, 0.0);
|
||||
FragPos = vec3(gl_Position);
|
||||
gl_Position = projection * view * gl_Position;
|
||||
EmitVertex();
|
||||
|
||||
TexCoord = vec3(gs_in[0].Extents.y, 0.0, gs_in[0].BlockType);
|
||||
TexCoord = vec3(0.0, gs_in[0].Extents.y, gs_in[0].BlockType);
|
||||
gl_Position = gl_in[0].gl_Position + vec4(0.0, gs_in[0].Extents.y, 0.0, 0.0);
|
||||
FragPos = vec3(gl_Position);
|
||||
gl_Position = projection * view * gl_Position;
|
||||
EmitVertex();
|
||||
|
||||
TexCoord = vec3(gs_in[0].Extents.y, gs_in[0].Extents.z, gs_in[0].BlockType);
|
||||
TexCoord = vec3(gs_in[0].Extents.z, gs_in[0].Extents.y, gs_in[0].BlockType);
|
||||
}
|
||||
else if(gs_in[0].Extents.y == 0){
|
||||
TexCoord = vec3(0.0, gs_in[0].Extents.z, gs_in[0].BlockType);
|
||||
gl_Position = gl_in[0].gl_Position + vec4(0.0, 0.0, gs_in[0].Extents.z, 0.0);
|
||||
TexCoord = vec3(gs_in[0].Extents.x, 0.0, gs_in[0].BlockType);
|
||||
gl_Position = gl_in[0].gl_Position + vec4(gs_in[0].Extents.x, 0.0, 0.0, 0.0);
|
||||
FragPos = vec3(gl_Position);
|
||||
gl_Position = projection * view * gl_Position;
|
||||
EmitVertex();
|
||||
|
||||
TexCoord = vec3(gs_in[0].Extents.x, 0.0, gs_in[0].BlockType);
|
||||
gl_Position = gl_in[0].gl_Position + vec4(gs_in[0].Extents.x, 0.0, 0.0, 0.0);
|
||||
TexCoord = vec3(0.0, gs_in[0].Extents.z, gs_in[0].BlockType);
|
||||
gl_Position = gl_in[0].gl_Position + vec4(0.0, 0.0, gs_in[0].Extents.z, 0.0);
|
||||
FragPos = vec3(gl_Position);
|
||||
gl_Position = projection * view * gl_Position;
|
||||
EmitVertex();
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
cmake_minimum_required(VERSION 3.2)
|
||||
project(OpenGLTest)
|
||||
|
||||
set(SOURCE_FILES main.cpp chunk.cpp chunkmanager.cpp chunkmesher.cpp chunkgenerator.cpp
|
||||
set(SOURCE_FILES main.cpp controls.cpp chunk.cpp chunkmanager.cpp chunkmesher.cpp chunkgenerator.cpp
|
||||
debugwindow.cpp renderer.cpp spacefilling.cpp stb_image.cpp utils.cpp OpenSimplexNoise.cpp)
|
||||
|
||||
add_executable(OpenGLTest ${SOURCE_FILES})
|
||||
|
|
|
@ -15,32 +15,27 @@ namespace Chunk
|
|||
return utils::coord3DTo1D(x, y, z, CHUNK_SIZE, CHUNK_SIZE, CHUNK_SIZE);
|
||||
}
|
||||
|
||||
chunk_index_t calculateIndex(glm::vec3 pos){
|
||||
return calculateIndex(static_cast<chunk_intcoord_t>(pos.x), static_cast<chunk_intcoord_t>(pos.y),
|
||||
static_cast<chunk_intcoord_t>(pos.z));
|
||||
}
|
||||
|
||||
chunk_index_t calculateIndex(chunk_intcoord_t i, chunk_intcoord_t j, chunk_intcoord_t k){
|
||||
return i | (j << 10) | (k << 20);
|
||||
}
|
||||
|
||||
Chunk::Chunk(glm::vec3 pos)
|
||||
{
|
||||
this->position = pos;
|
||||
this->setState(CHUNK_STATE_EMPTY, true);
|
||||
this->setBlocks(0, CHUNK_MAX_INDEX, Block::AIR);
|
||||
this->index = calculateIndex(pos);
|
||||
}
|
||||
|
||||
Chunk ::~Chunk()
|
||||
{
|
||||
}
|
||||
|
||||
void Chunk::createBuffers(){
|
||||
glGenVertexArrays(1, &(this->VAO));
|
||||
glGenBuffers(1, &(this->VBO));
|
||||
glGenBuffers(1, &(this->extentsBuffer));
|
||||
glGenBuffers(1, &(this->texinfoBuffer));
|
||||
|
||||
}
|
||||
|
||||
void Chunk::deleteBuffers(){
|
||||
glDeleteBuffers(1, &(this->VBO));
|
||||
glDeleteBuffers(1, &(this->extentsBuffer));
|
||||
glDeleteBuffers(1, &(this->texinfoBuffer));
|
||||
glDeleteVertexArrays(1, &(this->VAO));
|
||||
}
|
||||
|
||||
Block Chunk::getBlock(int x, int y, int z)
|
||||
{
|
||||
if(x < 0 || y < 0 || z < 0 || x > CHUNK_SIZE -1 || y > CHUNK_SIZE -1 || z > CHUNK_SIZE-1 ||
|
||||
|
@ -59,7 +54,7 @@ namespace Chunk
|
|||
this->blocks.insert(start < 0 ? 0 : start, end >= CHUNK_VOLUME ? CHUNK_VOLUME : end, b);
|
||||
}
|
||||
|
||||
void Chunk::setState(uint8_t nstate, bool value)
|
||||
void Chunk::setState(chunk_state_t nstate, bool value)
|
||||
{
|
||||
if (value)
|
||||
this->state.fetch_or(nstate);
|
||||
|
|
|
@ -6,8 +6,11 @@
|
|||
#include <thread>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtx/string_cast.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
#include <oneapi/tbb/parallel_for.h>
|
||||
|
||||
#include "block.hpp"
|
||||
#include "chunk.hpp"
|
||||
#include "chunkgenerator.hpp"
|
||||
|
@ -15,88 +18,67 @@
|
|||
#include "debugwindow.hpp"
|
||||
#include "globals.hpp"
|
||||
#include "renderer.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace chunkmanager
|
||||
{
|
||||
void blockpick(WorldUpdateMsg& msg); // There's no need of passing by value again (check
|
||||
// controls.cpp)
|
||||
void generate();
|
||||
void mesh();
|
||||
void send_to_chunk_meshing_thread(Chunk::Chunk* c, int priority);
|
||||
|
||||
/* Chunk holding data structures */
|
||||
// Concurrent hash table of chunks
|
||||
ChunkTable chunks;
|
||||
// Chunk indices. Centered at (0,0,0), going in concentric sphere outwards
|
||||
std::array<std::array<int, 3>, chunks_volume> chunks_indices;
|
||||
std::array<std::array<chunk_intcoord_t, 3>, chunks_volume> chunks_indices;
|
||||
|
||||
/* World Update messaging data structure */
|
||||
WorldUpdateMsgQueue WorldUpdateQueue;
|
||||
|
||||
/* Multithreading */
|
||||
std::atomic_bool should_run;
|
||||
std::thread gen_thread, mesh_thread, update_thread;
|
||||
|
||||
// Queue of chunks to be generated
|
||||
ChunkPriorityQueue chunks_to_generate_queue;
|
||||
// Queue of chunks to be meshed
|
||||
ChunkPriorityQueue chunks_to_mesh_queue;
|
||||
|
||||
int block_to_place{2};
|
||||
|
||||
WorldUpdateMsgQueue& getWorldUpdateQueue(){ return WorldUpdateQueue; }
|
||||
|
||||
// Init chunkmanager. Chunk indices and start threads
|
||||
int chunks_volume_real;
|
||||
void init(){
|
||||
int index{0};
|
||||
int rr{RENDER_DISTANCE * RENDER_DISTANCE};
|
||||
|
||||
int xp{0}, x{0};
|
||||
bool b = true;
|
||||
for(chunk_intcoord_t i = -RENDER_DISTANCE; i < RENDER_DISTANCE; i++)
|
||||
for(chunk_intcoord_t j = -RENDER_DISTANCE; j < RENDER_DISTANCE; j++)
|
||||
for(chunk_intcoord_t k = -RENDER_DISTANCE; k < RENDER_DISTANCE; k++){
|
||||
|
||||
// Iterate over all chunks, in concentric spheres starting fron the player and going outwards. Alternate left and right
|
||||
// Eq. of the sphere (x - a)² + (y - b)² + (z - c)² = r²
|
||||
while (xp <= RENDER_DISTANCE)
|
||||
{
|
||||
// Alternate between left and right
|
||||
if (b) x = +xp;
|
||||
else x = -xp;
|
||||
|
||||
// Step 1. At current x, get the corresponding y values (2nd degree equation, up to 2
|
||||
// possible results)
|
||||
int y1 = static_cast<int>(sqrt((rr) - x*x));
|
||||
|
||||
for (int y = -y1 + 1 ; y <= y1; y++)
|
||||
{
|
||||
// Step 2. At both y's, get the corresponding z values
|
||||
int z1 = static_cast<int>(sqrt( rr - x*x - y*y));
|
||||
|
||||
for (int z = -z1 + 1; z <= z1; z++){
|
||||
chunks_indices[index][0] = x;
|
||||
chunks_indices[index][1] = y;
|
||||
chunks_indices[index][2] = z;
|
||||
chunks_indices[index][0]=i;
|
||||
chunks_indices[index][1]=j;
|
||||
chunks_indices[index][2]=k;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!b)
|
||||
{
|
||||
xp++;
|
||||
b = true;
|
||||
}
|
||||
else b = false;
|
||||
}
|
||||
chunks_volume_real = index;
|
||||
|
||||
// Also init mesh data queue
|
||||
for(int i = 0; i < 10; i++)
|
||||
chunkmesher::getMeshDataQueue().push(new chunkmesher::MeshData());
|
||||
|
||||
should_run = true;
|
||||
update_thread = std::thread(update);
|
||||
gen_thread = std::thread(generate);
|
||||
mesh_thread = std::thread(mesh);
|
||||
|
||||
debug::window::set_parameter("block_type_return", &block_to_place);
|
||||
}
|
||||
|
||||
// Method for world generation thread(s)
|
||||
void generate(){
|
||||
while(should_run){
|
||||
ChunkPQEntry entry;
|
||||
while(chunks_to_generate_queue.try_pop(entry)) generateChunk(entry.first);
|
||||
if(chunks_to_generate_queue.try_pop(entry)){
|
||||
Chunk::Chunk* chunk = entry.first;
|
||||
generateChunk(chunk);
|
||||
chunk->setState(Chunk::CHUNK_STATE_IN_GENERATION_QUEUE, false);
|
||||
}
|
||||
}
|
||||
chunks_to_generate_queue.clear();
|
||||
}
|
||||
|
||||
// Method for chunk meshing thread(s)
|
||||
|
@ -105,113 +87,216 @@ namespace chunkmanager
|
|||
ChunkPQEntry entry;
|
||||
if(chunks_to_mesh_queue.try_pop(entry)){
|
||||
Chunk::Chunk* chunk = entry.first;
|
||||
if(chunk->getState(Chunk::CHUNK_STATE_GENERATED)){
|
||||
chunkmesher::mesh(chunk);
|
||||
renderer::getChunksToRender().insert(chunk);
|
||||
}
|
||||
chunkmesher::mesh(chunk);
|
||||
chunk->setState(Chunk::CHUNK_STATE_IN_MESHING_QUEUE, false);
|
||||
}
|
||||
}
|
||||
chunks_to_mesh_queue.clear();
|
||||
}
|
||||
|
||||
oneapi::tbb::concurrent_queue<Chunk::Chunk*> chunks_todelete;
|
||||
int nUnloaded{0};
|
||||
void send_to_chunk_meshing_thread(Chunk::Chunk* c, int priority){
|
||||
c->setState(Chunk::CHUNK_STATE_IN_MESHING_QUEUE, true);
|
||||
chunks_to_mesh_queue.push(std::make_pair(c, MESHING_PRIORITY_NORMAL));
|
||||
}
|
||||
|
||||
oneapi::tbb::concurrent_queue<chunk_index_t> chunks_todelete;
|
||||
void update(){
|
||||
while(should_run) {
|
||||
int chunkX=static_cast<int>(theCamera.getAtomicPosX() / CHUNK_SIZE);
|
||||
int chunkY=static_cast<int>(theCamera.getAtomicPosY() / CHUNK_SIZE);
|
||||
int chunkZ=static_cast<int>(theCamera.getAtomicPosZ() / CHUNK_SIZE);
|
||||
/* Setup variables for the whole loop */
|
||||
// Atomic is needed by parallel_for
|
||||
std::atomic_int nUnloaded{0}, nMarkUnload{0}, nExplored{0}, nMeshed{0}, nGenerated{0};
|
||||
std::atomic_int chunkX=static_cast<int>(theCamera.getAtomicPosX() / CHUNK_SIZE);
|
||||
std::atomic_int chunkY=static_cast<int>(theCamera.getAtomicPosY() / CHUNK_SIZE);
|
||||
std::atomic_int chunkZ=static_cast<int>(theCamera.getAtomicPosZ() / CHUNK_SIZE);
|
||||
|
||||
debug::window::set_parameter("px", theCamera.getAtomicPosX());
|
||||
debug::window::set_parameter("py", theCamera.getAtomicPosY());
|
||||
debug::window::set_parameter("pz", theCamera.getAtomicPosZ());
|
||||
debug::window::set_parameter("cx", chunkX);
|
||||
debug::window::set_parameter("cy", chunkY);
|
||||
debug::window::set_parameter("cz", chunkZ);
|
||||
debug::window::set_parameter("lx", theCamera.getFront().x);
|
||||
debug::window::set_parameter("ly", theCamera.getFront().y);
|
||||
debug::window::set_parameter("lz", theCamera.getFront().z);
|
||||
|
||||
// Update other chunks
|
||||
for(int i = 0; i < chunks_volume_real; i++) {
|
||||
const uint16_t x = chunks_indices[i][0] + chunkX;
|
||||
const uint16_t y = chunks_indices[i][1] + chunkY;
|
||||
const uint16_t z = chunks_indices[i][2] + chunkZ;
|
||||
const uint32_t index = calculateIndex(x, y, z);
|
||||
|
||||
if(x > 1023 || y > 1023 || z > 1023) continue;
|
||||
|
||||
ChunkTable::accessor a, a1, a2, b1, b2, c1, c2;
|
||||
if(!chunks.find(a, index)) chunks.emplace(a, std::make_pair(index, new Chunk::Chunk(glm::vec3(x,y,z))));
|
||||
|
||||
if(! (a->second->getState(Chunk::CHUNK_STATE_GENERATED))) {
|
||||
chunks_to_generate_queue.push(std::make_pair(a->second, GENERATION_PRIORITY_NORMAL));
|
||||
}else if(! (a->second->getState(Chunk::CHUNK_STATE_MESHED))){
|
||||
if(
|
||||
(x + 1 > 1023 || (chunks.find(a1, calculateIndex(x+1, y, z)) &&
|
||||
a1->second->getState(Chunk::CHUNK_STATE_GENERATED))) &&
|
||||
(x - 1 < 0|| (chunks.find(a1, calculateIndex(x-1, y, z)) &&
|
||||
a1->second->getState(Chunk::CHUNK_STATE_GENERATED))) &&
|
||||
(y + 1 > 1023 || (chunks.find(a1, calculateIndex(x, y+1, z)) &&
|
||||
a1->second->getState(Chunk::CHUNK_STATE_GENERATED))) &&
|
||||
(y - 1 < 0|| (chunks.find(a1, calculateIndex(x, y-1, z)) &&
|
||||
a1->second->getState(Chunk::CHUNK_STATE_GENERATED))) &&
|
||||
(z + 1 > 1023 || (chunks.find(a1, calculateIndex(x, y, z+1)) &&
|
||||
a1->second->getState(Chunk::CHUNK_STATE_GENERATED))) &&
|
||||
(z - 1 < 0|| (chunks.find(a1, calculateIndex(x, y, z-1)) &&
|
||||
a1->second->getState(Chunk::CHUNK_STATE_GENERATED)))
|
||||
)
|
||||
chunks_to_mesh_queue.push(std::make_pair(a->second, MESHING_PRIORITY_NORMAL));
|
||||
/* Process update messages before anything happens */
|
||||
WorldUpdateMsg msg;
|
||||
while(WorldUpdateQueue.try_pop(msg)){
|
||||
switch(msg.msg_type){
|
||||
case WorldUpdateMsgType::BLOCKPICK_BREAK:
|
||||
case WorldUpdateMsgType::BLOCKPICK_PLACE:
|
||||
blockpick(msg);
|
||||
break;
|
||||
}
|
||||
|
||||
a.release();
|
||||
}
|
||||
|
||||
debug::window::set_parameter("update_chunks_total", (int) (chunks.size()));
|
||||
debug::window::set_parameter("update_chunks_bucket", (int) (chunks.max_size()));
|
||||
|
||||
Chunk::Chunk* n;
|
||||
nUnloaded = 0;
|
||||
while(chunks_todelete.try_pop(n)){
|
||||
int x = static_cast<uint16_t>(n->getPosition().x);
|
||||
int y = static_cast<uint16_t>(n->getPosition().y);
|
||||
int z = static_cast<uint16_t>(n->getPosition().z);
|
||||
if(x > 1023 || y > 1023 || z > 1023) continue;
|
||||
const uint32_t index = calculateIndex(x, y, z);
|
||||
|
||||
chunks.erase(index);
|
||||
//delete n;
|
||||
nUnloaded++;
|
||||
/* Delete old chunks */
|
||||
// In my head it makes sense to first delete old chunks, then create new ones
|
||||
// I think it's easier for memory allocator to re-use the memory that was freed just
|
||||
// before, but this isn't backed be any evidence and I might be wrong. Anyway this way
|
||||
// works fine so I'm gonna keep it.
|
||||
chunk_index_t i;
|
||||
ChunkTable::accessor a;
|
||||
while(chunks_todelete.try_pop(i)){
|
||||
const chunk_index_t index = i;
|
||||
if(chunks.find(a, index)){
|
||||
Chunk::Chunk* c = a->second;
|
||||
// Use the accessor to erase the element
|
||||
// Using the key doesn't work
|
||||
if(chunks.erase(a)){
|
||||
nUnloaded++;
|
||||
renderer::getDeleteIndexQueue().push(index);
|
||||
delete c;
|
||||
} else {
|
||||
c->setState(Chunk::CHUNK_STATE_IN_DELETING_QUEUE, false);
|
||||
std::cout << "failed to delete " << index << std::endl;
|
||||
}
|
||||
} else std::cout << "no such element found to delete\n";
|
||||
}
|
||||
|
||||
/* Create new chunks around the player */
|
||||
for(int i = 0; i < chunks_volume; i++) {
|
||||
const chunk_intcoord_t x = chunks_indices[i][0] + chunkX;
|
||||
const chunk_intcoord_t y = chunks_indices[i][1] + chunkY;
|
||||
const chunk_intcoord_t z = chunks_indices[i][2] + chunkZ;
|
||||
|
||||
if(x < 0 || y < 0 || z < 0 || x > 1023 || y > 1023 || z > 1023) continue;
|
||||
nExplored++;
|
||||
|
||||
const chunk_index_t index = Chunk::calculateIndex(x, y, z);
|
||||
ChunkTable::accessor a;
|
||||
if(!chunks.find(a, index)) chunks.emplace(a, std::make_pair(index, new
|
||||
Chunk::Chunk(glm::vec3(x,y,z))));
|
||||
}
|
||||
|
||||
/* Update all the chunks */
|
||||
oneapi::tbb::parallel_for(chunks.range(), [&](ChunkTable::range_type &r){
|
||||
for(ChunkTable::iterator a = r.begin(); a != r.end(); a++){
|
||||
Chunk::Chunk* c = a->second;
|
||||
|
||||
int x = c->getPosition().x;
|
||||
int y = c->getPosition().y;
|
||||
int z = c->getPosition().z;
|
||||
int distx = x - chunkX;
|
||||
int disty = y - chunkY;
|
||||
int distz = z - chunkZ;
|
||||
|
||||
// Local variables avoid continously having to call atomic variables
|
||||
int gen{0}, mesh{0}, unload{0};
|
||||
|
||||
if(
|
||||
distx >= -RENDER_DISTANCE && distx < RENDER_DISTANCE &&
|
||||
disty >= -RENDER_DISTANCE && disty < RENDER_DISTANCE &&
|
||||
distz >= -RENDER_DISTANCE && distz < RENDER_DISTANCE
|
||||
){
|
||||
|
||||
// If within distance
|
||||
// Reset out-of-view flags
|
||||
c->setState(Chunk::CHUNK_STATE_OUTOFVISION, false);
|
||||
c->setState(Chunk::CHUNK_STATE_UNLOADED, false);
|
||||
|
||||
// If not yet generated
|
||||
if(!c->getState(Chunk::CHUNK_STATE_GENERATED)){
|
||||
if(c->isFree()){
|
||||
// Generate
|
||||
|
||||
// Mark as present in the queue before sending to avoid strange
|
||||
// a chunk being marked as in the queue after it was already
|
||||
// processed
|
||||
c->setState(Chunk::CHUNK_STATE_IN_GENERATION_QUEUE, true);
|
||||
chunks_to_generate_queue.push(std::make_pair(c, GENERATION_PRIORITY_NORMAL));
|
||||
}
|
||||
}else{
|
||||
gen++;
|
||||
// If generated but not yet meshed
|
||||
if(!c->getState(Chunk::CHUNK_STATE_MESHED)){
|
||||
ChunkTable::accessor a1;
|
||||
|
||||
// Checking if nearby chunks have been generated allows for seamless
|
||||
// borders between chunks
|
||||
if(c->isFree() &&
|
||||
(distx+1 >= RENDER_DISTANCE || x + 1 > 1023 || (chunks.find(a1, Chunk::calculateIndex(x+1, y, z)) &&
|
||||
a1->second->getState(Chunk::CHUNK_STATE_GENERATED))) &&
|
||||
(distx-1 < -RENDER_DISTANCE || x - 1 < 0 || (chunks.find(a1, Chunk::calculateIndex(x-1, y, z)) &&
|
||||
a1->second->getState(Chunk::CHUNK_STATE_GENERATED))) &&
|
||||
(disty+1 >= RENDER_DISTANCE || y + 1 > 1023 || (chunks.find(a1, Chunk::calculateIndex(x, y+1, z)) &&
|
||||
a1->second->getState(Chunk::CHUNK_STATE_GENERATED))) &&
|
||||
(disty-1 < -RENDER_DISTANCE || y - 1 < 0|| (chunks.find(a1, Chunk::calculateIndex(x, y-1, z)) &&
|
||||
a1->second->getState(Chunk::CHUNK_STATE_GENERATED))) &&
|
||||
(distz+1 >= RENDER_DISTANCE || z + 1 > 1023 || (chunks.find(a1, Chunk::calculateIndex(x, y, z+1)) &&
|
||||
a1->second->getState(Chunk::CHUNK_STATE_GENERATED))) &&
|
||||
(distz-1 < -RENDER_DISTANCE || z - 1 < 0|| (chunks.find(a1, Chunk::calculateIndex(x, y, z-1)) &&
|
||||
a1->second->getState(Chunk::CHUNK_STATE_GENERATED)))
|
||||
)
|
||||
{
|
||||
// Mesh
|
||||
|
||||
// Mark as present in the queue before sending to avoid strange
|
||||
// a chunk being marked as in the queue after it was already
|
||||
// processed
|
||||
send_to_chunk_meshing_thread(c, MESHING_PRIORITY_NORMAL);
|
||||
}
|
||||
}else mesh++;
|
||||
}
|
||||
|
||||
}else{
|
||||
// If not within distance
|
||||
if(c->getState(Chunk::CHUNK_STATE_OUTOFVISION)){
|
||||
// If enough time has passed, set to be deleted
|
||||
if(c->isFree() && glfwGetTime() - c->unload_timer >= UNLOAD_TIMEOUT){
|
||||
c->setState(Chunk::CHUNK_STATE_IN_DELETING_QUEUE, true);
|
||||
chunks_todelete.push(c->getIndex());
|
||||
unload++;
|
||||
}
|
||||
}else{
|
||||
// Mark as out of view, and start waiting time
|
||||
c->setState(Chunk::CHUNK_STATE_OUTOFVISION, true);
|
||||
c->setState(Chunk::CHUNK_STATE_UNLOADED, false);
|
||||
c->unload_timer = glfwGetTime();
|
||||
}
|
||||
}
|
||||
|
||||
// Update atomic variables only once at the end
|
||||
nGenerated += gen;
|
||||
nMeshed += mesh;
|
||||
nMarkUnload += unload;
|
||||
}
|
||||
});
|
||||
|
||||
debug::window::set_parameter("update_chunks_total", (int)chunks.size());
|
||||
debug::window::set_parameter("update_chunks_generated", (int) nGenerated);
|
||||
debug::window::set_parameter("update_chunks_meshed", (int) nMeshed);
|
||||
debug::window::set_parameter("update_chunks_freed", (int) nUnloaded);
|
||||
debug::window::set_parameter("update_chunks_explored", (int) nExplored);
|
||||
}
|
||||
}
|
||||
|
||||
// uint32_t is fine, since i'm limiting the coordinate to only use up to ten bits (1023). There's actually two spare bits
|
||||
uint32_t calculateIndex(uint16_t i, uint16_t j, uint16_t k){
|
||||
return i | (j << 10) | (k << 20);
|
||||
}
|
||||
|
||||
oneapi::tbb::concurrent_queue<Chunk::Chunk*>& getDeleteVector(){ return chunks_todelete; }
|
||||
std::array<std::array<int, 3>, chunks_volume>& getChunksIndices(){ return chunks_indices; }
|
||||
std::array<std::array<chunk_intcoord_t, 3>, chunks_volume>& getChunksIndices(){ return chunks_indices; }
|
||||
|
||||
void stop() {
|
||||
should_run=false;
|
||||
|
||||
std::cout << "Waiting for secondary threads to shut down" << std::endl;
|
||||
update_thread.join();
|
||||
std::cout << "Update thread has terminated" << std::endl;
|
||||
gen_thread.join();
|
||||
std::cout << "Generation thread has terminated" << std::endl;
|
||||
mesh_thread.join();
|
||||
}
|
||||
void destroy(){
|
||||
/*for(const auto& n : chunks){
|
||||
delete n.second;
|
||||
}*/
|
||||
std::cout << "Meshing thread has terminated" << std::endl;
|
||||
}
|
||||
|
||||
void blockpick(bool place){
|
||||
void destroy(){
|
||||
for(const auto& n : chunks){
|
||||
delete n.second;
|
||||
}
|
||||
chunks.clear();
|
||||
}
|
||||
|
||||
|
||||
glm::vec3 ray_intersect(glm::vec3 startposition, glm::vec3 startdir){
|
||||
int old_bx{0}, old_by{0}, old_bz{0};
|
||||
int old_px{0}, old_py{0}, old_pz{0};
|
||||
Chunk::Chunk* old_chunk{nullptr};
|
||||
glm::vec3 old_pos;
|
||||
|
||||
// cast a ray from the camera in the direction pointed by the camera itself
|
||||
glm::vec3 pos = glm::vec3(theCamera.getAtomicPosX(), theCamera.getAtomicPosY(),
|
||||
theCamera.getAtomicPosZ());
|
||||
glm::vec3 origin = startposition;
|
||||
glm::vec3 pos = origin;
|
||||
glm::vec3 front = startdir;
|
||||
for(float t = 0.0; t <= 10.0; t += 0.5){
|
||||
// traverse the ray a block at the time
|
||||
pos = theCamera.getPos() + t * theCamera.getFront();
|
||||
pos = origin + t*front;
|
||||
|
||||
// get which chunk and block the ray is at
|
||||
int px = ((int)(pos.x))/CHUNK_SIZE;
|
||||
|
@ -221,84 +306,196 @@ namespace chunkmanager
|
|||
int by = pos.y - py*CHUNK_SIZE;
|
||||
int bz = pos.z - pz*CHUNK_SIZE;
|
||||
|
||||
if(bx == old_bx && by == old_by && bz == old_bz) continue;
|
||||
|
||||
// exit early if the position is invalid or the chunk does not exist
|
||||
if(px < 0 || py < 0 || pz < 0 || px >= 1024 || py >= 1024 || pz >= 1024) continue;
|
||||
|
||||
ChunkTable::accessor a;
|
||||
if(!chunks.find(a, calculateIndex(px, py, pz))) continue;
|
||||
ChunkTable::const_accessor a;
|
||||
if(!chunks.find(a, Chunk::calculateIndex(px, py, pz))) continue;
|
||||
Chunk::Chunk* c = a->second;
|
||||
if(!c->getState(Chunk::CHUNK_STATE_GENERATED) || c->getState(Chunk::CHUNK_STATE_EMPTY)) continue;
|
||||
if(!c->isFree() || !c->getState(Chunk::CHUNK_STATE_GENERATED)){
|
||||
a.release();
|
||||
continue;
|
||||
}
|
||||
|
||||
Block b = c->getBlock(bx, by, bz);
|
||||
|
||||
a.release();
|
||||
|
||||
// if the block is non empty
|
||||
if(b != Block::AIR){
|
||||
if(b != Block::AIR) return pos;
|
||||
|
||||
// if placing a new block
|
||||
if(place){
|
||||
// Go half a block backwards on the ray, to check the block where the ray was
|
||||
// coming from
|
||||
// Doing this and not using normal adds the unexpected (and unwanted) ability to
|
||||
// place blocks diagonally, without faces colliding with the block that has
|
||||
// been clicked
|
||||
pos -= theCamera.getFront()*0.5f;
|
||||
old_chunk = c;
|
||||
old_bx = bx;
|
||||
old_by = by;
|
||||
old_bz = bz;
|
||||
old_px = px;
|
||||
old_py = py;
|
||||
old_pz = pz;
|
||||
old_pos = pos;
|
||||
|
||||
int px1 = ((int)(pos.x))/CHUNK_SIZE;
|
||||
int py1 = ((int)(pos.y))/CHUNK_SIZE;
|
||||
int pz1 = ((int)(pos.z))/CHUNK_SIZE;
|
||||
int bx1 = pos.x - px1*CHUNK_SIZE;
|
||||
int by1 = pos.y - py1*CHUNK_SIZE;
|
||||
int bz1 = pos.z - pz1*CHUNK_SIZE;
|
||||
}
|
||||
return glm::vec3(-1);
|
||||
}
|
||||
|
||||
// exit early if the position is invalid or the chunk does not exist
|
||||
if(px1 < 0 || py1 < 0 || pz1 < 0 || px1 >= 1024 || py1 >= 1024 || pz1 >= 1024) return;
|
||||
ChunkTable::accessor a1;
|
||||
if(!chunks.find(a1, calculateIndex(px1, py1, pz1))) return;
|
||||
Chunk::Chunk* c1 = a1->second;
|
||||
// place the new block (only stone for now)
|
||||
c1->setBlock((Block)block_to_place, bx1, by1, bz1);
|
||||
void blockpick(WorldUpdateMsg& msg){
|
||||
//std::cout << glm::to_string(ray_intersect(msg.cameraPos, msg.cameraFront)) << std::endl;
|
||||
glm::vec3 ray_pos = ray_intersect(msg.cameraPos, msg.cameraFront);
|
||||
if(ray_pos == glm::vec3(-1)) return;
|
||||
|
||||
// mark the mesh of the chunk the be updated
|
||||
chunks_to_mesh_queue.push(std::make_pair(c1, MESHING_PRIORITY_PLAYER_EDIT));
|
||||
chunks_to_mesh_queue.push(std::make_pair(c, MESHING_PRIORITY_PLAYER_EDIT));
|
||||
// Chunk in which the blockpick is happening
|
||||
int chunkx = (int)(ray_pos.x) / CHUNK_SIZE;
|
||||
int chunky = (int)(ray_pos.y) / CHUNK_SIZE;
|
||||
int chunkz = (int)(ray_pos.z) / CHUNK_SIZE;
|
||||
// Block (chunk coord) in which the blockpick is happening
|
||||
int blockx = ray_pos.x - chunkx*CHUNK_SIZE;
|
||||
int blocky = ray_pos.y - chunky*CHUNK_SIZE;
|
||||
int blockz = ray_pos.z - chunkz*CHUNK_SIZE;
|
||||
|
||||
debug::window::set_parameter("block_last_action", place);
|
||||
debug::window::set_parameter("block_last_action_block_type", (int)(Block::STONE));
|
||||
debug::window::set_parameter("block_last_action_x", px1*CHUNK_SIZE + bx1);
|
||||
debug::window::set_parameter("block_last_action_y", px1*CHUNK_SIZE + by1);
|
||||
debug::window::set_parameter("block_last_action_z", px1*CHUNK_SIZE + bz1);
|
||||
// The chunk must exist, otherwise ray_intersect would have returned an error
|
||||
// Also, the block must be different from AIR
|
||||
|
||||
ChunkTable::accessor a;
|
||||
if(!chunks.find(a, Chunk::calculateIndex(chunkx, chunky, chunkz))) return;
|
||||
Chunk::Chunk* c = a->second;
|
||||
if(!(c->isFree() && c->getState(Chunk::CHUNK_STATE_GENERATED))) return;
|
||||
|
||||
if(msg.msg_type == WorldUpdateMsgType::BLOCKPICK_BREAK){
|
||||
c->setBlock(Block::AIR, blockx, blocky, blockz);
|
||||
send_to_chunk_meshing_thread(c, MESHING_PRIORITY_PLAYER_EDIT);
|
||||
}else{
|
||||
// Traverse voxel using Amanatides&Woo traversal algorithm
|
||||
// http://www.cse.yorku.ca/~amana/research/grid.pdf
|
||||
|
||||
glm::vec3 pos = msg.cameraPos;
|
||||
glm::vec3 front = glm::normalize(pos - ray_pos);
|
||||
|
||||
// Original chunk in which the blockpick started
|
||||
const int ochunkX=chunkx, ochunkY = chunky, ochunkZ = chunkz;
|
||||
|
||||
// The ray has equation pos + t*front
|
||||
|
||||
// Initialize phase
|
||||
// Origin integer voxel coordinates
|
||||
// Avoid floating point accuracy errors: work as close to 0 as possible, translate
|
||||
// everything later
|
||||
int basex = std::floor(ray_pos.x);
|
||||
int basey = std::floor(ray_pos.y);
|
||||
int basez = std::floor(ray_pos.z);
|
||||
double x = ray_pos.x - basex;
|
||||
double y = ray_pos.y - basey;
|
||||
double z = ray_pos.z - basez;
|
||||
|
||||
auto sign = [=](double f){ return f > 0 ? 1 : f < 0 ? -1 : 0; };
|
||||
auto tmax = [=](double p, double dir){
|
||||
int s = sign(dir);
|
||||
|
||||
if(s > 0)
|
||||
return (1 - p) / dir;
|
||||
else if(s < 0)
|
||||
return -(p) / dir;
|
||||
return 0.0;
|
||||
};
|
||||
|
||||
|
||||
// Step
|
||||
int stepX = sign(front.x);
|
||||
int stepY = sign(front.y);
|
||||
int stepZ = sign(front.z);
|
||||
|
||||
// tMax: the value of t at which the ray crosses the first voxel boundary
|
||||
double tMaxX = tmax(x, front.x);
|
||||
double tMaxY = tmax(y, front.y);
|
||||
double tMaxZ = tmax(z, front.z);
|
||||
|
||||
// tDelta: how far along the ray along they ray (in units of t) for the _ component of such
|
||||
// a movement to equal the width of a voxel
|
||||
double tDeltaX = stepX / front.x;
|
||||
double tDeltaY = stepY / front.y;
|
||||
double tDeltaZ = stepZ / front.z;
|
||||
|
||||
for(int i = 0; i < 10; i++){
|
||||
if(tMaxX < tMaxY){
|
||||
if(tMaxX < tMaxZ) {
|
||||
x += stepX;
|
||||
tMaxX += tDeltaX;
|
||||
}else{
|
||||
z += stepZ;
|
||||
tMaxZ += tDeltaZ;
|
||||
}
|
||||
}else{
|
||||
// replace the current block with air to remove it
|
||||
c->setBlock( Block::AIR, bx, by, bz);
|
||||
|
||||
chunks_to_mesh_queue.push(std::make_pair(c, MESHING_PRIORITY_PLAYER_EDIT));
|
||||
|
||||
// When necessary, also mesh nearby chunks
|
||||
ChunkTable::accessor a1, a2, b1, b2, c1, c2;
|
||||
if(bx == 0 && px - 1 >= 0 && chunks.find(a1, calculateIndex(px - 1, py, pz)))
|
||||
chunkmesher::mesh(a1->second);
|
||||
if(by == 0 && py - 1 >= 0 && chunks.find(b1, calculateIndex(px, py - 1, pz)))
|
||||
chunkmesher::mesh(b1->second);
|
||||
if(bz == 0 && pz - 1 >= 0 && chunks.find(c1, calculateIndex(px, py, pz - 1)))
|
||||
chunkmesher::mesh(c1->second);
|
||||
if(bx == CHUNK_SIZE - 1 && px +1 < 1024 && chunks.find(a2, calculateIndex(px +1, py, pz)))
|
||||
chunkmesher::mesh(a2->second);
|
||||
if(by == CHUNK_SIZE - 1 && py +1 < 1024 && chunks.find(b2, calculateIndex(px, py +1, pz)))
|
||||
chunkmesher::mesh(b2->second);
|
||||
if(bz == CHUNK_SIZE - 1 && pz +1 < 1024 && chunks.find(c2, calculateIndex(px, py, pz +1)))
|
||||
chunkmesher::mesh(c2->second);
|
||||
|
||||
debug::window::set_parameter("block_last_action", place);
|
||||
debug::window::set_parameter("block_last_action_block_type", (int) (Block::AIR));
|
||||
debug::window::set_parameter("block_last_action_x", px*CHUNK_SIZE + bx);
|
||||
debug::window::set_parameter("block_last_action_y", py*CHUNK_SIZE + by);
|
||||
debug::window::set_parameter("block_last_action_z", pz*CHUNK_SIZE + bz);
|
||||
|
||||
if(tMaxY < tMaxZ){
|
||||
y += stepY;
|
||||
tMaxY += tDeltaY;
|
||||
}else{
|
||||
z += stepZ;
|
||||
tMaxZ += tDeltaZ;
|
||||
}
|
||||
}
|
||||
|
||||
int realx = basex + x;
|
||||
int realy = basey + y;
|
||||
int realz = basez + z;
|
||||
|
||||
chunkx = realx / CHUNK_SIZE;
|
||||
chunky = realy / CHUNK_SIZE;
|
||||
chunkz = realz / CHUNK_SIZE;
|
||||
|
||||
if(chunkx < 0 || chunky < 0 || chunkz < 0 || chunkx > 1023 || chunky > 1023 ||
|
||||
chunkz > 1023) continue;
|
||||
|
||||
blockx = realx - chunkx*CHUNK_SIZE;
|
||||
blocky = realy - chunky*CHUNK_SIZE;
|
||||
blockz = realz - chunkz*CHUNK_SIZE;
|
||||
|
||||
|
||||
Chunk::Chunk* chunk;
|
||||
ChunkTable::accessor b;
|
||||
|
||||
if(chunkx != ochunkX || chunky != ochunkY || chunkz != ochunkZ){
|
||||
if(!chunks.find(b, Chunk::calculateIndex(chunkx, chunky, chunkz)))
|
||||
continue;
|
||||
chunk = b->second;
|
||||
if(!(chunk->isFree() && chunk->getState(Chunk::CHUNK_STATE_GENERATED)))
|
||||
continue;
|
||||
|
||||
}else{
|
||||
chunk = c;
|
||||
}
|
||||
|
||||
if(chunk->getBlock(blockx, blocky, blockz) != Block::AIR) continue;
|
||||
chunk->setBlock(msg.block, blockx, blocky, blockz);
|
||||
send_to_chunk_meshing_thread(chunk, MESHING_PRIORITY_PLAYER_EDIT);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Release the chunk in which the blockpick started to avoid locks
|
||||
a.release();
|
||||
// When necessary, also mesh nearby chunks
|
||||
ChunkTable::accessor a1, a2, b1, b2, c1, c2;
|
||||
if(blockx == 0 && chunkx - 1 >= 0 && chunks.find(a1, Chunk::calculateIndex(chunkx - 1, chunky, chunkz)))
|
||||
send_to_chunk_meshing_thread(a1->second, MESHING_PRIORITY_PLAYER_EDIT);
|
||||
if(blocky == 0 && chunky - 1 >= 0 && chunks.find(b1, Chunk::calculateIndex(chunkx, chunky - 1, chunkz)))
|
||||
send_to_chunk_meshing_thread(b1->second, MESHING_PRIORITY_PLAYER_EDIT);
|
||||
if(blockz == 0 && chunkz - 1 >= 0 && chunks.find(c1, Chunk::calculateIndex(chunkx, chunky, chunkz - 1)))
|
||||
send_to_chunk_meshing_thread(c1->second, MESHING_PRIORITY_PLAYER_EDIT);
|
||||
if(blockx == CHUNK_SIZE - 1 && chunkx +1 < 1024 && chunks.find(a2, Chunk::calculateIndex(chunkx +1, chunky, chunkz)))
|
||||
send_to_chunk_meshing_thread(a2->second, MESHING_PRIORITY_PLAYER_EDIT);
|
||||
if(blocky == CHUNK_SIZE - 1 && chunky +1 < 1024 && chunks.find(b2, Chunk::calculateIndex(chunkx, chunky +1, chunkz)))
|
||||
send_to_chunk_meshing_thread(b2->second, MESHING_PRIORITY_PLAYER_EDIT);
|
||||
if(blockz == CHUNK_SIZE - 1 && chunkz +1 < 1024 && chunks.find(c2, Chunk::calculateIndex(chunkx, chunky, chunkz +1)))
|
||||
send_to_chunk_meshing_thread(c2->second, MESHING_PRIORITY_PLAYER_EDIT);
|
||||
|
||||
// Update debugging information
|
||||
|
||||
debug::window::set_parameter("block_last_action", msg.msg_type ==
|
||||
WorldUpdateMsgType::BLOCKPICK_PLACE);
|
||||
debug::window::set_parameter("block_last_action_block_type", (int)(msg.msg_type ==
|
||||
WorldUpdateMsgType::BLOCKPICK_PLACE ? msg.block : Block::AIR));
|
||||
debug::window::set_parameter("block_last_action_x", chunkx*CHUNK_SIZE+blockx);
|
||||
debug::window::set_parameter("block_last_action_y", chunky*CHUNK_SIZE+blocky);
|
||||
debug::window::set_parameter("block_last_action_z", chunkz*CHUNK_SIZE+blockz);
|
||||
}
|
||||
|
||||
Block getBlockAtPos(int x, int y, int z){
|
||||
|
@ -312,7 +509,7 @@ namespace chunkmanager
|
|||
|
||||
//std::cout << "Block at " << x << ", " << y << ", " << z << " is in chunk " << cx << "," << cy << "," << cz << "\n";
|
||||
ChunkTable::accessor a;
|
||||
if(!chunks.find(a, calculateIndex(cx, cy, cz))) return Block::NULLBLK;
|
||||
if(!chunks.find(a, Chunk::calculateIndex(cx, cy, cz))) return Block::NULLBLK;
|
||||
else {
|
||||
int bx = x % CHUNK_SIZE;
|
||||
int by = y % CHUNK_SIZE;
|
||||
|
@ -324,3 +521,4 @@ namespace chunkmanager
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -11,15 +11,24 @@
|
|||
#include "spacefilling.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
#define CHUNK_MESH_DATA_QUANTITY 100
|
||||
#define CHUNK_MESH_WORLD_LIMIT_BORDERS 0
|
||||
|
||||
namespace chunkmesher{
|
||||
|
||||
oneapi::tbb::concurrent_queue<MeshData*> MeshDataQueue;
|
||||
ChunkMeshDataQueue MeshDataQueue;
|
||||
|
||||
oneapi::tbb::concurrent_queue<MeshData*>& getMeshDataQueue(){ return MeshDataQueue; }
|
||||
ChunkMeshDataQueue& getMeshDataQueue(){ return MeshDataQueue; }
|
||||
|
||||
void init()
|
||||
{
|
||||
for(int i = 0; i < CHUNK_MESH_DATA_QUANTITY; i++)
|
||||
MeshDataQueue.push(new ChunkMeshData{});
|
||||
}
|
||||
|
||||
void mesh(Chunk::Chunk* chunk)
|
||||
{
|
||||
MeshData* mesh_data;
|
||||
ChunkMeshData* mesh_data;
|
||||
if(!MeshDataQueue.try_pop(mesh_data)) return;
|
||||
|
||||
/*
|
||||
|
@ -38,34 +47,27 @@ void mesh(Chunk::Chunk* chunk)
|
|||
*/
|
||||
|
||||
// Cleanup previous data
|
||||
mesh_data->numVertices = 0;
|
||||
mesh_data->chunk = chunk;
|
||||
mesh_data->vertices.clear();
|
||||
mesh_data->extents.clear();
|
||||
mesh_data->texinfo.clear();
|
||||
|
||||
// Abort if chunk is empty
|
||||
if(chunk->getState(Chunk::CHUNK_STATE_EMPTY)){
|
||||
chunk->setState(Chunk::CHUNK_STATE_MESHED, true);
|
||||
renderer::getMeshDataQueue().push(mesh_data);
|
||||
return;
|
||||
}
|
||||
mesh_data->clear();
|
||||
mesh_data->message_type = ChunkMeshDataType::MESH_UPDATE;
|
||||
mesh_data->index = chunk->getIndex();
|
||||
mesh_data->position = chunk->getPosition();
|
||||
|
||||
// convert tree to array since it is easier to work with it
|
||||
int length{0};
|
||||
std::unique_ptr<Block[]> blocks = chunk->getBlocksArray(&length);
|
||||
if(length == 0) {
|
||||
chunk->setState(Chunk::CHUNK_STATE_MESHED, true);
|
||||
renderer::getMeshDataQueue().push(mesh_data);
|
||||
return;
|
||||
}
|
||||
|
||||
std::unique_ptr<Block[]> blocks;
|
||||
|
||||
int k, l, u, v, w, h, n, j, i;
|
||||
int x[]{0, 0, 0};
|
||||
int q[]{0, 0, 0};
|
||||
int du[]{0, 0, 0};
|
||||
int dv[]{0, 0, 0};
|
||||
|
||||
// Abort if chunk is empty
|
||||
if(chunk->getState(Chunk::CHUNK_STATE_EMPTY)) goto end;
|
||||
|
||||
blocks = chunk->getBlocksArray(&length);
|
||||
if(length == 0) goto end;
|
||||
|
||||
std::array<Block, CHUNK_SIZE * CHUNK_SIZE> mask;
|
||||
for (bool backFace = true, b = false; b != backFace; backFace = backFace && b, b = !b)
|
||||
{
|
||||
|
@ -128,9 +130,15 @@ void mesh(Chunk::Chunk* chunk)
|
|||
// The else case provides face culling for adjacent solid faces
|
||||
// Checking for NULLBLK avoids creating empty faces if nearby chunk was not
|
||||
// yet generated
|
||||
#if CHUNK_MESH_WORLD_LIMIT_BORDERS == 1
|
||||
mask[n++] = b1 == b2 ? Block::NULLBLK
|
||||
: backFace ? b1 == Block::NULLBLK || b1 == Block::AIR ? b2 : Block::NULLBLK
|
||||
: b2 == Block::NULLBLK || b2 == Block::AIR ? b1 : Block::NULLBLK;
|
||||
#else
|
||||
mask[n++] = b1 == b2 ? Block::NULLBLK
|
||||
: backFace ? b1 == Block::AIR ? b2 : Block::NULLBLK
|
||||
: b2 == Block::AIR ? b1 : Block::NULLBLK;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -191,7 +199,7 @@ void mesh(Chunk::Chunk* chunk)
|
|||
|
||||
mesh_data->texinfo.push_back(backFace ? 0.0 : 1.0);
|
||||
mesh_data->texinfo.push_back((int)(mask[n]) - 2);
|
||||
mesh_data->numVertices++;
|
||||
mesh_data->num_vertices++;
|
||||
}
|
||||
|
||||
for (l = 0; l < h; ++l)
|
||||
|
@ -220,49 +228,8 @@ void mesh(Chunk::Chunk* chunk)
|
|||
}
|
||||
}
|
||||
|
||||
end:
|
||||
chunk->setState(Chunk::CHUNK_STATE_MESHED, true);
|
||||
renderer::getMeshDataQueue().push(mesh_data);
|
||||
}
|
||||
|
||||
void sendtogpu(MeshData* mesh_data)
|
||||
{
|
||||
if (mesh_data->numVertices > 0)
|
||||
{
|
||||
if(mesh_data->chunk->VAO == 0) mesh_data->chunk->createBuffers();
|
||||
|
||||
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
|
||||
glBindVertexArray(mesh_data->chunk->VAO);
|
||||
|
||||
// position attribute
|
||||
glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, mesh_data->vertices.size() * sizeof(GLfloat), &(mesh_data->vertices[0]), GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// normal attribute
|
||||
glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->extentsBuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, mesh_data->extents.size() * sizeof(GLfloat), &(mesh_data->extents[0]), GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)(0));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
// texcoords attribute
|
||||
glBindBuffer(GL_ARRAY_BUFFER, mesh_data->chunk->texinfoBuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, mesh_data->texinfo.size() * sizeof(GLfloat), &(mesh_data->texinfo[0]), GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void *)0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
// save the number of indices of the mesh, it is needed later for drawing
|
||||
mesh_data->chunk->numVertices = (GLuint)(mesh_data->numVertices);
|
||||
|
||||
// once data has been sent to the GPU, it can be cleared from system RAM
|
||||
mesh_data->vertices.clear();
|
||||
mesh_data->extents.clear();
|
||||
mesh_data->texinfo.clear();
|
||||
}
|
||||
|
||||
// mark the chunk mesh has loaded on GPU
|
||||
mesh_data->chunk->setState(Chunk::CHUNK_STATE_MESH_LOADED, true);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
#include "controls.hpp"
|
||||
|
||||
#include "camera.hpp"
|
||||
#include "chunkmanager.hpp"
|
||||
#include "debugwindow.hpp"
|
||||
#include "globals.hpp"
|
||||
#include "renderer.hpp"
|
||||
|
||||
namespace controls{
|
||||
/* Block picking */
|
||||
int block_to_place{2};
|
||||
float lastBlockPick=0.0;
|
||||
bool blockpick = false;
|
||||
|
||||
/* Cursor */
|
||||
bool cursor = false;
|
||||
|
||||
void init(){
|
||||
debug::window::set_parameter("block_type_return", &block_to_place);
|
||||
}
|
||||
|
||||
void update(GLFWwindow* window){
|
||||
float current_time = glfwGetTime();
|
||||
|
||||
/* BlockPicking */
|
||||
// Reset blockpicking if enough time has passed
|
||||
if(current_time - lastBlockPick > BLOCKPICK_TIMEOUT) blockpick = false;
|
||||
// Reset blockpicking if both mouse buttons are released
|
||||
if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_RELEASE && glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_2) == GLFW_RELEASE) blockpick = false;
|
||||
// Process block picking if a mouse button is pressed
|
||||
if((glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS ||
|
||||
glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_2 == GLFW_PRESS)) && !blockpick){
|
||||
|
||||
// Start timeout for next block pick action
|
||||
blockpick = true;
|
||||
lastBlockPick = current_time;
|
||||
|
||||
// Construct the message to send to chunkmanager
|
||||
|
||||
// WorldUpdateMsg is allocated on the stack
|
||||
// unlike ChunkMeshData, the fields of WorldUpdateMsg are few and light, so there's no
|
||||
// problem in passing them by value each time.
|
||||
// It also has the advantage of having less memory to manage, since I'm not allocating
|
||||
// anything on the heap
|
||||
|
||||
WorldUpdateMsg msg{};
|
||||
msg.cameraPos = theCamera.getPos();
|
||||
msg.cameraFront = theCamera.getFront();
|
||||
msg.time = current_time;
|
||||
msg.msg_type = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS ?
|
||||
WorldUpdateMsgType::BLOCKPICK_PLACE : WorldUpdateMsgType::BLOCKPICK_BREAK;
|
||||
msg.block = (Block)(block_to_place);
|
||||
|
||||
// Send to chunk manager
|
||||
chunkmanager::getWorldUpdateQueue().push(msg);
|
||||
}
|
||||
|
||||
/* SCREENSHOTS */
|
||||
if(glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS) renderer::saveScreenshot();
|
||||
if(glfwGetKey(window, GLFW_KEY_F3) == GLFW_PRESS) renderer::saveScreenshot(true);
|
||||
if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) {
|
||||
cursor = !cursor;
|
||||
glfwSetInputMode(window, GLFW_CURSOR, cursor ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_DISABLED);
|
||||
}
|
||||
}
|
||||
};
|
|
@ -69,6 +69,9 @@ namespace debug{
|
|||
ImGui::Text("FPS: %d", std::any_cast<int>(parameters.at("fps")));
|
||||
ImGui::Text("Frametime (ms): %f",
|
||||
std::any_cast<float>(parameters.at("frametime"))*1000);
|
||||
ImGui::Text("GPU: (%s) %s",
|
||||
std::any_cast<const GLubyte*>(parameters.at("gpu_vendor")),
|
||||
std::any_cast<const GLubyte*>(parameters.at("gpu_renderer")));
|
||||
//ImGui::PlotLines("Frame Times", arr, IM_ARRAYSIZE(arr);
|
||||
}
|
||||
|
||||
|
@ -79,6 +82,11 @@ namespace debug{
|
|||
ImGui::Text("Pointing in direction: %f, %f, %f",
|
||||
std::any_cast<float>(parameters.at("lx")),std::any_cast<float>(parameters.at("ly")),std::any_cast<float>(parameters.at("lz")) );
|
||||
|
||||
ImGui::SliderInt("Crosshair type",
|
||||
std::any_cast<int*>(parameters.at("crosshair_type_return")), 0, 1);
|
||||
ImGui::SliderInt("Block to place",
|
||||
std::any_cast<int*>(parameters.at("block_type_return")), 2, 6);
|
||||
|
||||
if(parameters.find("block_last_action") != parameters.end()){
|
||||
ImGui::Text("Last Block action: %s",
|
||||
std::any_cast<bool>(parameters.at("block_last_action")) ? "place" : "destroy");
|
||||
|
@ -87,26 +95,18 @@ namespace debug{
|
|||
ImGui::Text("Last Block action position: X: %d, Y: %d, Z: %d",
|
||||
std::any_cast<int>(parameters.at("block_last_action_x")),std::any_cast<int>(parameters.at("block_last_action_y")),std::any_cast<int>(parameters.at("block_last_action_z")) );
|
||||
}
|
||||
|
||||
ImGui::SliderInt("Crosshair type",
|
||||
std::any_cast<int*>(parameters.at("crosshair_type_return")), 0, 1);
|
||||
ImGui::SliderInt("Block to place",
|
||||
std::any_cast<int*>(parameters.at("block_type_return")), 2, 6);
|
||||
}
|
||||
|
||||
if(ImGui::CollapsingHeader("Mesh")){
|
||||
ImGui::Text("Total chunks updated: %d",
|
||||
ImGui::Text("Total chunk meshed: %d",
|
||||
std::any_cast<int>(parameters.at("render_chunks_total")));
|
||||
ImGui::Text("Of which renderable (not empty): %d",
|
||||
std::any_cast<int>(parameters.at("render_chunks_renderable")));
|
||||
ImGui::Text("Chunks rendered: %d",
|
||||
std::any_cast<int>(parameters.at("render_chunks_rendered")));
|
||||
ImGui::Text("Frustum culled: %d",
|
||||
std::any_cast<int>(parameters.at("render_chunks_culled")));
|
||||
ImGui::Text("Chunks out of view: %d",
|
||||
std::any_cast<int>(parameters.at("render_chunks_oof")));
|
||||
if(parameters.find("render_chunks_deleted") != parameters.end())
|
||||
ImGui::Text("Chunks deleted: %d",
|
||||
std::any_cast<int>(parameters.at("render_chunks_deleted")));
|
||||
ImGui::Text("Vertices in the scene: %d",
|
||||
ImGui::Text("Total vertices in the scene: %d",
|
||||
std::any_cast<int>(parameters.at("render_chunks_vertices")));
|
||||
ImGui::Checkbox("Wireframe",
|
||||
std::any_cast<bool*>(parameters.at("wireframe_return")));
|
||||
|
@ -115,13 +115,19 @@ namespace debug{
|
|||
if(ImGui::CollapsingHeader("Chunks")){
|
||||
ImGui::Text("Total chunks present: %d",
|
||||
std::any_cast<int>(parameters.at("update_chunks_total")));
|
||||
/*ImGui::Text("Chunks freed from memory: %d",
|
||||
std::any_cast<int>(parameters.at("update_chunks_delete")));*/
|
||||
ImGui::Text("Bucket size: %d",
|
||||
std::any_cast<int>(parameters.at("update_chunks_bucket")));
|
||||
ImGui::Text("Chunks generated: %d",
|
||||
std::any_cast<int>(parameters.at("update_chunks_generated")));
|
||||
ImGui::Text("Chunks meshed: %d",
|
||||
std::any_cast<int>(parameters.at("update_chunks_meshed")));
|
||||
ImGui::Text("Chunks actually freed from memory: %d",
|
||||
std::any_cast<int>(parameters.at("update_chunks_freed")));
|
||||
ImGui::Text("Chunks explored: %d",
|
||||
std::any_cast<int>(parameters.at("update_chunks_explored")));
|
||||
}
|
||||
}catch(const std::bad_any_cast& e){
|
||||
std::cout << e.what();
|
||||
std::cout << e.what() << std::endl;
|
||||
}catch(const std::out_of_range& e){
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
|
|
69
src/main.cpp
69
src/main.cpp
|
@ -1,5 +1,4 @@
|
|||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "main.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
@ -7,23 +6,18 @@
|
|||
#define GLOBALS_DEFINER
|
||||
#include "globals.hpp"
|
||||
#undef GLOBALS_DEFINER
|
||||
|
||||
#include "chunkmanager.hpp"
|
||||
#include "main.hpp"
|
||||
#include "controls.hpp"
|
||||
#include "debugwindow.hpp"
|
||||
#include "renderer.hpp"
|
||||
#include "spacefilling.hpp"
|
||||
#include "shader.hpp"
|
||||
#include "spacefilling.hpp"
|
||||
|
||||
float deltaTime = 0.0f; // Time between current frame and last frame
|
||||
float lastFrame = 0.0f; // Time of last frame
|
||||
float lastFPSFrame = 0.0f;
|
||||
int frames = 0;
|
||||
|
||||
float lastBlockPick=0.0;
|
||||
bool blockpick = false;
|
||||
bool cursor = false;
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
|
@ -61,16 +55,18 @@ int main()
|
|||
//glEnable(GL_FRAMEBUFFER_SRGB); //gamma correction done in fragment shader
|
||||
//glEnable(GL_CULL_FACE); //GL_BACK GL_CCW by default
|
||||
|
||||
std::cout << "Using GPU: " << glGetString(GL_VENDOR) << " " << glGetString(GL_RENDERER) << "\n";
|
||||
debug::window::set_parameter("gpu_vendor", glGetString(GL_VENDOR));
|
||||
debug::window::set_parameter("gpu_renderer", glGetString(GL_RENDERER));
|
||||
|
||||
wireframe = false;
|
||||
for(int i = 0; i < 360; i++){
|
||||
sines[i] = sin(3.14 / 180 * i);
|
||||
cosines[i] = cos(3.14 / 180 * i);
|
||||
}
|
||||
|
||||
SpaceFilling::initLUT();
|
||||
controls::init();
|
||||
chunkmanager::init();
|
||||
chunkmesher::init();
|
||||
debug::window::init(window);
|
||||
renderer::init(window);
|
||||
|
||||
|
@ -94,14 +90,26 @@ int main()
|
|||
glClearColor(0.431f, 0.694f, 1.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// Input handling
|
||||
// Only close event is handles by main
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
// the rest of input processing is handled by controls.cpp
|
||||
|
||||
// Input processing
|
||||
processInput(window);
|
||||
controls::update(window);
|
||||
|
||||
// Camera
|
||||
theCamera.update(window, deltaTime);
|
||||
|
||||
// Reset blockping timeout if 200ms have passed
|
||||
if(glfwGetTime() - lastBlockPick > 0.1) blockpick = false;
|
||||
debug::window::set_parameter("px", theCamera.getPos().x);
|
||||
debug::window::set_parameter("py", theCamera.getPos().y);
|
||||
debug::window::set_parameter("pz", theCamera.getPos().z);
|
||||
debug::window::set_parameter("cx", (int)(theCamera.getPos().x / CHUNK_SIZE));
|
||||
debug::window::set_parameter("cy", (int)(theCamera.getPos().y / CHUNK_SIZE));
|
||||
debug::window::set_parameter("cz", (int)(theCamera.getPos().z / CHUNK_SIZE));
|
||||
debug::window::set_parameter("lx", theCamera.getFront().x);
|
||||
debug::window::set_parameter("ly", theCamera.getFront().y);
|
||||
debug::window::set_parameter("lz", theCamera.getFront().z);
|
||||
|
||||
// Render pass
|
||||
renderer::render();
|
||||
|
@ -117,7 +125,6 @@ int main()
|
|||
// Cleanup allocated memory
|
||||
chunkmanager::destroy();
|
||||
renderer::destroy();
|
||||
debug::window::destroy();
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
|
@ -134,33 +141,3 @@ void mouse_callback(GLFWwindow *window, double xpos, double ypos)
|
|||
{
|
||||
theCamera.mouseCallback(window, xpos, ypos);
|
||||
}
|
||||
|
||||
void processInput(GLFWwindow *window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
|
||||
if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_2) == GLFW_PRESS && !blockpick){
|
||||
chunkmanager::blockpick(false);
|
||||
blockpick=true;
|
||||
lastBlockPick=glfwGetTime();
|
||||
}
|
||||
|
||||
if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS && !blockpick){
|
||||
chunkmanager::blockpick(true);
|
||||
blockpick=true;
|
||||
lastBlockPick=glfwGetTime();
|
||||
}
|
||||
|
||||
// Reset blockpicking if enough time has passed
|
||||
if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_RELEASE && glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_2) == GLFW_RELEASE) blockpick = false;
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS) renderer::saveScreenshot();
|
||||
if(glfwGetKey(window, GLFW_KEY_F3) == GLFW_PRESS) renderer::saveScreenshot(true);
|
||||
if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) {
|
||||
cursor = !cursor;
|
||||
glfwSetInputMode(window, GLFW_CURSOR, cursor ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_DISABLED);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
161
src/renderer.cpp
161
src/renderer.cpp
|
@ -1,7 +1,8 @@
|
|||
#include "renderer.hpp"
|
||||
|
||||
#include <oneapi/tbb/concurrent_vector.h>
|
||||
#include <oneapi/tbb/concurrent_queue.h>
|
||||
#include <glm/ext.hpp>
|
||||
#include <glm/gtx/string_cast.hpp>
|
||||
#include <oneapi/tbb/concurrent_hash_map.h>
|
||||
|
||||
#include "chunkmanager.hpp"
|
||||
#include "chunkmesher.hpp"
|
||||
|
@ -12,16 +13,18 @@
|
|||
#include "stb_image_write.h"
|
||||
|
||||
namespace renderer{
|
||||
RenderSet chunks_torender;
|
||||
oneapi::tbb::concurrent_vector<Chunk::Chunk*> render_todelete;
|
||||
oneapi::tbb::concurrent_queue<chunkmesher::MeshData*> MeshDataQueue;
|
||||
typedef oneapi::tbb::concurrent_hash_map<chunk_index_t, RenderInfo*> RenderTable;
|
||||
|
||||
RenderTable ChunksToRender;
|
||||
ChunkMeshDataQueue MeshDataQueue;
|
||||
IndexQueue MeshDataToDelete;
|
||||
|
||||
Shader* theShader, *quadShader;
|
||||
GLuint chunkTexture;
|
||||
|
||||
Shader* getRenderShader() { return theShader; }
|
||||
RenderSet& getChunksToRender(){ return chunks_torender; }
|
||||
oneapi::tbb::concurrent_queue<chunkmesher::MeshData*>& getMeshDataQueue(){ return MeshDataQueue; }
|
||||
ChunkMeshDataQueue& getMeshDataQueue(){ return MeshDataQueue; }
|
||||
IndexQueue& getDeleteIndexQueue(){ return MeshDataToDelete; }
|
||||
|
||||
GLuint renderTexFrameBuffer, renderTex, renderTexDepthBuffer, quadVAO, quadVBO;
|
||||
int screenWidth, screenHeight;
|
||||
|
@ -138,32 +141,69 @@ namespace renderer{
|
|||
theShader->use();
|
||||
theShader->setVec3("viewPos", cameraPos);
|
||||
|
||||
chunkmesher::MeshData* m;
|
||||
/* Process incoming mesh data */
|
||||
ChunkMeshData* m;
|
||||
while(MeshDataQueue.try_pop(m)){
|
||||
chunkmesher::sendtogpu(m);
|
||||
RenderTable::accessor a;
|
||||
RenderInfo* render_info;
|
||||
|
||||
if(ChunksToRender.find(a, m->index)){
|
||||
render_info = a->second;
|
||||
render_info->position = m->position;
|
||||
render_info->num_vertices = m->num_vertices;
|
||||
|
||||
// Always updated the mesh, even if it's empty
|
||||
// This should solve the problem of having floating quads when destroying a block
|
||||
// near chunk borders
|
||||
send_chunk_to_gpu(m, render_info);
|
||||
}else{
|
||||
render_info = new RenderInfo();
|
||||
render_info->index = m->index;
|
||||
render_info->position = m->position;
|
||||
render_info->num_vertices = m->num_vertices;
|
||||
|
||||
ChunksToRender.emplace(a, std::make_pair(render_info->index, render_info));
|
||||
|
||||
// Only send the mesh to the GPU if it's not empty
|
||||
if(render_info->num_vertices > 0) send_chunk_to_gpu(m, render_info);
|
||||
}
|
||||
|
||||
chunkmesher::getMeshDataQueue().push(m);
|
||||
}
|
||||
|
||||
for(auto& c : chunks_torender){
|
||||
float dist = glm::distance(c->getPosition(), cameraChunkPos);
|
||||
if(dist <= static_cast<float>(RENDER_DISTANCE)){
|
||||
if(!c->getState(Chunk::CHUNK_STATE_MESH_LOADED)) continue;
|
||||
/* Process chunks to be removed */
|
||||
chunk_index_t queue_index;
|
||||
while(MeshDataToDelete.try_pop(queue_index)){
|
||||
RenderTable::accessor a;
|
||||
|
||||
if(ChunksToRender.find(a, queue_index)){
|
||||
RenderInfo* render_info = a->second;
|
||||
render_info->deallocateBuffers();
|
||||
delete render_info;
|
||||
ChunksToRender.erase(a);
|
||||
}
|
||||
}
|
||||
|
||||
/* Render the chunks */
|
||||
// parallel_for cannot be used since all the rendering needs to happen in a single thread
|
||||
for(RenderTable::iterator i = ChunksToRender.begin(); i != ChunksToRender.end(); i++){
|
||||
RenderInfo* render_info = i->second;
|
||||
|
||||
if(render_info->num_vertices > 0)
|
||||
{
|
||||
total++;
|
||||
|
||||
// Increase total vertex count
|
||||
vertices += c->numVertices;
|
||||
|
||||
// reset out-of-vision and unload flags
|
||||
c->setState(Chunk::CHUNK_STATE_OUTOFVISION, false);
|
||||
c->setState(Chunk::CHUNK_STATE_UNLOADED, false);
|
||||
vertices += render_info->num_vertices;
|
||||
|
||||
// Perform frustum culling and eventually render
|
||||
glm::vec3 chunk = c->getPosition();
|
||||
glm::vec3 chunk = render_info->position;
|
||||
glm::vec4 chunkW = glm::vec4(chunk.x*static_cast<float>(CHUNK_SIZE), chunk.y*static_cast<float>(CHUNK_SIZE), chunk.z*static_cast<float>(CHUNK_SIZE),1.0);
|
||||
glm::mat4 model = glm::translate(glm::mat4(1.0), ((float)CHUNK_SIZE) * chunk);
|
||||
|
||||
// Check if all the corners of the chunk are outside any of the planes
|
||||
// TODO (?) implement frustum culling as per (Inigo Quilez)[https://iquilezles.org/articles/frustumcorrect/], and check each
|
||||
// plane against each corner of the chunk
|
||||
// plane against each corner of the chunk
|
||||
bool out=false;
|
||||
int a{0};
|
||||
for(int p = 0; p < 6; p++){
|
||||
|
@ -179,57 +219,25 @@ namespace renderer{
|
|||
|
||||
if (!out)
|
||||
{
|
||||
if(c->numVertices > 0)
|
||||
{
|
||||
theShader->setMat4("model", model);
|
||||
theShader->setMat4("view", theCamera.getView());
|
||||
theShader->setMat4("projection", theCamera.getProjection());
|
||||
theShader->setMat4("model", model);
|
||||
theShader->setMat4("view", theCamera.getView());
|
||||
theShader->setMat4("projection", theCamera.getProjection());
|
||||
|
||||
glBindVertexArray(c->VAO);
|
||||
glDrawArrays(GL_POINTS, 0, c->numVertices);
|
||||
glBindVertexArray(0);
|
||||
glBindVertexArray(render_info->VAO);
|
||||
glDrawArrays(GL_POINTS, 0, render_info->num_vertices);
|
||||
glBindVertexArray(0);
|
||||
|
||||
toGpu++;
|
||||
}
|
||||
toGpu++;
|
||||
}
|
||||
}else{
|
||||
// When the chunk is outside render distance
|
||||
|
||||
if(c->getState(Chunk::CHUNK_STATE_OUTOFVISION)){
|
||||
oof++;
|
||||
if(glfwGetTime() - c->unload_timer > UNLOAD_TIMEOUT){
|
||||
// If chunk was already out and enough time has passed
|
||||
// Mark the chunk to be unloaded
|
||||
// And mark is to be removed from the render set
|
||||
render_todelete.push_back(c);
|
||||
}
|
||||
} else{
|
||||
// Mark has out of vision and annotate when it started
|
||||
c->setState(Chunk::CHUNK_STATE_OUTOFVISION, true);
|
||||
c->setState(Chunk::CHUNK_STATE_UNLOADED, false);
|
||||
c->unload_timer = glfwGetTime();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
total = chunks_torender.size();
|
||||
debug::window::set_parameter("render_chunks_total", total);
|
||||
debug::window::set_parameter("render_chunks_total", (int)(ChunksToRender.size()));
|
||||
debug::window::set_parameter("render_chunks_rendered", toGpu);
|
||||
debug::window::set_parameter("render_chunks_renderable", total);
|
||||
debug::window::set_parameter("render_chunks_culled", total-toGpu);
|
||||
debug::window::set_parameter("render_chunks_oof", oof);
|
||||
debug::window::set_parameter("render_chunks_deleted", (int) (render_todelete.size()));
|
||||
debug::window::set_parameter("render_chunks_vertices", vertices);
|
||||
|
||||
for(auto& c : render_todelete){
|
||||
// we can get away with unsafe erase as access to the container is only done by this
|
||||
// thread
|
||||
c->deleteBuffers();
|
||||
chunks_torender.unsafe_erase(c);
|
||||
chunkmanager::getDeleteVector().push(c);
|
||||
}
|
||||
render_todelete.clear();
|
||||
|
||||
/* DISPLAY TEXTURE ON A QUAD THAT FILLS THE SCREEN */
|
||||
// Now to render the quad, with the texture on top
|
||||
// Switch to the default frame buffer
|
||||
|
@ -251,6 +259,37 @@ namespace renderer{
|
|||
debug::window::render();
|
||||
}
|
||||
|
||||
void send_chunk_to_gpu(ChunkMeshData* mesh_data, RenderInfo* render_info)
|
||||
{
|
||||
if(!render_info->buffers_allocated) render_info->allocateBuffers();
|
||||
|
||||
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
|
||||
glBindVertexArray(render_info->VAO);
|
||||
|
||||
// TODO: change GL_STATIC_DRAW to the one that means "few redraws and further in between"
|
||||
|
||||
// position attribute
|
||||
glBindBuffer(GL_ARRAY_BUFFER, render_info->VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, mesh_data->vertices.size() * sizeof(GLfloat), &(mesh_data->vertices[0]), GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// normal attribute
|
||||
glBindBuffer(GL_ARRAY_BUFFER, render_info->extentsBuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, mesh_data->extents.size() * sizeof(GLfloat), &(mesh_data->extents[0]), GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)(0));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
// texcoords attribute
|
||||
glBindBuffer(GL_ARRAY_BUFFER, render_info->texinfoBuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, mesh_data->texinfo.size() * sizeof(GLfloat), &(mesh_data->texinfo[0]), GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void *)0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height){
|
||||
resize_framebuffer(width, height);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue