Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/graphics/gfxman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void GraphicsManager::removeLight(Light *light) {
if (!_renderer)
return;

_renderer->addLight(light);
_renderer->removeLight(light);
}

TexturePtr GraphicsManager::createTexture(ImageDecoder &&decoder, const std::string &label) {
Expand Down
3 changes: 3 additions & 0 deletions src/graphics/opengl/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ void Renderer::drawWorld(const std::string &stage) {
void Renderer::drawLights() {
pushDebug("Draw Lights");

// flush any new additions or removals
flushLights();

auto stencilProgram = getProgram("deferredlight", "render_stencil", 0);
auto pointlightProgram = getProgram("deferredlight", "pointlight", 0);

Expand Down
23 changes: 20 additions & 3 deletions src/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,29 @@ void Graphics::Renderer::removeGUIElement(Graphics::GUIElement *gui) {
}

void Graphics::Renderer::addLight(Graphics::Light *light) {
_lights.emplace_back(light);
_light_queues_mutex.lock();
_light_add_queue.push(light);
_light_queues_mutex.unlock();
}

void Graphics::Renderer::removeLight(Graphics::Light *light) {
const auto iter = std::remove(_lights.begin(), _lights.end(), light);
_lights.erase(iter);
_light_queues_mutex.lock();
_light_remove_queue.push(light);
_light_queues_mutex.unlock();
}

void Graphics::Renderer::flushLights() {
_light_queues_mutex.lock();
while (!_light_add_queue.empty()) {
_lights.push_back(_light_add_queue.front());
_light_add_queue.pop();
}
while (!_light_remove_queue.empty()) {
const auto iter = std::remove(_lights.begin(), _lights.end(), _light_remove_queue.front());
_lights.erase(iter);
_light_remove_queue.pop();
}
_light_queues_mutex.unlock();
}

void Graphics::Renderer::setCamera(Graphics::Camera &camera) {
Expand Down
6 changes: 6 additions & 0 deletions src/graphics/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#ifndef AWE_RENDERER_H
#define AWE_RENDERER_H

#include <mutex>
#include <queue>
#include <vector>

#include "src/common/frustrum.h"
Expand Down Expand Up @@ -54,6 +56,7 @@ class Renderer {
void removeGUIElement(GUIElement *gui);
void addLight(Light *light);
void removeLight(Light *light);
void flushLights();

void setCamera(Camera &camera);

Expand Down Expand Up @@ -158,6 +161,9 @@ class Renderer {
std::vector<ImGuiElement *> _imguiElements;
std::vector<GUIElement *> _guiElements;
std::vector<Light *> _lights;
std::queue<Light *> _light_add_queue;
std::queue<Light *> _light_remove_queue;
std::mutex _light_queues_mutex;
};

} // End of namespace Graphics
Expand Down