Loaded all textures at the beginning of the application. Closes #7.

This commit is contained in:
Faerbit 2015-03-18 16:26:20 +01:00
parent 09282f51fd
commit 3680abfba4
4 changed files with 43 additions and 19 deletions

View File

@ -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; i<Material::getAllTextures()->size(); i++) {
lightingShader->setTexture("uTexture", Material::getAllTextures()->at(i), i+2);
}
for (unsigned int i = 0; i<depth_directionalMaps.size(); i++) {
// start with texture unit 1 because the first is reserved for the texture
lightingShader->setTexture("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; i<depth_cubeMaps.size(); i++){
// start with texture unit 4 because the first four are used by the texture and the directional shadow map
lightingShader->setTexture("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();
}

View File

@ -1,5 +1,8 @@
#include "material.hh"
std::set<SharedTexture2D> Material::allTexturesSet = std::set<SharedTexture2D>();
std::vector<SharedTexture2D> Material::allTexturesVector = std::vector<SharedTexture2D>();
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<SharedTexture2D>* Material::getAllTextures() {
return &allTexturesVector;
}

View File

@ -2,22 +2,26 @@
#define MATERIAL_HH_INCLUDED
#include <string>
#include <set>
#include <ACGL/OpenGL/Creator/Texture2DCreator.hh>
#include <ACGL/OpenGL/Data/TextureLoadStore.hh>
#include <ACGL/OpenGL/Managers.hh>
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<SharedTexture2D>* getAllTextures();
private:
ACGL::OpenGL::SharedTexture2D reference;
@ -26,6 +30,8 @@ class Material{
float specularFactor;
float shininess;
bool movingTexture;
static std::set<SharedTexture2D> allTexturesSet;
static std::vector<SharedTexture2D> allTexturesVector;
};
#endif

View File

@ -1,4 +1,6 @@
#include "object.hh"
//#include <iterator>
//#include <algorithm>
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) {