diff options
Diffstat (limited to 'graphics')
-rw-r--r-- | graphics/AssetManager.cpp | 33 | ||||
-rw-r--r-- | graphics/AssetManager.hpp | 2 | ||||
-rw-r--r-- | graphics/Camera3D.hpp | 2 | ||||
-rw-r--r-- | graphics/Display.cpp | 102 | ||||
-rw-r--r-- | graphics/Display.hpp | 3 |
5 files changed, 112 insertions, 30 deletions
diff --git a/graphics/AssetManager.cpp b/graphics/AssetManager.cpp index 62dac64..1840c63 100644 --- a/graphics/AssetManager.cpp +++ b/graphics/AssetManager.cpp @@ -73,6 +73,39 @@ std::string AssetManager::GetPathToAsset(std::string AssetName) { assetTypeFileExtensions.at(instance().assets[AssetName].type); } +std::string AssetManager::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 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: diff --git a/graphics/AssetManager.hpp b/graphics/AssetManager.hpp index b2ff570..c7ef81a 100644 --- a/graphics/AssetManager.hpp +++ b/graphics/AssetManager.hpp @@ -46,5 +46,7 @@ public: static Asset &GetAsset(std::string AssetName); static void LoadAsset(std::string AssetName); + + static std::string GetAssetNameByBlockId(unsigned short id); }; diff --git a/graphics/Camera3D.hpp b/graphics/Camera3D.hpp index 084462f..eac1f47 100644 --- a/graphics/Camera3D.hpp +++ b/graphics/Camera3D.hpp @@ -20,7 +20,7 @@ enum Camera_Movement { // Default camera values const GLfloat YAW = -90.0f; const GLfloat PITCH = 0.0f; -const GLfloat SPEED = 3.0f; +const GLfloat SPEED = 30.0f; const GLfloat SENSITIVTY = 0.2f; const GLfloat ZOOM = 45.0f; diff --git a/graphics/Display.cpp b/graphics/Display.cpp index 380a8ec..ff2d6a6 100644 --- a/graphics/Display.cpp +++ b/graphics/Display.cpp @@ -1,3 +1,4 @@ +#include <iomanip> #include "Display.hpp" #include "AssetManager.hpp" @@ -28,18 +29,27 @@ bool Display::IsClosed() { } void Display::SetPlayerPos(double playerX, double playerY, double playerZ) { - const int ChunkDistance = 2; - PositionI playerPos((int) playerX, (int) playerZ, (int) playerY); + camera.Position = glm::vec3(playerX, playerY, playerZ); + const float 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 delta = it.first-playerPos; - + 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.vs", "./shaders/simple.fs"); - Texture &texture1 = *(AssetManager::GetAsset("minecraft/textures/blocks/brick").data.texture); - Texture &texture2 = *(AssetManager::GetAsset("minecraft/textures/blocks/beacon").data.texture); GLfloat vertices[] = { -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, @@ -109,26 +119,6 @@ void Display::MainLoop() { shader.Use(); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture1.texture); - glUniform1i(glGetUniformLocation(shader.Program, "texture1"), 0); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, texture2.texture); - glUniform1i(glGetUniformLocation(shader.Program, "texture2"), 1); - - glm::vec3 cubePositions[] = { - glm::vec3(0.0f, 0.0f, 0.0f), - glm::vec3(2.0f, 5.0f, -15.0f), - glm::vec3(-1.5f, -2.2f, -2.5f), - glm::vec3(-3.8f, -2.0f, -12.3f), - glm::vec3(2.4f, -0.4f, -3.5f), - glm::vec3(-1.7f, 3.0f, -7.5f), - glm::vec3(1.3f, -2.0f, -2.5f), - glm::vec3(1.5f, 2.0f, -2.5f), - glm::vec3(1.5f, 0.2f, -1.5f), - glm::vec3(-1.3f, 1.0f, -1.5f) - }; - Camera3D camera; bool captureMouse = true; bool isRunning = true; @@ -171,6 +161,12 @@ void Display::MainLoop() { 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); @@ -196,21 +192,71 @@ void Display::MainLoop() { 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++) { + /*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)); + //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 §ionPos:toRender) { + Section §ion = 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); diff --git a/graphics/Display.hpp b/graphics/Display.hpp index c656f2f..314ef5d 100644 --- a/graphics/Display.hpp +++ b/graphics/Display.hpp @@ -12,7 +12,8 @@ class Display { sf::Window *window; World* world; - std::vector<Section*> toRender; + std::vector<PositionI> toRender; + Camera3D camera; public: Display(unsigned int winWidth, unsigned int winHeight, const char winTitle[9], World *worldPtr); |