Skip to content

Commit

Permalink
Merge branch 'dev' into geo-threepp
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Mar 16, 2024
2 parents 1c23002 + a804a5d commit c1b4b46
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 42 deletions.
2 changes: 1 addition & 1 deletion examples/extras/physics/physx_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ int main() {
camera.getWorldDirection(world);
rb->addForce(toPxVector3(world * 10000));

canvas.invokeLater([&, obj] {
renderer.invokeLater([&, obj] {
scene.remove(*obj);
},
2);// remove after 2 seconds
Expand Down
4 changes: 2 additions & 2 deletions examples/geometries/heightmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ int main() {
TextureLoader tl;
auto texture = tl.load("data/textures/terrain/aalesund_terrain.png");

canvas.invokeLater([&, texture, geometry] {
renderer.invokeLater([&, texture, geometry] {

material->map = texture;
material->needsUpdate();
Expand All @@ -130,7 +130,7 @@ int main() {
hudText.setText("Terrain loaded..", opts);
});

canvas.invokeLater([&] {
renderer.invokeLater([&] {
hud.remove(hudText);
},
2);
Expand Down
2 changes: 1 addition & 1 deletion examples/projects/Crane3R/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ int main() {
m.castShadow = true;
});

canvas.invokeLater([&, crane] {
renderer.invokeLater([&, crane] {
hud.remove(handle);
scene->add(crane);
endEffectorHelper->visible = true;
Expand Down
4 changes: 2 additions & 2 deletions examples/projects/Pathfinding/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ int main() {
auto obj = scene.getObjectByName(getMeshName(c));
obj->material()->as<MaterialWithColor>()->color.setHex(Color::green);
}
canvas.invokeLater([&] {
renderer.invokeLater([&] {
start = std::nullopt;
target = std::nullopt;
resetBlockColors(); }, 2);
} else {
std::cerr << "Unable to find path between " << *start << " and " << *target << std::endl;
canvas.invokeLater([&] {
renderer.invokeLater([&] {
start = std::nullopt;
target = std::nullopt;
resetBlockColors(); }, 1);
Expand Down
2 changes: 1 addition & 1 deletion examples/projects/Youbot/youbot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int main() {
std::shared_ptr<Youbot> youbot;
auto future = std::async([&] {
youbot = Youbot::create("data/models/collada/youbot.dae");
canvas.invokeLater([&] {
renderer.invokeLater([&] {
canvas.addKeyListener(*youbot);
scene->add(youbot);
handle.setText("Use WASD keys to steer robot", opts);
Expand Down
2 changes: 1 addition & 1 deletion examples/projects/Youbot/youbot_kine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ int main() {
youbot->add(targetHelper);
youbot->add(endEffectorHelper);
endEffectorHelper->visible = true;
canvas.invokeLater([&] {
renderer.invokeLater([&] {
canvas.addKeyListener(*youbot);
scene->add(youbot);
hud.remove(handle);
Expand Down
2 changes: 0 additions & 2 deletions include/threepp/canvas/Canvas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ namespace threepp {
// returns false if application should quit, true otherwise
bool animateOnce(const std::function<void()>& f);

void invokeLater(const std::function<void()>& f, float t = 0);

void close();

[[nodiscard]] void* windowPtr() const;
Expand Down
3 changes: 3 additions & 0 deletions include/threepp/renderers/GLRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "threepp/renderers/gl/GLShadowMap.hpp"
#include "threepp/renderers/gl/GLState.hpp"

#include <functional>
#include <memory>
#include <vector>

Expand Down Expand Up @@ -152,6 +153,8 @@ namespace threepp {

void resetState();

void invokeLater(const std::function<void()>& task, float delay = 0);

[[nodiscard]] const gl::GLInfo& info() const;

[[nodiscard]] std::optional<unsigned int> getGlTextureId(Texture& texture) const;
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ set(privateHeaders
"threepp/renderers/gl/UniformUtils.hpp"

"threepp/utils/RegexUtil.hpp"
"threepp/utils/TaskManager.hpp"

)

Expand Down
31 changes: 0 additions & 31 deletions src/threepp/canvas/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@

#include <iostream>
#include <optional>
#include <queue>

using namespace threepp;

namespace {

typedef std::pair<std::function<void()>, float> Task;

struct CustomComparator {
bool operator()(const Task& l, const Task& r) const { return l.second > r.second; }
};

#if EMSCRIPTEN
struct FunctionWrapper {
std::function<void()> loopFunction;
Expand Down Expand Up @@ -173,7 +166,6 @@ struct Canvas::Impl {
bool close_{false};
bool exitOnKeyEscape_;

std::priority_queue<Task, std::vector<Task>, CustomComparator> tasks_;
std::optional<std::function<void(WindowSize)>> resizeListener;

explicit Impl(Canvas& scope, const Canvas::Parameters& params)
Expand Down Expand Up @@ -248,26 +240,12 @@ struct Canvas::Impl {
glfwSetWindowSize(window, size.width, size.height);
}

inline void handleTasks() {
while (!tasks_.empty()) {
auto& task = tasks_.top();
if (task.second < glfwGetTime()) {
task.first();
tasks_.pop();
} else {
break;
}
}
}

bool animateOnce(const std::function<void()>& f) {

if (close_ || glfwWindowShouldClose(window)) {
return false;
}

handleTasks();

f();

glfwSwapBuffers(window);
Expand All @@ -289,10 +267,6 @@ struct Canvas::Impl {
this->resizeListener = std::move(f);
}

void invokeLater(const std::function<void()>& f, float t) {
tasks_.emplace(f, static_cast<float>(glfwGetTime()) + t);
}

void close() {

close_ = true;
Expand Down Expand Up @@ -416,11 +390,6 @@ void Canvas::onWindowResize(std::function<void(WindowSize)> f) {
pimpl_->onWindowResize(std::move(f));
}

void Canvas::invokeLater(const std::function<void()>& f, float t) {

pimpl_->invokeLater(f, t);
}

void Canvas::close() {

pimpl_->close();
Expand Down
44 changes: 43 additions & 1 deletion src/threepp/renderers/GLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "threepp/cameras/OrthographicCamera.hpp"
#include "threepp/core/InstancedBufferGeometry.hpp"
#include "threepp/materials/RawShaderMaterial.hpp"
#include "threepp/math/Frustum.hpp"

#include "threepp/objects/Group.hpp"
#include "threepp/objects/InstancedMesh.hpp"
Expand All @@ -33,17 +32,31 @@
#include "threepp/objects/SkinnedMesh.hpp"
#include "threepp/objects/Sprite.hpp"

#include "threepp/utils/TaskManager.hpp"

#ifndef EMSCRIPTEN
#include "threepp/utils/LoadGlad.hpp"
#else
#include <GLES3/gl32.h>
#endif

#include <chrono>
#include <cmath>


using namespace threepp;

namespace {

double getCurrentTimeInSeconds() {
using Clock = std::chrono::high_resolution_clock;
const auto now = Clock::now();
const auto duration = now.time_since_epoch();
return std::chrono::duration<double>(duration).count();
}

}// namespace


struct GLRenderer::Impl {

Expand Down Expand Up @@ -140,6 +153,10 @@ struct GLRenderer::Impl {

gl::GLShadowMap shadowMap;

// used for in-thread task execution
double previousTime{-1};
utils::TaskManager taskManager;

Impl(GLRenderer& scope, WindowSize size, const GLRenderer::Parameters& parameters)
: scope(scope), _size(size),
cubemaps(scope),
Expand All @@ -163,6 +180,11 @@ struct GLRenderer::Impl {
this->setScissor(0, 0, _size.width, _size.height);
}

void invokeLater(const std::function<void()>& task, float delay) {

taskManager.invokeLater(task, delay);
}

[[nodiscard]] std::optional<unsigned int> getGlTextureId(Texture& texture) const {

return textures.getGlTexture(texture);
Expand All @@ -188,8 +210,23 @@ struct GLRenderer::Impl {
}
}

void handleTasks() {
// handle tasks to be invoked on the render thread
if (previousTime < 0) {
previousTime = getCurrentTimeInSeconds();// first invocation
}

const auto currentTime = getCurrentTimeInSeconds();
const auto deltaTime = currentTime - previousTime;
previousTime = currentTime;

taskManager.handleTasks(deltaTime);
}

void render(Object3D* scene, Camera* camera) {

handleTasks();

// update scene graph

if (auto _scene = scene->as<Scene>()) {
Expand Down Expand Up @@ -1363,4 +1400,9 @@ std::optional<unsigned int> GLRenderer::getGlTextureId(Texture& texture) const {
return pimpl_->getGlTextureId(texture);
}

void GLRenderer::invokeLater(const std::function<void()>& task, float tThen) {

return pimpl_->invokeLater(task, tThen);
}

GLRenderer::~GLRenderer() = default;
48 changes: 48 additions & 0 deletions src/threepp/utils/TaskManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

#ifndef THREEPP_TASKMANAGER_HPP
#define THREEPP_TASKMANAGER_HPP

#include <functional>
#include <queue>
#include <utility>

namespace threepp::utils {

using Task = std::pair<std::function<void()>, double>;

class TaskManager {

public:
inline void handleTasks(double deltaTime) {

time += deltaTime;

while (!tasks_.empty()) {
auto& task = tasks_.top();
if (task.second < time) {
task.first();
tasks_.pop();
} else {
break;
}
}
}

void invokeLater(const std::function<void()>& f, double delay = 0) {

tasks_.emplace(f, time + delay);
}

private:
struct CustomComparator {
bool operator()(const Task& l, const Task& r) const { return l.second > r.second; }
};

double time{};

std::priority_queue<Task, std::vector<Task>, CustomComparator> tasks_;
};

}// namespace threepp::utils

#endif//THREEPP_TASKMANAGER_HPP

0 comments on commit c1b4b46

Please sign in to comment.