Finshed work now sorting objects after loading.

This commit is contained in:
Faerbit 2015-03-21 18:44:08 +01:00
parent a07a4bbb91
commit 1fd6058eaf
7 changed files with 31 additions and 40 deletions

View File

@ -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<std::vector<Object*>>(materialCount);
sortedObjects = std::vector<std::vector<Object*>>(textureCount);
for(unsigned int i = 0; i<sortedObjects.size(); i++) {
sortedObjects.at(i) = std::vector<Object*>();
}
for(unsigned int i = 0; i<objects.size(); i++){
sortedObjects.at(objects.at(i)->getMaterial()->getMaterialId()).push_back(objects.at(i));
sortedObjects.at(objects.at(i)->getMaterial()->getTextureUnit() - 2).push_back(objects.at(i));
}
}

View File

@ -12,7 +12,7 @@ class Chunk {
void render(SharedShaderProgram shader, bool lightingPass, bool texturePass,
glm::mat4* viewProjcetionMatrix, std::vector<glm::mat4>* additionalMatrices=0);
void addObject(Object* object);
void sortObjects(int textureUnits);
void sortObjects(int textureCount);
std::vector<std::vector<Object*>>* getSortedObjects();
private:
std::vector<Object*> objects;

View File

@ -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,7 +494,12 @@ void Graphics::render(double time)
// render the level
level->enqueueObjects(this);
for (unsigned int i = 0; i<Material::getAllTextures()->size(); i++) {
Material* material = &Material::getAllTextures()->at(i);
bool parametersSet = false;
for(unsigned int j = 0; j<renderQueue.size(); j++) {
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);
}
@ -506,7 +511,7 @@ void Graphics::render(double time)
lightingShader->setUniform("diffuseFactor", material->getDiffuseFactor());
lightingShader->setUniform("specularFactor", material->getSpecularFactor());
lightingShader->setUniform("shininess", material->getShininess());
for(unsigned int j = 0; j<renderQueue.size(); j++) {
}
for(unsigned int k = 0; k<renderQueue.at(j)->at(i).size(); k++) {
renderQueue.at(j)->at(i).at(k)->render(lightingShader, true, false, &lightingViewProjectionMatrix, &depthBiasVPs);
}
@ -514,6 +519,7 @@ void Graphics::render(double time)
}
}
}
renderQueue.clear();
if (renderDebug) {
debugDrawer.setDebugMode(btIDebugDraw::DBG_DrawWireframe);

View File

@ -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<chunks.size(); i++) {
for(unsigned int j = 0; j<chunks.at(i).size(); j++) {
chunks.at(i).at(j).sortObjects(materialCount);
chunks.at(i).at(j).sortObjects(textureCount);
}
}
// init
sortedCrossChunkObjects = std::vector<std::vector<Object*>>(materialCount);
sortedCrossChunkObjects = std::vector<std::vector<Object*>>(textureCount);
for(unsigned int i = 0; i<sortedCrossChunkObjects.size(); i++) {
sortedCrossChunkObjects.at(i) = std::vector<Object*>();
}
for(unsigned int i = 0; i<crossChunkObjects.size(); i++) {
sortedCrossChunkObjects.at(crossChunkObjects.at(i)->getMaterial()->getMaterialId())
sortedCrossChunkObjects.at(crossChunkObjects.at(i)->getMaterial()->getTextureUnit() - 2)
.push_back(crossChunkObjects.at(i));
}
}

View File

@ -81,7 +81,7 @@ class Level {
std::vector<std::vector<Chunk>>* 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<Object*> crossChunkObjects;

View File

@ -2,8 +2,6 @@
std::set<SharedTexture2D> Material::allTexturesSet = std::set<SharedTexture2D>();
std::vector<SharedTexture2D> Material::allTexturesVector = std::vector<SharedTexture2D>();
std::set<Material> Material::allMaterialsSet = std::set<Material>();
std::vector<Material> Material::allMaterialsVector = std::vector<Material*>();
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() {

View File

@ -24,11 +24,9 @@ class Material{
static std::vector<SharedTexture2D>* getAllTextures();
static std::vector<Material*>* 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<SharedTexture2D> allTexturesSet;
static std::vector<SharedTexture2D> allTexturesVector;
static std::set<Material> allMaterialsSet;
static std::vector<Material> allMaterialsVector;
};
#endif