Skip to content

Commit

Permalink
Wip, untested
Browse files Browse the repository at this point in the history
  • Loading branch information
Robadob committed Oct 18, 2024
1 parent 3befb59 commit 6ec944e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 28 deletions.
5 changes: 5 additions & 0 deletions include/flamegpu/visualiser/FLAMEGPU_Visualisation.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class FLAMEGPU_Visualisation {
const std::map<TexBufferConfig::Function, TexBufferConfig>& core_tex_buffers, const std::multimap<TexBufferConfig::Function, CustomTexBufferConfig>& tex_buffers) {
updateAgentStateBuffer(agent_name.c_str(), state_name.c_str(), buffLen, core_tex_buffers, tex_buffers);
}
void updateDynamicLine(const std::string &graph_name) {
updateDynamicLine(graph_name.c_str());
}
/**
* Provide an environment property, so it can be displayed
*/
Expand Down Expand Up @@ -81,6 +84,8 @@ class FLAMEGPU_Visualisation {
void requestBufferResizes(const char *agent_name, const char *state_name, const unsigned int buffLen, bool force);
void updateAgentStateBuffer(const char *agent_name, const char *state_name, const unsigned int buffLen,
const std::map<TexBufferConfig::Function, TexBufferConfig>& core_tex_buffers, const std::multimap<TexBufferConfig::Function, CustomTexBufferConfig>& tex_buffers);
void updateDynamicLine(const char* graph_name);

Visualiser *vis = nullptr;
LockHolder *lock = nullptr;
/**
Expand Down
4 changes: 4 additions & 0 deletions include/flamegpu/visualiser/config/ModelConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ struct ModelConfig {
* Store of user defined line renderings
*/
std::list<std::shared_ptr<LineConfig>> lines;
/**
* Store of user defined graph renderings
*/
std::map<std::string, std::shared_ptr<LineConfig>> dynamic_lines;
/**
* Store of user defined UI panels
*/
Expand Down
3 changes: 3 additions & 0 deletions src/flamegpu/visualiser/FLAMEGPU_Visualisation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ void FLAMEGPU_Visualisation::updateAgentStateBuffer(const char *agent_name, cons
void FLAMEGPU_Visualisation::registerEnvironmentProperty(const std::string& property_name, void* ptr, const std::type_index type, const unsigned int elements, const bool is_const) {
vis->registerEnvironmentProperty(property_name, ptr, type, elements, is_const);
}
void FLAMEGPU_Visualisation::updateDynamicLine(const char *graph_name) {
vis->updateDynamicLine(graph_name);
}
void FLAMEGPU_Visualisation::setStepCount(const unsigned int stepCount) {
vis->setStepCount(stepCount);
}
Expand Down
76 changes: 51 additions & 25 deletions src/flamegpu/visualiser/Visualiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,26 @@ Visualiser::RenderInfo::RenderInfo(const AgentStateConfig& vc,
entity->loadKeyFrameModel(vc.model_pathB);
}
}

void addLine(std::shared_ptr<Draw> &lines, const std::shared_ptr<LineConfig> &line, const std::string &name, bool replace) {
// Check it's valid
if (line->lineType == LineConfig::Type::Polyline) {
if (line->vertices.size() < 6 || line->vertices.size() % 3 != 0 || line->colors.size() % 4 != 0 || (line->colors.size() / 4) * 3 != line->vertices.size()) {
THROW SketchError("Polyline sketch contains invalid number of vertices (%d/3) or colours (%d/4).\n", line->vertices.size(), line->colors.size());
}
}
else if (line->lineType == LineConfig::Type::Lines) {
if (line->vertices.size() < 6 || line->vertices.size() % 6 != 0 || line->colors.size() % 4 != 0 || (line->colors.size() / 4) * 3 != line->vertices.size()) {
THROW SketchError("Lines sketch contains invalid number of vertices (%d/3) or colours (%d/4).\n", line->vertices.size(), line->colors.size());
}
}
// Convert to Draw
lines->begin(line->lineType == LineConfig::Type::Polyline ? Draw::Type::Polyline : Draw::Type::Lines, name);
for (size_t i = 0; i < line->vertices.size() / 3; ++i) {
lines->color(*reinterpret_cast<const glm::vec4*>(&line->colors[i * 4]));
lines->vertex(*reinterpret_cast<const glm::vec3*>(&line->vertices[i * 3]));
}
lines->save(replace);
}
Visualiser::Visualiser(const ModelConfig& modelcfg)
: hud(std::make_shared<HUD>(modelcfg.windowDimensions[0], modelcfg.windowDimensions[1]))
, camera(std::make_shared<NoClipCamera>(*reinterpret_cast<const glm::vec3*>(&modelcfg.cameraLocation[0]), *reinterpret_cast<const glm::vec3*>(&modelcfg.cameraTarget[0])))
Expand Down Expand Up @@ -149,9 +168,12 @@ Visualiser::Visualiser(const ModelConfig& modelcfg)
imguiPanel = std::make_shared<ImGuiPanel>(modelcfg.panels, *this);
hud->add(imguiPanel, HUD::AnchorV::North, HUD::AnchorH::West, glm::ivec2(0, 0), INT_MAX-2);
}
lines = std::make_shared<Draw>();
lines->setViewMatPtr(camera->getViewMatPtr());
lines->setProjectionMatPtr(&this->projMat);
lines_static = std::make_shared<Draw>();
lines_static->setViewMatPtr(camera->getViewMatPtr());
lines_static->setProjectionMatPtr(&this->projMat);
lines_dynamic = std::make_shared<Draw>();
lines_dynamic->setViewMatPtr(camera->getViewMatPtr());
lines_dynamic->setProjectionMatPtr(&this->projMat);
// Process static models
for (auto &sm : modelcfg.staticModels) {
std::shared_ptr<Entity> entity;
Expand Down Expand Up @@ -186,23 +208,10 @@ Visualiser::Visualiser(const ModelConfig& modelcfg)
}
// Process lines
for (auto &line : modelcfg.lines) {
// Check it's valid
if (line->lineType == LineConfig::Type::Polyline) {
if (line->vertices.size() < 6 || line->vertices.size() % 3 != 0 || line->colors.size() % 4 != 0 || (line->colors.size() / 4) * 3 != line->vertices.size()) {
THROW SketchError("Polyline sketch contains invalid number of vertices (%d/3) or colours (%d/4).\n", line->vertices.size(), line->colors.size());
}
} else if (line->lineType == LineConfig::Type::Lines) {
if (line->vertices.size() < 6 || line->vertices.size() % 6 != 0 || line->colors.size() % 4 != 0 || (line->colors.size() / 4) * 3 != line->vertices.size()) {
THROW SketchError("Lines sketch contains invalid number of vertices (%d/3) or colours (%d/4).\n", line->vertices.size(), line->colors.size());
}
}
// Convert to Draw
lines->begin(line->lineType == LineConfig::Type::Polyline ? Draw::Type::Polyline : Draw::Type::Lines, std::to_string(totalLines++));
for (size_t i = 0; i < line->vertices.size() / 3; ++i) {
lines->color(*reinterpret_cast<const glm::vec4*>(&line->colors[i * 4]));
lines->vertex(*reinterpret_cast<const glm::vec3*>(&line->vertices[i * 3]));
}
lines->save();
addLine(lines_static, line, std::to_string(totalLines++), false);
}
for (auto& [name, line] : modelcfg.dynamic_lines) {
addLine(lines_dynamic, line, name, false);
}
// Default lighting, single point light attached to camera
// Maybe in future let user specify lights instead of this
Expand Down Expand Up @@ -445,12 +454,21 @@ void Visualiser::render() {
hud->remove(splashScreen);
splashScreen.reset();
}
// Update dynamic lines if they have changed
{
auto lock = std::lock_guard<std::mutex>(lines_dynamic_mutex);
for (auto &name : lines_dynamic_updates)
addLine(lines_dynamic, modelConfig.dynamic_lines.at(name), name, true);
lines_dynamic_updates.clear();
}
// Render lines last, as they may contain alpha
if (renderLines) {
GL_CALL(glEnable(GL_BLEND));
GL_CALL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
for (unsigned int i = 0; i < totalLines; ++i)
lines->render(std::to_string(i));
lines_static->render(std::to_string(i));
for (const auto &[name, _] : modelConfig.dynamic_lines)
lines_dynamic->render(name);
GL_CALL(glDisable(GL_BLEND));
}
GL_CALL(glViewport(0, 0, windowDims.x, windowDims.y));
Expand Down Expand Up @@ -804,7 +822,8 @@ void Visualiser::deallocateGLObjects() {
for (auto &as : agentStates) {
as.second.entity.reset();
}
this->lines.reset();
this->lines_static.reset();
this->lines_dynamic.reset();
render_buffer.reset();
screenshot_buffer.reset();

Expand Down Expand Up @@ -896,8 +915,10 @@ void Visualiser::handleKeypress(SDL_Keycode keycode, int /*x*/, int /*y*/) {
break;
case SDLK_F5:
// Reload all shaders
if (this->lines)
this->lines->reload();
if (this->lines_static)
this->lines_static->reload();
if (this->lines_dynamic)
this->lines_dynamic->reload();
for (auto& as : this->agentStates)
as.second.entity->reload();
for (auto& sm : this->staticModels)
Expand Down Expand Up @@ -1241,6 +1262,11 @@ void Visualiser::setWindowIcon() {
if (surface)
SDL_SetWindowIcon(window, surface.get());
}
void Visualiser::updateDynamicLine(const std::string& name) {
auto lock = std::lock_guard<std::mutex>(lines_dynamic_mutex);
// Store a list of dynamic line updates, as they must occur in render thread
lines_dynamic_updates.insert(name);
}

} // namespace visualiser
} // namespace flamegpu
11 changes: 8 additions & 3 deletions src/flamegpu/visualiser/Visualiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Visualiser : public ViewportExt {

public:
explicit Visualiser(const ModelConfig &modelcfg);
~Visualiser();
~Visualiser() override;
/**
* Starts the render loop running
*/
Expand Down Expand Up @@ -128,7 +128,10 @@ class Visualiser : public ViewportExt {
* Provide the env_cache ptr for the specified environment property, for visualisation
*/
void registerEnvironmentProperty(const std::string& property_name, void* ptr, std::type_index type, unsigned int elements, bool is_const);

/**
* Update the dynamic_lines with the corresponding name
*/
void updateDynamicLine(const std::string &name);
private:
void run();
/**
Expand Down Expand Up @@ -358,7 +361,9 @@ class Visualiser : public ViewportExt {
/**
* User defined lines to be rendered
*/
std::shared_ptr<Draw> lines;
std::shared_ptr<Draw> lines_static, lines_dynamic;
std::set<std::string> lines_dynamic_updates;
std::mutex lines_dynamic_mutex;
/**
* Provides a simple default lighting configuration located at the camera using the old fixed function pipeline methods
*/
Expand Down

0 comments on commit 6ec944e

Please sign in to comment.