summaryrefslogtreecommitdiffstats
path: root/code/graphics
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--code/graphics/AssetManager.hpp52
-rw-r--r--code/graphics/Display.cpp268
-rw-r--r--src/graphics/AssetManager.cpp (renamed from code/graphics/AssetManager.cpp)2
-rw-r--r--src/graphics/Camera3D.cpp (renamed from code/graphics/Camera3D.cpp)0
-rw-r--r--src/graphics/Camera3D.hpp (renamed from code/graphics/Camera3D.hpp)0
-rw-r--r--src/graphics/Display.hpp (renamed from code/graphics/Display.hpp)4
-rw-r--r--src/graphics/Shader.cpp (renamed from code/graphics/Shader.cpp)0
-rw-r--r--src/graphics/Shader.hpp (renamed from code/graphics/Shader.hpp)0
-rw-r--r--src/graphics/Texture.cpp (renamed from code/graphics/Texture.cpp)0
-rw-r--r--src/graphics/Texture.hpp (renamed from code/graphics/Texture.hpp)0
10 files changed, 3 insertions, 323 deletions
diff --git a/code/graphics/AssetManager.hpp b/code/graphics/AssetManager.hpp
deleted file mode 100644
index c7ef81a..0000000
--- a/code/graphics/AssetManager.hpp
+++ /dev/null
@@ -1,52 +0,0 @@
-#pragma once
-
-#include <fstream>
-#include <string>
-#include <map>
-#include "../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 {
- AssetManager();
-
- ~AssetManager();
-
- AssetManager(const AssetManager &);
-
- AssetManager &operator=(const AssetManager &);
-
- std::map<std::string, Asset> assets;
-
- static AssetManager &instance() {
- static AssetManager 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/code/graphics/Display.cpp b/code/graphics/Display.cpp
deleted file mode 100644
index a68c35b..0000000
--- a/code/graphics/Display.cpp
+++ /dev/null
@@ -1,268 +0,0 @@
-#include <iomanip>
-#include "Display.hpp"
-#include "AssetManager.hpp"
-
-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);
- 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);
-}
-
-bool Display::IsClosed() {
- return !window->isOpen();
-}
-
-void Display::SetPlayerPos(double playerX, double playerY, double playerZ) {
- camera.Position = glm::vec3(playerX, playerY, playerZ);
- const int ChunkDistance = 1;
- PositionI playerChunk = PositionI((int) playerX / 16, (int) playerZ / 16, (int) playerY / 16);
- /*std::cout << "Player chunk position: " << playerChunk.GetX() << " "
- << playerChunk.GetZ() << " " << playerChunk.GetY() << std::endl;*/
- for (auto &it:world->m_sections) {
- PositionI chunkPosition = it.first;
- PositionI delta = chunkPosition - playerChunk;
- 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(it.first);
- }
- std::cout << "Chunks to render: " << toRender.size() << std::endl;
-}
-
-void Display::MainLoop() {
- Shader shader("./shaders/simple.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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
- };
- GLuint VBO, VAO, EBO;
- glGenBuffers(1, &VBO);
- glGenBuffers(1, &EBO);
- glGenVertexArrays(1, &VAO);
-
- glBindVertexArray(VAO);
- {
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
-
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid *) (3 * sizeof(GLfloat)));
- glEnableVertexAttribArray(2);
- }
- glBindVertexArray(0);
-
- shader.Use();
-
- bool captureMouse = true;
-
- 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;
- while (window->pollEvent(event)) {
- switch (event.type) {
- case sf::Event::Closed:
- window->close();
- 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 (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);
- }
- 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);
-
-
- //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 (GLuint i = 0; i < 10; i++) {
- glm::mat4 model;
- glm::vec3 cubePositions[] = {
- glm::vec3(0, 0, 0),
- glm::vec3(0, 0, 1),
- glm::vec3(0, 0, 2),
- glm::vec3(1, 0, 0),
- glm::vec3(1, 0, 1),
- glm::vec3(1, 0, 2),
- glm::vec3(2, 0, 0),
- glm::vec3(2, 0, 1),
- glm::vec3(2, 0, 2),
- glm::vec3(3, 0, 3),
- };
- if (toRender.size()<1)
- continue;
- model = glm::translate(model,
- glm::vec3(toRender[0].GetX() * 16, toRender[0].GetZ() * 16,
- toRender[0].GetY() * 16));
- model = glm::translate(model, cubePositions[i]);
-
- GLfloat angle = 20.0f * (i);
- //model = glm::rotate(model, glm::radians(angle * absTime), glm::vec3(1.0f, 0.3f, 0.5f));
- glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
-
- glDrawArrays(GL_TRIANGLES, 0, 36);
- }*/
-
- for (auto &sectionPos:toRender) {
- Section &section = world->m_sections[sectionPos];
- 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(PositionI(x, z, y));
- glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
- glUniform1i(blockLoc, block.id);
-
- std::string textureName = AssetManager::GetAssetNameByBlockId(block.id);
- if (textureName.find("air") != std::string::npos)
- continue;
- Texture &texture1 = *(AssetManager::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/code/graphics/AssetManager.cpp b/src/graphics/AssetManager.cpp
index 1840c63..f2f756d 100644
--- a/code/graphics/AssetManager.cpp
+++ b/src/graphics/AssetManager.cpp
@@ -4,7 +4,6 @@ const std::string pathToAssets = "./assets/";
const std::string pathToObjects = pathToAssets + "objects/";
const std::string pathToIndexFile = pathToAssets + "indexes/1.11.json";
const std::string pathToAssetsMc = "./assetsMc/";
-
const std::map<Asset::AssetType, std::string> assetTypeFileExtensions{
std::make_pair(Asset::AssetType::Texture, ".png"),
std::make_pair(Asset::AssetType::Lang, ".lang"),
@@ -12,6 +11,7 @@ const std::map<Asset::AssetType, std::string> assetTypeFileExtensions{
};
AssetManager::AssetManager() {
+ return;
std::ifstream indexFile(pathToIndexFile);
if (!indexFile) {
std::cerr << "Can't open file " << pathToIndexFile << std::endl;
diff --git a/code/graphics/Camera3D.cpp b/src/graphics/Camera3D.cpp
index eb740e4..eb740e4 100644
--- a/code/graphics/Camera3D.cpp
+++ b/src/graphics/Camera3D.cpp
diff --git a/code/graphics/Camera3D.hpp b/src/graphics/Camera3D.hpp
index eac1f47..eac1f47 100644
--- a/code/graphics/Camera3D.hpp
+++ b/src/graphics/Camera3D.hpp
diff --git a/code/graphics/Display.hpp b/src/graphics/Display.hpp
index 314ef5d..7458bcd 100644
--- a/code/graphics/Display.hpp
+++ b/src/graphics/Display.hpp
@@ -1,7 +1,7 @@
#pragma once
#include <SFML/Window.hpp>
-#include "../World.hpp"
+#include "../world/World.hpp"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
@@ -12,7 +12,7 @@
class Display {
sf::Window *window;
World* world;
- std::vector<PositionI> toRender;
+ std::vector<Vector> toRender;
Camera3D camera;
public:
Display(unsigned int winWidth, unsigned int winHeight, const char winTitle[9], World *worldPtr);
diff --git a/code/graphics/Shader.cpp b/src/graphics/Shader.cpp
index c84e169..c84e169 100644
--- a/code/graphics/Shader.cpp
+++ b/src/graphics/Shader.cpp
diff --git a/code/graphics/Shader.hpp b/src/graphics/Shader.hpp
index 8178d2a..8178d2a 100644
--- a/code/graphics/Shader.hpp
+++ b/src/graphics/Shader.hpp
diff --git a/code/graphics/Texture.cpp b/src/graphics/Texture.cpp
index 0104530..0104530 100644
--- a/code/graphics/Texture.cpp
+++ b/src/graphics/Texture.cpp
diff --git a/code/graphics/Texture.hpp b/src/graphics/Texture.hpp
index 8e3f1af..8e3f1af 100644
--- a/code/graphics/Texture.hpp
+++ b/src/graphics/Texture.hpp