From 1fd6058eafb2c47ec190889481131a1961a1e1b2 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Sat, 21 Mar 2015 18:44:08 +0100 Subject: [PATCH] Finshed work now sorting objects after loading. --- game/chunk.cc | 6 +++--- game/chunk.hh | 2 +- game/graphics.cc | 38 ++++++++++++++++++++++---------------- game/level.cc | 8 ++++---- game/level.hh | 2 +- game/material.cc | 11 ----------- game/material.hh | 4 ---- 7 files changed, 31 insertions(+), 40 deletions(-) diff --git a/game/chunk.cc b/game/chunk.cc index 87f6374..cb57cfe 100644 --- a/game/chunk.cc +++ b/game/chunk.cc @@ -20,14 +20,14 @@ void Chunk::addObject(Object* object) { objects.push_back(object); } -void Chunk::sortObjects(int materialCount) { +void Chunk::sortObjects(int textureCount) { // init - sortedObjects = std::vector>(materialCount); + sortedObjects = std::vector>(textureCount); for(unsigned int i = 0; i(); } for(unsigned int i = 0; igetMaterial()->getMaterialId()).push_back(objects.at(i)); + sortedObjects.at(objects.at(i)->getMaterial()->getTextureUnit() - 2).push_back(objects.at(i)); } } diff --git a/game/chunk.hh b/game/chunk.hh index 1739f45..8184fc2 100644 --- a/game/chunk.hh +++ b/game/chunk.hh @@ -12,7 +12,7 @@ class Chunk { void render(SharedShaderProgram shader, bool lightingPass, bool texturePass, glm::mat4* viewProjcetionMatrix, std::vector* additionalMatrices=0); void addObject(Object* object); - void sortObjects(int textureUnits); + void sortObjects(int textureCount); std::vector>* getSortedObjects(); private: std::vector objects; diff --git a/game/graphics.cc b/game/graphics.cc index 7c93229..8f63d5e 100644 --- a/game/graphics.cc +++ b/game/graphics.cc @@ -198,9 +198,9 @@ void Graphics::init(Level* level) { lightingShader->setUniform("fogColorNight", level->getFogColourNight()); lightingShader->setUniform("ambientColor", level->getAmbientLight()); - level->sortObjects(Material::getAllMaterials()->size()); + level->sortObjects(Material::getAllTextures()->size()); #ifdef SAXUM_DEBUG - std::cout << "There were " << Material::getAllMaterials()->size() + std::cout << "There were " << Material::getAllTextures()->size() << " materials used in this level." << std::endl; #endif } @@ -494,26 +494,32 @@ void Graphics::render(double time) // render the level level->enqueueObjects(this); for (unsigned int i = 0; isize(); i++) { - Material* material = &Material::getAllTextures()->at(i); - if (material->isMoving()) { - lightingShader->setUniform("movingTexture", true); - } - else { - lightingShader->setUniform("movingTexture", false); - } - lightingShader->setUniform("uTexture", material->getTextureUnit()); - lightingShader->setUniform("ambientFactor", material->getAmbientFactor()); - lightingShader->setUniform("diffuseFactor", material->getDiffuseFactor()); - lightingShader->setUniform("specularFactor", material->getSpecularFactor()); - lightingShader->setUniform("shininess", material->getShininess()); + bool parametersSet = false; for(unsigned int j = 0; jat(i).size(); k++) { + if(renderQueue.at(j)->at(i).size() != 0) { + if (!parametersSet) { + parametersSet = true; + Material* material = renderQueue.at(j)->at(i).at(0)->getMaterial(); + if (material->isMoving()) { + lightingShader->setUniform("movingTexture", true); + } + else { + lightingShader->setUniform("movingTexture", false); + } + lightingShader->setUniform("uTexture", material->getTextureUnit()); + lightingShader->setUniform("ambientFactor", material->getAmbientFactor()); + lightingShader->setUniform("diffuseFactor", material->getDiffuseFactor()); + lightingShader->setUniform("specularFactor", material->getSpecularFactor()); + lightingShader->setUniform("shininess", material->getShininess()); + } + for(unsigned int k = 0; kat(i).size(); k++) { renderQueue.at(j)->at(i).at(k)->render(lightingShader, true, false, &lightingViewProjectionMatrix, &depthBiasVPs); - } + } } } } } + renderQueue.clear(); if (renderDebug) { debugDrawer.setDebugMode(btIDebugDraw::DBG_DrawWireframe); diff --git a/game/level.cc b/game/level.cc index 71666b0..20e9443 100644 --- a/game/level.cc +++ b/game/level.cc @@ -136,19 +136,19 @@ void Level::enqueueObjects(Graphics* graphics) { graphics->enqueueObjects(&sortedCrossChunkObjects); } -void Level::sortObjects(int materialCount) { +void Level::sortObjects(int textureCount) { for(unsigned int i = 0; i>(materialCount); + sortedCrossChunkObjects = std::vector>(textureCount); for(unsigned int i = 0; i(); } for(unsigned int i = 0; igetMaterial()->getMaterialId()) + sortedCrossChunkObjects.at(crossChunkObjects.at(i)->getMaterial()->getTextureUnit() - 2) .push_back(crossChunkObjects.at(i)); } } diff --git a/game/level.hh b/game/level.hh index 183d8a2..69b8635 100644 --- a/game/level.hh +++ b/game/level.hh @@ -81,7 +81,7 @@ class Level { std::vector>* getChunks(); void addToSpecificChunk(Object* object, int xPosition, int zPosition); void enqueueObjects(Graphics* graphics); - void sortObjects(int materialCount); + void sortObjects(int textureCount); private: lua_State* luaState=nullptr; std::vector crossChunkObjects; diff --git a/game/material.cc b/game/material.cc index 248ac71..9f16e96 100644 --- a/game/material.cc +++ b/game/material.cc @@ -2,8 +2,6 @@ std::set Material::allTexturesSet = std::set(); std::vector Material::allTexturesVector = std::vector(); -std::set Material::allMaterialsSet = std::set(); -std::vector Material::allMaterialsVector = std::vector(); Material::Material(std::string filePath, float ambientFactor, float diffuseFactor, float specularFactor, float shininess, bool movingTexture) { @@ -25,15 +23,6 @@ Material::Material(std::string filePath, float ambientFactor, float diffuseFacto std::find(std::begin(*Material::getAllTextures()), // first two texture units are used by the loading screen std::end(*Material::getAllTextures()), reference)) + 2; - - unsigned int materialCount = allMaterialsSet.size(); - allMaterialsSet.insert(*this); - if (allMaterialsSet.size() != materialCount) { - allMaterialsVector.push_back(*this); - } - materialId = std::distance(Material::getAllMaterials()->begin(), - std::find(std::begin(*Material::getAllMaterials()), - std::end(*Material::getAllMaterials()), *this)); } Material::Material() { diff --git a/game/material.hh b/game/material.hh index 59f6696..0adc91a 100644 --- a/game/material.hh +++ b/game/material.hh @@ -24,11 +24,9 @@ class Material{ static std::vector* getAllTextures(); static std::vector* getAllMaterials(); int getTextureUnit(); - int getMaterialId(); private: int textureUnit; - int materialId; ACGL::OpenGL::SharedTexture2D reference; float ambientFactor; float diffuseFactor; @@ -37,8 +35,6 @@ class Material{ bool movingTexture; static std::set allTexturesSet; static std::vector allTexturesVector; - static std::set allMaterialsSet; - static std::vector allMaterialsVector; }; #endif