From b192dcec842cf043ae936c446bb252259b415ab5 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Wed, 7 Jun 2017 19:21:39 +0500 Subject: 2017-06-07 --- src/core/AssetManager.cpp | 19 +++++--- src/core/AssetManager.hpp | 8 +++- src/core/Core.cpp | 109 ++++++++++++++++++++-------------------------- 3 files changed, 68 insertions(+), 68 deletions(-) (limited to 'src/core') diff --git a/src/core/AssetManager.cpp b/src/core/AssetManager.cpp index eaa002f..bcf50bc 100644 --- a/src/core/AssetManager.cpp +++ b/src/core/AssetManager.cpp @@ -3,9 +3,11 @@ namespace fs = std::experimental::filesystem; -const fs::path pathToAssets = "./assets/"; -const fs::path pathToAssetsList = "./items.json"; -const fs::path pathToTextureIndex = "./textures.json"; +//const fs::path pathToAssets = "./assets/"; +//const fs::path pathToAssetsList = "./items.json"; +//const fs::path pathToTextureIndex = "./textures.json"; +const std::string pathToAssetsList = "./items.json"; +const std::string pathToTextureIndex = "./textures.json"; AssetManager::AssetManager() { LoadIds(); @@ -63,7 +65,7 @@ TextureCoordinates AssetManager::GetTextureByAssetName(std::string AssetName) { std::string AssetManager::GetTextureAssetNameByBlockId(BlockTextureId block) { //Block sides: 0 - bottom, 1 - top, 2 - north, 3 - south, 4 - west, 5 - east 6 - every side - std::map lookupTable = { + const std::map lookupTable = { {BlockTextureId(0, 0), "minecraft/textures/blocks/air"}, {BlockTextureId(1, 0), "minecraft/textures/blocks/stone"}, {BlockTextureId(1, 1), "minecraft/textures/blocks/stone_granite"}, @@ -78,7 +80,11 @@ std::string AssetManager::GetTextureAssetNameByBlockId(BlockTextureId block) { {BlockTextureId(3, 0), "minecraft/textures/blocks/dirt"}, {BlockTextureId(4, 0), "minecraft/textures/blocks/cobblestone"}, }; - return lookupTable[block]; + auto ret = lookupTable.find(block); + if (ret == lookupTable.end()) + return ""; + else + return ret->second; } GLuint AssetManager::GetTextureAtlas() { @@ -86,5 +92,6 @@ GLuint AssetManager::GetTextureAtlas() { } TextureCoordinates AssetManager::GetTextureByBlock(BlockTextureId block) { - return this->GetTextureByAssetName(this->GetTextureAssetNameByBlockId(block)); + std::string assetName = this->GetTextureAssetNameByBlockId(block); + return this->GetTextureByAssetName(assetName); } diff --git a/src/core/AssetManager.hpp b/src/core/AssetManager.hpp index b378764..ebb339f 100644 --- a/src/core/AssetManager.hpp +++ b/src/core/AssetManager.hpp @@ -33,12 +33,18 @@ struct BlockTextureId { int state:4; int side:3; + + bool operator<(const BlockTextureId &rhs) const { if (id < rhs.id) return true; if (rhs.id < id) return false; - return state < rhs.state; + if (state < rhs.state) + return true; + if (rhs.state < state) + return false; + return side < rhs.side; } }; diff --git a/src/core/Core.cpp b/src/core/Core.cpp index ade043e..3257cb6 100644 --- a/src/core/Core.cpp +++ b/src/core/Core.cpp @@ -185,7 +185,7 @@ const GLfloat uv_coords[] = { Core::Core() { LOG(INFO) << "Core initializing..."; - InitSfml(1280, 720, "AltCraft"); + InitSfml(900, 450, "AltCraft"); glCheckError(); InitGlew(); glCheckError(); @@ -275,7 +275,8 @@ void Core::InitSfml(unsigned int WinWidth, unsigned int WinHeight, std::string W contextSetting.depthBits = 24; window = new sf::Window(sf::VideoMode(WinWidth, WinHeight), WinTitle, sf::Style::Default, contextSetting); glCheckError(); - window->setVerticalSyncEnabled(true); + //window->setVerticalSyncEnabled(true); + //window->setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width / 2, sf::VideoMode::getDesktopMode().height / 2)); window->setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width / 2 - window->getSize().x / 2, sf::VideoMode::getDesktopMode().height / 2 - window->getSize().y / 2)); @@ -377,7 +378,8 @@ void Core::RenderWorld() { GLint modelLoc = glGetUniformLocation(shader->Program, "model"); GLint projectionLoc = glGetUniformLocation(shader->Program, "projection"); GLint viewLoc = glGetUniformLocation(shader->Program, "view"); - GLint blockLoc = glGetUniformLocation(shader->Program, "block"); + GLint blockLoc = glGetUniformLocation(shader->Program, "Block"); + GLint stateLoc = glGetUniformLocation(shader->Program, "State"); GLint timeLoc = glGetUniformLocation(shader->Program, "time"); glm::mat4 projection = glm::perspective(camera.Zoom, (float) width() / (float) height(), 0.1f, 10000000.0f); glm::mat4 view = camera.GetViewMatrix(); @@ -389,7 +391,7 @@ void Core::RenderWorld() { glBindVertexArray(VAO); - for (auto §ionPos:toRender) { + for (auto §ionPos : toRender) { Section §ion = gameState->world.m_sections.find(sectionPos)->second; for (int y = 0; y < 16; y++) { for (int z = 0; z < 16; z++) { @@ -405,6 +407,7 @@ void Core::RenderWorld() { glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glUniform1i(blockLoc, block.id); + glUniform1i(stateLoc, block.state); glDrawArrays(GL_TRIANGLES, 0, 36); } @@ -457,22 +460,24 @@ void Core::PrepareToWorldRendering() { std::vector textureCoordinates; std::vector indexes; GLint totalTextures; - for (int id = 0; id < 4096; id++) { + for (int id = 1; id < 4096; id++) { bool isReachedEnd = true; for (int state = 0; state < 16; state++) { - if (!assetManager->GetTextureByBlock(BlockTextureId(id, state, 6)) || + BlockTextureId blockTextureId(id, state, 6); + if (!assetManager->GetTextureByBlock(blockTextureId) && !assetManager->GetTextureByBlock(BlockTextureId(id, state, 0))) { continue; } isReachedEnd = false; - int side = assetManager->GetTextureByBlock(BlockTextureId(id, state, 6)) ? 6 : 0; + int side = assetManager->GetTextureByBlock(blockTextureId) ? 6 : 0; do { int index = (side << 16) | (id << 4) | state; TextureCoordinates tc = assetManager->GetTextureByBlock(BlockTextureId(id, state, side)); textureCoordinates.push_back(glm::vec4(tc.x, tc.y, tc.w, tc.h)); indexes.push_back(index); - /*LOG(ERROR) << "Encoded (" << side << " " << id << " " << state << ") as " << index << " (" - << std::bitset<20>(index) << ")";*/ + /*LOG(ERROR) << "Encoded texture (" << id << " " << state << " " << side << ") as " << index << " (" + << std::bitset<19>(index) << ")" << " = " << tc.x << "," << tc.y << "," << tc.w << "," + << tc.h;*/ /*LOG(FATAL)<(index); side = 0x7; id = 0xFFF; @@ -484,20 +489,20 @@ void Core::PrepareToWorldRendering() { st = state; index = i | si | st; LOG(FATAL) << std::bitset<18>(index) << " (" << index << "): " << std::bitset<18>(si) << " " - << std::bitset<18>(i) << " " << std::bitset<18>(st);*/ + << std::bitset<18>(i) << " " << std::bitset<18>(st);*/ /*if (rand() == 73) //Almost impossible(Almost==1/32768) { - int index = 393233; - LOG(WARNING) << std::bitset<20>(index) << "(" << index << ")"; - int side = (index & 0xE0000) >> 16; - int id = (index & 0xFF0) >> 4; - int state = index & 0xF; - LOG(WARNING) << std::bitset<20>(side) << " " << std::bitset<20>(id) << " " - << std::bitset<20>(state); - LOG(FATAL) << side << " " << id << " " << state; + int index = 393233; + LOG(WARNING) << std::bitset<20>(index) << "(" << index << ")"; + int side = (index & 0xE0000) >> 16; + int id = (index & 0xFF0) >> 4; + int state = index & 0xF; + LOG(WARNING) << std::bitset<20>(side) << " " << std::bitset<20>(id) << " " + << std::bitset<20>(state); + LOG(FATAL) << side << " " << id << " " << state; }*/ side++; - } while (side < 7); + } while (side < 6); } if (isReachedEnd) break; @@ -506,52 +511,45 @@ void Core::PrepareToWorldRendering() { totalTextures = indexes.size(); LOG(INFO) << "Created " << totalTextures << " texture indexes"; CHECK_EQ(indexes.size(), textureCoordinates.size()) << "Arrays of textureCoordinates and of indexes is not equals"; - CHECK_LE(totalTextures, 2048) << "There is more texture indexes, than GLSL buffer allows"; - - for (auto& it:indexes){ - LOG(WARNING)<Program, "TextureIndexes"); - glUniformBlockBinding(shader->Program, ubo, 0); + glUniformBlockBinding(shader->Program, ubo, bp1); glGenBuffers(1, &UBO); glBindBuffer(GL_UNIFORM_BUFFER, UBO); - glBufferData(GL_UNIFORM_BUFFER, indexes.size() * sizeof(GLint), NULL, GL_STATIC_DRAW); - glBindBufferRange(GL_UNIFORM_BUFFER, 0, UBO, 0, indexes.size() * sizeof(GLint)); - glBufferSubData(GL_UNIFORM_BUFFER, 0, indexes.size() * sizeof(GLint), &indexes[0]); + glBindBufferBase(GL_UNIFORM_BUFFER, bp1, UBO); + glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::vec4) + sizeof(glm::vec4) * 1023, NULL, GL_STATIC_DRAW); + glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GLint), &totalTextures); //copy totalTextures + for (int i = 0; i < indexes.size(); i++) { + size_t baseOffset = sizeof(glm::vec4); + size_t itemOffset = sizeof(glm::vec4); + size_t offset = baseOffset + i * itemOffset; + /*int index = indexes[i]; + int side = (index & 0x70000) >> 16; + int id = (index & 0xFF0) >> 4; + int state = index & 0xF; + LOG(WARNING) << "Copying " << indexes[i] << " at " << offset<<" side is "<Program, "TextureData"); - glUniformBlockBinding(shader->Program, ubo2, 1); + GLuint bp2 = 1; + GLuint ubo2_index = glGetUniformBlockIndex(shader->Program, "TextureData"); + glUniformBlockBinding(shader->Program, ubo2_index, bp2); glGenBuffers(1, &UBO2); glBindBuffer(GL_UNIFORM_BUFFER, UBO2); + glBindBufferBase(GL_UNIFORM_BUFFER, bp2, UBO2); glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::vec4) * 1024, NULL, GL_STATIC_DRAW); - glBindBufferRange(GL_UNIFORM_BUFFER, 1, UBO2, 0, 1024 * sizeof(glm::vec4)); - glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::vec4) * textureCoordinates.size(), textureCoordinates.data());*/ - - /* - GLuint ubo3 = glGetUniformBlockIndex(shader->Program, "TextureData2"); - glUniformBlockBinding(shader->Program, ubo3, 2); - glGenBuffers(1, &UBO3); - glBindBuffer(GL_UNIFORM_BUFFER, UBO3); - glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::vec4) * 1024, NULL, GL_STATIC_DRAW); - glBindBufferRange(GL_UNIFORM_BUFFER, 2, UBO3, 0, 1024 * sizeof(glm::vec4));*/ - - glBindBuffer(GL_UNIFORM_BUFFER,0); + glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::vec4) * textureCoordinates.size(), textureCoordinates.data()); + glBindBuffer(GL_UNIFORM_BUFFER, 0); glCheckError(); } void Core::UpdateChunksToRender() { camera.Position = glm::vec3(gameState->g_PlayerX, gameState->g_PlayerY, gameState->g_PlayerZ); toRender.clear(); - const float ChunkDistance = 1; + const float ChunkDistance = 2; Vector playerChunk = Vector(floor(gameState->g_PlayerX / 16.0f), floor(gameState->g_PlayerY / 16.0f), floor(gameState->g_PlayerZ / 16.0f)); for (auto &it:gameState->world.m_sections) { @@ -562,17 +560,6 @@ void Core::UpdateChunksToRender() { toRender.push_back(chunkPosition); } LOG(INFO) << "Chunks to render: " << toRender.size(); - - /*std::map totalBlocks; - for (auto §ion:toRender) - for (int x = 0; x < 16; x++) - for (int y = 0; y < 16; y++) - for (int z = 0; z < 16; z++) - totalBlocks[gameState->world.m_sections.find(section)->second.GetBlock(Vector(x, y, z))]++; - for (auto &it:totalBlocks) { - LOG(WARNING) << it.first.id << ":" << (int) it.first.state << " = " << it.second << " (" - << std::bitset<13>(it.first.id) << ")"; - }*/ } void Core::UpdateGameState() { -- cgit v1.2.3