From 4d3db6f2859247c1423e34563a7e2feacc96f1a6 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Mon, 26 Jan 2015 00:06:31 +0100 Subject: [PATCH] Updating lights only every half a second now, for performance. --- graphics.cc | 85 ++++++++++++++++++++++++++++++----------------------- graphics.hh | 5 ++-- main.cc | 2 +- 3 files changed, 53 insertions(+), 39 deletions(-) diff --git a/graphics.cc b/graphics.cc index 4a9fc69..909cd5d 100644 --- a/graphics.cc +++ b/graphics.cc @@ -16,6 +16,7 @@ Graphics::Graphics(glm::uvec2 windowSize, float nearPlane, float farPlane) { } Graphics::Graphics() { + lastUpdate = 0.0f; } void Graphics::init(Level* level) { @@ -74,7 +75,7 @@ glm::uvec2 Graphics::getWindowSize() { return windowSize; } -void Graphics::render() +void Graphics::render(double time) { // At first render shadows depthShader->use(); @@ -119,44 +120,19 @@ void Graphics::render() lightingShader->use(); - //set lighting parameters if (level->getLights()->size() > 0) { - lightingShader->setUniform("lightCount", (int) level->getLights()->size()); - - // TODO look into doing this less often, offload to another thread? - // TODO figure out how to deal with bigger numbers of lights. load the nearest on demand? - // Build light position array - glm::vec3 lightSources[level->getLights()->size()]; - for(unsigned int i = 0; igetLights()->size(); i++) { - lightSources[i] = level->getLights()->at(i).getPosition(); - } - glUniform3fv(lightingShader->getUniformLocation("lightSources"), - sizeof(lightSources), (GLfloat*) lightSources); - // Build light colour array - glm::vec3 lightColours[level->getLights()->size()]; - for(unsigned int i = 0; igetLights()->size(); i++) { - lightColours[i] = level->getLights()->at(i).getColour(); - } - glUniform3fv(lightingShader->getUniformLocation("lightColors"), - sizeof(lightColours), (GLfloat*) lightColours); - // Build light attenuation array - float lightIntensities[level->getLights()->size()]; - for(unsigned int i = 0; igetLights()->size(); i++) { - lightIntensities[i] = level->getLights()->at(i).getIntensity(); - } - glUniform1fv(lightingShader->getUniformLocation("lightIntensities"), - sizeof(lightIntensities), (GLfloat*) lightIntensities); - lightingShader->setTexture("shadowMap_cube", depth_cubeMaps.at(0), 4); } - // set directional Light - if(level->getDirectionalLight()) { - lightingShader->setUniform("directionalLightVector", - level->getDirectionalLight()->getPosition()); - lightingShader->setUniform("directionalColor", - level->getDirectionalLight()->getColour()); - lightingShader->setUniform("directionalIntensity", - level->getDirectionalLight()->getIntensity()); + + //set lighting parameters + + // TODO look into doing this less often, offload to another thread? + // TODO figure out how to deal with bigger numbers of lights. load the nearest on demand? + double nextUpdate = lastUpdate + 0.5f; + if (time >= nextUpdate) + { + updateLights(); + lastUpdate = time; } // convert texture to homogenouse coordinates @@ -190,6 +166,43 @@ void Graphics::render() level->render(lightingShader, true, &lightingViewProjectionMatrix, &shadowVPs); } +void Graphics::updateLights() { + if (level->getLights()->size() > 0) { + lightingShader->setUniform("lightCount", (int) level->getLights()->size()); + + // Build light position array + glm::vec3 lightSources[level->getLights()->size()]; + for(unsigned int i = 0; igetLights()->size(); i++) { + lightSources[i] = level->getLights()->at(i).getPosition(); + } + glUniform3fv(lightingShader->getUniformLocation("lightSources"), + sizeof(lightSources), (GLfloat*) lightSources); + // Build light colour array + glm::vec3 lightColours[level->getLights()->size()]; + for(unsigned int i = 0; igetLights()->size(); i++) { + lightColours[i] = level->getLights()->at(i).getColour(); + } + glUniform3fv(lightingShader->getUniformLocation("lightColors"), + sizeof(lightColours), (GLfloat*) lightColours); + // Build light attenuation array + float lightIntensities[level->getLights()->size()]; + for(unsigned int i = 0; igetLights()->size(); i++) { + lightIntensities[i] = level->getLights()->at(i).getIntensity(); + } + glUniform1fv(lightingShader->getUniformLocation("lightIntensities"), + sizeof(lightIntensities), (GLfloat*) lightIntensities); + } + // set directional Light + if(level->getDirectionalLight()) { + lightingShader->setUniform("directionalLightVector", + level->getDirectionalLight()->getPosition()); + lightingShader->setUniform("directionalColor", + level->getDirectionalLight()->getColour()); + lightingShader->setUniform("directionalIntensity", + level->getDirectionalLight()->getIntensity()); + } +} + void Graphics::resize(glm::uvec2 windowSize) { this->windowSize = windowSize; depthTexture->resize(glm::vec2(windowSize.x, windowSize.y)); diff --git a/graphics.hh b/graphics.hh index 2977fdb..c26425b 100644 --- a/graphics.hh +++ b/graphics.hh @@ -13,13 +13,14 @@ class Graphics { Graphics(glm::uvec2 windowSize, float nearPlane, float farPlane); Graphics(); void init(Level* level); - void render(); + void render(double time); glm::mat4 buildViewMatrix(Level* level); glm::uvec2 getWindowSize(); void resize(glm::uvec2 windowSize); float getFarPlane(); private: - void setGLFWHintsForOpenGLVersion( unsigned int _version ); + void updateLights(); + double lastUpdate; glm::uvec2 windowSize; float nearPlane; float farPlane; diff --git a/main.cc b/main.cc index f4a2e47..c639348 100644 --- a/main.cc +++ b/main.cc @@ -177,8 +177,8 @@ int main( int argc, char *argv[] ) } } + app.getGraphics()->render(now); lastUpdate = now; - app.getGraphics()->render(); openGLCriticalError();