diff --git a/res/content/base/blocks/pane.json b/res/content/base/blocks/pane.json index 28d684b4..d2379085 100644 --- a/res/content/base/blocks/pane.json +++ b/res/content/base/blocks/pane.json @@ -8,7 +8,8 @@ "pane" ], "model": "aabb", - "hitbox": [0.0, 0.0, 0.0, 1.0, 1.0, 0.1], + "hitbox": [0.0, 0.0, 0.0, 1.0, 1.0, 0.2], "light-passing": true, - "sky-light-passing": true -} \ No newline at end of file + "sky-light-passing": true, + "rotation": "pipe" +} diff --git a/src/assets/AssetsLoader.h b/src/assets/AssetsLoader.h index cac41923..43e3bdca 100644 --- a/src/assets/AssetsLoader.h +++ b/src/assets/AssetsLoader.h @@ -7,10 +7,10 @@ #include #include -#define ASSET_TEXTURE 1 -#define ASSET_SHADER 2 -#define ASSET_FONT 3 -#define ASSET_ATLAS 4 +const short ASSET_TEXTURE = 1; +const short ASSET_SHADER = 2; +const short ASSET_FONT = 3; +const short ASSET_ATLAS = 4; class Assets; diff --git a/src/audio/Audio.cpp b/src/audio/Audio.cpp index 5562d2e9..0b1e6a7c 100644 --- a/src/audio/Audio.cpp +++ b/src/audio/Audio.cpp @@ -17,12 +17,12 @@ std::vector Audio::freebuffers; bool ALSource::setBuffer(ALBuffer* buffer) { alSourcei(id, AL_BUFFER, buffer->id); - return alCheck(); + return alCheckErrorsMacro(); } bool ALSource::play(){ alSourcePlay(id); - return alCheck(); + return alCheckErrorsMacro(); } bool ALSource::isPlaying() { @@ -33,33 +33,33 @@ bool ALSource::isPlaying() { bool ALSource::setPosition(glm::vec3 position) { alSource3f(id, AL_POSITION, position.x, position.y, position.z); - return alCheck(); + return alCheckErrorsMacro(); } bool ALSource::setVelocity(glm::vec3 velocity) { alSource3f(id, AL_VELOCITY, velocity.x, velocity.y, velocity.z); - return alCheck(); + return alCheckErrorsMacro(); } bool ALSource::setLoop(bool loop) { alSourcei(id, AL_LOOPING, AL_TRUE ? loop : AL_FALSE); - return alCheck(); + return alCheckErrorsMacro(); } bool ALSource::setGain(float gain) { alSourcef(id, AL_GAIN, gain); - return alCheck(); + return alCheckErrorsMacro(); } bool ALSource::setPitch(float pitch) { alSourcef(id, AL_PITCH, pitch); - return alCheck(); + return alCheckErrorsMacro(); } bool ALBuffer::load(int format, const char* data, int size, int freq) { alBufferData(id, format, data, size, freq); - return alCheck(); + return alCheckErrorsMacro(); } @@ -72,7 +72,7 @@ bool Audio::initialize() { alcCloseDevice(device); return false; } - if (!alCheck()) + if (!alCheckErrorsMacro()) return false; ALCint size; @@ -91,13 +91,13 @@ bool Audio::initialize() { void Audio::finalize(){ for (ALSource* source : allsources){ if (source->isPlaying()){ - alSourceStop(source->id); alCheck(); + alSourceStop(source->id); alCheckErrorsMacro(); } - alDeleteSources(1, &source->id); alCheck(); + alDeleteSources(1, &source->id); alCheckErrorsMacro(); } for (ALBuffer* buffer : allbuffers){ - alDeleteBuffers(1, &buffer->id); alCheck(); + alDeleteBuffers(1, &buffer->id); alCheckErrorsMacro(); } alcMakeContextCurrent(context); @@ -121,7 +121,7 @@ ALSource* Audio::getFreeSource(){ } ALuint id; alGenSources(1, &id); - if (!alCheck()) + if (!alCheckErrorsMacro()) return nullptr; ALSource* source = new ALSource(id); @@ -141,7 +141,7 @@ ALBuffer* Audio::getFreeBuffer(){ } ALuint id; alGenBuffers(1, &id); - if (!alCheck()) + if (!alCheckErrorsMacro()) return nullptr; ALBuffer* buffer = new ALBuffer(id); @@ -160,7 +160,7 @@ void Audio::freeBuffer(ALBuffer* buffer){ bool Audio::get_available_devices(std::vector& devicesVec){ const ALCchar* devices; devices = alcGetString(device, ALC_DEVICE_SPECIFIER); - if (!alCheck()) + if (!alCheckErrorsMacro()) return false; const char* ptr = devices; @@ -180,9 +180,9 @@ void Audio::setListener(glm::vec3 position, glm::vec3 velocity, glm::vec3 at, gl ALfloat listenerOri[] = { at.x, at.y, at.z, up.x, up.y, up.z }; alListener3f(AL_POSITION, position.x, position.y, position.z); - alCheck(); + alCheckErrorsMacro(); alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.z); - alCheck(); + alCheckErrorsMacro(); alListenerfv(AL_ORIENTATION, listenerOri); - alCheck(); + alCheckErrorsMacro(); } diff --git a/src/audio/audioutil.h b/src/audio/audioutil.h index f48c261a..2044aaed 100644 --- a/src/audio/audioutil.h +++ b/src/audio/audioutil.h @@ -7,7 +7,7 @@ #include -#define alCheck() check_al_errors(__FILE__, __LINE__) +#define alCheckErrorsMacro() check_al_errors(__FILE__, __LINE__) bool check_al_errors(const std::string& filename, const std::uint_fast32_t line); diff --git a/src/constants.h b/src/constants.h index 49a97749..4162d5fc 100644 --- a/src/constants.h +++ b/src/constants.h @@ -1,30 +1,28 @@ #ifndef SRC_CONSTANTS_H_ #define SRC_CONSTANTS_H_ -#include +#include #include "typedefs.h" -#define ENGINE_VERSION_MAJOR 0 -#define ENGINE_VERSION_MINOR 15 -#define STR_(x) #x -#define STR(x) STR_(x) -#define ENGINE_VERSION STR(ENGINE_VERSION_MAJOR) "." STR(ENGINE_VERSION_MINOR) +const int ENGINE_VERSION_MAJOR = 0; +const int ENGINE_VERSION_MINOR = 15; -#define CHUNK_W 16 -#define CHUNK_H 256 -#define CHUNK_D 16 +const int CHUNK_W = 16; +const int CHUNK_H = 256; +const int CHUNK_D = 16; /* Chunk volume (count of voxels per Chunk) */ -#define CHUNK_VOL (CHUNK_W * CHUNK_H * CHUNK_D) +const int CHUNK_VOL = (CHUNK_W * CHUNK_H * CHUNK_D); /* BLOCK_VOID is block id used to mark non-existing voxel (voxel of missing chunk) */ -#define BLOCK_VOID (blockid_t)((2 << (sizeof(blockid_t)*CHAR_BIT)) - 1) +const blockid_t BLOCK_VOID = std::numeric_limits::max(); inline uint vox_index(int x, int y, int z, int w=CHUNK_W, int d=CHUNK_D) { return (y * d + z) * w + x; } -#define SHADERS_FOLDER "shaders" +//cannot replace defines with const while used for substitution +#define SHADERS_FOLDER "shaders" #define TEXTURES_FOLDER "textures" #define FONTS_FOLDER "fonts" diff --git a/src/core_defs.h b/src/core_defs.h index ffd26795..f0c46afe 100644 --- a/src/core_defs.h +++ b/src/core_defs.h @@ -3,22 +3,22 @@ /* blocks and bindings used in engine code */ -#define BLOCK_AIR 0 +const int BLOCK_AIR = 0; -#define BIND_MOVE_FORWARD "movement.forward" -#define BIND_MOVE_BACK "movement.back" -#define BIND_MOVE_LEFT "movement.left" -#define BIND_MOVE_RIGHT "movement.right" -#define BIND_MOVE_JUMP "movement.jump" -#define BIND_MOVE_SPRINT "movement.sprint" -#define BIND_MOVE_CROUCH "movement.crouch" -#define BIND_MOVE_CHEAT "movement.cheat" -#define BIND_CAM_ZOOM "camera.zoom" -#define BIND_PLAYER_NOCLIP "player.noclip" -#define BIND_PLAYER_FLIGHT "player.flight" -#define BIND_PLAYER_ATTACK "player.attack" -#define BIND_PLAYER_BUILD "player.build" -#define BIND_PLAYER_PICK "player.pick" -#define BIND_HUD_INVENTORY "hud.inventory" +const std::string BIND_MOVE_FORWARD = "movement.forward"; +const std::string BIND_MOVE_BACK = "movement.back"; +const std::string BIND_MOVE_LEFT = "movement.left"; +const std::string BIND_MOVE_RIGHT = "movement.right"; +const std::string BIND_MOVE_JUMP = "movement.jump"; +const std::string BIND_MOVE_SPRINT = "movement.sprint"; +const std::string BIND_MOVE_CROUCH = "movement.crouch"; +const std::string BIND_MOVE_CHEAT = "movement.cheat"; +const std::string BIND_CAM_ZOOM = "camera.zoom"; +const std::string BIND_PLAYER_NOCLIP = "player.noclip"; +const std::string BIND_PLAYER_FLIGHT = "player.flight"; +const std::string BIND_PLAYER_ATTACK = "player.attack"; +const std::string BIND_PLAYER_BUILD = "player.build"; +const std::string BIND_PLAYER_PICK = "player.pick"; +const std::string BIND_HUD_INVENTORY = "hud.inventory"; #endif // SRC_CORE_DEFS_H_ \ No newline at end of file diff --git a/src/engine.cpp b/src/engine.cpp index 844a0ba5..a974a83d 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -98,7 +98,7 @@ void Engine::mainloop() { Window::swapInterval(settings.display.swapInterval); Window::swapBuffers(); - Events::pullEvents(); + Events::pollEvents(); } } diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index f4b76816..9067fbaa 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -25,14 +25,14 @@ #include #include -#define SECTION_POSITION 1 -#define SECTION_ROTATION 2 -#define SECTION_FLAGS 3 -#define PLAYER_FLAG_FLIGHT 0x1 -#define PLAYER_FLAG_NOCLIP 0x2 +const int SECTION_POSITION = 1; +const int SECTION_ROTATION = 2; +const int SECTION_FLAGS = 3; +const int PLAYER_FLAG_FLIGHT = 0x1; +const int PLAYER_FLAG_NOCLIP = 0x2; -#define WORLD_SECTION_MAIN 1 -#define WORLD_SECTION_DAYNIGHT 2 +const int WORLD_SECTION_MAIN = 1; +const int WORLD_SECTION_DAYNIGHT = 2; using glm::ivec2; using glm::vec3; diff --git a/src/files/WorldFiles.h b/src/files/WorldFiles.h index 82628b39..4433fa45 100644 --- a/src/files/WorldFiles.h +++ b/src/files/WorldFiles.h @@ -13,13 +13,13 @@ #include "../typedefs.h" -#define REGION_SIZE_BIT 5 -#define REGION_SIZE (1 << (REGION_SIZE_BIT)) -#define REGION_VOL ((REGION_SIZE) * (REGION_SIZE)) -#define REGION_FORMAT_VERSION 1 +const uint REGION_SIZE_BIT = 5; +const uint REGION_SIZE = (1 << (REGION_SIZE_BIT)); +const uint REGION_VOL = ((REGION_SIZE) * (REGION_SIZE)); +const uint REGION_FORMAT_VERSION = 1; +const uint WORLD_FORMAT_VERSION = 1; #define REGION_FORMAT_MAGIC ".VOXREG" #define WORLD_FORMAT_MAGIC ".VOXWLD" -#define WORLD_FORMAT_VERSION 1 class Player; class Chunk; diff --git a/src/frontend/WorldRenderer.cpp b/src/frontend/WorldRenderer.cpp index 2afa059b..c562f8a4 100644 --- a/src/frontend/WorldRenderer.cpp +++ b/src/frontend/WorldRenderer.cpp @@ -193,7 +193,12 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera){ const vec3 pos = PlayerController::selectedBlockPosition; const vec3 point = PlayerController::selectedPointPosition; const vec3 norm = PlayerController::selectedBlockNormal; - const AABB& hitbox = block->hitbox; + AABB hitbox = block->hitbox; + if (block->rotatable) { + auto states = PlayerController::selectedBlockStates; + block->rotations.variants[states].transform(hitbox); + } + const vec3 center = pos + hitbox.center(); const vec3 size = hitbox.size(); linesShader->use(); diff --git a/src/frontend/gui/controls.cpp b/src/frontend/gui/controls.cpp index 35f9cc57..818aaaf7 100644 --- a/src/frontend/gui/controls.cpp +++ b/src/frontend/gui/controls.cpp @@ -14,9 +14,9 @@ using glm::vec2; using glm::vec3; using glm::vec4; -#define KEY_ESCAPE 256 -#define KEY_ENTER 257 -#define KEY_BACKSPACE 259 +const uint KEY_ESCAPE = 256; +const uint KEY_ENTER = 257; +const uint KEY_BACKSPACE = 259; using namespace gui; diff --git a/src/graphics/Batch2D.cpp b/src/graphics/Batch2D.cpp index a44dc9b6..dce99572 100644 --- a/src/graphics/Batch2D.cpp +++ b/src/graphics/Batch2D.cpp @@ -5,7 +5,7 @@ #include -#define VERTEX_SIZE 8 +const uint B2D_VERTEX_SIZE = 8; using glm::vec2; using glm::vec3; @@ -16,7 +16,7 @@ Batch2D::Batch2D(size_t capacity) : capacity(capacity), offset(0), color(1.0f, 1 {2}, {2}, {4}, {0} }; - buffer = new float[capacity * VERTEX_SIZE]; + buffer = new float[capacity * B2D_VERTEX_SIZE]; mesh = new Mesh(buffer, 0, attrs); index = 0; @@ -75,7 +75,7 @@ void Batch2D::texture(Texture* new_texture){ } void Batch2D::point(float x, float y, float r, float g, float b, float a){ - if (index + 6*VERTEX_SIZE >= capacity) + if (index + 6*B2D_VERTEX_SIZE >= capacity) render(GL_TRIANGLES); vertex(x, y, 0, 0, r,g,b,a); @@ -83,7 +83,7 @@ void Batch2D::point(float x, float y, float r, float g, float b, float a){ } void Batch2D::line(float x1, float y1, float x2, float y2, float r, float g, float b, float a){ - if (index + 6*VERTEX_SIZE >= capacity) + if (index + 6*B2D_VERTEX_SIZE >= capacity) render(GL_TRIANGLES); vertex(x1, y1, 0, 0, r,g,b,a); @@ -96,7 +96,7 @@ void Batch2D::rect(float x, float y, float w, float h){ const float g = color.g; const float b = color.b; const float a = color.a; - if (index + 6*VERTEX_SIZE >= capacity) + if (index + 6*B2D_VERTEX_SIZE >= capacity) render(GL_TRIANGLES); vertex(x, y, 0, 0, r,g,b,a); @@ -117,7 +117,7 @@ void Batch2D::rect( bool flippedX, bool flippedY, vec4 tint) { - if (index + 6*VERTEX_SIZE >= capacity) + if (index + 6*B2D_VERTEX_SIZE >= capacity) render(GL_TRIANGLES); float centerX = w*ox; @@ -205,7 +205,7 @@ void Batch2D::rect( void Batch2D::rect(float x, float y, float w, float h, float u, float v, float tx, float ty, float r, float g, float b, float a){ - if (index + 6*VERTEX_SIZE >= capacity) + if (index + 6*B2D_VERTEX_SIZE >= capacity) render(GL_TRIANGLES); vertex(x, y, u, v+ty, r,g,b,a); vertex(x+w, y+h, u+tx, v, r,g,b,a); @@ -222,7 +222,7 @@ void Batch2D::rect(float x, float y, float w, float h, float r2, float g2, float b2, float r3, float g3, float b3, float r4, float g4, float b4, int sh){ - if (index + 30*VERTEX_SIZE >= capacity) + if (index + 30*B2D_VERTEX_SIZE >= capacity) render(GL_TRIANGLES); vec2 v0 = vec2(x+h/2,y+h/2); vec2 v1 = vec2(x+w-sh,y); @@ -317,7 +317,7 @@ void Batch2D::blockSprite(float x, float y, float w, float h, const UVRegion reg float scalex = regions[3].u2-regions[3].u1; float scaley = regions[3].v2-regions[3].v1; - if (this->index + 18*VERTEX_SIZE >= capacity) + if (this->index + 18*B2D_VERTEX_SIZE >= capacity) render(); float d = (w + h) * 0.5f; @@ -376,7 +376,7 @@ void Batch2D::blockSprite(float x, float y, float w, float h, const UVRegion reg } void Batch2D::render(unsigned int gl_primitive) { - mesh->reload(buffer, index / VERTEX_SIZE); + mesh->reload(buffer, index / B2D_VERTEX_SIZE); mesh->draw(gl_primitive); index = 0; } diff --git a/src/graphics/Batch3D.cpp b/src/graphics/Batch3D.cpp index 1c4ab995..1fda4c65 100644 --- a/src/graphics/Batch3D.cpp +++ b/src/graphics/Batch3D.cpp @@ -6,7 +6,7 @@ #include #include "../typedefs.h" -#define VERTEX_SIZE 9 +const uint B3D_VERTEX_SIZE = 9; using glm::vec2; using glm::vec3; @@ -19,7 +19,7 @@ Batch3D::Batch3D(size_t capacity) {3}, {2}, {4}, {0} }; - buffer = new float[capacity * VERTEX_SIZE]; + buffer = new float[capacity * B3D_VERTEX_SIZE]; mesh = new Mesh(buffer, 0, attrs); index = 0; @@ -84,7 +84,7 @@ void Batch3D::face(const vec3& coord, float w, float h, const vec3& axisY, const UVRegion& region, const vec4& tint) { - if (index + VERTEX_SIZE * 6 > capacity) { + if (index + B3D_VERTEX_SIZE * 6 > capacity) { flush(); } vertex(coord, region.u1, region.v1, @@ -118,7 +118,7 @@ void Batch3D::sprite(vec3 pos, vec3 up, vec3 right, float w, float h, const UVRe const float g = color.g; const float b = color.b; const float a = color.a; - if (index + 6*VERTEX_SIZE >= capacity) { + if (index + 6*B3D_VERTEX_SIZE >= capacity) { flush(); } @@ -179,7 +179,7 @@ void Batch3D::blockCube(const vec3 size, const UVRegion(&texfaces)[6], const vec } void Batch3D::flush() { - mesh->reload(buffer, index / VERTEX_SIZE); + mesh->reload(buffer, index / B3D_VERTEX_SIZE); mesh->draw(); index = 0; } diff --git a/src/graphics/BlocksRenderer.cpp b/src/graphics/BlocksRenderer.cpp index bfd52bb6..f9a9f573 100644 --- a/src/graphics/BlocksRenderer.cpp +++ b/src/graphics/BlocksRenderer.cpp @@ -17,7 +17,7 @@ using glm::ivec3; using glm::vec3; using glm::vec4; -#define VERTEX_SIZE 6 +const uint BlocksRenderer::VERTEX_SIZE = 6; BlocksRenderer::BlocksRenderer(size_t capacity, const Content* content, @@ -42,6 +42,7 @@ BlocksRenderer::~BlocksRenderer() { delete[] indexBuffer; } +/* Basic vertex add method */ void BlocksRenderer::vertex(const vec3& coord, float u, float v, const vec4& light) { @@ -75,13 +76,15 @@ void BlocksRenderer::index(int a, int b, int c, int d, int e, int f) { indexOffset += 4; } -void BlocksRenderer::face(const vec3& coord, float w, float h, - const vec3& axisX, - const vec3& axisY, - const UVRegion& region, - const vec4(&lights)[4], - const vec4& tint) { - if (vertexOffset + VERTEX_SIZE * 4 > capacity) { +/* Add face with precalculated lights */ +void BlocksRenderer::face(const vec3& coord, + float w, float h, + const vec3& axisX, + const vec3& axisY, + const UVRegion& region, + const vec4(&lights)[4], + const vec4& tint) { + if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) { overflow = true; return; } @@ -92,34 +95,8 @@ void BlocksRenderer::face(const vec3& coord, float w, float h, index(0, 1, 3, 1, 2, 3); } -void BlocksRenderer::face(const vec3& coord, float w, float h, - const vec3& axisX, - const vec3& axisY, - const UVRegion& region, - const vec4(&lights)[4], - const vec4& tint, - bool rotated) { - if (vertexOffset + VERTEX_SIZE * 4 > capacity) { - overflow = true; - return; - } - if (rotated) { - vertex(coord, region.u2, region.v1, lights[0] * tint); - vertex(coord + axisX * w, region.u2, region.v2, lights[1] * tint); - vertex(coord + axisX * w + axisY * h, region.u1, region.v2, lights[2] * tint); - vertex(coord + axisY * h, region.u1, region.v1, lights[3] * tint); - index(0, 1, 2, 0, 2, 3); - } - else { - vertex(coord, region.u1, region.v1, lights[0] * tint); - vertex(coord + axisX * w, region.u2, region.v1, lights[1] * tint); - vertex(coord + axisX * w + axisY * h, region.u2, region.v2, lights[2] * tint); - vertex(coord + axisY * h, region.u1, region.v2, lights[3] * tint); - index(0, 1, 2, 0, 2, 3); - } -} - -void BlocksRenderer::vertex(const ivec3& coord, float u, float v, +void BlocksRenderer::vertex(const ivec3& coord, + float u, float v, const vec4& tint, const ivec3& axisX, const ivec3& axisY, @@ -128,13 +105,23 @@ void BlocksRenderer::vertex(const ivec3& coord, float u, float v, vertex(coord, u, v, light * tint); } +void BlocksRenderer::vertex(const vec3& coord, + float u, float v, + const vec4& tint, + const ivec3& axisX, + const ivec3& axisY, + const ivec3& axisZ) { + vec4 light = pickSoftLight(ivec3(coord.x, coord.y, coord.z)+axisZ, axisX, axisY); + vertex(coord, u, v, light * tint); +} + void BlocksRenderer::face(const ivec3& coord, const ivec3& axisX, const ivec3& axisY, const ivec3& axisZ, const ivec3& laxisZ, const UVRegion& region) { - if (vertexOffset + VERTEX_SIZE * 4 > capacity) { + if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) { overflow = true; return; } @@ -152,45 +139,69 @@ void BlocksRenderer::face(const ivec3& coord, index(0, 1, 2, 0, 2, 3); } -void BlocksRenderer::cube(const vec3& coord, const vec3& size, const UVRegion(&texfaces)[6]) { - vec4 lights[]{ vec4(),vec4(),vec4(),vec4() }; +void BlocksRenderer::face(const ivec3& coord_, + const ivec3& axisX, + const ivec3& axisY, + const ivec3& axisZ, + const ivec3& laxisZ, + const vec3& offset, + float width, + float height, + float depth, + const UVRegion& region) { + if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) { + overflow = true; + return; + } - face(coord, size.x, size.y, vec3(1, 0, 0), vec3(0, 1, 0), texfaces[0], lights); - face(coord + vec3(size.x, 0, -size.z), size.x, size.y, vec3(-1, 0, 0), vec3(0, 1, 0), texfaces[1], lights); + const vec3 X(axisX); + const vec3 Y(axisY); + const vec3 Z(axisZ); - face(coord + vec3(0, size.y, 0), size.x, size.z, vec3(1, 0, 0), vec3(0, 0, -1), texfaces[2], lights); - face(coord + vec3(0, 0, -size.z), size.x, size.z, vec3(1, 0, 0), vec3(0, 0, 1), texfaces[3], lights); + const vec3 sunVector = vec3(0.431934f, 0.863868f, 0.259161f); + float d = glm::dot(Z, sunVector); + d = 0.75f + d*0.25f; - face(coord + vec3(0, 0, -size.z), size.z, size.y, vec3(0, 0, 1), vec3(0, 1, 0), texfaces[4], lights); - face(coord + vec3(size.x, 0, 0), size.z, size.y, vec3(0, 0, -1), vec3(0, 1, 0), texfaces[5], lights); + vec4 tint(d); + vec3 coord(vec3(coord_) + offset); + + vertex(coord + Z*depth, region.u1, region.v1, tint, axisX, axisY, laxisZ); + vertex(coord + Z*depth + X*width, region.u2, region.v1, tint, axisX, axisY, laxisZ); + vertex(coord + Z*depth + X*width + Y*height, region.u2, region.v2, tint, axisX, axisY, laxisZ); + vertex(coord + Z*depth + Y*height, region.u1, region.v2, tint, axisX, axisY, laxisZ); + index(0, 1, 2, 0, 2, 3); } -void BlocksRenderer::blockCube(int x, int y, int z, const vec3& size, const UVRegion(&texfaces)[6], ubyte group) { +void BlocksRenderer::blockCube(int x, int y, int z, const UVRegion(&texfaces)[6], ubyte group) { vec4 lights[]{ vec4(1.0f), vec4(1.0f), vec4(1.0f), vec4(1.0f) }; if (isOpen(x, y, z + 1, group)) { - face(vec3(x, y, z), size.x, size.y, vec3(1, 0, 0), vec3(0, 1, 0), texfaces[5], lights, vec4(1.0f)); + face(vec3(x, y, z), 1, 1, vec3(1, 0, 0), vec3(0, 1, 0), texfaces[5], lights, vec4(1.0f)); } if (isOpen(x, y, z - 1, group)) { - face(vec3(x + size.x, y, z - size.z), size.x, size.y, vec3(-1, 0, 0), vec3(0, 1, 0), texfaces[4], lights, vec4(1.0f)); + face(vec3(x + 1, y, z - 1), 1, 1, vec3(-1, 0, 0), vec3(0, 1, 0), texfaces[4], lights, vec4(1.0f)); } if (isOpen(x, y + 1, z, group)) { - face(vec3(x, y + size.y, z), size.x, size.z, vec3(1, 0, 0), vec3(0, 0, -1), texfaces[3], lights); + face(vec3(x, y + 1, z), 1, 1, vec3(1, 0, 0), vec3(0, 0, -1), texfaces[3], lights); } if (isOpen(x, y - 1, z, group)) { - face(vec3(x, y, z - size.z), size.x, size.z, vec3(1, 0, 0), vec3(0, 0, 1), texfaces[2], lights, vec4(1.0f)); + face(vec3(x, y, z - 1), 1, 1, vec3(1, 0, 0), vec3(0, 0, 1), texfaces[2], lights, vec4(1.0f)); } if (isOpen(x - 1, y, z, group)) { - face(vec3(x, y, z - size.z), size.z, size.y, vec3(0, 0, 1), vec3(0, 1, 0), texfaces[0], lights, vec4(1.0f)); + face(vec3(x, y, z - 1), 1, 1, vec3(0, 0, 1), vec3(0, 1, 0), texfaces[0], lights, vec4(1.0f)); } if (isOpen(x + 1, y, z, group)) { - face(vec3(x + size.x, y, z), size.z, size.y, vec3(0, 0, -1), vec3(0, 1, 0), texfaces[1], lights, vec4(1.0f)); + face(vec3(x + 1, y, z), 1, 1, vec3(0, 0, -1), vec3(0, 1, 0), texfaces[1], lights, vec4(1.0f)); } } -void BlocksRenderer::blockXSprite(int x, int y, int z, const vec3& size, const UVRegion& texface1, const UVRegion& texface2, float spread) { +void BlocksRenderer::blockXSprite(int x, int y, int z, + const vec3& size, + const UVRegion& texface1, + const UVRegion& texface2, + float spread) { vec4 lights[]{ pickSoftLight({x, y + 1, z}, {1, 0, 0}, {0, 1, 0}), pickSoftLight({x + 1, y + 1, z}, {1, 0, 0}, {0, 1, 0}), @@ -204,73 +215,64 @@ void BlocksRenderer::blockXSprite(int x, int y, int z, const vec3& size, const U const float w = size.x / 1.41f; const float tint = 0.8f; - face(vec3(x + xs + (1.0 - w) * 0.5f, y, - z + zs - 1 + (1.0 - w) * 0.5f), w, size.y, - vec3(1.0f, 0, 1.0f), vec3(0, 1, 0), texface1, lights, vec4(tint)); - face(vec3(x + xs - (1.0 - w) * 0.5f + 1, y, - z + zs - (1.0 - w) * 0.5f), w, size.y, - vec3(-1.0f, 0, -1.0f), vec3(0, 1, 0), texface1, lights, vec4(tint)); + face(vec3(x + xs + (1.0 - w) * 0.5f, y, z + zs - 1 + (1.0 - w) * 0.5f), + w, size.y, vec3(1.0f, 0, 1.0f), vec3(0, 1, 0), + texface1, lights, vec4(tint)); + face(vec3(x + xs - (1.0 - w) * 0.5f + 1, y, z + zs - (1.0 - w) * 0.5f), + w, size.y, vec3(-1.0f, 0, -1.0f), vec3(0, 1, 0), + texface1, lights, vec4(tint)); - face(vec3(x + xs + (1.0 - w) * 0.5f, y, - z + zs - (1.0 - w) * 0.5f), w, size.y, - vec3(1.0f, 0, -1.0f), vec3(0, 1, 0), texface2, lights, vec4(tint)); - face(vec3(x + xs - (1.0 - w) * 0.5f + 1, y, - z + zs + (1.0 - w) * 0.5f - 1), w, size.y, - vec3(-1.0f, 0, 1.0f), vec3(0, 1, 0), texface2, lights, vec4(tint)); + face(vec3(x + xs + (1.0 - w) * 0.5f, y, z + zs - (1.0 - w) * 0.5f), + w, size.y, vec3(1.0f, 0, -1.0f), vec3(0, 1, 0), + texface2, lights, vec4(tint)); + face(vec3(x + xs - (1.0 - w) * 0.5f + 1, y, z + zs + (1.0 - w) * 0.5f - 1), + w, size.y, vec3(-1.0f, 0, 1.0f), vec3(0, 1, 0), + texface2, lights, vec4(tint)); } -void BlocksRenderer::blockCubeShaded(const vec3& pos, const vec3& size, const UVRegion(&texfaces)[6], const Block* block, ubyte states) { - int rot = 0; - float x = pos.x; - float y = pos.y; - float z = pos.z; - { - vec4 lights[]{ - pickSoftLight(x, y, z + 1, {1, 0, 0}, {0, 1, 0}), - pickSoftLight(x + 1, y, z + 1, {1, 0, 0}, {0, 1, 0}), - pickSoftLight(x + 1, y + 1, z + 1, {1, 0, 0}, {0, 1, 0}), - pickSoftLight(x, y + 1, z + 1, {1, 0, 0}, {0, 1, 0}) }; - face(vec3(x, y, z), size.x, size.y, vec3(1, 0, 0), vec3(0, 1, 0), texfaces[5], lights, vec4(0.9f), rot == 1); - } { - vec4 lights[]{ - pickSoftLight(pos.x, pos.y, pos.z - 1, {-1, 0, 0}, {0, 1, 0}), - pickSoftLight(pos.x - 1, pos.y, pos.z - 1, {-1, 0, 0}, {0, 1, 0}), - pickSoftLight(pos.x - 1, pos.y + 1, pos.z - 1, {-1, 0, 0}, {0, 1, 0}), - pickSoftLight(pos.x, pos.y + 1, pos.z - 1, {-1, 0, 0}, {0, 1, 0}) }; - face(vec3(x + size.x, y, z - size.z), size.x, size.y, vec3(-1, 0, 0), vec3(0, 1, 0), texfaces[4], lights, vec4(0.75f), rot == 1); - } { - vec4 lights[]{ - pickSoftLight(x, pos.y + 1, pos.z + 1, {1, 0, 0}, {0, 0, 1}), - pickSoftLight(x + 1, pos.y + 1, pos.z + 1, {1, 0, 0}, {0, 0, 1}), - pickSoftLight(x + 1, pos.y + 1, pos.z, {1, 0, 0}, {0, 0, 1}), - pickSoftLight(x, pos.y + 1, pos.z, {1, 0, 0}, {0, 0, 1}) }; +/* AABB blocks render method (WIP) */ +void BlocksRenderer::blockCubeShaded(const ivec3& icoord, + const vec3& offset, + const vec3& size, + const UVRegion(&texfaces)[6], + const Block* block, ubyte states) { - face(vec3(x, y + size.y, z), size.x, size.z, vec3(1, 0, 0), vec3(0, 0, -1), texfaces[3], lights, vec4(1.0f), rot == 1); - } { - vec4 lights[]{ - pickSoftLight(pos.x, pos.y - 1, pos.z - 1, {1, 0, 0}, {0, 0, -1}), - pickSoftLight(pos.x + 1, y - 1, pos.z - 1, {1, 0, 0}, {0, 0,-1}), - pickSoftLight(pos.x + 1, y - 1, pos.z, {1, 0, 0}, {0, 0, -1}), - pickSoftLight(x, y - 1, z, {1, 0, 0}, {0, 0, -1}) }; - face(vec3(x, y, z - size.z), size.x, size.z, vec3(1, 0, 0), vec3(0, 0, 1), texfaces[2], lights, vec4(0.6f), rot == 1); - } { - vec4 lights[]{ - pickSoftLight(x - 1, y, z - 1, {0, 0, -1}, {0, 1, 0}), - pickSoftLight(x - 1, y, z, {0, 0, -1}, {0, 1, 0}), - pickSoftLight(x - 1, y + 1, z, {0, 0, -1}, {0, 1, 0}), - pickSoftLight(x - 1, y + 1, z - 1, {0, 0, -1}, {0, 1, 0}) }; - face(vec3(x, y, z - size.z), size.z, size.y, vec3(0, 0, 1), vec3(0, 1, 0), texfaces[0], lights, vec4(0.7f), rot == 3); - } { - vec4 lights[]{ - pickSoftLight(x + 1, y, z, {0, 0, -1}, {0, 1, 0}), - pickSoftLight(x + 1, y, z - 1, {0, 0, -1}, {0, 1, 0}), - pickSoftLight(x + 1, y + 1, z - 1, {0, 0, -1}, {0, 1, 0}), - pickSoftLight(x + 1, y + 1, z, {0, 0, -1}, {0, 1, 0}) }; - face(vec3(x + size.x, y, z), size.z, size.y, vec3(0, 0, -1), vec3(0, 1, 0), texfaces[1], lights, vec4(0.8f), rot == 3); + ivec3 X(1, 0, 0); + ivec3 Y(0, 1, 0); + ivec3 Z(0, 0, 1); + ivec3 loff(0); + ivec3 coord = icoord; + if (block->rotatable) { + auto& rotations = block->rotations; + auto& orient = rotations.variants[states & BLOCK_ROT_MASK]; + X = orient.axisX; + Y = orient.axisY; + Z = orient.axisZ; + coord += orient.fix; + loff -= orient.fix; } + + vec3 fX(X); + vec3 fY(Y); + vec3 fZ(Z); + + vec3 local = offset.x*vec3(X)+offset.y*vec3(Y)+offset.z*vec3(-Z); + //local -= loff; + face(coord, X, Y, Z, Z+loff, local-size.z*fZ, size.x, size.y, size.z, texfaces[5]); + face(coord+X, -X, Y, -Z, Z-Z-X+loff, local-size.z*fZ, size.x, size.y, 0.0f, texfaces[4]); + + face(coord+Y, X, -Z, Y, Y-Y+loff, local, size.x, size.z, 0.0f, texfaces[3]); //; + face(coord+X, -X, -Z, -Y, -Y+Z+loff, local, size.x, size.z, 0.0f, texfaces[2]); //; + + face(coord+X, -Z, Y, X, X-X+loff, local, size.z, size.y, 0.0f, texfaces[1]); //; + face(coord+Y, -Z, -Y, -X, -X+Z+loff, local, size.z, size.y, 0.0f, texfaces[0]); //; } -void BlocksRenderer::blockCubeShaded(int x, int y, int z, const UVRegion(&texfaces)[6], const Block* block, ubyte states) { +/* Fastest solid shaded blocks render method */ +void BlocksRenderer::blockCubeShaded(int x, int y, int z, + const UVRegion(&texfaces)[6], + const Block* block, + ubyte states) { ubyte group = block->drawGroup; ivec3 X(1, 0, 0); @@ -310,7 +312,9 @@ void BlocksRenderer::blockCubeShaded(int x, int y, int z, const UVRegion(&texfac // Does block allow to see other blocks sides (is it transparent) bool BlocksRenderer::isOpen(int x, int y, int z, ubyte group) const { - blockid_t id = voxelsBuffer->pickBlockId(chunk->x * CHUNK_W + x, y, chunk->z * CHUNK_D + z); + blockid_t id = voxelsBuffer->pickBlockId(chunk->x * CHUNK_W + x, + y, + chunk->z * CHUNK_D + z); if (id == BLOCK_VOID) return false; const Block& block = *blockDefsCache[id]; @@ -321,7 +325,9 @@ bool BlocksRenderer::isOpen(int x, int y, int z, ubyte group) const { } bool BlocksRenderer::isOpenForLight(int x, int y, int z) const { - blockid_t id = voxelsBuffer->pickBlockId(chunk->x * CHUNK_W + x, y, chunk->z * CHUNK_D + z); + blockid_t id = voxelsBuffer->pickBlockId(chunk->x * CHUNK_W + x, + y, + chunk->z * CHUNK_D + z); if (id == BLOCK_VOID) return false; const Block& block = *blockDefsCache[id]; @@ -333,7 +339,9 @@ bool BlocksRenderer::isOpenForLight(int x, int y, int z) const { vec4 BlocksRenderer::pickLight(int x, int y, int z) const { if (isOpenForLight(x, y, z)) { - light_t light = voxelsBuffer->pickLight(chunk->x * CHUNK_W + x, y, chunk->z * CHUNK_D + z); + light_t light = voxelsBuffer->pickLight(chunk->x * CHUNK_W + x, + y, + chunk->z * CHUNK_D + z); return vec4(Lightmap::extract(light, 0) / 15.0f, Lightmap::extract(light, 1) / 15.0f, Lightmap::extract(light, 2) / 15.0f, @@ -349,7 +357,8 @@ vec4 BlocksRenderer::pickLight(const ivec3& coord) const { } vec4 BlocksRenderer::pickSoftLight(const ivec3& coord, - const ivec3& right, const ivec3& up) const { + const ivec3& right, + const ivec3& up) const { return ( pickLight(coord) + pickLight(coord - right) + @@ -358,11 +367,13 @@ vec4 BlocksRenderer::pickSoftLight(const ivec3& coord, } vec4 BlocksRenderer::pickSoftLight(float x, float y, float z, - const ivec3& right, const ivec3& up) const { + const ivec3& right, + const ivec3& up) const { return pickSoftLight({int(round(x)), int(round(y)), int(round(z))}, right, up); } -void BlocksRenderer::render(const voxel* voxels, int atlas_size) { +#include +void BlocksRenderer::render(const voxel* voxels) { int begin = chunk->bottom * (CHUNK_W * CHUNK_D); int end = chunk->top * (CHUNK_W * CHUNK_D); for (const auto drawGroup : *content->drawGroups) { @@ -372,31 +383,42 @@ void BlocksRenderer::render(const voxel* voxels, int atlas_size) { const Block& def = *blockDefsCache[id]; if (!id || def.drawGroup != drawGroup) continue; - const UVRegion texfaces[6]{ cache->getRegion(id, 0), cache->getRegion(id, 1), - cache->getRegion(id, 2), cache->getRegion(id, 3), - cache->getRegion(id, 4), cache->getRegion(id, 5)}; + const UVRegion texfaces[6]{ cache->getRegion(id, 0), + cache->getRegion(id, 1), + cache->getRegion(id, 2), + cache->getRegion(id, 3), + cache->getRegion(id, 4), + cache->getRegion(id, 5)}; int x = i % CHUNK_W; int y = i / (CHUNK_D * CHUNK_W); int z = (i / CHUNK_D) % CHUNK_W; switch (def.model) { case BlockModel::block: if (def.rt.emissive) { - blockCube(x, y, z, vec3(1.0f), texfaces, def.drawGroup); + blockCube(x, y, z, texfaces, def.drawGroup); } else { blockCubeShaded(x, y, z, texfaces, &def, vox.states); } break; case BlockModel::xsprite: { - blockXSprite(x, y, z, vec3(1.0f), texfaces[FACE_MX], texfaces[FACE_MZ], 1.0f); + blockXSprite(x, y, z, vec3(1.0f), + texfaces[FACE_MX], texfaces[FACE_MZ], 1.0f); break; } case BlockModel::aabb: { - vec3 size = def.hitbox.size(); - vec3 off = def.hitbox.min(); - off.z *= -1.0f; - off.z = -1.0f-off.z + size.z; - blockCubeShaded(off+vec3(x,y,z), size, texfaces, &def, vox.states); + AABB hitbox = def.hitbox; + hitbox.a = vec3(1.0f)-hitbox.a; + hitbox.b = vec3(1.0f)-hitbox.b; + std::cout << hitbox.a.x << " " << hitbox.a.y << " " << hitbox.a.z << " --- "; + std::cout << hitbox.b.x << " " << hitbox.b.y << " " << hitbox.b.z << std::endl; + + vec3 size = hitbox.size(); + + std::cout << "s " << size.x << " " << size.y << " " << size.z << std::endl; + vec3 off = hitbox.min(); + std::cout << "m " << off.x << " " << off.y << " " << off.z << std::endl; + blockCubeShaded(ivec3(x,y,z), off, size, texfaces, &def, vox.states); break; } default: @@ -408,7 +430,7 @@ void BlocksRenderer::render(const voxel* voxels, int atlas_size) { } } -Mesh* BlocksRenderer::render(const Chunk* chunk, int atlas_size, const ChunksStorage* chunks) { +Mesh* BlocksRenderer::render(const Chunk* chunk, const ChunksStorage* chunks) { this->chunk = chunk; voxelsBuffer->setPosition(chunk->x * CHUNK_W - 1, 0, chunk->z * CHUNK_D - 1); chunks->getVoxels(voxelsBuffer, settings.graphics.backlight); @@ -416,10 +438,11 @@ Mesh* BlocksRenderer::render(const Chunk* chunk, int atlas_size, const ChunksSto vertexOffset = 0; indexOffset = indexSize = 0; const voxel* voxels = chunk->voxels; - render(voxels, atlas_size); + render(voxels); const vattr attrs[]{ {3}, {2}, {1}, {0} }; - Mesh* mesh = new Mesh(vertexBuffer, vertexOffset / VERTEX_SIZE, indexBuffer, indexSize, attrs); + size_t vcount = vertexOffset / BlocksRenderer::VERTEX_SIZE; + Mesh* mesh = new Mesh(vertexBuffer, vcount, indexBuffer, indexSize, attrs); return mesh; } diff --git a/src/graphics/BlocksRenderer.h b/src/graphics/BlocksRenderer.h index a805d4a6..24559192 100644 --- a/src/graphics/BlocksRenderer.h +++ b/src/graphics/BlocksRenderer.h @@ -18,6 +18,7 @@ class ChunksStorage; class ContentGfxCache; class BlocksRenderer { + static const uint VERTEX_SIZE; const Content* const content; float* vertexBuffer; int* indexBuffer; @@ -43,20 +44,18 @@ class BlocksRenderer { const glm::ivec3& axisY, const glm::ivec3& axisZ); - void face(const glm::vec3& coord, float w, float h, - const glm::vec3& axisX, - const glm::vec3& axisY, - const UVRegion& region, - const glm::vec4(&lights)[4], - const glm::vec4& tint); + void vertex(const glm::vec3& coord, float u, float v, + const glm::vec4& brightness, + const glm::ivec3& axisX, + const glm::ivec3& axisY, + const glm::ivec3& axisZ); void face(const glm::vec3& coord, float w, float h, const glm::vec3& axisX, const glm::vec3& axisY, const UVRegion& region, const glm::vec4(&lights)[4], - const glm::vec4& tint, - bool rotated); + const glm::vec4& tint); void face(const glm::ivec3& coord, const glm::ivec3& axisX, @@ -65,6 +64,17 @@ class BlocksRenderer { const glm::ivec3& laxisZ, const UVRegion& region); + void face(const glm::ivec3& coord, + const glm::ivec3& axisX, + const glm::ivec3& axisY, + const glm::ivec3& axisZ, + const glm::ivec3& laxisZ, + const glm::vec3& offset, + float width, + float height, + float depth, + const UVRegion& region); + void face(const glm::vec3& coord, float w, float h, const glm::vec3& axisX, const glm::vec3& axisY, @@ -73,12 +83,15 @@ class BlocksRenderer { face(coord, w, h, axisX, axisY, region, lights, glm::vec4(1.0f)); } - void cube(const glm::vec3& coord, const glm::vec3& size, const UVRegion(&faces)[6]); - void blockCube(int x, int y, int z, const glm::vec3& size, const UVRegion(&faces)[6], ubyte group); - /* Fastest solid shaded blocks render method */ + void blockCube(int x, int y, int z, const UVRegion(&faces)[6], ubyte group); + void blockCubeShaded(int x, int y, int z, const UVRegion(&faces)[6], const Block* block, ubyte states); - /* AABB blocks render method (WIP)*/ - void blockCubeShaded(const glm::vec3& pos, const glm::vec3& size, const UVRegion(&faces)[6], const Block* block, ubyte states); + void blockCubeShaded(const glm::ivec3& coord, + const glm::vec3& offset, + const glm::vec3& size, + const UVRegion(&faces)[6], + const Block* block, + ubyte states); void blockXSprite(int x, int y, int z, const glm::vec3& size, const UVRegion& face1, const UVRegion& face2, float spread); bool isOpenForLight(int x, int y, int z) const; @@ -88,12 +101,12 @@ class BlocksRenderer { glm::vec4 pickLight(const glm::ivec3& coord) const; glm::vec4 pickSoftLight(const glm::ivec3& coord, const glm::ivec3& right, const glm::ivec3& up) const; glm::vec4 pickSoftLight(float x, float y, float z, const glm::ivec3& right, const glm::ivec3& up) const; - void render(const voxel* voxels, int atlas_size); + void render(const voxel* voxels); public: BlocksRenderer(size_t capacity, const Content* content, const ContentGfxCache* cache, const EngineSettings& settings); virtual ~BlocksRenderer(); - Mesh* render(const Chunk* chunk, int atlas_size, const ChunksStorage* chunks); + Mesh* render(const Chunk* chunk, const ChunksStorage* chunks); VoxelsVolume* getVoxelsBuffer() const; }; diff --git a/src/graphics/ChunksRenderer.cpp b/src/graphics/ChunksRenderer.cpp index ff4058bb..b87ad1ee 100644 --- a/src/graphics/ChunksRenderer.cpp +++ b/src/graphics/ChunksRenderer.cpp @@ -22,7 +22,7 @@ ChunksRenderer::~ChunksRenderer() { shared_ptr ChunksRenderer::render(Chunk* chunk) { chunk->setModified(false); - Mesh* mesh = renderer->render(chunk, 16, level->chunksStorage); + Mesh* mesh = renderer->render(chunk, level->chunksStorage); auto sptr = shared_ptr(mesh); meshes[ivec2(chunk->x, chunk->z)] = sptr; return sptr; diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp index 0abf7962..17b934d9 100644 --- a/src/graphics/Font.cpp +++ b/src/graphics/Font.cpp @@ -29,7 +29,7 @@ bool Font::isPrintableChar(int c) { } } -#define RES 16 +const int RES = 16; int Font::calcWidth(std::wstring text) { return text.length() * 8; diff --git a/src/graphics/Font.h b/src/graphics/Font.h index 6df79e08..e2e73123 100644 --- a/src/graphics/Font.h +++ b/src/graphics/Font.h @@ -7,9 +7,9 @@ class Texture; class Batch2D; -#define STYLE_NONE 0 -#define STYLE_SHADOW 1 -#define STYLE_OUTLINE 2 +const uint STYLE_NONE = 0; +const uint STYLE_SHADOW = 1; +const uint STYLE_OUTLINE = 2; class Font { int lineHeight_; diff --git a/src/graphics/LineBatch.cpp b/src/graphics/LineBatch.cpp index 37a74015..efb41afc 100644 --- a/src/graphics/LineBatch.cpp +++ b/src/graphics/LineBatch.cpp @@ -3,7 +3,7 @@ #include -#define LB_VERTEX_SIZE (3+4) +const uint LB_VERTEX_SIZE = (3+4); LineBatch::LineBatch(size_t capacity) : capacity(capacity) { const vattr attrs[] = { {3},{4}, {0} }; diff --git a/src/logic/ChunksController.cpp b/src/logic/ChunksController.cpp index f7ae440a..936afb45 100644 --- a/src/logic/ChunksController.cpp +++ b/src/logic/ChunksController.cpp @@ -18,8 +18,8 @@ #include "../world/World.h" #include "../maths/voxmaths.h" -#define MAX_WORK_PER_FRAME 16 -#define MIN_SURROUNDING 9 +const uint MAX_WORK_PER_FRAME = 16; +const uint MIN_SURROUNDING = 9; using std::unique_ptr; using std::shared_ptr; diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index 0e1a5478..386ce889 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -15,15 +15,15 @@ #include "../core_defs.h" -#define CAM_SHAKE_OFFSET 0.025f -#define CAM_SHAKE_OFFSET_Y 0.031f -#define CAM_SHAKE_SPEED 1.6f -#define CAM_SHAKE_DELTA_K 10.0f -#define ZOOM_SPEED 16.0f -#define CROUCH_ZOOM 0.9f -#define RUN_ZOOM 1.1f -#define C_ZOOM 0.1f -#define CROUCH_SHIFT_Y -0.2f +const float CAM_SHAKE_OFFSET = 0.025f; +const float CAM_SHAKE_OFFSET_Y = 0.031f; +const float CAM_SHAKE_SPEED = 1.6f; +const float CAM_SHAKE_DELTA_K = 10.0f; +const float ZOOM_SPEED = 16.0f; +const float CROUCH_ZOOM = 0.9f; +const float RUN_ZOOM = 1.1f; +const float C_ZOOM = 0.1f; +const float CROUCH_SHIFT_Y = -0.2f; using glm::vec2; using glm::vec3; @@ -110,6 +110,7 @@ vec3 PlayerController::selectedBlockPosition; vec3 PlayerController::selectedPointPosition; vec3 PlayerController::selectedBlockNormal; int PlayerController::selectedBlockId = -1; +int PlayerController::selectedBlockStates = 0; PlayerController::PlayerController(Level* level, const EngineSettings& settings) : level(level), @@ -133,6 +134,7 @@ void PlayerController::update(float delta, bool input, bool pause) { updateInteraction(); } else { selectedBlockId = -1; + selectedBlockStates = 0; } } @@ -207,6 +209,7 @@ void PlayerController::updateInteraction(){ if (vox != nullptr){ player->selectedVoxel = *vox; selectedBlockId = vox->id; + selectedBlockStates = vox->states; selectedBlockPosition = iend; selectedPointPosition = end; selectedBlockNormal = norm; @@ -248,5 +251,6 @@ void PlayerController::updateInteraction(){ } } else { selectedBlockId = -1; + selectedBlockStates = 0; } } \ No newline at end of file diff --git a/src/logic/PlayerController.h b/src/logic/PlayerController.h index e9e029fd..a2f7ccf9 100644 --- a/src/logic/PlayerController.h +++ b/src/logic/PlayerController.h @@ -40,6 +40,7 @@ public: static glm::vec3 selectedBlockNormal; static glm::vec3 selectedPointPosition; static int selectedBlockId; + static int selectedBlockStates; PlayerController(Level* level, const EngineSettings& settings); void update(float delta, bool input, bool pause); diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index e67ab020..1ff92c25 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -8,13 +8,13 @@ #include -#define CROUCH_SPEED_MUL 0.35f -#define RUN_SPEED_MUL 1.5f -#define PLAYER_GROUND_DAMPING 10.0f -#define PLAYER_AIR_DAMPING 7.0f -#define FLIGHT_SPEED_MUL 4.0f -#define CHEAT_SPEED_MUL 5.0f -#define JUMP_FORCE 7.0f +const float CROUCH_SPEED_MUL = 0.35f; +const float RUN_SPEED_MUL = 1.5f; +const float PLAYER_GROUND_DAMPING = 10.0f; +const float PLAYER_AIR_DAMPING = 7.0f; +const float FLIGHT_SPEED_MUL = 4.0f; +const float CHEAT_SPEED_MUL = 5.0f; +const float JUMP_FORCE = 7.0f; Player::Player(glm::vec3 position, float speed, Camera* camera) : speed(speed), diff --git a/src/physics/PhysicsSolver.cpp b/src/physics/PhysicsSolver.cpp index 6eb82af9..a8b8f09a 100644 --- a/src/physics/PhysicsSolver.cpp +++ b/src/physics/PhysicsSolver.cpp @@ -5,7 +5,7 @@ #include "../voxels/Block.h" #include "../voxels/Chunks.h" -#define E 0.03 +const double E = 0.03; using glm::vec3; diff --git a/src/settings.h b/src/settings.h index daf2f7b2..59a0fcb7 100644 --- a/src/settings.h +++ b/src/settings.h @@ -18,7 +18,9 @@ struct DisplaySettings { /* GLFW swap interval value, 0 - unlimited fps, 1 - vsync*/ int swapInterval = 1; /* Window title */ - const char* title = "VoxelEngine-Cpp v" ENGINE_VERSION; + std::string title = "VoxelEngine-Cpp v" + + std::to_string(ENGINE_VERSION_MAJOR) + "." + + std::to_string(ENGINE_VERSION_MINOR); }; struct ChunksSettings { diff --git a/src/voxels/Block.cpp b/src/voxels/Block.cpp index 4f42d573..085646ed 100644 --- a/src/voxels/Block.cpp +++ b/src/voxels/Block.cpp @@ -1,12 +1,24 @@ #include "Block.h" -BlockRotProfile BlockRotProfile::PIPE {{ - { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { 0, 0, 0 } }, // BLOCK_DIR_PY - { { 0,-1, 0 }, { 1, 0, 0 }, { 0, 0, 1 }, { 0, 1, 0 } }, // BLOCK_DIR_PX - { { 1, 0, 0 }, { 0, 0, 1 }, { 0,-1, 0 }, { 0, 0,-1 } }, // BLOCK_DIR_PZ - { { 0, 1, 0 }, {-1, 0, 0 }, { 0, 0, 1 }, { 1, 0, 0 } }, // BLOCK_DIR_MX - { {-1, 0, 0 }, { 0,-1, 0 }, { 0, 0, 1 }, { 1, 1, 0 } }, // BLOCK_DIR_MY - { { 1, 0, 0 }, { 0, 0,-1 }, { 0, 1, 0 }, { 0, 1, 0 } }, // BLOCK_DIR_MZ +using glm::vec3; + +void CoordSystem::transform(AABB& aabb) { + vec3 X(axisX); + vec3 Y(axisY); + vec3 Z(axisZ); + aabb.a = X * aabb.a.x + Y * aabb.a.y + Z * aabb.a.z; + aabb.b = X * aabb.b.x + Y * aabb.b.y + Z * aabb.b.z; + aabb.a += fix2; + aabb.b += fix2; +} + +const BlockRotProfile BlockRotProfile::PIPE {{ + // Vertical + {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {0, 0, 0}, {0, 0, 0}}, + // X-Aligned + {{0, -1, 0}, {1, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1, 0}}, + // Z-Aligned + {{1, 0, 0}, {0, 0, 1}, {0, -1, 0}, {0, 0, -1}, {0, 1, 0}}, }}; Block::Block(std::string name) diff --git a/src/voxels/Block.h b/src/voxels/Block.h index 42fdf0cc..fa6002db 100644 --- a/src/voxels/Block.h +++ b/src/voxels/Block.h @@ -7,14 +7,14 @@ #include "../maths/aabb.h" #include "../typedefs.h" -#define FACE_MX 0 -#define FACE_PX 1 -#define FACE_MY 2 -#define FACE_PY 3 -#define FACE_MZ 4 -#define FACE_PZ 5 +const uint FACE_MX = 0; +const uint FACE_PX = 1; +const uint FACE_MY = 2; +const uint FACE_PY = 3; +const uint FACE_MZ = 4; +const uint FACE_PZ = 5; -#define BLOCK_AABB_GRID 16 +const uint BLOCK_AABB_GRID = 16; struct CoordSystem { glm::ivec3 axisX; @@ -22,6 +22,9 @@ struct CoordSystem { glm::ivec3 axisZ; // Grid 3d position fix offset (for negative vectors) glm::ivec3 fix; + glm::ivec3 fix2; + + void transform(AABB& aabb); }; struct BlockRotProfile { @@ -30,7 +33,7 @@ struct BlockRotProfile { /* Wood logs, pillars, pipes 3 orientations supported */ - static BlockRotProfile PIPE; + static const BlockRotProfile PIPE; }; enum class BlockModel { diff --git a/src/voxels/Chunk.h b/src/voxels/Chunk.h index 8e751386..31c1cd32 100644 --- a/src/voxels/Chunk.h +++ b/src/voxels/Chunk.h @@ -4,11 +4,13 @@ #include #include "../constants.h" -#define CHUNK_MODIFIED 0x1 -#define CHUNK_READY 0x2 -#define CHUNK_LOADED 0x4 -#define CHUNK_LIGHTED 0x8 -#define CHUNK_UNSAVED 0x10 +struct ChunkFlag{ + static const int MODIFIED = 0x1; + static const int READY = 0x2; + static const int LOADED = 0x4; + static const int LIGHTED = 0x8; + static const int UNSAVED = 0x10; +}; #define CHUNK_DATA_LEN (CHUNK_VOL*2) struct voxel; @@ -19,10 +21,6 @@ struct RenderData { size_t size; }; -#define BIT_ON(f,i) do{f|= i;} while(0) -#define BIT_OFF(f,i) do{f&=~(i);} while(0) -#define BITSET(f,i,s) if (s) BIT_ON(f,i); else BIT_OFF(f,i); - class Chunk { public: int x, z; @@ -43,26 +41,33 @@ public: Chunk* clone() const; // flags getters/setters below + + void SETFLAGS(int mask, bool value){ + if (value) + flags |= mask; + else + flags &= ~(mask); + } - inline bool isUnsaved() const {return flags & CHUNK_UNSAVED;} + inline bool isUnsaved() const {return flags & ChunkFlag::UNSAVED;} - inline bool isModified() const {return flags & CHUNK_MODIFIED;} + inline bool isModified() const {return flags & ChunkFlag::MODIFIED;} - inline bool isLighted() const {return flags & CHUNK_LIGHTED;} + inline bool isLighted() const {return flags & ChunkFlag::LIGHTED;} - inline bool isLoaded() const {return flags & CHUNK_LOADED;} + inline bool isLoaded() const {return flags & ChunkFlag::LOADED;} - inline bool isReady() const {return flags & CHUNK_READY;} + inline bool isReady() const {return flags & ChunkFlag::READY;} - inline void setUnsaved(bool flag) {BITSET(flags, CHUNK_UNSAVED, flag);} + inline void setUnsaved(bool newState) {SETFLAGS(ChunkFlag::UNSAVED, newState);} - inline void setModified(bool flag) {BITSET(flags, CHUNK_MODIFIED, flag);} + inline void setModified(bool newState) {SETFLAGS(ChunkFlag::MODIFIED, newState);} - inline void setLoaded(bool flag) {BITSET(flags, CHUNK_LOADED, flag);} + inline void setLoaded(bool newState) {SETFLAGS(ChunkFlag::LOADED, newState);} - inline void setLighted(bool flag) {BITSET(flags, CHUNK_LIGHTED, flag);} + inline void setLighted(bool newState) {SETFLAGS(ChunkFlag::LIGHTED, newState);} - inline void setReady(bool flag) {BITSET(flags, CHUNK_READY, flag);} + inline void setReady(bool newState) {SETFLAGS(ChunkFlag::READY, newState);} ubyte* encode() const; bool decode(ubyte* data); diff --git a/src/voxels/WorldGenerator.cpp b/src/voxels/WorldGenerator.cpp index 4374b3ac..9ef3761c 100644 --- a/src/voxels/WorldGenerator.cpp +++ b/src/voxels/WorldGenerator.cpp @@ -16,7 +16,7 @@ #include "../maths/voxmaths.h" #include "../core_defs.h" -#define SEA_LEVEL 55 +const int SEA_LEVEL = 55; class Map2D { int x, z; diff --git a/src/voxels/voxel.h b/src/voxels/voxel.h index 40e0083f..9bd2d2f0 100644 --- a/src/voxels/voxel.h +++ b/src/voxels/voxel.h @@ -3,15 +3,12 @@ #include "../typedefs.h" -#define BLOCK_DIR_PX 0x1 -#define BLOCK_DIR_PY 0x0 -#define BLOCK_DIR_PZ 0x2 -#define BLOCK_DIR_MX 0x3 -#define BLOCK_DIR_MY 0x4 -#define BLOCK_DIR_MZ 0x5 +#define BLOCK_DIR_X 0x1 +#define BLOCK_DIR_Y 0x0 +#define BLOCK_DIR_Z 0x2 // limited to 16 block orientations -#define BLOCK_ROT_MASK 0xF +const int BLOCK_ROT_MASK = 0xF; struct voxel { blockid_t id; diff --git a/src/window/Events.cpp b/src/window/Events.cpp index 0b779e7b..fbc3c31c 100644 --- a/src/window/Events.cpp +++ b/src/window/Events.cpp @@ -1,8 +1,12 @@ +#include #include "Events.h" #include #include #include +const short KEYS_BUFFER_SIZE = 1032; +const short _MOUSE_KEYS_OFFSET = 1024; + bool* Events::_keys; uint* Events::_frames; uint Events::_current = 0; @@ -18,11 +22,11 @@ std::vector Events::pressedKeys; std::unordered_map Events::bindings; int Events::initialize(){ - _keys = new bool[1032]; - _frames = new uint[1032]; + _keys = new bool[KEYS_BUFFER_SIZE]; + _frames = new uint[KEYS_BUFFER_SIZE]; - memset(_keys, false, 1032*sizeof(bool)); - memset(_frames, 0, 1032*sizeof(uint)); + memset(_keys, false, KEYS_BUFFER_SIZE*sizeof(bool)); + memset(_frames, 0, KEYS_BUFFER_SIZE*sizeof(uint)); return 0; } @@ -32,26 +36,28 @@ void Events::finalize(){ delete[] _frames; } +// Returns bool repr. of key state bool Events::pressed(int keycode){ - if (keycode < 0 || keycode >= _MOUSE_BUTTONS) + if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE){ + // VERY bad behaviour and it happens constantly! (so console-printing is not a good idea) return false; + } return _keys[keycode]; } -bool Events::jpressed(int keycode){ - if (keycode < 0 || keycode >= _MOUSE_BUTTONS) - return false; - return _keys[keycode] && _frames[keycode] == _current; +// Returns bool repr. of key state +bool Events::jpressed(int keycode){ + return Events::pressed(keycode) && _frames[keycode] == _current; } +// Returns bool repr. of mouse key state bool Events::clicked(int button){ - int index = _MOUSE_BUTTONS+button; - return _keys[index]; + return Events::pressed(_MOUSE_KEYS_OFFSET + button); } +// Returns bool repr. of mouse key state bool Events::jclicked(int button){ - int index = _MOUSE_BUTTONS+button; - return _keys[index] && _frames[index] == _current; + return Events::jpressed(_MOUSE_KEYS_OFFSET + button); } void Events::toggleCursor(){ @@ -59,7 +65,7 @@ void Events::toggleCursor(){ Window::setCursorMode(_cursor_locked ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL); } -void Events::pullEvents(){ +void Events::pollEvents(){ _current++; deltaX = 0.0f; deltaY = 0.0f; diff --git a/src/window/Events.h b/src/window/Events.h index 226a5096..f9eea2c8 100644 --- a/src/window/Events.h +++ b/src/window/Events.h @@ -10,6 +10,9 @@ typedef unsigned int uint; +extern const short KEYS_BUFFER_SIZE; +extern const short _MOUSE_KEYS_OFFSET; + class Events { public: static bool* _keys; @@ -28,7 +31,7 @@ public: static int initialize(); static void finalize(); - static void pullEvents(); + static void pollEvents(); static bool pressed(int keycode); static bool jpressed(int keycode); @@ -43,6 +46,4 @@ public: static bool jactive(std::string name); }; -#define _MOUSE_BUTTONS 1024 - #endif /* WINDOW_EVENTS_H_ */ diff --git a/src/window/Window.cpp b/src/window/Window.cpp index eabab4a0..8093f93d 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -34,23 +34,27 @@ void cursor_position_callback(GLFWwindow*, double xpos, double ypos) { void mouse_button_callback(GLFWwindow*, int button, int action, int) { if (action == GLFW_PRESS) { - Events::_keys[_MOUSE_BUTTONS + button] = true; - Events::_frames[_MOUSE_BUTTONS + button] = Events::_current; + // Unsafe assignments! (no checks) + Events::_keys[_MOUSE_KEYS_OFFSET + button] = true; + Events::_frames[_MOUSE_KEYS_OFFSET + button] = Events::_current; } else if (action == GLFW_RELEASE) { - Events::_keys[_MOUSE_BUTTONS + button] = false; - Events::_frames[_MOUSE_BUTTONS + button] = Events::_current; + // Unsafe assignments! (no checks) + Events::_keys[_MOUSE_KEYS_OFFSET + button] = false; + Events::_frames[_MOUSE_KEYS_OFFSET + button] = Events::_current; } } void key_callback(GLFWwindow*, int key, int scancode, int action, int /*mode*/) { if (key == GLFW_KEY_UNKNOWN) return; if (action == GLFW_PRESS) { + // Unsafe assignments! (no checks) Events::_keys[key] = true; Events::_frames[key] = Events::_current; Events::pressedKeys.push_back(key); } else if (action == GLFW_RELEASE) { + // Unsafe assignments! (no checks) Events::_keys[key] = false; Events::_frames[key] = Events::_current; } @@ -124,7 +128,7 @@ int Window::initialize(DisplaySettings& settings){ glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); glfwWindowHint(GLFW_SAMPLES, settings.samples); - window = glfwCreateWindow(width, height, settings.title, nullptr, nullptr); + window = glfwCreateWindow(width, height, settings.title.c_str(), nullptr, nullptr); if (window == nullptr){ cerr << "Failed to create GLFW Window" << endl; glfwTerminate(); diff --git a/src/window/input.h b/src/window/input.h index 289cd6ea..6316cb7e 100644 --- a/src/window/input.h +++ b/src/window/input.h @@ -3,96 +3,96 @@ #include -namespace keycode { - extern int ENTER; - extern int TAB; - extern int SPACE; - extern int BACKSPACE; - extern int LEFT_CONTROL; - extern int LEFT_SHIFT; - extern int LEFT_ALT; - extern int RIGHT_CONTROL; - extern int RIGHT_SHIFT; - extern int RIGHT_ALT; - extern int ESCAPE; - extern int CAPS_LOCK; - extern int LEFT; - extern int RIGHT; - extern int DOWN; - extern int UP; - extern int F1; - extern int F2; - extern int F3; - extern int F4; - extern int F5; - extern int F6; - extern int F7; - extern int F8; - extern int F9; - extern int F10; - extern int F11; - extern int F12; - extern int A; - extern int B; - extern int C; - extern int D; - extern int E; - extern int F; - extern int G; - extern int H; - extern int I; - extern int J; - extern int K; - extern int L; - extern int M; - extern int N; - extern int O; - extern int P; - extern int Q; - extern int R; - extern int S; - extern int T; - extern int U; - extern int V; - extern int W; - extern int X; - extern int Y; - extern int Z; - extern int NUM_0; - extern int NUM_1; - extern int NUM_2; - extern int NUM_3; - extern int NUM_4; - extern int NUM_5; - extern int NUM_6; - extern int NUM_7; - extern int NUM_8; - extern int NUM_9; - extern int MENU; - extern int PAUSE; - extern int INSERT; - extern int LEFT_SUPER; - extern int RIGHT_SUPER; - extern int DELETE; - extern int PAGE_UP; - extern int PAGE_DOWN; - extern int HOME; - extern int END; - extern int PRINT_SCREEN; - extern int NUM_LOCK; - extern int LEFT_BRACKET; - extern int RIGHT_BRACKET; +struct keycode { + static int ENTER; + static int TAB; + static int SPACE; + static int BACKSPACE; + static int LEFT_CONTROL; + static int LEFT_SHIFT; + static int LEFT_ALT; + static int RIGHT_CONTROL; + static int RIGHT_SHIFT; + static int RIGHT_ALT; + static int ESCAPE; + static int CAPS_LOCK; + static int LEFT; + static int RIGHT; + static int DOWN; + static int UP; + static int F1; + static int F2; + static int F3; + static int F4; + static int F5; + static int F6; + static int F7; + static int F8; + static int F9; + static int F10; + static int F11; + static int F12; + static int A; + static int B; + static int C; + static int D; + static int E; + static int F; + static int G; + static int H; + static int I; + static int J; + static int K; + static int L; + static int M; + static int N; + static int O; + static int P; + static int Q; + static int R; + static int S; + static int T; + static int U; + static int V; + static int W; + static int X; + static int Y; + static int Z; + static int NUM_0; + static int NUM_1; + static int NUM_2; + static int NUM_3; + static int NUM_4; + static int NUM_5; + static int NUM_6; + static int NUM_7; + static int NUM_8; + static int NUM_9; + static int MENU; + static int PAUSE; + static int INSERT; + static int LEFT_SUPER; + static int RIGHT_SUPER; + static int DELETE; + static int PAGE_UP; + static int PAGE_DOWN; + static int HOME; + static int END; + static int PRINT_SCREEN; + static int NUM_LOCK; + static int LEFT_BRACKET; + static int RIGHT_BRACKET; - extern const std::string name(int code); -} + static const std::string name(int code); +}; -namespace mousecode { - extern int BUTTON_1; - extern int BUTTON_2; - extern int BUTTON_3; +struct mousecode { + static int BUTTON_1; + static int BUTTON_2; + static int BUTTON_3; - extern const std::string name(int code); -} + static const std::string name(int code); +}; enum class inputtype { keyboard,