From fc2ce96771dbb8c57f31d4db3c5cb96c6f66330f Mon Sep 17 00:00:00 2001 From: Faerbit Date: Tue, 24 Mar 2015 19:50:26 +0100 Subject: [PATCH] Moving updateClosestLights() to level class. --- game/graphics.cc | 63 ++++++++++++++++-------------------------------- game/graphics.hh | 4 +-- game/level.cc | 32 +++++++++++++++++++++--- game/level.hh | 7 ++++-- 4 files changed, 56 insertions(+), 50 deletions(-) diff --git a/game/graphics.cc b/game/graphics.cc index b8654a6..0e87e44 100644 --- a/game/graphics.cc +++ b/game/graphics.cc @@ -180,7 +180,7 @@ void Graphics::init(Level* level) { bindTextureUnits(); - updateClosestLights(); + closestLights = level->getClosestLights(); // set shader variables that stay the same across the runtime of the application skydomeShader->use(); @@ -377,13 +377,13 @@ void Graphics::render(double time) framebuffer_cube->bind(); - for (unsigned int i_pointlight = 0; i_pointlightsize() && i_pointlight < maxShadowRenderCount; i_pointlight++) { // render each side of the cube for (int i_face = 0; i_face<6; i_face++) { glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i_face, depth_cubeMaps.at(i_pointlight)->getObjectName(), 0); glClear(GL_DEPTH_BUFFER_BIT); - glm::mat4 viewMatrix = glm::lookAt(closestLights.at(i_pointlight).getPosition(), - closestLights.at(i_pointlight).getPosition() + looking_directions[i_face], upvectors[i_face]); + glm::mat4 viewMatrix = glm::lookAt(closestLights->at(i_pointlight)->getPosition(), + closestLights->at(i_pointlight)->getPosition() + looking_directions[i_face], upvectors[i_face]); glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix; std::vector viewMatrixVector = std::vector(); viewMatrixVector.push_back(viewMatrix); @@ -616,62 +616,41 @@ void Graphics::render(double time) } } -bool Graphics::compareLightDistances(Light a, Light b) { - if (glm::distance(this->level->getCameraCenter()->getPosition(), a.getPosition()) < - glm::distance(this->level->getCameraCenter()->getPosition(), b.getPosition())) { - return true; - } - else { - return false; - } -} - -void Graphics::updateClosestLights() { - closestLights = std::vector(*level->getLights()); - std::sort(closestLights.begin(), - closestLights.end(), - [this](Light a, Light b) {return compareLightDistances(a, b); }); - if (level->getLights()->size() > 15) { - closestLights = std::vector(&closestLights[0], - &closestLights[15]); - } -} - void Graphics::updateLights() { - updateClosestLights(); - if (closestLights.size() > 0) { + closestLights = level->getClosestLights(); + if (closestLights->size() > 0) { lightingShader->use(); - lightingShader->setUniform("lightCount", (int) closestLights.size()); - lightingShader->setUniform("maxShadowRenderCount", std::min((int) closestLights.size(), (int)maxShadowRenderCount)); + lightingShader->setUniform("lightCount", (int) closestLights->size()); + lightingShader->setUniform("maxShadowRenderCount", std::min((int) closestLights->size(), (int)maxShadowRenderCount)); // Build light position array - glm::vec3 lightSources[closestLights.size()]; - for(unsigned int i = 0; isize()]; + for(unsigned int i = 0; isize(); i++) { + lightSources[i] = closestLights->at(i)->getPosition(); } glUniform3fv(lightingShader->getUniformLocation("lightSources"), sizeof(lightSources), (GLfloat*) lightSources); // Build light colour array - glm::vec3 lightColours[closestLights.size()]; - for(unsigned int i = 0; isize()]; + for(unsigned int i = 0; isize(); i++) { + lightColours[i] = closestLights->at(i)->getColour(); } glUniform3fv(lightingShader->getUniformLocation("lightColors"), sizeof(lightColours), (GLfloat*) lightColours); // Build light attenuation array - float lightIntensities[closestLights.size()]; - for(unsigned int i = 0; isize()]; + for(unsigned int i = 0; isize(); i++) { + lightIntensities[i] = closestLights->at(i)->getIntensity(); } glUniform1fv(lightingShader->getUniformLocation("lightIntensities"), sizeof(lightIntensities), (GLfloat*) lightIntensities); } // set directional Light - bool isFlame[closestLights.size()]; + bool isFlame[closestLights->size()]; closestFlames = std::vector(); - for (unsigned int i = 0; isize(); i++) { + if (closestLights->at(i)->isFlame()) { + closestFlames.push_back(closestLights->at(i)->getFlame()); isFlame[i] = true; } else { diff --git a/game/graphics.hh b/game/graphics.hh index e0f3fab..3808657 100644 --- a/game/graphics.hh +++ b/game/graphics.hh @@ -37,8 +37,6 @@ class Graphics { private: void bindTextureUnits(); void updateLights(); - void updateClosestLights(); - bool compareLightDistances(Light a, Light b); void saveDepthBufferToDisk(int face, std::string); double lastLightUpdate; double lastWindUpdate; @@ -54,7 +52,7 @@ class Graphics { std::string loadingScreenContinuePath; SharedTexture2D loadingScreen; SharedTexture2D loadingContinueScreen; - std::vector closestLights; + std::vector* closestLights; std::vector closestFlames; SharedShaderProgram loadingShader; SharedShaderProgram lightingShader; diff --git a/game/level.cc b/game/level.cc index 74c7f8f..ceb4dfb 100644 --- a/game/level.cc +++ b/game/level.cc @@ -18,6 +18,9 @@ Level::~Level() { lua_close(luaState); } delete(waterPlane); + for(unsigned int i = 0; i* Level::getLights() { +std::vector* Level::getLights() { return &lights; } @@ -375,7 +378,8 @@ void Level::setCameraCenter(Object* object) { } void Level::addLight(Light light) { - this->lights.push_back(light); + Light *add_light = new Light(light); + this->lights.push_back(add_light); } void Level::preloadLightPosition(float xPos, float yPos, float zPos){ @@ -384,7 +388,7 @@ void Level::preloadLightPosition(float xPos, float yPos, float zPos){ void Level::addLightByParameters(float redColour, float greenColour, float blueColour, float intensity, float flameYOffset, float flameHeight, float flameWidth){ glm::vec3 colour = glm::vec3(redColour, greenColour, blueColour); - this->lights.push_back(Light(nextLightPosition, colour, intensity, flameYOffset, flameHeight, flameWidth)); + this->lights.push_back(new Light(nextLightPosition, colour, intensity, flameYOffset, flameHeight, flameWidth)); } void Level::deleteFourLights(){ @@ -466,3 +470,25 @@ std::vector>* Level::getChunks() { Object* Level::getWaterPlane() { return waterPlane; } + +bool Level::compareLightDistances(Light* a, Light* b) { + if (glm::distance(cameraCenter->getPosition(), a->getPosition()) < + glm::distance(cameraCenter->getPosition(), b->getPosition())) { + return true; + } + else { + return false; + } +} + +std::vector* Level::getClosestLights() { + closestLights = std::vector(lights); + std::sort(closestLights.begin(), + closestLights.end(), + [this](Light* a, Light* b) {return compareLightDistances(a, b); }); + if (lights.size() > 15) { + closestLights = std::vector(&closestLights[0], + &closestLights[15]); + } + return &closestLights; +} diff --git a/game/level.hh b/game/level.hh index a646954..b28a24b 100644 --- a/game/level.hh +++ b/game/level.hh @@ -37,7 +37,7 @@ class Level { std::vector* shadowVPs=0); glm::vec3 getAmbientLight(); Light* getDirectionalLight(); - std::vector* getLights(); + std::vector* getLights(); Object* getCameraCenter(); Camera* getCamera(); glm::vec3 getCameraPosition(); @@ -84,6 +84,7 @@ class Level { void addToSpecificChunk(Object* object, int xPosition, int zPosition); void enqueueObjects(Graphics* graphics); void sortObjects(int textureCount); + std::vector* getClosestLights(); private: lua_State* luaState=nullptr; std::vector crossChunkObjects; @@ -91,7 +92,8 @@ class Level { std::vector allObjects; std::vector physicsObjects; std::vector> chunks; - std::vector lights; + std::vector lights; + std::vector closestLights; std::vector triggers; Object* waterPlane=nullptr; glm::vec3 ambientLight; @@ -111,6 +113,7 @@ class Level { glm::vec3 nextLightPosition; int chunkSize; float farPlane; + bool compareLightDistances(Light* a, Light* b); }; #endif