Updating lights only every half a second now, for performance.
This commit is contained in:
parent
6fa2e4e1b8
commit
4cef0b31d3
83
graphics.cc
83
graphics.cc
@ -16,6 +16,7 @@ Graphics::Graphics(glm::uvec2 windowSize, float nearPlane, float farPlane) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Graphics::Graphics() {
|
Graphics::Graphics() {
|
||||||
|
lastUpdate = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Graphics::init(Level* level) {
|
void Graphics::init(Level* level) {
|
||||||
@ -74,7 +75,7 @@ glm::uvec2 Graphics::getWindowSize() {
|
|||||||
return windowSize;
|
return windowSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Graphics::render()
|
void Graphics::render(double time)
|
||||||
{
|
{
|
||||||
// At first render shadows
|
// At first render shadows
|
||||||
depthShader->use();
|
depthShader->use();
|
||||||
@ -119,44 +120,19 @@ void Graphics::render()
|
|||||||
|
|
||||||
lightingShader->use();
|
lightingShader->use();
|
||||||
|
|
||||||
//set lighting parameters
|
|
||||||
if (level->getLights()->size() > 0) {
|
if (level->getLights()->size() > 0) {
|
||||||
lightingShader->setUniform("lightCount", (int) level->getLights()->size());
|
lightingShader->setTexture("shadowMap_cube", depth_cubeMaps.at(0), 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
//set lighting parameters
|
||||||
|
|
||||||
// TODO look into doing this less often, offload to another thread?
|
// 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?
|
// TODO figure out how to deal with bigger numbers of lights. load the nearest on demand?
|
||||||
// Build light position array
|
double nextUpdate = lastUpdate + 0.5f;
|
||||||
glm::vec3 lightSources[level->getLights()->size()];
|
if (time >= nextUpdate)
|
||||||
for(unsigned int i = 0; i<level->getLights()->size(); i++) {
|
{
|
||||||
lightSources[i] = level->getLights()->at(i).getPosition();
|
updateLights();
|
||||||
}
|
lastUpdate = time;
|
||||||
glUniform3fv(lightingShader->getUniformLocation("lightSources"),
|
|
||||||
sizeof(lightSources), (GLfloat*) lightSources);
|
|
||||||
// Build light colour array
|
|
||||||
glm::vec3 lightColours[level->getLights()->size()];
|
|
||||||
for(unsigned int i = 0; i<level->getLights()->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; i<level->getLights()->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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert texture to homogenouse coordinates
|
// convert texture to homogenouse coordinates
|
||||||
@ -190,6 +166,43 @@ void Graphics::render()
|
|||||||
level->render(lightingShader, true, &lightingViewProjectionMatrix, &shadowVPs);
|
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; i<level->getLights()->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; i<level->getLights()->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; i<level->getLights()->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) {
|
void Graphics::resize(glm::uvec2 windowSize) {
|
||||||
this->windowSize = windowSize;
|
this->windowSize = windowSize;
|
||||||
depthTexture->resize(glm::vec2(windowSize.x, windowSize.y));
|
depthTexture->resize(glm::vec2(windowSize.x, windowSize.y));
|
||||||
|
@ -13,13 +13,14 @@ class Graphics {
|
|||||||
Graphics(glm::uvec2 windowSize, float nearPlane, float farPlane);
|
Graphics(glm::uvec2 windowSize, float nearPlane, float farPlane);
|
||||||
Graphics();
|
Graphics();
|
||||||
void init(Level* level);
|
void init(Level* level);
|
||||||
void render();
|
void render(double time);
|
||||||
glm::mat4 buildViewMatrix(Level* level);
|
glm::mat4 buildViewMatrix(Level* level);
|
||||||
glm::uvec2 getWindowSize();
|
glm::uvec2 getWindowSize();
|
||||||
void resize(glm::uvec2 windowSize);
|
void resize(glm::uvec2 windowSize);
|
||||||
float getFarPlane();
|
float getFarPlane();
|
||||||
private:
|
private:
|
||||||
void setGLFWHintsForOpenGLVersion( unsigned int _version );
|
void updateLights();
|
||||||
|
double lastUpdate;
|
||||||
glm::uvec2 windowSize;
|
glm::uvec2 windowSize;
|
||||||
float nearPlane;
|
float nearPlane;
|
||||||
float farPlane;
|
float farPlane;
|
||||||
|
Loading…
Reference in New Issue
Block a user