From 1a00a11917028723f33f294db6200dd4899104e1 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 4 Aug 2021 01:45:04 +0300 Subject: [PATCH] All changes made for 10th part of tutorial voxel_engine.cpp - changed structure, added character controls, physics almost all files are modified (including shaders) + new source directory - physics --- res/block_.png | Bin 0 -> 4870 bytes res/block_white.png | Bin 0 -> 2039 bytes res/main.glslv | 13 +- src/files/WorldFiles.cpp | 23 ++- src/files/WorldFiles.h | 4 +- src/graphics/Shader.cpp | 21 +++ src/graphics/Shader.h | 4 + src/lighting/LightSolver.cpp | 2 + src/loaders/png_loading.cpp | 124 +++++++++++++- src/loaders/png_loading.h | 7 - src/physics/Hitbox.cpp | 4 + src/physics/Hitbox.h | 20 +++ src/physics/PhysicsSolver.cpp | 148 +++++++++++++++++ src/physics/PhysicsSolver.h | 21 +++ src/voxel_engine.cpp | 302 +++++++++++++++++++++------------- src/voxels/Block.h | 1 + src/voxels/Chunk.h | 2 +- src/voxels/Chunks.cpp | 15 +- src/voxels/Chunks.h | 6 +- src/window/Camera.cpp | 11 +- src/window/Camera.h | 2 + src/window/Window.cpp | 6 + 22 files changed, 587 insertions(+), 149 deletions(-) create mode 100644 res/block_.png create mode 100644 res/block_white.png create mode 100644 src/physics/Hitbox.cpp create mode 100644 src/physics/Hitbox.h create mode 100644 src/physics/PhysicsSolver.cpp create mode 100644 src/physics/PhysicsSolver.h diff --git a/res/block_.png b/res/block_.png new file mode 100644 index 0000000000000000000000000000000000000000..d4941919d6900bc32cb8e080a2950892ea537d27 GIT binary patch literal 4870 zcmV+h6Z!0kP) zaB^>EX>4U6ba`-PAZ2)IW&i+q+O3%BktDYfh5y3}nSlE+2UkVN1Ddbz0o^?_yOcym za4NRz1`_#>%v!Vm`1fP}#h=eHo0v+?EoaMLY_a*yLACdqBi&V5?WZH0@n1~gurG0iN&3H+nH~=%FN60 zsCNwJc;&xt=6~J%JDYi)sT4U|Ke-|w`an-)4?#hBnvi4_j@6jDqnLyXwboX9^DW?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%>6)Hv7(UE9&%&SpG>MUc zfrEj;0muSoTLA`!1|W-t0jt!3`IB$fvfp1R%6Nbqy9x}=2m^t}j)Kt;7!84;76Ko- YxWDe^aC%-I_z2{5Pgg&ebxsLQ023<5`Tzg` literal 0 HcmV?d00001 diff --git a/res/main.glslv b/res/main.glslv index f2b67588..1e1cb98a 100644 --- a/res/main.glslv +++ b/res/main.glslv @@ -7,15 +7,16 @@ layout (location = 2) in vec4 v_light; out vec4 a_color; out vec2 a_texCoord; -uniform mat4 model; -uniform mat4 projview; +uniform mat4 u_model; +uniform mat4 u_projview; +uniform vec3 u_skyLightColor; +uniform float u_gamma; void main(){ - vec4 position = projview * model * vec4(v_position, 1.0); - a_color = vec4(v_light.r,v_light.g,v_light.b,1.0f); + vec4 position = u_projview * u_model * vec4(v_position, 1.0); + a_color = vec4(pow(v_light.rgb, vec3(u_gamma)),1.0f); a_texCoord = v_texCoord; - a_color.rgb += v_light.a; + a_color.rgb += u_skyLightColor * v_light.a*0.5; a_color.rgb *= 1.0-position.z*0.0025; - //a_color.rgb = pow(a_color.rgb, vec3(1.0/0.7)); gl_Position = position; } diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index d4a8c7e3..6df02573 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -14,6 +14,8 @@ union { #include #include +unsigned long WorldFiles::totalCompressed = 0; + int bytes2Int(const unsigned char* src, unsigned int offset){ return (src[offset] << 24) | (src[offset+1] << 16) | (src[offset+2] << 8) | (src[offset+3]); } @@ -26,11 +28,13 @@ void int2Bytes(int value, char* dest, unsigned int offset){ } WorldFiles::WorldFiles(const char* directory, size_t mainBufferCapacity) : directory(directory){ - mainBuffer = new char[mainBufferCapacity]; + mainBufferIn = new char[CHUNK_VOL*2]; + mainBufferOut = new char[mainBufferCapacity]; } WorldFiles::~WorldFiles(){ - delete[] mainBuffer; + delete[] mainBufferIn; + delete[] mainBufferOut; std::unordered_map::iterator it; for (it = regions.begin(); it != regions.end(); it++){ char** region = it->second; @@ -66,9 +70,11 @@ void WorldFiles::put(const char* chunkData, int x, int y){ if (targetChunk == nullptr){ targetChunk = new char[CHUNK_VOL]; region[localY * REGION_SIZE + localX] = targetChunk; + totalCompressed += CHUNK_VOL; } for (unsigned int i = 0; i < CHUNK_VOL; i++) targetChunk[i] = chunkData[i]; + } std::string WorldFiles::getRegionFile(int x, int y) { @@ -123,19 +129,19 @@ bool WorldFiles::readChunk(int x, int y, char* out){ input.read((char*)(&offset), 4); // Ordering bytes from big-endian to machine order (any, just reading) offset = bytes2Int((const unsigned char*)(&offset), 0); + assert (offset < 1000000); if (offset == 0){ input.close(); return false; } - input.seekg(offset); input.read((char*)(&offset), 4); size_t compressedSize = bytes2Int((const unsigned char*)(&offset), 0); - input.read(mainBuffer, compressedSize); + input.read(mainBufferIn, compressedSize); input.close(); - decompressRLE(mainBuffer, compressedSize, out, CHUNK_VOL); + decompressRLE(mainBufferIn, compressedSize, out, CHUNK_VOL); return true; } @@ -150,8 +156,8 @@ void WorldFiles::write(){ int y; longToCoords(x,y, it->first); - unsigned int size = writeRegion(mainBuffer, x,y, it->second); - write_binary_file(getRegionFile(x,y), mainBuffer, size); + unsigned int size = writeRegion(mainBufferOut, x,y, it->second); + write_binary_file(getRegionFile(x,y), mainBufferOut, size); } } @@ -165,8 +171,11 @@ unsigned int WorldFiles::writeRegion(char* out, int x, int y, char** region){ char* chunk = region[i]; if (chunk == nullptr){ chunk = new char[CHUNK_VOL]; + assert((((i % REGION_SIZE) + x * REGION_SIZE) >> REGION_SIZE_BIT) == x); + assert((((i / REGION_SIZE) + y * REGION_SIZE) >> REGION_SIZE_BIT) == y); if (readChunk((i % REGION_SIZE) + x * REGION_SIZE, (i / REGION_SIZE) + y * REGION_SIZE, chunk)){ region[i] = chunk; + totalCompressed += CHUNK_VOL; } else { delete[] chunk; chunk = nullptr; diff --git a/src/files/WorldFiles.h b/src/files/WorldFiles.h index bc816adf..7cde5a42 100644 --- a/src/files/WorldFiles.h +++ b/src/files/WorldFiles.h @@ -15,9 +15,11 @@ * */ class WorldFiles { public: + static unsigned long totalCompressed; std::unordered_map regions; std::string directory; - char* mainBuffer; + char* mainBufferIn; + char* mainBufferOut; WorldFiles(const char* directory, size_t mainBufferCapacity); ~WorldFiles(); diff --git a/src/graphics/Shader.cpp b/src/graphics/Shader.cpp index 34108c55..fbfc17bd 100644 --- a/src/graphics/Shader.cpp +++ b/src/graphics/Shader.cpp @@ -26,6 +26,27 @@ void Shader::uniformMatrix(std::string name, glm::mat4 matrix){ glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(matrix)); } +void Shader::uniform1i(std::string name, int x){ + GLuint transformLoc = glGetUniformLocation(id, name.c_str()); + glUniform1i(transformLoc, x); +} + +void Shader::uniform1f(std::string name, float x){ + GLuint transformLoc = glGetUniformLocation(id, name.c_str()); + glUniform1f(transformLoc, x); +} + +void Shader::uniform2f(std::string name, float x, float y){ + GLuint transformLoc = glGetUniformLocation(id, name.c_str()); + glUniform2f(transformLoc, x, y); +} + +void Shader::uniform3f(std::string name, float x, float y, float z){ + GLuint transformLoc = glGetUniformLocation(id, name.c_str()); + glUniform3f(transformLoc, x,y,z); +} + + Shader* load_shader(std::string vertexFile, std::string fragmentFile) { // Reading Files diff --git a/src/graphics/Shader.h b/src/graphics/Shader.h index baed340d..9507d348 100644 --- a/src/graphics/Shader.h +++ b/src/graphics/Shader.h @@ -13,6 +13,10 @@ public: void use(); void uniformMatrix(std::string name, glm::mat4 matrix); + void uniform1i(std::string name, int x); + void uniform1f(std::string name, float x); + void uniform2f(std::string name, float x, float y); + void uniform3f(std::string name, float x, float y, float z); }; extern Shader* load_shader(std::string vertexFile, std::string fragmentFile); diff --git a/src/lighting/LightSolver.cpp b/src/lighting/LightSolver.cpp index 1ec493a8..c101539c 100644 --- a/src/lighting/LightSolver.cpp +++ b/src/lighting/LightSolver.cpp @@ -1,3 +1,4 @@ +#include #include "LightSolver.h" #include "Lightmap.h" #include "../voxels/Chunks.h" @@ -24,6 +25,7 @@ void LightSolver::add(int x, int y, int z, int emission) { } void LightSolver::add(int x, int y, int z) { + assert (chunks != nullptr); add(x,y,z, chunks->getLight(x,y,z, channel)); } diff --git a/src/loaders/png_loading.cpp b/src/loaders/png_loading.cpp index 9d69322b..c101632c 100644 --- a/src/loaders/png_loading.cpp +++ b/src/loaders/png_loading.cpp @@ -1,17 +1,15 @@ -/* - * png_loading.cpp - * - * Created on: Feb 10, 2020 - * Author: MihailRis - */ - #include "png_loading.h" #include #include -#include #include "../graphics/Texture.h" +// comment line below for use spng instead of libpng +#define LIBPNG + +#ifdef LIBPNG +#include + int _png_load(const char* file, int* width, int* height){ FILE *f; int is_png, bit_depth, color_type, row_bytes; @@ -113,6 +111,116 @@ int _png_load(const char* file, int* width, int* height){ fclose( f ); return texture; } +#else +#include +#include +#include + +int _png_load(const char* file, int* pwidth, int* pheight){ + int r = 0; + FILE *png; + char *pngbuf = nullptr; + spng_ctx *ctx = nullptr; + unsigned char *out = nullptr; + + png = fopen(file, "rb"); + if (png == nullptr){ + std::cerr << "could not to open file " << file << std::endl; + return 0; + } + + fseek(png, 0, SEEK_END); + long siz_pngbuf = ftell(png); + rewind(png); + if(siz_pngbuf < 1) { + std::cerr << "could not to read file " << file << std::endl; + return 0; + } + pngbuf = new char[siz_pngbuf]; + if(fread(pngbuf, siz_pngbuf, 1, png) != 1){ + std::cerr << "fread() failed" << std::endl; + return 0; + } + ctx = spng_ctx_new(0); + if (ctx == nullptr){ + std::cerr << "spng_ctx_new() failed" << std::endl; + return 0; + } + r = spng_set_crc_action(ctx, SPNG_CRC_USE, SPNG_CRC_USE); + if (r){ + std::cerr << "spng_set_crc_action() error: " << spng_strerror(r) << std::endl; + return 0; + } + r = spng_set_png_buffer(ctx, pngbuf, siz_pngbuf); + if (r){ + std::cerr << "spng_set_png_buffer() error: " << spng_strerror(r) << std::endl; + return 0; + } + + spng_ihdr ihdr; + r = spng_get_ihdr(ctx, &ihdr); + if (r){ + std::cerr << "spng_get_ihdr() error: " << spng_strerror(r) << std::endl; + return 0; + } + + char *clr_type_str; + if(ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE) + clr_type_str = "grayscale"; + else if(ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR) + clr_type_str = "truecolor"; + else if(ihdr.color_type == SPNG_COLOR_TYPE_INDEXED) + clr_type_str = "indexed color"; + else if(ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE_ALPHA) + clr_type_str = "grayscale with alpha"; + else + clr_type_str = "truecolor with alpha"; + + size_t out_size; + r = spng_decoded_image_size(ctx, SPNG_FMT_RGBA8, &out_size); + if (r){ + std::cerr << "spng_decoded_image_size() error: " << spng_strerror(r) << std::endl; + return 0; + } + out = new unsigned char[out_size]; + r = spng_decode_image(ctx, out, out_size, SPNG_FMT_RGBA8, 0); + if (r){ + std::cerr << "spng_decode_image() error: " << spng_strerror(r) << std::endl; + return 0; + } + + unsigned char* flipped = new unsigned char[out_size]; + + for (size_t i = 0; i < ihdr.height; i+=1){ + size_t rowsize = ihdr.width*4; + for (size_t j = 0; j < rowsize; j++){ + flipped[(ihdr.height-i-1)*rowsize+j] = out[i*rowsize+j]; + } + } + delete[] out; + + unsigned int texture; + int alpha = GL_RGBA; + + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ihdr.width, ihdr.height, 0, + alpha, GL_UNSIGNED_BYTE, (GLvoid *) flipped); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glBindTexture(GL_TEXTURE_2D, 0); + + pwidth[0] = ihdr.width; + pheight[0] = ihdr.height; + + spng_ctx_free(ctx); + delete[] pngbuf; + + return texture; +} + +#endif Texture* load_texture(std::string filename){ int width, height; diff --git a/src/loaders/png_loading.h b/src/loaders/png_loading.h index a8f502df..e4dc3847 100644 --- a/src/loaders/png_loading.h +++ b/src/loaders/png_loading.h @@ -1,10 +1,3 @@ -/* - * png_loading.h - * - * Created on: Feb 10, 2020 - * Author: MihailRis - */ - #ifndef LOADERS_PNG_LOADING_H_ #define LOADERS_PNG_LOADING_H_ diff --git a/src/physics/Hitbox.cpp b/src/physics/Hitbox.cpp new file mode 100644 index 00000000..ea9d1a8e --- /dev/null +++ b/src/physics/Hitbox.cpp @@ -0,0 +1,4 @@ +#include "Hitbox.h" + +Hitbox::Hitbox(vec3 position, vec3 halfsize) : position(position), halfsize(halfsize), velocity(0.0f,0.0f,0.0f) { +} diff --git a/src/physics/Hitbox.h b/src/physics/Hitbox.h new file mode 100644 index 00000000..2e86ee4b --- /dev/null +++ b/src/physics/Hitbox.h @@ -0,0 +1,20 @@ +#ifndef PHYSICS_HITBOX_H_ +#define PHYSICS_HITBOX_H_ + +#include +#include +#include + +using namespace glm; + +class Hitbox { +public: + vec3 position; + vec3 halfsize; + vec3 velocity; + bool grounded = false; + + Hitbox(vec3 position, vec3 halfsize); +}; + +#endif /* PHYSICS_HITBOX_H_ */ diff --git a/src/physics/PhysicsSolver.cpp b/src/physics/PhysicsSolver.cpp new file mode 100644 index 00000000..240a3aa5 --- /dev/null +++ b/src/physics/PhysicsSolver.cpp @@ -0,0 +1,148 @@ +#include "PhysicsSolver.h" +#include "Hitbox.h" +#include "../voxels/Chunks.h" + +#include + +#define E 0.01 + +PhysicsSolver::PhysicsSolver(vec3 gravity) : gravity(gravity) { +} + +void PhysicsSolver::step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned substeps, bool shifting) { + for (unsigned i = 0; i < substeps; i++){ + float dt = delta / (float)substeps; + vec3& pos = hitbox->position; + vec3& half = hitbox->halfsize; + vec3& vel = hitbox->velocity; + vel.x += gravity.x*dt; + vel.y += gravity.y*dt; + vel.z += gravity.z*dt; + + float px = pos.x; + float pz = pos.z; + + if (vel.x < 0.0){ + for (int y = floor(pos.y-half.y+E); y <= floor(pos.y+half.y-E); y++){ + for (int z = floor(pos.z-half.z+E); z <= floor(pos.z+half.z-E); z++){ + int x = floor(pos.x-half.x-E); + if (chunks->isObstacle(x,y,z)){ + vel.x *= 0.0; + pos.x = x + 1 + half.x + E; + break; + } + } + } + } + if (vel.x > 0.0){ + for (int y = floor(pos.y-half.y+E); y <= floor(pos.y+half.y-E); y++){ + for (int z = floor(pos.z-half.z+E); z <= floor(pos.z+half.z-E); z++){ + int x = floor(pos.x+half.x+E); + if (chunks->isObstacle(x,y,z)){ + vel.x *= 0.0; + pos.x = x - half.x - E; + break; + } + } + } + } + + if (vel.z < 0.0){ + for (int y = floor(pos.y-half.y+E); y <= floor(pos.y+half.y-E); y++){ + for (int x = floor(pos.x-half.x+E); x <= floor(pos.x+half.x-E); x++){ + int z = floor(pos.z-half.z-E); + if (chunks->isObstacle(x,y,z)){ + vel.z *= 0.0; + pos.z = z + 1 + half.z + E; + break; + } + } + } + } + + if (vel.z > 0.0){ + for (int y = floor(pos.y-half.y+E); y <= floor(pos.y+half.y-E); y++){ + for (int x = floor(pos.x-half.x+E); x <= floor(pos.x+half.x-E); x++){ + int z = floor(pos.z+half.z+E); + if (chunks->isObstacle(x,y,z)){ + vel.z *= 0.0; + pos.z = z - half.z - E; + break; + } + } + } + } + + hitbox->grounded = false; + if (vel.y < 0.0){ + for (int x = floor(pos.x-half.x+E); x <= floor(pos.x+half.x-E); x++){ + for (int z = floor(pos.z-half.z+E); z <= floor(pos.z+half.z-E); z++){ + int y = floor(pos.y-half.y-E); + if (chunks->isObstacle(x,y,z)){ + vel.y *= 0.0; + pos.y = y + 1 + half.y; + int f = 18.0; + vel.x *= max(0.0, 1.0 - dt * f); + vel.z *= max(0.0, 1.0 - dt * f); + hitbox->grounded = true; + break; + } + } + } + } + if (vel.y > 0.0){ + for (int x = floor(pos.x-half.x+E); x <= floor(pos.x+half.x-E); x++){ + for (int z = floor(pos.z-half.z+E); z <= floor(pos.z+half.z-E); z++){ + int y = floor(pos.y+half.y+E); + if (chunks->isObstacle(x,y,z)){ + vel.y *= 0.0; + pos.y = y - half.y - E; + break; + } + } + } + } + + pos.x += vel.x * dt; + pos.y += vel.y * dt; + pos.z += vel.z * dt; + + if (shifting && hitbox->grounded){ + int y = floor(pos.y-half.y-E); + + hitbox->grounded = false; + for (int x = floor(px-half.x+E); x <= floor(px+half.x-E); x++){ + for (int z = floor(pos.z-half.z+E); z <= floor(pos.z+half.z-E); z++){ + if (chunks->isObstacle(x,y,z)){ + hitbox->grounded = true; + break; + } + } + } + if (!hitbox->grounded) + pos.z = pz; + hitbox->grounded = false; + + for (int x = floor(pos.x-half.x+E); x <= floor(pos.x+half.x-E); x++){ + for (int z = floor(pz-half.z+E); z <= floor(pz+half.z-E); z++){ + if (chunks->isObstacle(x,y,z)){ + hitbox->grounded = true; + break; + } + } + } + if (!hitbox->grounded) + pos.x = px; + + hitbox->grounded = true; + } + } +} + +bool PhysicsSolver::isBlockInside(int x, int y, int z, Hitbox* hitbox) { + vec3& pos = hitbox->position; + vec3& half = hitbox->halfsize; + return x >= floor(pos.x-half.x) && x <= floor(pos.x+half.x) && + z >= floor(pos.z-half.z) && z <= floor(pos.z+half.z) && + y >= floor(pos.y-half.y) && y <= floor(pos.y+half.y); +} diff --git a/src/physics/PhysicsSolver.h b/src/physics/PhysicsSolver.h new file mode 100644 index 00000000..467d78d7 --- /dev/null +++ b/src/physics/PhysicsSolver.h @@ -0,0 +1,21 @@ +#ifndef PHYSICS_PHYSICSSOLVER_H_ +#define PHYSICS_PHYSICSSOLVER_H_ + +#include +#include +#include + +using namespace glm; + +class Hitbox; +class Chunks; + +class PhysicsSolver { + vec3 gravity; +public: + PhysicsSolver(vec3 gravity); + void step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned substeps, bool shifting); + bool isBlockInside(int x, int y, int z, Hitbox* hitbox); +}; + +#endif /* PHYSICS_PHYSICSSOLVER_H_ */ diff --git a/src/voxel_engine.cpp b/src/voxel_engine.cpp index a3fe8c45..f8e9a4b0 100644 --- a/src/voxel_engine.cpp +++ b/src/voxel_engine.cpp @@ -32,6 +32,8 @@ using namespace glm; #include "lighting/LightSolver.h" #include "lighting/Lightmap.h" #include "lighting/Lighting.h" +#include "physics/Hitbox.h" +#include "physics/PhysicsSolver.h" int WIDTH = 1280; int HEIGHT = 720; @@ -49,90 +51,166 @@ int attrs[] = { 2, 0 //null terminator }; -int main() { - Window::initialize(WIDTH, HEIGHT, "Window 2.0"); - Events::initialize(); +Mesh *crosshair; +Shader *shader, *linesShader, *crosshairShader; +Texture *texture; +LineBatch *lineBatch; - Shader* shader = load_shader("res/main.glslv", "res/main.glslf"); +Chunks* chunks; +WorldFiles* wfile; + +// All in-game definitions (blocks, items, etc..) +void setup_definitions() { + // AIR + Block* block = new Block(0,0); + block->drawGroup = 1; + block->lightPassing = true; + block->obstacle = false; + Block::blocks[block->id] = block; + + // STONE + block = new Block(1,2); + Block::blocks[block->id] = block; + + // GRASS + block = new Block(2,4); + block->textureFaces[2] = 2; + block->textureFaces[3] = 1; + Block::blocks[block->id] = block; + + // LAMP + block = new Block(3,3); + block->emission[0] = 15; + block->emission[1] = 14; + block->emission[2] = 13; + Block::blocks[block->id] = block; + + // GLASS + block = new Block(4,5); + block->drawGroup = 2; + block->lightPassing = true; + Block::blocks[block->id] = block; + + // PLANKS + block = new Block(5,6); + Block::blocks[block->id] = block; +} + +// Shaders, textures, renderers +int initialize_assets() { + shader = load_shader("res/main.glslv", "res/main.glslf"); if (shader == nullptr){ std::cerr << "failed to load shader" << std::endl; Window::terminate(); return 1; } - Shader* crosshairShader = load_shader("res/crosshair.glslv", "res/crosshair.glslf"); + crosshairShader = load_shader("res/crosshair.glslv", "res/crosshair.glslf"); if (crosshairShader == nullptr){ std::cerr << "failed to load crosshair shader" << std::endl; Window::terminate(); return 1; } - Shader* linesShader = load_shader("res/lines.glslv", "res/lines.glslf"); + linesShader = load_shader("res/lines.glslv", "res/lines.glslf"); if (linesShader == nullptr){ std::cerr << "failed to load lines shader" << std::endl; Window::terminate(); return 1; } - Texture* texture = load_texture("res/block.png"); + texture = load_texture("res/block.png"); if (texture == nullptr){ std::cerr << "failed to load texture" << std::endl; delete shader; Window::terminate(); return 1; } + return 0; +} - { - // AIR - Block* block = new Block(0,0); - block->drawGroup = 1; - block->lightPassing = true; - Block::blocks[block->id] = block; +void draw_world(Camera* camera){ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - // STONE - block = new Block(1,2); - Block::blocks[block->id] = block; - - // GRASS - block = new Block(2,4); - block->textureFaces[2] = 2; - block->textureFaces[3] = 1; - Block::blocks[block->id] = block; - - // LAMP - block = new Block(3,3); - block->emission[0] = 10; - block->emission[1] = 0; - block->emission[2] = 0; - Block::blocks[block->id] = block; - - // GLASS - block = new Block(4,5); - block->drawGroup = 2; - block->lightPassing = true; - Block::blocks[block->id] = block; - - // PLANKS - block = new Block(5,6); - Block::blocks[block->id] = block; + // Draw VAO + shader->use(); + shader->uniformMatrix("u_projview", camera->getProjection()*camera->getView()); + shader->uniform1f("u_gamma", 1.6f); + shader->uniform3f("u_skyLightColor", 0.1*2,0.15*2,0.2*2); + texture->bind(); + mat4 model(1.0f); + for (size_t i = 0; i < chunks->volume; i++){ + Chunk* chunk = chunks->chunks[i]; + if (chunk == nullptr) + continue; + Mesh* mesh = chunks->meshes[i]; + if (mesh == nullptr) + continue; + model = glm::translate(mat4(1.0f), vec3(chunk->x*CHUNK_W+0.5f, chunk->y*CHUNK_H+0.5f, chunk->z*CHUNK_D+0.5f)); + shader->uniformMatrix("u_model", model); + mesh->draw(GL_TRIANGLES); } - WorldFiles wfile = WorldFiles("world/", 24*1024*1024); - Chunks* chunks = new Chunks(16*4,1,16*4, 0,0,0); - VoxelRenderer renderer(1024*1024*8); - LineBatch* lineBatch = new LineBatch(4096); + crosshairShader->use(); + crosshair->draw(GL_LINES); + + linesShader->use(); + linesShader->uniformMatrix("u_projview", camera->getProjection()*camera->getView()); + glLineWidth(2.0f); + lineBatch->render(); +} + +// Deleting GL objects like shaders, textures +void finalize_assets(){ + delete shader; + delete texture; + delete crosshair; + delete crosshairShader; + delete linesShader; + delete lineBatch; +} + +// Save all world data to files +void write_world(){ + for (unsigned int i = 0; i < chunks->volume; i++){ + Chunk* chunk = chunks->chunks[i]; + if (chunk == nullptr) + continue; + wfile->put((const char*)chunk->voxels, chunk->x, chunk->z); + } + + wfile->write(); +} + +// Deleting world data from memory +void close_world(){ + delete chunks; + delete wfile; +} + +int main() { + setup_definitions(); + + Window::initialize(WIDTH, HEIGHT, "Window 2.0"); + Events::initialize(); + + int result = initialize_assets(); + if (result){ + Window::terminate(); + return result; + } + + wfile = new WorldFiles("world/", REGION_VOL * (CHUNK_VOL * 2 + 8)); + chunks = new Chunks(32,1,32, 0,0,0); + VoxelRenderer renderer(1024*1024); + lineBatch = new LineBatch(4096); + PhysicsSolver physics(vec3(0,-16.0f,0)); Lighting::initialize(chunks); - glClearColor(0.0f,0.0f,0.0f,1); - - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - Mesh* crosshair = new Mesh(vertices, 4, attrs); + crosshair = new Mesh(vertices, 4, attrs); Camera* camera = new Camera(vec3(32,32,32), radians(90.0f)); + Hitbox* hitbox = new Hitbox(vec3(32,32,32), vec3(0.2f,0.9f,0.2f)); float lastTime = glfwGetTime(); float delta = 0.0f; @@ -140,13 +218,15 @@ int main() { float camX = 0.0f; float camY = 0.0f; - float speed = 15; + float playerSpeed = 4.0f; int choosenBlock = 1; + long frame = 0; - glfwSwapInterval(0); + glfwSwapInterval(1); while (!Window::isShouldClose()){ + frame++; float currentTime = glfwGetTime(); delta = currentTime - lastTime; lastTime = currentTime; @@ -164,22 +244,58 @@ int main() { } } - if (Events::pressed(GLFW_KEY_W)){ - camera->position += camera->front * delta * speed; + // Controls + bool sprint = Events::pressed(GLFW_KEY_LEFT_CONTROL); + bool shift = Events::pressed(GLFW_KEY_LEFT_SHIFT) && hitbox->grounded && !sprint; + + float speed = playerSpeed; + int substeps = (int)(delta * 1000); + substeps = (substeps <= 0 ? 1 : (substeps > 100 ? 100 : substeps)); + physics.step(chunks, hitbox, delta, substeps, shift); + camera->position.x = hitbox->position.x; + camera->position.y = hitbox->position.y + 0.5f; + camera->position.z = hitbox->position.z; + + float dt = min(1.0f, delta * 16); + if (shift){ + speed *= 0.25f; + camera->position.y -= 0.2f; + camera->zoom = 0.9f * dt + camera->zoom * (1.0f - dt); + } else if (sprint){ + speed *= 1.5f; + camera->zoom = 1.1f * dt + camera->zoom * (1.0f - dt); + } else { + camera->zoom = dt + camera->zoom * (1.0f - dt); } - if (Events::pressed(GLFW_KEY_S)){ - camera->position -= camera->front * delta * speed; - } - if (Events::pressed(GLFW_KEY_D)){ - camera->position += camera->right * delta * speed; - } - if (Events::pressed(GLFW_KEY_A)){ - camera->position -= camera->right * delta * speed; + if (Events::pressed(GLFW_KEY_SPACE) && hitbox->grounded){ + hitbox->velocity.y = 6.0f; } - chunks->setCenter(camera->position.x,0,camera->position.z); + vec3 dir(0,0,0); + if (Events::pressed(GLFW_KEY_W)){ + dir.x += camera->dir.x; + dir.z += camera->dir.z; + } + if (Events::pressed(GLFW_KEY_S)){ + dir.x -= camera->dir.x; + dir.z -= camera->dir.z; + } + if (Events::pressed(GLFW_KEY_D)){ + dir.x += camera->right.x; + dir.z += camera->right.z; + } + if (Events::pressed(GLFW_KEY_A)){ + dir.x -= camera->right.x; + dir.z -= camera->right.z; + } + if (length(dir) > 0.0f) + dir = normalize(dir); + hitbox->velocity.x = dir.x * speed; + hitbox->velocity.z = dir.z * speed; + + chunks->setCenter(wfile, camera->position.x,0,camera->position.z); chunks->_buildMeshes(&renderer); - chunks->loadVisible(&wfile); + chunks->loadVisible(wfile); if (Events::_cursor_locked){ camY += -Events::deltaY / Window::height * 2; @@ -215,62 +331,24 @@ int main() { int x = (int)(iend.x)+(int)(norm.x); int y = (int)(iend.y)+(int)(norm.y); int z = (int)(iend.z)+(int)(norm.z); - chunks->set(x, y, z, choosenBlock); - Lighting::onBlockSet(x,y,z, choosenBlock); + if (!physics.isBlockInside(x,y,z, hitbox)){ + chunks->set(x, y, z, choosenBlock); + Lighting::onBlockSet(x,y,z, choosenBlock); + } } } } - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - // Draw VAO - shader->use(); - shader->uniformMatrix("projview", camera->getProjection()*camera->getView()); - texture->bind(); - mat4 model(1.0f); - for (size_t i = 0; i < chunks->volume; i++){ - Chunk* chunk = chunks->chunks[i]; - if (chunk == nullptr) - continue; - Mesh* mesh = chunks->meshes[i]; - if (mesh == nullptr) - continue; - model = glm::translate(mat4(1.0f), vec3(chunk->x*CHUNK_W+0.5f, chunk->y*CHUNK_H+0.5f, chunk->z*CHUNK_D+0.5f)); - shader->uniformMatrix("model", model); - mesh->draw(GL_TRIANGLES); - } - - crosshairShader->use(); - crosshair->draw(GL_LINES); - - linesShader->use(); - linesShader->uniformMatrix("projview", camera->getProjection()*camera->getView()); - glLineWidth(2.0f); - lineBatch->render(); + draw_world(camera); Window::swapBuffers(); Events::pullEvents(); } + write_world(); + close_world(); + Lighting::finalize(); - - for (unsigned int i = 0; i < chunks->volume; i++){ - Chunk* chunk = chunks->chunks[i]; - if (chunk == nullptr) - continue; - wfile.put((const char*)chunk->voxels, chunk->x, chunk->z); - } - - wfile.write(); - - delete shader; - delete texture; - delete chunks; - delete crosshair; - delete crosshairShader; - delete linesShader; - delete lineBatch; - + finalize_assets(); Window::terminate(); return 0; } diff --git a/src/voxels/Block.h b/src/voxels/Block.h index ce4ec452..09d4108f 100644 --- a/src/voxels/Block.h +++ b/src/voxels/Block.h @@ -11,6 +11,7 @@ public: unsigned char emission[3]; unsigned char drawGroup = 0; bool lightPassing = false; + bool obstacle = true; Block(unsigned int id, int texture); }; diff --git a/src/voxels/Chunk.h b/src/voxels/Chunk.h index 8cb3815c..68467e66 100644 --- a/src/voxels/Chunk.h +++ b/src/voxels/Chunk.h @@ -2,7 +2,7 @@ #define VOXELS_CHUNK_H_ #define CHUNK_W 16 -#define CHUNK_H 128 +#define CHUNK_H 64 #define CHUNK_D 16 #define CHUNK_VOL (CHUNK_W * CHUNK_H * CHUNK_D) diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index e0ccbc8d..b6eea117 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -1,6 +1,7 @@ #include "Chunks.h" #include "Chunk.h" #include "voxel.h" +#include "Block.h" #include "WorldGenerator.h" #include "../lighting/Lightmap.h" #include "../files/WorldFiles.h" @@ -129,6 +130,13 @@ voxel* Chunks::get(int x, int y, int z){ return &chunk->voxels[(ly * CHUNK_D + lz) * CHUNK_W + lx]; } +bool Chunks::isObstacle(int x, int y, int z){ + voxel* v = get(x,y,z); + if (v == nullptr) + return true; // void - is obstacle + return Block::blocks[v->id]->obstacle; +} + unsigned char Chunks::getLight(int x, int y, int z, int channel){ x -= ox * CHUNK_W; y -= oy * CHUNK_H; @@ -292,7 +300,7 @@ voxel* Chunks::rayCast(vec3 a, vec3 dir, float maxDist, vec3& end, vec3& norm, v return nullptr; } -void Chunks::setCenter(int x, int y, int z) { +void Chunks::setCenter(WorldFiles* worldFiles, int x, int y, int z) { int cx = x / CHUNK_W; int cy = y / CHUNK_H; int cz = z / CHUNK_D; @@ -306,7 +314,7 @@ void Chunks::setCenter(int x, int y, int z) { cy -= h/2; cz -= d/2; if (cx != 0 || cy != 0 || cz != 0) - translate(cx,cy,cz); + translate(worldFiles, cx,cy,cz); } bool Chunks::loadVisible(WorldFiles* worldFiles){ @@ -349,7 +357,7 @@ bool Chunks::loadVisible(WorldFiles* worldFiles){ return true; } -void Chunks::translate(int dx, int dy, int dz){ +void Chunks::translate(WorldFiles* worldFiles, int dx, int dy, int dz){ for (unsigned int i = 0; i < volume; i++){ chunksSecond[i] = nullptr; meshesSecond[i] = nullptr; @@ -365,6 +373,7 @@ void Chunks::translate(int dx, int dy, int dz){ continue; Mesh* mesh = meshes[(y * d + z) * w + x]; if (nx < 0 || ny < 0 || nz < 0 || nx >= w || ny >= h || nz >= d){ + worldFiles->put((const char*)chunk->voxels, chunk->x, chunk->z); delete chunk; delete mesh; continue; diff --git a/src/voxels/Chunks.h b/src/voxels/Chunks.h index b70e4baf..2c5641b8 100644 --- a/src/voxels/Chunks.h +++ b/src/voxels/Chunks.h @@ -33,8 +33,10 @@ public: void set(int x, int y, int z, int id); voxel* rayCast(vec3 start, vec3 dir, float maxLength, vec3& end, vec3& norm, vec3& iend); - void setCenter(int x, int y, int z); - void translate(int x, int y, int z); + bool isObstacle(int x, int y, int z); + + void setCenter(WorldFiles* worldFiles, int x, int y, int z); + void translate(WorldFiles* worldFiles, int x, int y, int z); bool loadVisible(WorldFiles* worldFiles); bool _buildMeshes(VoxelRenderer* renderer); diff --git a/src/window/Camera.cpp b/src/window/Camera.cpp index 05bc0361..4dfcc2b7 100644 --- a/src/window/Camera.cpp +++ b/src/window/Camera.cpp @@ -10,7 +10,7 @@ #include -Camera::Camera(vec3 position, float fov) : position(position), fov(fov), rotation(1.0f) { +Camera::Camera(vec3 position, float fov) : position(position), fov(fov), zoom(1.0f), rotation(1.0f) { updateVectors(); } @@ -18,6 +18,13 @@ void Camera::updateVectors(){ front = vec3(rotation * vec4(0,0,-1,1)); right = vec3(rotation * vec4(1,0,0,1)); up = vec3(rotation * vec4(0,1,0,1)); + dir = vec3(rotation * vec4(0,0,-1,1)); + dir.y = 0; + float len = length(dir); + if (len > 0.0f){ + dir.x /= len; + dir.z /= len; + } } void Camera::rotate(float x, float y, float z){ @@ -30,7 +37,7 @@ void Camera::rotate(float x, float y, float z){ mat4 Camera::getProjection(){ float aspect = (float)Window::width / (float)Window::height; - return glm::perspective(fov, aspect, 0.1f, 1500.0f); + return glm::perspective(fov*zoom, aspect, 0.05f, 1500.0f); } mat4 Camera::getView(){ diff --git a/src/window/Camera.h b/src/window/Camera.h index c42a0a93..91648adb 100644 --- a/src/window/Camera.h +++ b/src/window/Camera.h @@ -17,9 +17,11 @@ public: vec3 front; vec3 up; vec3 right; + vec3 dir; vec3 position; float fov; + float zoom; mat4 rotation; Camera(vec3 position, float fov); diff --git a/src/window/Window.cpp b/src/window/Window.cpp index c3cf644f..ef6d48d8 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -30,6 +30,12 @@ int Window::initialize(int width, int height, const char* title){ } glViewport(0,0, width, height); + glClearColor(0.0f,0.0f,0.0f,1); + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + Window::width = width; Window::height = height; return 0;