From a07a4bbb91bc5bd058b4069f64d14812393962b1 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Sat, 21 Mar 2015 15:05:22 +0100 Subject: [PATCH] Commiting unfinished work. --- game/chunk.cc | 21 +++++++++++++++------ game/chunk.hh | 4 +++- game/graphics.cc | 46 ++++++++++++++++++++++++++-------------------- game/graphics.hh | 3 ++- game/level.cc | 22 +++++++++++++++++++--- game/level.hh | 7 ++++++- game/material.cc | 15 +++++++++++++-- game/material.hh | 5 +++++ 8 files changed, 89 insertions(+), 34 deletions(-) diff --git a/game/chunk.cc b/game/chunk.cc index 7d20fb4..87f6374 100644 --- a/game/chunk.cc +++ b/game/chunk.cc @@ -16,12 +16,21 @@ void Chunk::render(SharedShaderProgram shader, bool lightingPass, bool texturePa } } -void Chunk::enqueueObjects(std::vector>* renderQueue) { - for(unsigned int i = 0; iat(objects.at(i)->getMaterial()->getTextureUnit() - 2).push_back(objects.at(i)); - } -} - void Chunk::addObject(Object* object) { objects.push_back(object); } + +void Chunk::sortObjects(int materialCount) { + // init + sortedObjects = std::vector>(materialCount); + for(unsigned int i = 0; i(); + } + for(unsigned int i = 0; igetMaterial()->getMaterialId()).push_back(objects.at(i)); + } +} + +std::vector>* Chunk::getSortedObjects() { + return &sortedObjects; +} diff --git a/game/chunk.hh b/game/chunk.hh index f6d0510..1739f45 100644 --- a/game/chunk.hh +++ b/game/chunk.hh @@ -12,7 +12,9 @@ class Chunk { void render(SharedShaderProgram shader, bool lightingPass, bool texturePass, glm::mat4* viewProjcetionMatrix, std::vector* additionalMatrices=0); void addObject(Object* object); - void enqueueObjects(std::vector>* renderQueue); + void sortObjects(int textureUnits); + std::vector>* getSortedObjects(); private: std::vector objects; + std::vector> sortedObjects; }; diff --git a/game/graphics.cc b/game/graphics.cc index 2897768..7c93229 100644 --- a/game/graphics.cc +++ b/game/graphics.cc @@ -198,10 +198,11 @@ void Graphics::init(Level* level) { lightingShader->setUniform("fogColorNight", level->getFogColourNight()); lightingShader->setUniform("ambientColor", level->getAmbientLight()); - renderQueue = std::vector>(Material::getAllTextures()->size()); - for (unsigned int i = 0; i(); - } + level->sortObjects(Material::getAllMaterials()->size()); + #ifdef SAXUM_DEBUG + std::cout << "There were " << Material::getAllMaterials()->size() + << " materials used in this level." << std::endl; + #endif } void Graphics::bindTextureUnits(){ @@ -491,23 +492,24 @@ void Graphics::render(double time) if (renderWorld) { // render the level - level->enqueueObjects(&renderQueue); - for (unsigned int i = 0; igetMaterial(); - if (material->isMoving()) { - lightingShader->setUniform("movingTexture", true); + 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()); + for(unsigned int j = 0; jat(i).size(); k++) { + renderQueue.at(j)->at(i).at(k)->render(lightingShader, true, false, &lightingViewProjectionMatrix, &depthBiasVPs); } - 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 j = 0; jrender(lightingShader, true, false, &lightingViewProjectionMatrix, &depthBiasVPs); } } } @@ -750,3 +752,7 @@ void Graphics::setRenderWorld(bool state) { bool Graphics::getRenderWorld() { return renderWorld; } + +void Graphics::enqueueObjects(std::vector>* queue){ + renderQueue.push_back(queue); +} diff --git a/game/graphics.hh b/game/graphics.hh index ea5ad44..e0f3fab 100644 --- a/game/graphics.hh +++ b/game/graphics.hh @@ -33,6 +33,7 @@ class Graphics { bool getRenderFlames(); bool getRenderDebug(); bool getRenderWorld(); + void enqueueObjects(std::vector>* queue); private: void bindTextureUnits(); void updateLights(); @@ -88,7 +89,7 @@ class Graphics { SharedArrayBuffer debug_ab; SharedVertexArrayObject debug_vao; SharedShaderProgram debugShader; - std::vector> renderQueue; + std::vector>*> renderQueue; }; #endif diff --git a/game/level.cc b/game/level.cc index 83e1158..71666b0 100644 --- a/game/level.cc +++ b/game/level.cc @@ -1,6 +1,7 @@ #include "level.hh" #include "loader.hh" #include +#include "graphics.hh" Level::Level(std::string xmlFilePath) { // default value @@ -101,7 +102,7 @@ void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, } } -void Level::enqueueObjects(std::vector>* renderQueue) { +void Level::enqueueObjects(Graphics* graphics) { int renderDistance = 0; if ((int)farPlane % chunkSize == 0) { renderDistance = (((int)skydomeSize)+chunkSize/2)/chunkSize; @@ -129,11 +130,26 @@ void Level::enqueueObjects(std::vector>* renderQueue) { } for(unsigned int i = xStart; i<=xEnd; i++) { for(unsigned int j = zStart; j<=zEnd; j++) { - chunks.at(i).at(j).enqueueObjects(renderQueue); + graphics->enqueueObjects(chunks.at(i).at(j).getSortedObjects()); } } + graphics->enqueueObjects(&sortedCrossChunkObjects); +} + +void Level::sortObjects(int materialCount) { + for(unsigned int i = 0; i>(materialCount); + for(unsigned int i = 0; i(); + } for(unsigned int i = 0; iat(crossChunkObjects.at(i)->getMaterial()->getTextureUnit() - 2).push_back(crossChunkObjects.at(i)); + sortedCrossChunkObjects.at(crossChunkObjects.at(i)->getMaterial()->getMaterialId()) + .push_back(crossChunkObjects.at(i)); } } diff --git a/game/level.hh b/game/level.hh index bc3980a..183d8a2 100644 --- a/game/level.hh +++ b/game/level.hh @@ -21,6 +21,9 @@ extern "C" { } #include "LuaBridge.h" +// forward declaration +class Graphics; + class Level { public: Level(std::string xmlFilePath); @@ -77,10 +80,12 @@ class Level { void generateChunks(int chunkSize); std::vector>* getChunks(); void addToSpecificChunk(Object* object, int xPosition, int zPosition); - void enqueueObjects(std::vector>* renderQueue); + void enqueueObjects(Graphics* graphics); + void sortObjects(int materialCount); private: lua_State* luaState=nullptr; std::vector crossChunkObjects; + std::vector> sortedCrossChunkObjects; std::vector allObjects; std::vector physicsObjects; std::vector> chunks; diff --git a/game/material.cc b/game/material.cc index 63cbc21..248ac71 100644 --- a/game/material.cc +++ b/game/material.cc @@ -2,6 +2,8 @@ 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) { @@ -14,15 +16,24 @@ Material::Material(std::string filePath, float ambientFactor, float diffuseFacto this->specularFactor = specularFactor; this->shininess = shininess; this->movingTexture = movingTexture; - unsigned int count = allTexturesSet.size(); + unsigned int textureCount = allTexturesSet.size(); allTexturesSet.insert(reference); - if (allTexturesSet.size() != count) { + if (allTexturesSet.size() != textureCount) { allTexturesVector.push_back(reference); } textureUnit = std::distance(Material::getAllTextures()->begin(), 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 cc9fc5d..59f6696 100644 --- a/game/material.hh +++ b/game/material.hh @@ -22,10 +22,13 @@ class Material{ float getShininess(); bool isMoving(); 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; @@ -34,6 +37,8 @@ class Material{ bool movingTexture; static std::set allTexturesSet; static std::vector allTexturesVector; + static std::set allMaterialsSet; + static std::vector allMaterialsVector; }; #endif