main
Brett 2023-02-13 23:37:18 -05:00
parent a62bbdc654
commit 9bcded0383
36 changed files with 289 additions and 71 deletions

View File

@ -48,7 +48,7 @@ if (USE_EXTRAS)
#set_target_properties(FinalProject PROPERTIES LINK_FLAGS "-s DEMANGLE_SUPPORT=1 --preload-file ${CMAKE_SOURCE_DIR}/assets --bind")
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/assets DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/)
#set_target_properties(FinalProject PROPERTIES LINK_FLAGS "-s DEMANGLE_SUPPORT=1 --preload-file 'assets' --bind")
set_target_properties(FinalProject PROPERTIES LINK_FLAGS "-sMAX_WEBGL_VERSION=2 -s ASSERTIONS=1 -sUSE_GLFW=3 -sFULL_ES3 --preload-file 'assets'")
set_target_properties(FinalProject PROPERTIES LINK_FLAGS "-sMAX_WEBGL_VERSION=2 -s ASSERTIONS=1 -sUSE_GLFW=3 --preload-file 'assets'")
# these flags will be set by cmake automatically based on build type
#set_target_properties(FinalProject PROPERTIES COMPILE_FLAGS "-O3")
#set_target_properties(FinalProject PROPERTIES COMPILE_FLAGS "-g")

Binary file not shown.

View File

@ -8,7 +8,7 @@
19 685 1675834610617266094 libraries/glfw-3.3.8/src/CMakeFiles/glfw.dir/vulkan.c.o 7ac098ea2e755b71
2 377 1675835240591243670 libraries/glfw-3.3.8/src/CMakeFiles/glfw.dir/window.c.o a716fdf6afcb3ac2
19 807 1675834610745269744 libraries/glfw-3.3.8/src/CMakeFiles/glfw.dir/x11_init.c.o 7a310a4749a2e05
0 98 1676321976951230956 build.ninja 190f84590f6ee728
0 98 1676326264824686580 build.ninja 190f84590f6ee728
2 434 1675915441258513581 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/format.cpp.o 342a5daa6ca9681d
18 706 1675834610629266436 libraries/glfw-3.3.8/src/CMakeFiles/glfw.dir/init.c.o 6051295f860fb367
1099 1278 1676232079155824369 libraries/BLT/libBLT.a 163b10f2e7f6635
@ -38,3 +38,13 @@
2 316 1675881681815231506 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/filesystem.cpp.o ac1bd7bf33d73c31
1 1057 1676232078943818647 CMakeFiles/FinalProject.dir/src/render/gl.cpp.o d59ba6eec5836ace
706 883 1675834610809271567 libraries/glfw-3.3.8/src/CMakeFiles/glfw.dir/linux_joystick.c.o afc8940b6e7eeae1
1 433 1676342795367607759 CMakeFiles/FinalProject.dir/src/world/chunk/storage.cpp.o c62e5c67ebfa06d
1 670 1676342795599614016 CMakeFiles/FinalProject.dir/src/render/camera.cpp.o 7f43512660c0023
1 802 1676342795731617574 CMakeFiles/FinalProject.dir/src/main.cpp.o 460fff588e5870a1
1 813 1676342795743617899 CMakeFiles/FinalProject.dir/src/render/window.cpp.o aa0f847cb87c783c
1 1014 1676342795943623289 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o c71aa549750c4a5
1 1096 1676342796027625557 CMakeFiles/FinalProject.dir/src/render/gl.cpp.o d59ba6eec5836ace
1096 1207 1676342796135628469 FinalProject 1d2a6005b7208ba1
1 909 1676342906630613400 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/logging.cpp.o 13e8110c601a4029
909 1200 1676342906918621193 libraries/BLT/libBLT.a 163b10f2e7f6635
1200 1288 1676342907010623684 FinalProject 1d2a6005b7208ba1

Binary file not shown.

View File

@ -1,3 +1,3 @@
Start testing: Feb 12 15:01 EST
Start testing: Feb 13 21:49 EST
----------------------------------------------------------
End testing: Feb 12 15:01 EST
End testing: Feb 13 21:49 EST

View File

@ -1 +1 @@
/usr/bin/em++ -g -sMAX_WEBGL_VERSION=2 -s ASSERTIONS=1 -sUSE_GLFW=3 -sFULL_ES3 --preload-file 'assets' @CMakeFiles/FinalProject.dir/objects1 -o FinalProject.js @CMakeFiles/FinalProject.dir/linkLibs.rsp
/usr/bin/em++ -g -sMAX_WEBGL_VERSION=2 -s ASSERTIONS=1 -sUSE_GLFW=3 --preload-file 'assets' @CMakeFiles/FinalProject.dir/objects1 -o FinalProject.js @CMakeFiles/FinalProject.dir/linkLibs.rsp

View File

@ -272,6 +272,9 @@ CMakeFiles/FinalProject.dir/src/main.cpp.o: \
/usr/share/emscripten/cache/sysroot/include/c++/v1/__ranges/enable_view.h \
/usr/share/emscripten/cache/sysroot/include/c++/v1/__string \
/home/brett/Documents/Brock/CS\ 3P98/Final\ Project/include/render/window.h \
/usr/share/emscripten/cache/sysroot/include/GLES2/gl2.h \
/usr/share/emscripten/cache/sysroot/include/GLES2/gl2platform.h \
/usr/share/emscripten/cache/sysroot/include/KHR/khrplatform.h \
/usr/share/emscripten/cache/sysroot/include/emscripten.h \
/usr/share/emscripten/cache/sysroot/include/emscripten/emscripten.h \
/usr/share/emscripten/cache/sysroot/include/emscripten/em_asm.h \
@ -283,7 +286,6 @@ CMakeFiles/FinalProject.dir/src/main.cpp.o: \
/usr/share/emscripten/cache/sysroot/include/GLFW/glfw3.h \
/usr/share/emscripten/cache/sysroot/include/GL/gl.h \
/usr/share/emscripten/cache/sysroot/include/GL/glext.h \
/usr/share/emscripten/cache/sysroot/include/KHR/khrplatform.h \
/home/brett/Documents/Brock/CS\ 3P98/Final\ Project/libraries/BLT/include/blt/std/math.h \
/usr/share/emscripten/cache/sysroot/include/c++/v1/cmath \
/usr/share/emscripten/cache/sysroot/include/compat/math.h \

View File

@ -2,6 +2,17 @@ CMakeFiles/FinalProject.dir/src/render/gl.cpp.o: \
/home/brett/Documents/Brock/CS\ 3P98/Final\ Project/src/render/gl.cpp \
/home/brett/Documents/Brock/CS\ 3P98/Final\ Project/include/render/gl.h \
/home/brett/Documents/Brock/CS\ 3P98/Final\ Project/include/render/window.h \
/usr/share/emscripten/cache/sysroot/include/GLES2/gl2.h \
/usr/share/emscripten/cache/sysroot/include/GLES2/gl2platform.h \
/usr/share/emscripten/cache/sysroot/include/KHR/khrplatform.h \
/usr/share/emscripten/cache/sysroot/include/c++/v1/stdint.h \
/usr/share/emscripten/cache/sysroot/include/c++/v1/__config \
/usr/share/emscripten/cache/sysroot/include/c++/v1/__config_site \
/usr/share/emscripten/cache/sysroot/include/pthread.h \
/usr/lib/llvm-14/lib/clang/14.0.6/include/stdint.h \
/usr/share/emscripten/cache/sysroot/include/stdint.h \
/usr/share/emscripten/cache/sysroot/include/bits/alltypes.h \
/usr/share/emscripten/cache/sysroot/include/bits/stdint.h \
/usr/share/emscripten/cache/sysroot/include/emscripten.h \
/usr/share/emscripten/cache/sysroot/include/emscripten/emscripten.h \
/usr/share/emscripten/cache/sysroot/include/emscripten/em_asm.h \
@ -12,20 +23,11 @@ CMakeFiles/FinalProject.dir/src/render/gl.cpp.o: \
/usr/share/emscripten/cache/sysroot/include/emscripten/version.h \
/usr/share/emscripten/cache/sysroot/include/GLFW/glfw3.h \
/usr/share/emscripten/cache/sysroot/include/c++/v1/stddef.h \
/usr/share/emscripten/cache/sysroot/include/c++/v1/__config \
/usr/share/emscripten/cache/sysroot/include/c++/v1/__config_site \
/usr/share/emscripten/cache/sysroot/include/pthread.h \
/usr/lib/llvm-14/lib/clang/14.0.6/include/stddef.h \
/usr/lib/llvm-14/lib/clang/14.0.6/include/__stddef_max_align_t.h \
/usr/share/emscripten/cache/sysroot/include/c++/v1/__nullptr \
/usr/share/emscripten/cache/sysroot/include/c++/v1/stdint.h \
/usr/lib/llvm-14/lib/clang/14.0.6/include/stdint.h \
/usr/share/emscripten/cache/sysroot/include/stdint.h \
/usr/share/emscripten/cache/sysroot/include/bits/alltypes.h \
/usr/share/emscripten/cache/sysroot/include/bits/stdint.h \
/usr/share/emscripten/cache/sysroot/include/GL/gl.h \
/usr/share/emscripten/cache/sysroot/include/GL/glext.h \
/usr/share/emscripten/cache/sysroot/include/KHR/khrplatform.h \
/home/brett/Documents/Brock/CS\ 3P98/Final\ Project/libraries/BLT/include/blt/std/math.h \
/usr/share/emscripten/cache/sysroot/include/c++/v1/initializer_list \
/usr/share/emscripten/cache/sysroot/include/c++/v1/cstddef \

View File

@ -1,6 +1,17 @@
CMakeFiles/FinalProject.dir/src/render/window.cpp.o: \
/home/brett/Documents/Brock/CS\ 3P98/Final\ Project/src/render/window.cpp \
/home/brett/Documents/Brock/CS\ 3P98/Final\ Project/include/render/window.h \
/usr/share/emscripten/cache/sysroot/include/GLES2/gl2.h \
/usr/share/emscripten/cache/sysroot/include/GLES2/gl2platform.h \
/usr/share/emscripten/cache/sysroot/include/KHR/khrplatform.h \
/usr/share/emscripten/cache/sysroot/include/c++/v1/stdint.h \
/usr/share/emscripten/cache/sysroot/include/c++/v1/__config \
/usr/share/emscripten/cache/sysroot/include/c++/v1/__config_site \
/usr/share/emscripten/cache/sysroot/include/pthread.h \
/usr/lib/llvm-14/lib/clang/14.0.6/include/stdint.h \
/usr/share/emscripten/cache/sysroot/include/stdint.h \
/usr/share/emscripten/cache/sysroot/include/bits/alltypes.h \
/usr/share/emscripten/cache/sysroot/include/bits/stdint.h \
/usr/share/emscripten/cache/sysroot/include/emscripten.h \
/usr/share/emscripten/cache/sysroot/include/emscripten/emscripten.h \
/usr/share/emscripten/cache/sysroot/include/emscripten/em_asm.h \
@ -11,20 +22,11 @@ CMakeFiles/FinalProject.dir/src/render/window.cpp.o: \
/usr/share/emscripten/cache/sysroot/include/emscripten/version.h \
/usr/share/emscripten/cache/sysroot/include/GLFW/glfw3.h \
/usr/share/emscripten/cache/sysroot/include/c++/v1/stddef.h \
/usr/share/emscripten/cache/sysroot/include/c++/v1/__config \
/usr/share/emscripten/cache/sysroot/include/c++/v1/__config_site \
/usr/share/emscripten/cache/sysroot/include/pthread.h \
/usr/lib/llvm-14/lib/clang/14.0.6/include/stddef.h \
/usr/lib/llvm-14/lib/clang/14.0.6/include/__stddef_max_align_t.h \
/usr/share/emscripten/cache/sysroot/include/c++/v1/__nullptr \
/usr/share/emscripten/cache/sysroot/include/c++/v1/stdint.h \
/usr/lib/llvm-14/lib/clang/14.0.6/include/stdint.h \
/usr/share/emscripten/cache/sysroot/include/stdint.h \
/usr/share/emscripten/cache/sysroot/include/bits/alltypes.h \
/usr/share/emscripten/cache/sysroot/include/bits/stdint.h \
/usr/share/emscripten/cache/sysroot/include/GL/gl.h \
/usr/share/emscripten/cache/sysroot/include/GL/glext.h \
/usr/share/emscripten/cache/sysroot/include/KHR/khrplatform.h \
/home/brett/Documents/Brock/CS\ 3P98/Final\ Project/libraries/BLT/include/blt/std/math.h \
/usr/share/emscripten/cache/sysroot/include/c++/v1/initializer_list \
/usr/share/emscripten/cache/sysroot/include/c++/v1/cstddef \

View File

@ -33,3 +33,49 @@
2 409 1676094324978000454 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/filesystem.cpp.o 49e1226ae352c85c
1 1218 1676232405100658939 CMakeFiles/FinalProject.dir/src/render/gl.cpp.o 2e02d50528893aea
311 524 1675879686313903031 libraries/glfw-3.3.8/src/CMakeFiles/glfw.dir/linux_joystick.c.o 3df40cb71360060f
1 418 1676342959700050381 CMakeFiles/FinalProject.dir/src/world/chunk/storage.cpp.o 8466edd0d9c6fbd8
1 636 1676342959916056232 CMakeFiles/FinalProject.dir/src/render/camera.cpp.o b8ecbf9d580cb1ce
1 733 1676342960012058836 CMakeFiles/FinalProject.dir/src/render/window.cpp.o fa6296e23b056a91
1 794 1676342960072060459 CMakeFiles/FinalProject.dir/src/main.cpp.o d6fd815a69105af1
1 853 1676342960132062088 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
1 1199 1676342960476071406 CMakeFiles/FinalProject.dir/src/render/gl.cpp.o 2e02d50528893aea
1 1274 1676342960552073465 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/logging.cpp.o a4d3cad66687d105
1274 1353 1676342960624075419 libraries/BLT/libBLT.a 163b10f2e7f6635
1353 1434 1676342960712077804 FinalProject e2e3313cdc6f5890
1 821 1676342992684944487 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
822 903 1676342992764946656 FinalProject e2e3313cdc6f5890
1 730 1676343073567139725 CMakeFiles/FinalProject.dir/src/main.cpp.o d6fd815a69105af1
730 801 1676343073639141681 FinalProject e2e3313cdc6f5890
2 751 1676343083775417051 CMakeFiles/FinalProject.dir/src/main.cpp.o d6fd815a69105af1
751 829 1676343083851419116 FinalProject e2e3313cdc6f5890
1 764 1676343095695740956 CMakeFiles/FinalProject.dir/src/main.cpp.o d6fd815a69105af1
764 839 1676343095767742915 FinalProject e2e3313cdc6f5890
1 286 1676347707994543363 CMakeFiles/FinalProject.dir/src/world/registry.cpp.o 3a6f3370180acf81
1 411 1676347708118546768 CMakeFiles/FinalProject.dir/src/world/chunk/storage.cpp.o 8466edd0d9c6fbd8
1 813 1676347708518557748 CMakeFiles/FinalProject.dir/src/main.cpp.o d6fd815a69105af1
1 789 1676347718558833379 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
789 883 1676347718650835905 FinalProject e2e3313cdc6f5890
2 402 1676347879019239298 CMakeFiles/FinalProject.dir/src/world/chunk/storage.cpp.o 8466edd0d9c6fbd8
2 786 1676347879403249843 CMakeFiles/FinalProject.dir/src/main.cpp.o d6fd815a69105af1
2 826 1676347879443250941 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
826 896 1676347879515252919 FinalProject e2e3313cdc6f5890
2 367 1676347920740385109 CMakeFiles/FinalProject.dir/src/world/chunk/storage.cpp.o 8466edd0d9c6fbd8
1 812 1676347921184397303 CMakeFiles/FinalProject.dir/src/main.cpp.o d6fd815a69105af1
2 850 1676347921224398401 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
850 925 1676347921300400489 FinalProject e2e3313cdc6f5890
1 738 1676347991758335700 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
738 812 1676347991830337678 FinalProject e2e3313cdc6f5890
1 767 1676348340671921556 CMakeFiles/FinalProject.dir/src/main.cpp.o d6fd815a69105af1
1 789 1676348340695922215 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
789 869 1676348340771924303 FinalProject e2e3313cdc6f5890
1 781 1676348494136171853 CMakeFiles/FinalProject.dir/src/main.cpp.o d6fd815a69105af1
1 850 1676348494204173737 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
851 924 1676348494276175733 FinalProject e2e3313cdc6f5890
1 802 1676349157234497649 CMakeFiles/FinalProject.dir/src/main.cpp.o d6fd815a69105af1
1 832 1676349157262498421 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
832 909 1676349157338500519 FinalProject e2e3313cdc6f5890
1 857 1676349184543250718 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
857 932 1676349184619252813 FinalProject e2e3313cdc6f5890
1 812 1676349209335934338 CMakeFiles/FinalProject.dir/src/main.cpp.o d6fd815a69105af1
1 912 1676349209435937096 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
912 1010 1676349209535939851 FinalProject e2e3313cdc6f5890

View File

@ -1,3 +1,3 @@
Start testing: Feb 12 15:07 EST
Start testing: Feb 13 23:33 EST
----------------------------------------------------------
End testing: Feb 12 15:07 EST
End testing: Feb 13 23:33 EST

View File

@ -55,11 +55,11 @@ namespace fp {
inline void update(void* new_data, int data_size) {
bind();
// optimization technique is to not reallocate the memory on the GPU if the new buffer size is not larger than our current buffer
//if (data_size <= size){
if (data_size <= size){
// we can do this as long as we overwrite from the beginning. Since the new draw call will only use of to size of the allocated buffer
// to do all its drawing, the extra space unused can be ignored and saved for future use.
// glBufferSubData(type, 0, data_size, new_data);
//} else
glBufferSubData(type, 0, data_size, new_data);
} else
glBufferData(type, data_size, new_data, mem_type);
data = new_data;
size = data_size;

View File

@ -15,8 +15,10 @@
#ifndef __EMSCRIPTEN__
#include <glad/gles2.h>
#else
#include <GLES2/gl2.h>
#include <emscripten.h>
#define GL_GLEXT_PROTOTYPES
#define EGL_EGLEXT_PROTOTYPES
#endif
#include <GLFW/glfw3.h>
#include <blt/std/math.h>

View File

@ -11,6 +11,7 @@
#include <vector>
#include "blt/std/logging.h"
#include <world/chunk/typedefs.h>
#include <world/registry.h>
// contains storage classes for block IDs inside chunks plus eventual lookup of block states
@ -18,23 +19,32 @@ namespace fp {
class block_storage {
private:
char* blocks;
block_type* blocks;
public:
block_storage() {
blocks = new char[CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE];
blocks = new unsigned char[CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE];
for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE; i++)
blocks[i] = 0;
blocks[i] = fp::registry::AIR;
}
~block_storage() {
delete[] blocks;
}
[[nodiscard]] inline char get(const block_pos& pos) const {
[[nodiscard]] inline block_type get(const block_pos& pos) const {
return blocks[pos.z * CHUNK_SIZE * CHUNK_SIZE + pos.y * CHUNK_SIZE + pos.x];
}
inline void set(const block_pos& pos, char blockID) {
[[nodiscard]] inline block_type getBounded(bool& outside, const block_pos& pos) const {
if (pos.x < 0 || pos.x >= CHUNK_SIZE || pos.y < 0 || pos.y >= CHUNK_SIZE || pos.z < 0 || pos.z >= CHUNK_SIZE) {
outside = true;
return fp::registry::AIR;
}
outside = false;
return get(pos);
}
inline void set(const block_pos& pos, block_type blockID) {
blocks[pos.z * CHUNK_SIZE * CHUNK_SIZE + pos.y * CHUNK_SIZE + pos.x] = blockID;
}
};
@ -49,7 +59,7 @@ namespace fp {
auto new_x = array[i] + (float)pos.x;
auto new_y = array[i + 1] + (float)pos.y;
auto new_z = array[i + 2] + (float)pos.z;
BLT_TRACE("Creating translated vertex {%f, %f, %f} from array position [%d, %d, %d]", new_x, new_y, new_z, i, i + 1, i + 2);
// BLT_TRACE("Creating translated vertex {%f, %f, %f} from array position [%d, %d, %d]", new_x, new_y, new_z, i, i + 1, i + 2);
vertices.push_back(new_x);
vertices.push_back(new_y);
vertices.push_back(new_z);

View File

@ -24,7 +24,7 @@ namespace fp {
Z_NEG = 5,
};
enum chunk_status {
enum chunk_mesh_status {
// chunk is okay and needs no special action
OKAY = 0,
// chunk needs its VAO updated with the newest mesh
@ -35,6 +35,11 @@ namespace fp {
FULL_MESH = 3
};
enum chunk_update_status {
NONE = 0,
NEIGHBOUR_CREATE = 1,
};
struct chunk_pos {
int x, y, z;
};

View File

@ -70,7 +70,8 @@ namespace fp {
VAO* chunk_vao;
chunk_pos pos;
chunk_status dirtiness = OKAY;
chunk_mesh_status dirtiness = OKAY;
chunk_update_status status = NONE;
unsigned long render_size = 0;
public:
explicit chunk(chunk_pos pos): pos(pos) {
@ -94,14 +95,50 @@ namespace fp {
}
};
struct chunk_neighbours {
fp::chunk* neighbours[6];
inline chunk*& operator[](int i) {
return neighbours[i];
}
};
class world {
private:
std::unordered_map<chunk_pos, chunk*, _static::chunk_pos_hash, _static::chunk_pos_equality> chunk_storage;
protected:
void generateFullMesh(mesh_storage* mesh, chunk* chunk);
void generateEdgeMesh(mesh_storage* mesh, chunk* chunk);
void generateChunkMesh(chunk* chunk);
inline void getNeighbours(const chunk_pos& pos, chunk_neighbours& neighbours) {
neighbours[X_POS] = getChunk(chunk_pos{pos.x + 1, pos.y, pos.z});
neighbours[X_NEG] = getChunk(chunk_pos{pos.x - 1, pos.y, pos.z});
neighbours[Y_POS] = getChunk(chunk_pos{pos.x, pos.y + 1, pos.z});
neighbours[Y_NEG] = getChunk(chunk_pos{pos.x, pos.y - 1, pos.z});
neighbours[Z_POS] = getChunk(chunk_pos{pos.x, pos.y, pos.z + 1});
neighbours[Z_NEG] = getChunk(chunk_pos{pos.x, pos.y, pos.z - 1});
}
inline void insertChunk(chunk* chunk) {
chunk_storage.insert({chunk->pos, chunk});
chunk_neighbours chunkNeighbours{};
getNeighbours(chunk->pos, chunkNeighbours);
for (auto* p : chunkNeighbours.neighbours){
if (p)
p->status = NEIGHBOUR_CREATE;
}
}
inline chunk* getChunk(const chunk_pos& pos){
const auto map_pos = chunk_storage.find(pos);
if (map_pos == chunk_storage.end())
return nullptr;
return map_pos->second;
}
inline chunk* getChunk(const block_pos& pos) {
@ -111,6 +148,10 @@ namespace fp {
public:
world() {
insertChunk(new chunk({0, 0, 0}));
insertChunk(new chunk({0, 1, 0}));
insertChunk(new chunk({0, -1, 0}));
insertChunk(new chunk({1, 0, 0}));
insertChunk(new chunk({0, 0, 1}));
insertChunk(new chunk({-1, 0, 0}));
insertChunk(new chunk({0, 0, -1}));
insertChunk(new chunk({-1, 0, -1}));
@ -120,15 +161,20 @@ namespace fp {
void render(fp::shader& shader);
inline void setBlock(const block_pos& pos, char blockID) {
inline bool setBlock(const block_pos& pos, block_type blockID) {
auto c = getChunk(pos);
if (!c)
return false;
// mark the chunk for a mesh update
c->dirtiness = FULL_MESH;
c->storage->set(_static::world_to_internal(pos), blockID);
return true;
}
inline char getBlock(const block_pos& pos) {
inline block_type getBlock(const block_pos& pos) {
auto c = getChunk(pos);
if (!c)
return fp::registry::AIR;
return c->storage->get(_static::world_to_internal(pos));
}

View File

@ -7,4 +7,37 @@
#ifndef FINALPROJECT_REGISTRY_H
#define FINALPROJECT_REGISTRY_H
namespace fp {
typedef unsigned char block_type;
}
namespace fp::registry {
enum block_visibility {
OPAQUE = 0,
TRANSLUCENT = 1,
TRANSPARENT = 2,
};
struct block_properties {
block_visibility visibility = OPAQUE;
};
constexpr block_type AIR = 0;
constexpr block_type STONE = 1;
void registerBlock(block_type id, block_properties properties);
block_properties get(block_type id);
/**
* Registers all the default blocks used by the engine
*/
inline void registerDefaultBlocks(){
registerBlock(AIR, {TRANSPARENT});
registerBlock(STONE, {});
}
}
#endif //FINALPROJECT_REGISTRY_H

@ -1 +1 @@
Subproject commit e50cc74bf3ba56455e0db3208d95af3546561341
Subproject commit a196a2aa7db167fb293cbbea59516c50c45b48c5

View File

@ -40,12 +40,15 @@ int main() {
blt::logging::init(logging_properties);
fp::window::init();
fp::registry::registerDefaultBlocks();
chunk_shader = new fp::shader(shader_chunk_vert, shader_chunk_frag);
world = new fp::world();
world->setBlock({0, 0, 0}, 1);
world->setBlock({2, 2, 2}, 1);
for (int i = 1; i < CHUNK_SIZE; i++)
for (int j = 0; j < 3; j++)
for (int k = 5; k < CHUNK_SIZE; k++)
world->setBlock({i,j,k}, 1);
world->setBlock({-2, 2, 2}, 1);
world->setBlock({-2, 2, -2}, 1);
world->setBlock({2, 2, -2}, 1);
@ -60,7 +63,7 @@ int main() {
* make sure you render at a proper smooth rate that lines up properly with the browser and monitor."
* https://emscripten.org/docs/api_reference/emscripten.h.html
*/
emscripten_set_main_loop(loop, 60, true);
emscripten_set_main_loop(loop, 0, true);
#else
while(!fp::window::isCloseRequested())
loop();

View File

@ -23,8 +23,6 @@ void updateCursorGrabbedState(){
#ifndef __EMSCRIPTEN__
if (fp::window::isKeyPressed(GLFW_KEY_ESCAPE) && fp::window::keyState())
fp::window::mouseGrabbed(!fp::window::mouseGrabbed());
#else
//fp::window::mouseGrabbed(true);
#endif
}

View File

@ -36,6 +36,7 @@ double mouse_last_y;
#ifdef __EMSCRIPTEN__
EM_BOOL on_pointerlockchange(int eventType, const EmscriptenPointerlockChangeEvent *event, void *userData) {
BLT_TRACE("Emscripten pointer lock event status %d", event->isActive);
if (event->isActive != fp::window::mouseGrabbed())
glfwSetInputMode(global_window, GLFW_CURSOR, event->isActive ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
return 0;
}

View File

@ -5,30 +5,75 @@
*/
#include <world/chunk/world.h>
void fp::world::generateChunkMesh(fp::chunk* chunk) {
auto* meshStorage = new mesh_storage;
void fp::world::generateFullMesh(mesh_storage* mesh, fp::chunk* chunk) {
// checks to outside the bounds of the chunk should not have faces added. this will be handled by the partial mesh!
bool outside = false;
for (int i = 1; i < CHUNK_SIZE-1; i++) {
for (int j = 1; j < CHUNK_SIZE-1; j++) {
for (int k = 1; k < CHUNK_SIZE-1; k++) {
auto block = chunk->storage->get({i, j, k});
// opaque visibility is always 0. Non-zero values (true) are what we care about since opaque blocks are completely hidden
if (!fp::registry::get(block).visibility) {
if (fp::registry::get(chunk->storage->getBounded(outside, {i-1, j, k})).visibility && !outside)
mesh->addFace(X_NEG, {i, j, k});
if (fp::registry::get(chunk->storage->getBounded(outside, {i+1, j, k})).visibility && !outside)
mesh->addFace(X_POS, {i, j, k});
if (fp::registry::get(chunk->storage->getBounded(outside, {i, j-1, k})).visibility && !outside)
mesh->addFace(Y_NEG, {i, j, k});
if (fp::registry::get(chunk->storage->getBounded(outside, {i, j+1, k})).visibility && !outside)
mesh->addFace(Y_POS, {i, j, k});
if (fp::registry::get(chunk->storage->getBounded(outside, {i, j, k-1})).visibility && !outside)
mesh->addFace(Z_NEG, {i, j, k});
if (fp::registry::get(chunk->storage->getBounded(outside, {i, j, k+1})).visibility && !outside)
mesh->addFace(Z_POS, {i, j, k});
}
}
}
}
chunk->dirtiness = PARTIAL_MESH;
}
void fp::world::generateEdgeMesh(mesh_storage* mesh, fp::chunk* chunk) {
// don't try to regen the chunk mesh unless there is a chance all neighbours are not null
if (chunk->status != chunk_update_status::NEIGHBOUR_CREATE)
return;
chunk_neighbours neighbours {};
getNeighbours(chunk->pos, neighbours);
// if none of the neighbours exist we cannot continue!
for (auto* neighbour : neighbours.neighbours){
if (!neighbour)
return;
}
if (chunk->dirtiness == FULL_MESH) { // full chunk mesh
for (int i = 0; i < CHUNK_SIZE; i++){
for (int j = 0; j < CHUNK_SIZE; j++){
for (int k = 0; k < CHUNK_SIZE; k++) {
auto block = chunk->storage->get({i, j, k});
if (block != 0) {
meshStorage->addFace(X_NEG, {i, j, k});
meshStorage->addFace(X_POS, {i, j, k});
meshStorage->addFace(Y_NEG, {i, j, k});
meshStorage->addFace(Y_POS, {i, j, k});
meshStorage->addFace(Z_NEG, {i, j, k});
meshStorage->addFace(Z_POS, {i, j, k});
auto block = chunk->storage->get({0, i, j});
if (!fp::registry::get(block).visibility) {
auto neighbour = neighbours[X_NEG]->storage->get({CHUNK_SIZE-1, i, j});
if (fp::registry::get(neighbour).visibility)
mesh->addFace(X_NEG, {0, i, j});
}
}
}
}
} else if (chunk->dirtiness == PARTIAL_MESH){ // partial chunk mesh (had null edges)
}
chunk->mesh = meshStorage;
chunk->status = NONE;
chunk->dirtiness = REFRESH;
}
void fp::world::generateChunkMesh(fp::chunk* chunk) {
if (chunk->mesh == nullptr)
chunk->mesh = new mesh_storage;
if (chunk->dirtiness == FULL_MESH) { // full chunk mesh
generateFullMesh(chunk->mesh, chunk);
} else if (chunk->dirtiness == PARTIAL_MESH){ // partial chunk mesh (had null neighbours)
generateEdgeMesh(chunk->mesh, chunk);
}
chunk->dirtiness = REFRESH;
}
@ -42,6 +87,9 @@ void fp::world::render(fp::shader& shader) {
for (const auto& chunk_pair : chunk_storage) {
auto chunk = chunk_pair.second;
if (chunk == nullptr)
continue;
if (chunk->dirtiness > REFRESH){
generateChunkMesh(chunk);
}
@ -55,7 +103,7 @@ void fp::world::render(fp::shader& shader) {
// upload the new vertices to the GPU
chunk->chunk_vao->getVBO(0)->update(vertices);
chunk->render_size = vertices.size();
chunk->render_size = vertices.size() / 3;
// delete the memory from the CPU.
delete(chunk->mesh);
@ -69,7 +117,6 @@ void fp::world::render(fp::shader& shader) {
shader.setMatrix("translation", translation);
chunk->chunk_vao->bind();
glEnableVertexAttribArray(0);
//glDrawElements(GL_TRIANGLES, render_size, GL_UNSIGNED_INT, nullptr);
glDrawArrays(GL_TRIANGLES, 0, (int)chunk->render_size);
glDisableVertexAttribArray(0);
}

View File

@ -4,3 +4,14 @@
* See LICENSE file for license detail
*/
#include <world/registry.h>
#include <unordered_map>
std::unordered_map<fp::block_type, fp::registry::block_properties> blocks;
void fp::registry::registerBlock(fp::block_type id, fp::registry::block_properties properties) {
blocks[id] = properties;
}
fp::registry::block_properties fp::registry::get(fp::block_type id) {
return blocks.at(id);
}