diff options
Diffstat (limited to 'src/graphics')
-rw-r--r-- | src/graphics/AssetManager_old.cpp | 146 | ||||
-rw-r--r-- | src/graphics/AssetManager_old.hpp | 53 | ||||
-rw-r--r-- | src/graphics/Display.cpp | 353 | ||||
-rw-r--r-- | src/graphics/Display.hpp | 33 |
4 files changed, 0 insertions, 585 deletions
diff --git a/src/graphics/AssetManager_old.cpp b/src/graphics/AssetManager_old.cpp deleted file mode 100644 index ef856ca..0000000 --- a/src/graphics/AssetManager_old.cpp +++ /dev/null @@ -1,146 +0,0 @@ -#include "AssetManager_old.hpp" - -const std::string pathToAssets = "./assets/"; -const std::string pathToObjects = pathToAssets + "objects/"; -const std::string pathToIndexFile = pathToAssets + "indexes/1.11.json"; -const std::string pathToAssetsMc = "./assets/"; -const std::map<Asset::AssetType, std::string> assetTypeFileExtensions{ - std::make_pair(Asset::AssetType::Texture, ".png"), - std::make_pair(Asset::AssetType::Lang, ".lang"), - std::make_pair(Asset::AssetType::Sound, ".ogg"), -}; - -AssetManager_old::AssetManager_old() { - return; - std::ifstream indexFile(pathToIndexFile); - if (!indexFile) { - std::cerr << "Can't open file " << pathToIndexFile << std::endl; - } - nlohmann::json json = nlohmann::json::parse(indexFile)["objects"]; - for (auto it = json.begin(); it != json.end(); ++it) { - size_t fileNameExtensionPos = -1; - std::string name = it.key(); - Asset::AssetType type = Asset::Unknown; - for (auto &it:assetTypeFileExtensions) { - if ((fileNameExtensionPos = name.find(it.second)) != std::string::npos) { - type = it.first; - name = name.substr(0, fileNameExtensionPos); - break; - } - } - std::string hash = it.value()["hash"].get<std::string>(); - size_t size = it.value()["size"].get<int>(); - Asset asset{name, hash, Asset::AssetData(), size, type}; - this->assets[name] = asset; - } -} - -AssetManager_old::~AssetManager_old() { - -} - -Asset &AssetManager_old::GetAsset(std::string AssetName) { - if (instance().assets.find(AssetName) == instance().assets.end() || !instance().assets[AssetName].isParsed()) - LoadAsset(AssetName); - return instance().assets[AssetName]; -} - -void AssetManager_old::LoadAsset(std::string AssetName) { - if (instance().assets.find(AssetName) != instance().assets.end() && instance().assets[AssetName].isParsed()) - return; - std::string AssetFileName = GetPathToAsset(AssetName); - Asset &asset = instance().assets[AssetName]; - - - if (asset.type == Asset::Texture) { - asset.data.texture = new Texture(AssetFileName,GL_CLAMP_TO_BORDER,GL_NEAREST); - //asset.data.texture.loadFromFile((asset.name + assetTypeFileExtensions.at(asset.type))); - } -} - -std::string AssetManager_old::GetPathToAsset(std::string AssetName) { - if (instance().assets.find(AssetName) != instance().assets.end()){ - auto it = instance().assets.find(AssetName); - return pathToObjects + std::string(instance().assets[AssetName].hash.c_str(), 2) + "/" + - instance().assets[AssetName].hash; - } - - instance().assets[AssetName].hash=""; - instance().assets[AssetName].type=Asset::AssetType::Texture; - instance().assets[AssetName].name=AssetName; - instance().assets[AssetName].size=0; - return pathToAssetsMc + "" + instance().assets[AssetName].name + - assetTypeFileExtensions.at(instance().assets[AssetName].type); -} - -std::string AssetManager_old::GetAssetNameByBlockId(unsigned short id) { - std::string assetBase = "minecraft/textures/blocks/"; - std::string textureName; - switch (id){ - case 0: - textureName="air"; - break; - case 1: - textureName="stone"; - break; - case 2: - textureName="grass"; - break; - case 3: - textureName="dirt"; - break; - case 4: - textureName="cobblestone"; - break; - case 16: - textureName="coal_ore"; - break; - case 17: - textureName="log_oak"; - break; - case 31: - textureName="air"; - break; - default: - //std::cout<<id<<std::endl; - textureName="beacon"; - break; - } - return assetBase+textureName; -} - -bool Asset::isParsed() { - switch (type) { - case Unknown: - return false; - break; - case Texture: - return this->data.texture != nullptr; - break; - case Sound: - return false; - break; - case Model: - return false; - break; - case Lang: - return false; - break; - } -} - -Asset::~Asset() { - switch (type) { - case Unknown: - break; - case Texture: - delete this->data.texture; - break; - case Sound: - break; - case Model: - break; - case Lang: - break; - } -} diff --git a/src/graphics/AssetManager_old.hpp b/src/graphics/AssetManager_old.hpp deleted file mode 100644 index 23a11a7..0000000 --- a/src/graphics/AssetManager_old.hpp +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once - -#include <fstream> -#include <string> -#include <map> -#include <experimental/filesystem> -#include <nlohmann/json.hpp> -#include "Texture.hpp" - -struct Asset { - std::string name = ""; - std::string hash = ""; - union AssetData{ - Texture *texture; - } data; - size_t size = 0; - enum AssetType { - Unknown, - Texture, - Sound, - Model, - Lang, - } type = Unknown; - bool isParsed(); - ~Asset(); -}; - -class AssetManager_old { - AssetManager_old(); - - ~AssetManager_old(); - - AssetManager_old(const AssetManager_old &); - - AssetManager_old &operator=(const AssetManager_old &); - - std::map<std::string, Asset> assets; - - static AssetManager_old &instance() { - static AssetManager_old assetManager; - return assetManager; - } - - static std::string GetPathToAsset(std::string AssetName); -public: - - static Asset &GetAsset(std::string AssetName); - - static void LoadAsset(std::string AssetName); - - static std::string GetAssetNameByBlockId(unsigned short id); -}; - diff --git a/src/graphics/Display.cpp b/src/graphics/Display.cpp deleted file mode 100644 index 63498fa..0000000 --- a/src/graphics/Display.cpp +++ /dev/null @@ -1,353 +0,0 @@ -#include <iomanip> -#include "Display.hpp" -#include "AssetManager_old.hpp" - -/*GLfloat vertices[] = { - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, - - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - - -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f -}; -GLuint indices[] = { - 0, 1, 2, - 0, 2, 3 -};*/ -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, -}; - - -Display::Display(unsigned int winWidth, unsigned int winHeight, const char *winTitle, World *worldPtr) : world( - worldPtr) { - sf::ContextSettings contextSetting; - contextSetting.majorVersion = 3; - contextSetting.minorVersion = 3; - contextSetting.attributeFlags = contextSetting.Core; - contextSetting.depthBits = 24; - window = new sf::Window(sf::VideoMode(winWidth, winHeight), winTitle, sf::Style::Default, contextSetting); - window->setVerticalSyncEnabled(true); - window->setMouseCursorVisible(false); - window->setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width / 2 - window->getSize().x/2, - sf::VideoMode::getDesktopMode().height / 2 - window->getSize().y/2)); - - sf::Mouse::setPosition(sf::Vector2i(window->getSize().x / 2, window->getSize().y / 2), *window); - - - //Glew - glewExperimental = GL_TRUE; - if (glewInit() != GLEW_OK) { - std::cout << "Failed to initialize GLEW" << std::endl; - throw 3; - } - glViewport(0, 0, width(), height()); - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); - glFrontFace(GL_CCW); -} - -bool Display::IsClosed() { - return !window->isOpen(); -} - -void Display::SetPlayerPos(double playerX, double playerY, double playerZ) { - camera.Position = glm::vec3(playerX, playerY, playerZ); - toRender.clear(); - const float ChunkDistance = 1; - Vector playerChunk = Vector(floor(playerX / 16.0f), floor(playerY / 16.0f), floor(playerZ / 16.0f)); - for (auto &it:world->m_sections) { - Vector chunkPosition = it.first; - Vector delta = chunkPosition - playerChunk; - //std::cout << delta.GetDistance() << std::endl; - if (delta.GetDistance() > ChunkDistance) - continue; - /*std::cout << "Rendering " << delta.GetDistance() << " Detailed: " << delta.GetX() << " " << delta.GetZ() << " " - << delta.GetY() << std::endl << - "\t" << chunkPosition.GetX() << " " << chunkPosition.GetZ() << " " - << chunkPosition.GetY() << std::endl;*/ - toRender.push_back(chunkPosition); - } - std::cout << "Chunks to render: " << toRender.size() << std::endl; -} - -void Display::MainLoop() { - Shader shader("./shaders/simple.vs", "./shaders/simple.fs"); - - GLuint VBO, VAO, VBO2; - glGenBuffers(1, &VBO); - glGenBuffers(1, &VBO2); - glGenVertexArrays(1, &VAO); - - glBindVertexArray(VAO); - { - glBindBuffer(GL_ARRAY_BUFFER, VBO2); - glBufferData(GL_ARRAY_BUFFER, sizeof(uv_coords), uv_coords, GL_STATIC_DRAW); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0); - glEnableVertexAttribArray(2); - - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0); - glEnableVertexAttribArray(0); - } - glBindVertexArray(0); - - shader.Use(); - - bool captureMouse = true; - - //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - - bool isRunning = true; - while (isRunning) { - static sf::Clock clock, clock1; - float deltaTime = clock.getElapsedTime().asSeconds(); - float absTime = clock1.getElapsedTime().asSeconds(); - clock.restart(); - sf::Event event; - /*static sf::Clock clock2; - if (clock2.getElapsedTime().asSeconds() > 5.0f) { - clock2.restart(); - SetPlayerPos(camera.Position.x, camera.Position.y, camera.Position.z); - }*/ - while (window->pollEvent(event)) { - switch (event.type) { - case sf::Event::Closed: - isRunning = false; - break; - case sf::Event::Resized: - glViewport(0, 0, width(), height()); - break; - case sf::Event::KeyPressed: - switch (event.key.code) { - case sf::Keyboard::Escape: - isRunning = false; - break; - case sf::Keyboard::T: - captureMouse = !captureMouse; - window->setMouseCursorVisible(!captureMouse); - sf::Mouse::setPosition(sf::Vector2i(window->getSize().x / 2, window->getSize().y / 2), - *window); - break; - case sf::Keyboard::R: - shader.Reload(); - break; - default: - break; - } - case sf::Event::MouseWheelScrolled: - camera.ProcessMouseScroll(event.mouseWheelScroll.delta); - break; - default: - break; - } - } - std::ostringstream toWindow; - glm::highp_vec3 cameraPosition(camera.Position); - toWindow << std::setprecision(2) << std::fixed << "Pos: " << cameraPosition.x << ", " << cameraPosition.y - << ", " << cameraPosition.z << "; "; - toWindow << "FPS: " << (1.0f / deltaTime) << " "; - window->setTitle(toWindow.str()); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) - camera.ProcessKeyboard(Camera_Movement::FORWARD, deltaTime); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) - camera.ProcessKeyboard(Camera_Movement::BACKWARD, deltaTime); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) - camera.ProcessKeyboard(Camera_Movement::LEFT, deltaTime); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) - camera.ProcessKeyboard(Camera_Movement::RIGHT, deltaTime); - if (captureMouse) { - sf::Vector2i mousePos = sf::Mouse::getPosition(*window); - sf::Vector2i center = sf::Vector2i(window->getSize().x / 2, window->getSize().y / 2); - sf::Mouse::setPosition(center, *window); - int deltaX = (mousePos - center).x, deltaY = (center - mousePos).y; - camera.ProcessMouseMovement(deltaX, deltaY); - } - - //Render code - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - shader.Use(); - - 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 timeLoc = glGetUniformLocation(shader.Program, "time"); - glm::mat4 projection = glm::perspective(camera.Zoom, (float) width() / (float) height(), 0.1f, 1000.0f); - glm::mat4 view = camera.GetViewMatrix(); - glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); - glUniform1f(timeLoc, absTime); - - glBindVertexArray(VAO); - - for (auto §ionPos:toRender) { - Section §ion = world->m_sections.find(sectionPos)->second; - for (int y = 0; y < 16; y++) { - for (int z = 0; z < 16; z++) { - for (int x = 0; x < 16; x++) { - glm::mat4 model; - model = glm::translate(model, - glm::vec3(sectionPos.GetX() * 16, sectionPos.GetY() * 16, - sectionPos.GetZ() * 16)); - model = glm::translate(model, glm::vec3(x, y, z)); - - Block block = section.GetBlock(Vector(x, y, z)); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); - glUniform1i(blockLoc, block.id); - - std::string textureName = AssetManager_old::GetAssetNameByBlockId(block.id); - if (textureName.find("air") != std::string::npos) - continue; - Texture &texture1 = *(AssetManager_old::GetAsset(textureName).data.texture); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture1.texture); - glUniform1i(glGetUniformLocation(shader.Program, "blockTexture"), 0); - - glDrawArrays(GL_TRIANGLES, 0, 36); - } - } - } - } - glBindVertexArray(0); - - //End of render code - - window->display(); - } - -} diff --git a/src/graphics/Display.hpp b/src/graphics/Display.hpp deleted file mode 100644 index 7458bcd..0000000 --- a/src/graphics/Display.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include <SFML/Window.hpp> -#include "../world/World.hpp" -#include <glm/glm.hpp> -#include <glm/gtc/matrix_transform.hpp> -#include <glm/gtc/type_ptr.hpp> -#include "Shader.hpp" -#include "Texture.hpp" -#include "Camera3D.hpp" - -class Display { - sf::Window *window; - World* world; - std::vector<Vector> toRender; - Camera3D camera; -public: - Display(unsigned int winWidth, unsigned int winHeight, const char winTitle[9], World *worldPtr); - - bool IsClosed(); - - void SetPlayerPos(double playerX, double playerY, double playerZ); - - void MainLoop(); - - unsigned int width() { - return window->getSize().x; - } - - unsigned int height() { - return window->getSize().y; - } -};
\ No newline at end of file |