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();
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_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
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<glm::mat4> viewMatrixVector = std::vector<glm::mat4>();
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() {
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; i<closestLights.size(); i++) {
lightSources[i] = closestLights.at(i).getPosition();
glm::vec3 lightSources[closestLights->size()];
for(unsigned int i = 0; i<closestLights->size(); 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; i<closestLights.size(); i++) {
lightColours[i] = closestLights.at(i).getColour();
glm::vec3 lightColours[closestLights->size()];
for(unsigned int i = 0; i<closestLights->size(); 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; i<closestLights.size(); i++) {
lightIntensities[i] = closestLights.at(i).getIntensity();
float lightIntensities[closestLights->size()];
for(unsigned int i = 0; i<closestLights->size(); 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<Flame*>();
for (unsigned int i = 0; i<closestLights.size(); i++) {
if (closestLights.at(i).isFlame()) {
closestFlames.push_back(closestLights.at(i).getFlame());
for (unsigned int i = 0; i<closestLights->size(); i++) {
if (closestLights->at(i)->isFlame()) {
closestFlames.push_back(closestLights->at(i)->getFlame());
isFlame[i] = true;
}
else {

View File

@ -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<Light> closestLights;
std::vector<Light*>* closestLights;
std::vector<Flame*> closestFlames;
SharedShaderProgram loadingShader;
SharedShaderProgram lightingShader;

View File

@ -18,6 +18,9 @@ Level::~Level() {
lua_close(luaState);
}
delete(waterPlane);
for(unsigned int i = 0; i<lights.size(); i++) {
delete(lights.at(i));
}
}
void Level::load() {
@ -238,7 +241,7 @@ glm::vec3 Level::getAmbientLight() {
return ambientLight;
}
std::vector<Light>* Level::getLights() {
std::vector<Light*>* 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<std::vector<Chunk>>* 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<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);
glm::vec3 getAmbientLight();
Light* getDirectionalLight();
std::vector<Light>* getLights();
std::vector<Light*>* 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<Light*>* getClosestLights();
private:
lua_State* luaState=nullptr;
std::vector<Object*> crossChunkObjects;
@ -91,7 +92,8 @@ class Level {
std::vector<Object*> allObjects;
std::vector<Object*> physicsObjects;
std::vector<std::vector<Chunk>> chunks;
std::vector<Light> lights;
std::vector<Light*> lights;
std::vector<Light*> closestLights;
std::vector<Trigger> 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