From 3680abfba42ab7e4b35776ec10679d848e01f4cb Mon Sep 17 00:00:00 2001 From: Faerbit Date: Wed, 18 Mar 2015 16:26:20 +0100 Subject: [PATCH] Loaded all textures at the beginning of the application. Closes #7. --- game/graphics.cc | 37 ++++++++++++++++++++----------------- game/material.cc | 12 ++++++++++++ game/material.hh | 8 +++++++- game/object.cc | 5 ++++- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/game/graphics.cc b/game/graphics.cc index bbb8235..5c177c7 100644 --- a/game/graphics.cc +++ b/game/graphics.cc @@ -202,37 +202,40 @@ void Graphics::init(Level* level) { void Graphics::bindTextureUnits(){ lightingShader->use(); + unsigned int textureCount = Material::getAllTextures()->size(); + glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &number_of_texture_units); + printf("Your graphics card supports %d texture units.\n", number_of_texture_units); + // Exit if we need more texture units + if (number_of_texture_units < (int)textureCount + 18) { + printf("You need at least %d texture units to run this application. Exiting\n", textureCount + 18); + exit(-1); + } + for(unsigned int i = 0; isize(); i++) { + lightingShader->setTexture("uTexture", Material::getAllTextures()->at(i), i+2); + } for (unsigned int i = 0; isetTexture("shadowMap_directional" + std::to_string(i), depth_directionalMaps.at(i), i+1); + lightingShader->setTexture("shadowMap_directional" + std::to_string(i), depth_directionalMaps.at(i), textureCount + i + 2); } if (level->getLights()->size() > 0) { for(unsigned int i = 0; isetTexture("shadowMap_cube" + std::to_string(i), depth_cubeMaps.at(i), i+4); + lightingShader->setTexture("shadowMap_cube" + std::to_string(i), depth_cubeMaps.at(i), textureCount + i + 5); } } flamePostShader->use(); - flamePostShader->setTexture("light_fbo", light_fbo_color_texture, 14); + flamePostShader->setTexture("light_fbo", light_fbo_color_texture, textureCount + 15); skydomeShader->use(); - skydomeShader->setTexture("nightTexture", level->getSkydome()->getNightTexture()->getReference(), 15); + skydomeShader->setTexture("nightTexture", level->getSkydome()->getNightTexture()->getReference(), textureCount + 16); loadingShader->use(); - loadingShader->setTexture("screen", loadingScreen, 16); - loadingShader->setTexture("screenContinue", loadingContinueScreen, 17); + loadingShader->setTexture("screen", loadingScreen, textureCount + 17); + loadingShader->setTexture("screenContinue", loadingContinueScreen, textureCount + 18); + printf("This application used %d texture units.\n", textureCount + 18); } void Graphics::renderLoadingScreen() { - glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &number_of_texture_units); - printf("Your graphics card supports %d texture units.\n", number_of_texture_units); - // Exit if we need more texture units - if (number_of_texture_units < 18) { - printf("You need at least 18 texture units to run this application. Exiting\n"); - exit(-1); - } loadingScreen = Texture2DFileManager::the()->get(Texture2DCreator(loadingScreenPath)); loadingScreen->generateMipmaps(); loadingScreen->setMinFilter(GL_NEAREST_MIPMAP_LINEAR); @@ -286,8 +289,8 @@ void Graphics::renderLoadingScreen() { .attributeLocations(fullscreen_quad_loading->getAttributeLocations()).create(); loadingShader->use(); loadingShader->setUniform("time", 0.0f); - loadingShader->setTexture("screen", loadingScreen, 16); - loadingShader->setTexture("screenContinue", loadingContinueScreen, 17); + loadingShader->setTexture("screen", loadingScreen, 0); + loadingShader->setTexture("screenContinue", loadingContinueScreen, 1); fullscreen_quad_loading->render(); } diff --git a/game/material.cc b/game/material.cc index 40776a8..e70df5c 100644 --- a/game/material.cc +++ b/game/material.cc @@ -1,5 +1,8 @@ #include "material.hh" +std::set Material::allTexturesSet = std::set(); +std::vector Material::allTexturesVector = std::vector(); + Material::Material(std::string filePath, float ambientFactor, float diffuseFactor, float specularFactor, float shininess, bool movingTexture) { reference = ACGL::OpenGL::Texture2DFileManager::the()->get(ACGL::OpenGL::Texture2DCreator(filePath)); @@ -11,6 +14,11 @@ Material::Material(std::string filePath, float ambientFactor, float diffuseFacto this->specularFactor = specularFactor; this->shininess = shininess; this->movingTexture = movingTexture; + unsigned int count = allTexturesSet.size(); + allTexturesSet.insert(reference); + if (allTexturesSet.size() != count) { + allTexturesVector.push_back(reference); + } } Material::Material() { @@ -42,3 +50,7 @@ float Material::getShininess() { bool Material::isMoving(){ return movingTexture; } + +std::vector* Material::getAllTextures() { + return &allTexturesVector; +} diff --git a/game/material.hh b/game/material.hh index 4789ca8..40f224a 100644 --- a/game/material.hh +++ b/game/material.hh @@ -2,22 +2,26 @@ #define MATERIAL_HH_INCLUDED #include +#include #include #include #include +using namespace ACGL::OpenGL; + class Material{ public: Material(std::string filePath, float ambientFactor, float diffuseFactor, float specularFactor, float shininess, bool movingTexture = false); Material(); - ACGL::OpenGL::SharedTexture2D getReference(); + SharedTexture2D getReference(); ~Material(); float getAmbientFactor(); float getDiffuseFactor(); float getSpecularFactor(); float getShininess(); bool isMoving(); + static std::vector* getAllTextures(); private: ACGL::OpenGL::SharedTexture2D reference; @@ -26,6 +30,8 @@ class Material{ float specularFactor; float shininess; bool movingTexture; + static std::set allTexturesSet; + static std::vector allTexturesVector; }; #endif diff --git a/game/object.cc b/game/object.cc index ea09ddf..da834a2 100644 --- a/game/object.cc +++ b/game/object.cc @@ -1,4 +1,6 @@ #include "object.hh" +//#include +//#include Object::Object(Model model, Material material, glm::vec3 position, glm::vec3 rotation, bool renderable) : Entity(position, rotation) { @@ -27,7 +29,8 @@ void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, else { shader->setUniform("movingTexture", false); } - shader->setTexture("uTexture", material.getReference(), 0); + auto textureUnit = std::distance(Material::getAllTextures()->begin(), std::find(std::begin(*Material::getAllTextures()), std::end(*Material::getAllTextures()), material.getReference())); + shader->setUniform("uTexture", (int)textureUnit + 2); shader->setUniform("modelMatrix", modelMatrix); } if (lightingPass) {