diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-08-20 17:21:29 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-08-20 17:21:29 +0200 |
commit | c62e2cd49649109f0135e56ee8add0dab2254d01 (patch) | |
tree | 7e6240186a3801daf0189d1883081b93b5f1eb5c /src | |
parent | 2017-08-19 (diff) | |
download | AltCraft-c62e2cd49649109f0135e56ee8add0dab2254d01.tar AltCraft-c62e2cd49649109f0135e56ee8add0dab2254d01.tar.gz AltCraft-c62e2cd49649109f0135e56ee8add0dab2254d01.tar.bz2 AltCraft-c62e2cd49649109f0135e56ee8add0dab2254d01.tar.lz AltCraft-c62e2cd49649109f0135e56ee8add0dab2254d01.tar.xz AltCraft-c62e2cd49649109f0135e56ee8add0dab2254d01.tar.zst AltCraft-c62e2cd49649109f0135e56ee8add0dab2254d01.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/AssetManager.cpp | 2 | ||||
-rw-r--r-- | src/Block.cpp | 14 | ||||
-rw-r--r-- | src/Block.hpp | 9 | ||||
-rw-r--r-- | src/GameState.cpp | 6 | ||||
-rw-r--r-- | src/GameState.hpp | 3 | ||||
-rw-r--r-- | src/Network.cpp | 2 | ||||
-rw-r--r-- | src/Packet.hpp | 18 | ||||
-rw-r--r-- | src/Render.cpp | 11 | ||||
-rw-r--r-- | src/Renderer.hpp | 5 | ||||
-rw-r--r-- | src/RendererEntity.cpp | 17 | ||||
-rw-r--r-- | src/RendererEntity.hpp | 7 | ||||
-rw-r--r-- | src/RendererSection.cpp | 119 | ||||
-rw-r--r-- | src/RendererSection.hpp | 3 | ||||
-rw-r--r-- | src/RendererSky.cpp | 140 | ||||
-rw-r--r-- | src/RendererSky.hpp | 16 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 78 | ||||
-rw-r--r-- | src/RendererWorld.hpp | 15 | ||||
-rw-r--r-- | src/Section.cpp | 87 | ||||
-rw-r--r-- | src/Section.hpp | 2 | ||||
-rw-r--r-- | src/ThreadGame.cpp | 2 | ||||
-rw-r--r-- | src/World.cpp | 12 |
21 files changed, 393 insertions, 175 deletions
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 28c08fa..22dac8d 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -20,7 +20,7 @@ void AssetManager::LoadIds() { int id = it["type"].get<int>(); int state = it["meta"].get<int>(); std::string blockName = it["text_type"].get<std::string>(); - assetIds[blockName] = Block(id, state); + assetIds[blockName] = Block(id, state, 0, 0); } LOG(INFO) << "Loaded " << assetIds.size() << " ids"; } diff --git a/src/Block.cpp b/src/Block.cpp index e88068a..4c907d6 100644 --- a/src/Block.cpp +++ b/src/Block.cpp @@ -2,16 +2,6 @@ Block::~Block() {} -Block::Block(unsigned short id, unsigned char state) : id(id), state(state) {} +Block::Block(unsigned short id, unsigned char state, unsigned char light, unsigned char sky) : id(id), state(state), light(light), sky (sky) {} -Block::Block() : id(0), state(0) {} - -bool operator<(const Block &lhs, const Block &rhs) { - if (lhs.id < rhs.id) - return true; - if (lhs.id == rhs.id) { - if (lhs.state != rhs.state) - return lhs.state < rhs.state; - } - return false; -} +Block::Block() : id(0), state(0), light(0), sky(0) {}
\ No newline at end of file diff --git a/src/Block.hpp b/src/Block.hpp index 2f823fe..367e559 100644 --- a/src/Block.hpp +++ b/src/Block.hpp @@ -3,13 +3,12 @@ struct Block { Block(); - Block(unsigned short id, unsigned char state); + Block(unsigned short id, unsigned char state, unsigned char light, unsigned char sky); ~Block(); unsigned short id : 13; unsigned char state : 4; - //unsigned char light:4; -}; - -bool operator<(const Block &lhs, const Block &rhs);
\ No newline at end of file + unsigned char light : 4; + unsigned char sky : 4; +};
\ No newline at end of file diff --git a/src/GameState.cpp b/src/GameState.cpp index c49b332..e536df0 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -358,8 +358,12 @@ void GameState::UpdatePacket() LOG(INFO) << "Spawn position is " << g_SpawnPosition.x << " " << g_SpawnPosition.y << " " << g_SpawnPosition.z; break; } - case TimeUpdate: + case TimeUpdate: { + auto packet = std::static_pointer_cast<PacketTimeUpdate>(ptr); + WorldAge = packet->WorldAge; + TimeOfDay = packet->TimeOfDay; break; + } case Title: break; case SoundEffect: diff --git a/src/GameState.hpp b/src/GameState.hpp index f09e5ab..6dbb194 100644 --- a/src/GameState.hpp +++ b/src/GameState.hpp @@ -69,5 +69,8 @@ public: double g_PlayerVelocityY = 0; double g_PlayerVelocityZ = 0; + long long WorldAge = 0; + long long TimeOfDay = 0; + std::shared_ptr<GameState> gs; }; diff --git a/src/Network.cpp b/src/Network.cpp index 4ab85d3..71de943 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -205,7 +205,7 @@ std::shared_ptr<Packet> Network::ParsePacketPlay(PacketNamePlayCB id) { case SpawnPosition: return std::make_shared<PacketSpawnPosition>(); case TimeUpdate: - break; + return std::make_shared<PacketTimeUpdate>(); case Title: break; case SoundEffect: diff --git a/src/Packet.hpp b/src/Packet.hpp index 6318eab..e3d09fd 100644 --- a/src/Packet.hpp +++ b/src/Packet.hpp @@ -801,4 +801,22 @@ struct PacketMultiBlockChange : Packet { int BlockId; }; std::vector<Record> Records; +}; + +struct PacketTimeUpdate : Packet { + void ToStream(StreamOutput *stream) override { + + } + + void FromStream(StreamInput *stream) override { + WorldAge = stream->ReadLong(); + TimeOfDay = stream->ReadLong(); + } + + int GetPacketId() override { + return PacketNamePlayCB::TimeUpdate; + } + + long long WorldAge; + long long TimeOfDay; };
\ No newline at end of file diff --git a/src/Render.cpp b/src/Render.cpp index a175278..56541e9 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -5,7 +5,7 @@ #include "AssetManager.hpp" #include "Event.hpp" -Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) : timer(std::chrono::milliseconds(16)) { +Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) : timer(std::chrono::milliseconds(0)) { InitSfml(windowWidth, windowHeight, windowTitle); glCheckError(); InitGlew(); @@ -47,20 +47,21 @@ void Render::InitGlew() { glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(GL_CCW); - //glEnable(GL_BLEND); - //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glLineWidth(3.0); glCheckError(); } void Render::PrepareToRendering() { //TextureAtlas texture glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, AssetManager::Instance().GetTextureAtlas()); + glBindTexture(GL_TEXTURE_2D, AssetManager::Instance().GetTextureAtlas()); AssetManager::Instance().GetTextureAtlasIndexes(); } void Render::RenderFrame() { - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (renderWorld) diff --git a/src/Renderer.hpp b/src/Renderer.hpp index cd0d7b0..53f6cc8 100644 --- a/src/Renderer.hpp +++ b/src/Renderer.hpp @@ -9,12 +9,13 @@ public: void SetActiveVao(GLuint Vao); void SetActiveShader(GLuint Shader); unsigned int WindowWidth, WindowHeight; + long long TimeOfDay; }; -struct Renderer { +/*struct Renderer { virtual ~Renderer() = default; virtual void Render(RenderState& renderState) = 0; virtual void PrepareResources() = 0; virtual void PrepareRender() = 0; virtual bool IsNeedResourcesPrepare() = 0; -};
\ No newline at end of file +};*/
\ No newline at end of file diff --git a/src/RendererEntity.cpp b/src/RendererEntity.cpp index 6a92cec..0bc2606 100644 --- a/src/RendererEntity.cpp +++ b/src/RendererEntity.cpp @@ -147,20 +147,7 @@ void RendererEntity::Render(RenderState & renderState) { glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glUniform3f(colorLoc, entity.renderColor.x, entity.renderColor.y, entity.renderColor.z); glCheckError(); - glDrawArrays(GL_TRIANGLES, 0, 36); + glDrawArrays(GL_LINE_STRIP, 0, 36); glCheckError(); -} - -void RendererEntity::PrepareResources() -{ -} - -void RendererEntity::PrepareRender() -{ -} - -bool RendererEntity::IsNeedResourcesPrepare() -{ - return false; -} +}
\ No newline at end of file diff --git a/src/RendererEntity.hpp b/src/RendererEntity.hpp index 23fd029..578e24b 100644 --- a/src/RendererEntity.hpp +++ b/src/RendererEntity.hpp @@ -8,17 +8,14 @@ #include "Entity.hpp" #include "World.hpp" -class RendererEntity :Renderer { +class RendererEntity { unsigned int entityId; World *world; public: RendererEntity(World *ptr, unsigned int id); ~RendererEntity(); - void Render(RenderState& renderState) override; - void PrepareResources() override; - void PrepareRender() override; - bool IsNeedResourcesPrepare() override; + void Render(RenderState& renderState); GLint modelLoc = 0; GLint colorLoc = 0; diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index 609333c..0965453 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -32,15 +32,21 @@ RendererSection::~RendererSection() { refCounterVbo[VboTextures]--; refCounterVbo[VboModels]--; refCounterVbo[VboColors]--; + refCounterVbo[VboLights]--; refCounterVao[Vao]--; + if (refCounterVbo[VboTextures] <= 0) glDeleteBuffers(1, &VboTextures); if (refCounterVbo[VboModels] <= 0) glDeleteBuffers(1, &VboTextures); + if (refCounterVbo[VboColors] <= 0) glDeleteBuffers(1, &VboColors); + if (refCounterVbo[VboLights] <= 0) + glDeleteBuffers(1, &VboLights); + if (refCounterVao[Vao] <= 0) glDeleteVertexArrays(1, &Vao); } @@ -93,6 +99,11 @@ RendererSection::RendererSection(RendererSectionData data) { refCounterVbo[VboColors] = 0; refCounterVbo[VboColors]++; + glGenBuffers(1, &VboLights); + if (refCounterVbo.find(VboLights) == refCounterVbo.end()) + refCounterVbo[VboLights] = 0; + refCounterVbo[VboLights]++; + glGenVertexArrays(1, &Vao); if (refCounterVao.find(Vao) == refCounterVao.end()) refCounterVao[Vao] = 0; @@ -108,43 +119,50 @@ RendererSection::RendererSection(RendererSectionData data) { //Cube UVs GLuint UvAttribPos = 2; -glBindBuffer(GL_ARRAY_BUFFER, VboUvs); -glVertexAttribPointer(UvAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); -glEnableVertexAttribArray(UvAttribPos); - -//Textures -GLuint textureAttribPos = 7; -glBindBuffer(GL_ARRAY_BUFFER, VboTextures); -glVertexAttribPointer(textureAttribPos, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr); -glEnableVertexAttribArray(textureAttribPos); -glVertexAttribDivisor(textureAttribPos, 1); -glCheckError(); - -//Blocks models -GLuint matAttribPos = 8; -size_t sizeOfMat4 = 4 * 4 * sizeof(GLfloat); -glBindBuffer(GL_ARRAY_BUFFER, VboModels); -glVertexAttribPointer(matAttribPos + 0, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, nullptr); -glVertexAttribPointer(matAttribPos + 1, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *)(1 * 4 * sizeof(GLfloat))); -glVertexAttribPointer(matAttribPos + 2, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *)(2 * 4 * sizeof(GLfloat))); -glVertexAttribPointer(matAttribPos + 3, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *)(3 * 4 * sizeof(GLfloat))); -glEnableVertexAttribArray(matAttribPos + 0); -glEnableVertexAttribArray(matAttribPos + 1); -glEnableVertexAttribArray(matAttribPos + 2); -glEnableVertexAttribArray(matAttribPos + 3); -glVertexAttribDivisor(matAttribPos + 0, 1); -glVertexAttribDivisor(matAttribPos + 1, 1); -glVertexAttribDivisor(matAttribPos + 2, 1); -glVertexAttribDivisor(matAttribPos + 3, 1); - -//Color -GLuint colorAttribPos = 12; -glBindBuffer(GL_ARRAY_BUFFER, VboColors); -glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr); -glEnableVertexAttribArray(colorAttribPos); -glVertexAttribDivisor(colorAttribPos, 1); - -glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, VboUvs); + glVertexAttribPointer(UvAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); + glEnableVertexAttribArray(UvAttribPos); + + //Textures + GLuint textureAttribPos = 7; + glBindBuffer(GL_ARRAY_BUFFER, VboTextures); + glVertexAttribPointer(textureAttribPos, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr); + glEnableVertexAttribArray(textureAttribPos); + glVertexAttribDivisor(textureAttribPos, 1); + glCheckError(); + + //Blocks models + GLuint matAttribPos = 8; + size_t sizeOfMat4 = 4 * 4 * sizeof(GLfloat); + glBindBuffer(GL_ARRAY_BUFFER, VboModels); + glVertexAttribPointer(matAttribPos + 0, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, nullptr); + glVertexAttribPointer(matAttribPos + 1, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *)(1 * 4 * sizeof(GLfloat))); + glVertexAttribPointer(matAttribPos + 2, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *)(2 * 4 * sizeof(GLfloat))); + glVertexAttribPointer(matAttribPos + 3, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *)(3 * 4 * sizeof(GLfloat))); + glEnableVertexAttribArray(matAttribPos + 0); + glEnableVertexAttribArray(matAttribPos + 1); + glEnableVertexAttribArray(matAttribPos + 2); + glEnableVertexAttribArray(matAttribPos + 3); + glVertexAttribDivisor(matAttribPos + 0, 1); + glVertexAttribDivisor(matAttribPos + 1, 1); + glVertexAttribDivisor(matAttribPos + 2, 1); + glVertexAttribDivisor(matAttribPos + 3, 1); + + //Color + GLuint colorAttribPos = 12; + glBindBuffer(GL_ARRAY_BUFFER, VboColors); + glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr); + glEnableVertexAttribArray(colorAttribPos); + glVertexAttribDivisor(colorAttribPos, 1); + + //Light + GLuint lightAttribPos = 13; + glBindBuffer(GL_ARRAY_BUFFER, VboLights); + glVertexAttribPointer(lightAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); + glEnableVertexAttribArray(lightAttribPos); + glVertexAttribDivisor(lightAttribPos, 1); + + glBindBuffer(GL_ARRAY_BUFFER, 0); } glBindVertexArray(0); glCheckError(); @@ -160,6 +178,9 @@ glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, VboColors); glBufferData(GL_ARRAY_BUFFER, data.colors.size() * sizeof(glm::vec3), data.colors.data(), GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, VboLights); + glBufferData(GL_ARRAY_BUFFER, data.lights.size() * sizeof(glm::vec2), data.lights.data(), GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); numOfFaces = data.textures.size(); @@ -171,6 +192,7 @@ RendererSection::RendererSection(const RendererSection &other) { this->VboModels = other.VboModels; this->VboTextures = other.VboTextures; this->VboColors = other.VboColors; + this->VboLights = other.VboLights; this->sectionPos = other.sectionPos; this->Vao = other.Vao; this->numOfFaces = other.numOfFaces; @@ -179,6 +201,7 @@ RendererSection::RendererSection(const RendererSection &other) { refCounterVbo[VboTextures]++; refCounterVbo[VboModels]++; refCounterVbo[VboColors]++; + refCounterVbo[VboLights]++; refCounterVao[Vao]++; } @@ -193,7 +216,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) for (int x = 0; x < 16; x++) { Block block = section.GetBlock(Vector(x, y, z)); if (block.id == 0) - continue; + continue; auto testBlockNonExist = [&](Vector block) -> bool { Vector offset; @@ -216,28 +239,22 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) offset = Vector(0, 0, 1); block.z = 0; } - if (offset != Vector(0, 0, 0)) { + if (offset != Vector(0, 0, 0)) { if (std::find(sectionsList.begin(), sectionsList.end(), sectionPosition + offset) == sectionsList.end()) return true; const Section& blockSection = world->GetSection(sectionPosition + offset); return blockSection.GetBlock(block).id == 0 || blockSection.GetBlock(block).id == 31 || blockSection.GetBlock(block).id == 18; } return section.GetBlock(block).id == 0 || section.GetBlock(block).id == 31 || section.GetBlock(block).id == 18; - /*return block.x == -1 || block.x == 16 || block.y == -1 || block.y == 16 || block.z == -1 || block.z == 16 || - section.GetBlock(block).id == 0 || section.GetBlock(block).id == 31 || section.GetBlock(block).id == 18;*/ }; unsigned char isVisible = 0; - /*if (x == 0 || x == 15 || y == 0 || y == 15 || z == 0 || z == 15) { - isVisible = 0b1111'1111; //All faces is visible - } else {*/ isVisible |= testBlockNonExist(Vector(x - 1, y, z)) << 0; isVisible |= testBlockNonExist(Vector(x + 1, y, z)) << 1; isVisible |= testBlockNonExist(Vector(x, y + 1, z)) << 2; isVisible |= testBlockNonExist(Vector(x, y - 1, z)) << 3; isVisible |= testBlockNonExist(Vector(x, y, z - 1)) << 4; isVisible |= testBlockNonExist(Vector(x, y, z + 1)) << 5; - //} if (isVisible == 0x00) continue; @@ -255,6 +272,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) for (int i = 0; i < 4; i++) { textures.push_back(texture->second); colors.push_back(color); + lights.push_back(glm::vec2(block.light, block.sky)); } glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0.15f, 0, 0.15f)); faceTransform = glm::scale(faceTransform, glm::vec3(1.0f, 0.9f, 1.0f)); @@ -280,6 +298,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690, 0.0078125, 0.00442477876106194690)); //Fallback TNT texture colors.push_back(color); + lights.push_back(glm::vec2(block.light, block.sky)); } if (isVisible >> 1 & 0x1) { //west side X- glm::mat4 faceTransform = glm::translate(transform, glm::vec3(1, 0, 0)); @@ -294,6 +313,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690, 0.0078125, 0.00442477876106194690)); //Fallback TNT texture colors.push_back(color); + lights.push_back(glm::vec2(block.light, block.sky)); } if (isVisible >> 2 & 0x1) { //Top side Y+ glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0, 1, 0)); @@ -308,6 +328,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) colors.push_back(color); else colors.push_back(biomeColor); + lights.push_back(glm::vec2(block.light, block.sky)); } if (isVisible >> 3 & 0x1) { //Bottom side Y- glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0, 0, 0)); @@ -321,6 +342,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690, 0.0078125, 0.00442477876106194690)); //Fallback TNT texture colors.push_back(color); + lights.push_back(glm::vec2(block.light, block.sky)); } if (isVisible >> 4 & 0x1) { //south side Z+ glm::mat4 faceTransform = glm::translate(transform, glm::vec3(1, 0, 0)); @@ -334,6 +356,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690, 0.0078125, 0.00442477876106194690)); //Fallback TNT texture colors.push_back(color); + lights.push_back(glm::vec2(block.light, block.sky)); } if (isVisible >> 5 & 0x1) { //north side Z- glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0, 0, 1)); @@ -350,6 +373,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690, 0.0078125, 0.00442477876106194690)); //Fallback TNT texture colors.push_back(color); + lights.push_back(glm::vec2(block.light, block.sky)); } } } @@ -359,4 +383,9 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) textures.shrink_to_fit(); models.shrink_to_fit(); colors.shrink_to_fit(); + + /*for (auto& it : lights) { + it.x = 8; + it.y = 16; + }*/ } diff --git a/src/RendererSection.hpp b/src/RendererSection.hpp index fe5e96b..9e5fd99 100644 --- a/src/RendererSection.hpp +++ b/src/RendererSection.hpp @@ -17,6 +17,7 @@ struct RendererSectionData { std::vector<glm::mat4> models; std::vector<glm::vec4> textures; std::vector<glm::vec3> colors; + std::vector<glm::vec2> lights; size_t hash; Vector sectionPos; @@ -24,7 +25,7 @@ struct RendererSectionData { }; class RendererSection { - GLuint Vao, VboTextures, VboModels, VboColors; + GLuint Vao, VboTextures, VboModels, VboColors, VboLights; static GLuint VboVertices, VboUvs; static std::map<GLuint, int> refCounterVbo; diff --git a/src/RendererSky.cpp b/src/RendererSky.cpp new file mode 100644 index 0000000..e0e367b --- /dev/null +++ b/src/RendererSky.cpp @@ -0,0 +1,140 @@ +#include "RendererSky.hpp" + +const GLfloat vertices[] = { + //Z+ edge + -0.5f, -0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, + + //Z- edge + -0.5f, 0.5f, -0.5f, + -0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, + -0.5f, 0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, + 0.5f, 0.5f, -0.5f, + + //X+ edge + -0.5f, -0.5f, 0.5f, + -0.5f, -0.5f, -0.5f, + -0.5f, 0.5f, -0.5f, + -0.5f, -0.5f, 0.5f, + -0.5f, 0.5f, -0.5f, + -0.5f, 0.5f, 0.5f, + + //X- edge + 0.5f, 0.5f, -0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, + 0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, 0.5f, -0.5f, + + //Y+ edge + -0.5f, 0.5f, 0.5f, + 0.5f, 0.5f, -0.5f, + 0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, -0.5f, + 0.5f, 0.5f, -0.5f, + -0.5f, 0.5f, 0.5f, + + //Y- edge + 0.5f, -0.5f, -0.5f, + -0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, -0.5f, + -0.5f, -0.5f, -0.5f, + -0.5f, -0.5f, 0.5f, +}; + +const GLfloat uv_coords[] = { + //Z+ + 0.0f, 1.0f, + 0.0f, 0.0f, + 1.0f, 0.0f, + 0.0f, 1.0f, + 1.0f, 0.0f, + 1.0f, 1.0f, + + //Z- + 1.0f, 0.0f, + 1.0f, 1.0f, + 0.0f, 0.0f, + 0.0f, 0.0f, + 1.0f, 1.0f, + 0.0f, 1.0f, + + //X+ + 0.0f, 0.0f, + 1.0f, 0.0f, + 0.0f, 1.0f, + 0.0f, 1.0f, + 1.0f, 0.0f, + 1.0f, 1.0f, + + //X- + 0.0f, 0.0f, + 1.0f, 1.0f, + 0.0f, 1.0f, + 0.0f, 0.0f, + 1.0f, 0.0f, + 1.0f, 1.0f, + + //Y+ + 0.0f, 0.0f, + 1.0f, 1.0f, + 0.0f, 1.0f, + 0.0f, 0.0f, + 1.0f, 0.0f, + 1.0f, 1.0f, + + //Y- + 1.0f, 0.0f, + 0.0f, 1.0f, + 0.0f, 0.0f, + 1.0f, 1.0f, + 0.0f, 1.0f, + 1.0f, 0.0f, +}; + +RendererSky::RendererSky() { + glGenBuffers(1, &VboVert); + glBindBuffer(GL_ARRAY_BUFFER, VboVert); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + + glGenBuffers(1, &VboUv); + glBindBuffer(GL_ARRAY_BUFFER, VboUv); + glBufferData(GL_ARRAY_BUFFER, sizeof(uv_coords), uv_coords, GL_STATIC_DRAW); + glGenVertexArrays(1, &Vao); + + glBindVertexArray(Vao); + { + glBindBuffer(GL_ARRAY_BUFFER, VboVert); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); + glEnableVertexAttribArray(0); + + glBindBuffer(GL_ARRAY_BUFFER, VboUv); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0); + glEnableVertexAttribArray(1); + } + glBindVertexArray(0); + glCheckError(); +} + +RendererSky::~RendererSky() +{ + glDeleteBuffers(1, &VboVert); + glDeleteBuffers(1, &VboUv); + glDeleteVertexArrays(1, &Vao); + //glCheckError(); +} + +void RendererSky::Render(RenderState &renderState) +{ + renderState.SetActiveVao(Vao); + glDrawArrays(GL_TRIANGLES, 0, 36); + glCheckError(); +} diff --git a/src/RendererSky.hpp b/src/RendererSky.hpp new file mode 100644 index 0000000..4dd0892 --- /dev/null +++ b/src/RendererSky.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include <glm/glm.hpp> + +#include "Utility.hpp" +#include "Shader.hpp" +#include "Renderer.hpp" +#include "AssetManager.hpp" + +class RendererSky { + GLuint VboVert, VboUv, Vao; +public: + RendererSky(); + ~RendererSky(); + void Render(RenderState &renderState); +};
\ No newline at end of file diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 194ddd8..9bd2ccf 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -10,7 +10,7 @@ void RendererWorld::WorkerFunction(size_t workerId) { Vector vec = data.Task; sectionsMutex.lock(); - auto& result = sections.find(vec); + auto result = sections.find(vec); if (result != sections.end()) { if (result->second.GetHash() != gs->world.GetSection(result->first).GetHash()) { sectionsMutex.unlock(); @@ -84,7 +84,7 @@ void RendererWorld::UpdateAllSections(VectorF playerPos) } RendererWorld::RendererWorld(std::shared_ptr<GameState> ptr):gs(ptr) { - MaxRenderingDistance = 2; + MaxRenderingDistance = 10; numOfWorkers = 4; PrepareRender(); @@ -94,7 +94,7 @@ RendererWorld::RendererWorld(std::shared_ptr<GameState> ptr):gs(ptr) { sectionsMutex.lock(); auto it = sections.find(vec); if (it == sections.end()) { - LOG(ERROR) << "Deleting wrong sectionRenderer"; + //LOG(ERROR) << "Deleting wrong sectionRenderer"; sectionsMutex.unlock(); return; } @@ -178,6 +178,8 @@ RendererWorld::RendererWorld(std::shared_ptr<GameState> ptr):gs(ptr) { for (int i = 0; i < numOfWorkers; i++) workers.push_back(std::thread(&RendererWorld::WorkerFunction, this, i)); + + //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); } RendererWorld::~RendererWorld() { @@ -193,9 +195,11 @@ RendererWorld::~RendererWorld() { workers[i].join(); delete blockShader; delete entityShader; + delete skyShader; } void RendererWorld::Render(RenderState & renderState) { + renderState.TimeOfDay = gs->TimeOfDay; renderState.SetActiveShader(blockShader->Program); glCheckError(); @@ -250,7 +254,7 @@ void RendererWorld::Render(RenderState & renderState) { renderState.SetActiveShader(entityShader->Program); glCheckError(); projectionLoc = glGetUniformLocation(entityShader->Program, "projection"); - viewLoc = glGetUniformLocation(entityShader->Program, "view"); + viewLoc = glGetUniformLocation(entityShader->Program, "view"); glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); glCheckError(); @@ -262,10 +266,55 @@ void RendererWorld::Render(RenderState & renderState) { it.Render(renderState); } -} -void RendererWorld::PrepareResources() { - LOG(ERROR) << "Incorrect call"; + + renderState.SetActiveShader(skyShader->Program); + projectionLoc = glGetUniformLocation(skyShader->Program, "projection"); + viewLoc = glGetUniformLocation(skyShader->Program, "view"); + glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); + glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); + glm::mat4 model = glm::mat4(); + model = glm::translate(model, gs->Position()); + const float scale = 1000000.0f; + model = glm::scale(model, glm::vec3(scale, scale, scale)); + float shift = gs->TimeOfDay / 24000.0f; + if (shift < 0) + shift *= -1.0f; + model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1.0f, 0.0f)); + model = glm::rotate(model, glm::radians(360.0f * shift), glm::vec3(-1.0f, 0.0f, 0.0f)); + modelLoc = glGetUniformLocation(skyShader->Program, "model"); + glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); + + glCheckError(); + + const int sunriseMin = 22000; + const int sunriseMax = 23500; + const int moonriseMin = 12000; + const int moonriseMax = 13500; + const float sunriseLength = sunriseMax - sunriseMin; + const float moonriseLength = moonriseMax - moonriseMin; + + float mixLevel=0; + int dayTime = gs->TimeOfDay; + if (dayTime < 0) + dayTime *= -1; + while (dayTime > 24000) + dayTime -= 24000; + if (dayTime > 0 && dayTime < moonriseMin || dayTime > sunriseMax) //day + mixLevel = 1.0; + if (dayTime > moonriseMax && dayTime < sunriseMin) //night + mixLevel = 0.0; + if (dayTime >= sunriseMin && dayTime <= sunriseMax) //sunrise + mixLevel = (dayTime - sunriseMin) / sunriseLength; + if (dayTime >= moonriseMin && dayTime <= moonriseMax) { //moonrise + float timePassed = (dayTime - moonriseMin); + mixLevel = 1.0 - (timePassed / moonriseLength); + } + + glUniform1f(glGetUniformLocation(skyShader->Program,"DayTime"), mixLevel); + + rendererSky.Render(renderState); + glCheckError(); } void RendererWorld::PrepareRender() { @@ -274,11 +323,18 @@ void RendererWorld::PrepareRender() { glUniform1i(glGetUniformLocation(blockShader->Program, "textureAtlas"), 0); entityShader = new Shader("./shaders/entity.vs", "./shaders/entity.fs"); -} -bool RendererWorld::IsNeedResourcesPrepare() { - LOG(ERROR) << "Incorrect call"; - return false; + skyShader = new Shader("./shaders/sky.vs", "./shaders/sky.fs"); + skyShader->Use(); + glUniform1i(glGetUniformLocation(skyShader->Program, "textureAtlas"), 0); + TextureCoordinates skyTexture = AssetManager::Instance().GetTextureByAssetName("minecraft/textures/entity/end_portal"); + glUniform4f(glGetUniformLocation(skyShader->Program, "skyTexture"),skyTexture.x,skyTexture.y,skyTexture.w,skyTexture.h); + TextureCoordinates sunTexture = AssetManager::Instance().GetTextureByAssetName("minecraft/textures/environment/sun"); + glUniform4f(glGetUniformLocation(skyShader->Program, "sunTexture"), sunTexture.x, sunTexture.y, sunTexture.w, sunTexture.h); + TextureCoordinates moonTexture = AssetManager::Instance().GetTextureByAssetName("minecraft/textures/environment/moon_phases"); + moonTexture.w /= 4.0f; //First phase will be fine for now + moonTexture.h /= 2.0f; + glUniform4f(glGetUniformLocation(skyShader->Program, "moonTexture"), moonTexture.x, moonTexture.y, moonTexture.w, moonTexture.h); } void RendererWorld::Update(double timeToUpdate) { diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index e7bcdf0..5bc575c 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -2,12 +2,13 @@ #include "RendererSection.hpp" #include "RendererEntity.hpp" +#include "RendererSky.hpp" #include "GameState.hpp" #include "Shader.hpp" #include <glm/gtc/type_ptr.hpp> -class RendererWorld: public Renderer { +class RendererWorld { //General std::shared_ptr<GameState> gs; EventListener listener; @@ -27,14 +28,18 @@ class RendererWorld: public Renderer { //Entities Shader *entityShader; std::vector<RendererEntity> entities; + //Sky + Texture *skyTexture; + Shader *skyShader; + RendererSky rendererSky; public: RendererWorld(std::shared_ptr<GameState> ptr); ~RendererWorld(); - void Render(RenderState& renderState) override; - void PrepareResources() override; - void PrepareRender() override; - bool IsNeedResourcesPrepare() override; + void Render(RenderState& renderState); + void PrepareResources(); + void PrepareRender(); + bool IsNeedResourcesPrepare(); double MaxRenderingDistance; diff --git a/src/Section.cpp b/src/Section.cpp index 3fb100f..7a9f947 100644 --- a/src/Section.cpp +++ b/src/Section.cpp @@ -9,69 +9,19 @@ Block &Section::GetBlock(Vector pos) { Block Section::GetBlock(Vector pos) const { + if (blocks.empty()) { + static Block fallback; + return fallback; + } return blocks[pos.y * 256 + pos.z * 16 + pos.x]; } double totalParsingTime = 0; -//void Section::Parse() { -// if (!m_blocks.empty()) -// return; -// -// long long *longArray = reinterpret_cast<long long *>(m_dataBlocks); -// for (size_t i = 0; i < m_dataBlocksLen / 8; i++) -// endswap(&longArray[i]); -// std::vector<unsigned short> blocks; -// blocks.reserve(4096); -// { -// auto begin = std::chrono::steady_clock::now(); -// int bitPos = 0; -// unsigned short t = 0; -// for (size_t i = 0; i < m_dataBlocksLen; i++) { -// for (int j = 0; j < 8; j++) { -// t |= (m_dataBlocks[i] & 0x01) ? 0x80 : 0x00; -// t >>= 1; -// m_dataBlocks[i] >>= 1; -// bitPos++; -// if (bitPos >= m_bitsPerBlock) { -// bitPos = 0; -// t >>= m_bitsPerBlock - 1; -// blocks.push_back(t); -// t = 0; -// } -// } -// } -// auto end = std::chrono::steady_clock::now(); -// std::chrono::duration<double, std::milli> time = end - begin; -// totalParsingTime += time.count(); -// } -// std::vector<byte> light; -// light.reserve(4096); -// for (int i = 0; i < 2048; i++) { -// byte t = m_dataLight[i]; -// byte first = t & 0b11110000; -// byte second = t >> 4; -// light.push_back(first); -// light.push_back(second); -// } -// for (int i = 0; i < 4096; i++) { -// unsigned short blockId = m_palette.size() > 0 ? m_palette[blocks[i]] : blocks[i]; -// Block block(blockId >> 4, blockId & 0xF); -// m_blocks.push_back(block); -// } -// delete[] m_dataBlocks; -// m_dataBlocksLen = 0; -// m_dataBlocks = nullptr; -// delete[] m_dataLight; -// m_dataLight = nullptr; -// delete[] m_dataSkyLight; -// m_dataSkyLight = nullptr; -// -// parseWaiter.notify_all(); -//} - Section::Section(PackedSection data) { + if (data.blocks.empty()) + return; worldPosition = data.position; long long *longArray = reinterpret_cast<long long *>(data.blocks.data()); @@ -105,14 +55,28 @@ Section::Section(PackedSection data) light.reserve(4096); for (int i = 0; i < 2048; i++) { byte t = data.light[i]; - byte first = t & 0b11110000; - byte second = t >> 4; - light.push_back(first); - light.push_back(second); + byte first = t & 0xF; + byte second = (t >> 4) & 0xF; + light.push_back(0); + light.push_back(0); } + + std::vector<byte> sky; + if (!data.sky.empty()) { + sky.reserve(4096); + for (int i = 0; i < 2048; i++) { + byte t = data.sky[i]; + byte first = t & 0xF; + byte second = (t >> 4) & 0xF; + sky.push_back(first); + sky.push_back(0xF); + } + } + + for (int i = 0; i < 4096; i++) { unsigned short blockId = data.palette.size() > 0 ? data.palette[blocks[i]] : blocks[i]; - Block block(blockId >> 4, blockId & 0xF); + Block block(blockId >> 4, blockId & 0xF, light[i], sky.empty() ? 0 : sky[i]); this->blocks.push_back(block); } } @@ -130,7 +94,6 @@ void swap(Section &a, Section &b) { Section::Section(const Section &other) { worldPosition = other.worldPosition; this->blocks = other.blocks; - //std::copy(other.blocks.begin(), other.blocks.end(), std::back_inserter(blocks)); } Vector Section::GetPosition() const { diff --git a/src/Section.hpp b/src/Section.hpp index 5422cf3..c7deeb1 100644 --- a/src/Section.hpp +++ b/src/Section.hpp @@ -28,6 +28,8 @@ struct PackedSection { PackedSection(Vector position, byte *dataBlocks, size_t dataBlocksLength, byte *dataLight, byte *dataSky, byte bitsPerBlock, std::vector<unsigned short> palette); + + PackedSection() = default; }; class Section { diff --git a/src/ThreadGame.cpp b/src/ThreadGame.cpp index ef7a95e..5e1d710 100644 --- a/src/ThreadGame.cpp +++ b/src/ThreadGame.cpp @@ -25,6 +25,8 @@ void ThreadGame::Execute() { listener.RegisterHandler(EventType::Disconnected, [this](EventData eventData) { std::this_thread::sleep_for(std::chrono::milliseconds(500)); + if (!gs) + return; gs->gs.reset(); gs.reset(); }); diff --git a/src/World.cpp b/src/World.cpp index 740c276..c79d1b7 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -130,12 +130,16 @@ std::vector<Vector> World::GetSectionsList() { return sectionsList; } +static Section fallbackSection = Section(PackedSection()); + const Section &World::GetSection(Vector sectionPos) { auto result = cachedSections.find(sectionPos); if (result == cachedSections.end()) { auto it = sections.find(sectionPos); - if (it == sections.end()) - LOG(ERROR) << "BAD"; + if (it == sections.end()) { + LOG(ERROR) << "Accessed not loaded section " << sectionPos; + return fallbackSection; + } Section section(it->second); auto result = cachedSections.insert(std::make_pair(sectionPos, section)); return result.first->second; @@ -216,7 +220,7 @@ void World::DeleteEntity(unsigned int EntityId) void World::ParseChunkData(std::shared_ptr<PacketBlockChange> packet) { Block& block = this->GetBlock(packet->Position); - block = Block(packet->BlockId >> 4, packet->BlockId & 15); + block = Block(packet->BlockId >> 4, packet->BlockId & 15, block.light, block.sky); Vector sectionPos(std::floor(packet->Position.x / 16.0), std::floor(packet->Position.y / 16.0), std::floor(packet->Position.z / 16.0)); EventAgregator::PushEvent(EventType::ChunkChanged, ChunkChangedData{ sectionPos }); } @@ -229,7 +233,7 @@ void World::ParseChunkData(std::shared_ptr<PacketMultiBlockChange> packet) { int z = (it.HorizontalPosition & 15) + (packet->ChunkZ * 16); Vector worldPos(x, y, z); Block& block = GetBlock(worldPos); - block = Block(it.BlockId >> 4, it.BlockId & 15); + block = Block(it.BlockId >> 4, it.BlockId & 15, block.light, block.sky); Vector sectionPos(packet->ChunkX, std::floor(it.YCoordinate / 16.0), packet->ChunkZ); if (std::find(changedSections.begin(), changedSections.end(), sectionPos) == changedSections.end()) |