Moving updateClosestLights() to level class.

This commit is contained in:
Faerbit 2015-03-24 19:50:26 +01:00
parent 2b2e05f009
commit fc2ce96771
4 changed files with 56 additions and 50 deletions

View File

@ -180,7 +180,7 @@ void Graphics::init(Level* level) {
bindTextureUnits(); bindTextureUnits();
updateClosestLights(); closestLights = level->getClosestLights();
// set shader variables that stay the same across the runtime of the application // set shader variables that stay the same across the runtime of the application
skydomeShader->use(); skydomeShader->use();
@ -377,13 +377,13 @@ void Graphics::render(double time)
framebuffer_cube->bind(); framebuffer_cube->bind();
for (unsigned int i_pointlight = 0; i_pointlight<closestLights.size() && i_pointlight < maxShadowRenderCount; i_pointlight++) { for (unsigned int i_pointlight = 0; i_pointlight<closestLights->size() && i_pointlight < maxShadowRenderCount; i_pointlight++) {
// render each side of the cube // render each side of the cube
for (int i_face = 0; i_face<6; i_face++) { 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); 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); glClear(GL_DEPTH_BUFFER_BIT);
glm::mat4 viewMatrix = glm::lookAt(closestLights.at(i_pointlight).getPosition(), glm::mat4 viewMatrix = glm::lookAt(closestLights->at(i_pointlight)->getPosition(),
closestLights.at(i_pointlight).getPosition() + looking_directions[i_face], upvectors[i_face]); closestLights->at(i_pointlight)->getPosition() + looking_directions[i_face], upvectors[i_face]);
glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix; glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix;
std::vector<glm::mat4> viewMatrixVector = std::vector<glm::mat4>(); std::vector<glm::mat4> viewMatrixVector = std::vector<glm::mat4>();
viewMatrixVector.push_back(viewMatrix); 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<Light>(*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<Light>(&closestLights[0],
&closestLights[15]);
}
}
void Graphics::updateLights() { void Graphics::updateLights() {
updateClosestLights(); closestLights = level->getClosestLights();
if (closestLights.size() > 0) { if (closestLights->size() > 0) {
lightingShader->use(); lightingShader->use();
lightingShader->setUniform("lightCount", (int) closestLights.size()); lightingShader->setUniform("lightCount", (int) closestLights->size());
lightingShader->setUniform("maxShadowRenderCount", std::min((int) closestLights.size(), (int)maxShadowRenderCount)); lightingShader->setUniform("maxShadowRenderCount", std::min((int) closestLights->size(), (int)maxShadowRenderCount));
// Build light position array // Build light position array
glm::vec3 lightSources[closestLights.size()]; glm::vec3 lightSources[closestLights->size()];
for(unsigned int i = 0; i<closestLights.size(); i++) { for(unsigned int i = 0; i<closestLights->size(); i++) {
lightSources[i] = closestLights.at(i).getPosition(); lightSources[i] = closestLights->at(i)->getPosition();
} }
glUniform3fv(lightingShader->getUniformLocation("lightSources"), glUniform3fv(lightingShader->getUniformLocation("lightSources"),
sizeof(lightSources), (GLfloat*) lightSources); sizeof(lightSources), (GLfloat*) lightSources);
// Build light colour array // Build light colour array
glm::vec3 lightColours[closestLights.size()]; glm::vec3 lightColours[closestLights->size()];
for(unsigned int i = 0; i<closestLights.size(); i++) { for(unsigned int i = 0; i<closestLights->size(); i++) {
lightColours[i] = closestLights.at(i).getColour(); lightColours[i] = closestLights->at(i)->getColour();
} }
glUniform3fv(lightingShader->getUniformLocation("lightColors"), glUniform3fv(lightingShader->getUniformLocation("lightColors"),
sizeof(lightColours), (GLfloat*) lightColours); sizeof(lightColours), (GLfloat*) lightColours);
// Build light attenuation array // Build light attenuation array
float lightIntensities[closestLights.size()]; float lightIntensities[closestLights->size()];
for(unsigned int i = 0; i<closestLights.size(); i++) { for(unsigned int i = 0; i<closestLights->size(); i++) {
lightIntensities[i] = closestLights.at(i).getIntensity(); lightIntensities[i] = closestLights->at(i)->getIntensity();
} }
glUniform1fv(lightingShader->getUniformLocation("lightIntensities"), glUniform1fv(lightingShader->getUniformLocation("lightIntensities"),
sizeof(lightIntensities), (GLfloat*) lightIntensities); sizeof(lightIntensities), (GLfloat*) lightIntensities);
} }
// set directional Light // set directional Light
bool isFlame[closestLights.size()]; bool isFlame[closestLights->size()];
closestFlames = std::vector<Flame*>(); closestFlames = std::vector<Flame*>();
for (unsigned int i = 0; i<closestLights.size(); i++) { for (unsigned int i = 0; i<closestLights->size(); i++) {
if (closestLights.at(i).isFlame()) { if (closestLights->at(i)->isFlame()) {
closestFlames.push_back(closestLights.at(i).getFlame()); closestFlames.push_back(closestLights->at(i)->getFlame());
isFlame[i] = true; isFlame[i] = true;
} }
else { else {

View File

@ -37,8 +37,6 @@ class Graphics {
private: private:
void bindTextureUnits(); void bindTextureUnits();
void updateLights(); void updateLights();
void updateClosestLights();
bool compareLightDistances(Light a, Light b);
void saveDepthBufferToDisk(int face, std::string); void saveDepthBufferToDisk(int face, std::string);
double lastLightUpdate; double lastLightUpdate;
double lastWindUpdate; double lastWindUpdate;
@ -54,7 +52,7 @@ class Graphics {
std::string loadingScreenContinuePath; std::string loadingScreenContinuePath;
SharedTexture2D loadingScreen; SharedTexture2D loadingScreen;
SharedTexture2D loadingContinueScreen; SharedTexture2D loadingContinueScreen;
std::vector<Light> closestLights; std::vector<Light*>* closestLights;
std::vector<Flame*> closestFlames; std::vector<Flame*> closestFlames;
SharedShaderProgram loadingShader; SharedShaderProgram loadingShader;
SharedShaderProgram lightingShader; SharedShaderProgram lightingShader;

View File

@ -18,6 +18,9 @@ Level::~Level() {
lua_close(luaState); lua_close(luaState);
} }
delete(waterPlane); delete(waterPlane);
for(unsigned int i = 0; i<lights.size(); i++) {
delete(lights.at(i));
}
} }
void Level::load() { void Level::load() {
@ -238,7 +241,7 @@ glm::vec3 Level::getAmbientLight() {
return ambientLight; return ambientLight;
} }
std::vector<Light>* Level::getLights() { std::vector<Light*>* Level::getLights() {
return &lights; return &lights;
} }
@ -375,7 +378,8 @@ void Level::setCameraCenter(Object* object) {
} }
void Level::addLight(Light light) { 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){ 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){ 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); 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(){ void Level::deleteFourLights(){
@ -466,3 +470,25 @@ std::vector<std::vector<Chunk>>* Level::getChunks() {
Object* Level::getWaterPlane() { Object* Level::getWaterPlane() {
return waterPlane; 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<Light*>* Level::getClosestLights() {
closestLights = std::vector<Light*>(lights);
std::sort(closestLights.begin(),
closestLights.end(),
[this](Light* a, Light* b) {return compareLightDistances(a, b); });
if (lights.size() > 15) {
closestLights = std::vector<Light*>(&closestLights[0],
&closestLights[15]);
}
return &closestLights;
}

View File

@ -37,7 +37,7 @@ class Level {
std::vector<glm::mat4>* shadowVPs=0); std::vector<glm::mat4>* shadowVPs=0);
glm::vec3 getAmbientLight(); glm::vec3 getAmbientLight();
Light* getDirectionalLight(); Light* getDirectionalLight();
std::vector<Light>* getLights(); std::vector<Light*>* getLights();
Object* getCameraCenter(); Object* getCameraCenter();
Camera* getCamera(); Camera* getCamera();
glm::vec3 getCameraPosition(); glm::vec3 getCameraPosition();
@ -84,6 +84,7 @@ class Level {
void addToSpecificChunk(Object* object, int xPosition, int zPosition); void addToSpecificChunk(Object* object, int xPosition, int zPosition);
void enqueueObjects(Graphics* graphics); void enqueueObjects(Graphics* graphics);
void sortObjects(int textureCount); void sortObjects(int textureCount);
std::vector<Light*>* getClosestLights();
private: private:
lua_State* luaState=nullptr; lua_State* luaState=nullptr;
std::vector<Object*> crossChunkObjects; std::vector<Object*> crossChunkObjects;
@ -91,7 +92,8 @@ class Level {
std::vector<Object*> allObjects; std::vector<Object*> allObjects;
std::vector<Object*> physicsObjects; std::vector<Object*> physicsObjects;
std::vector<std::vector<Chunk>> chunks; std::vector<std::vector<Chunk>> chunks;
std::vector<Light> lights; std::vector<Light*> lights;
std::vector<Light*> closestLights;
std::vector<Trigger> triggers; std::vector<Trigger> triggers;
Object* waterPlane=nullptr; Object* waterPlane=nullptr;
glm::vec3 ambientLight; glm::vec3 ambientLight;
@ -111,6 +113,7 @@ class Level {
glm::vec3 nextLightPosition; glm::vec3 nextLightPosition;
int chunkSize; int chunkSize;
float farPlane; float farPlane;
bool compareLightDistances(Light* a, Light* b);
}; };
#endif #endif