From be3a301dc07b468bae7dda12d005b52189130d1c Mon Sep 17 00:00:00 2001 From: Emil Wallberg Date: Mon, 31 Mar 2025 19:31:43 +0200 Subject: [PATCH] Blackhole as background decupled blackhole from anchor. Fix so that the blackhole can be used as a background to other 3D Objects and there for had to make it so that the blackhole rendering is independet of the camera anchor node --- modules/blackhole/rendering/kdtree.cpp | 14 +++++++----- .../rendering/renderableblackhole.cpp | 22 +++++++++++-------- .../blackhole/rendering/renderableblackhole.h | 2 +- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/modules/blackhole/rendering/kdtree.cpp b/modules/blackhole/rendering/kdtree.cpp index 89cb677aae..bf6a2e4724 100644 --- a/modules/blackhole/rendering/kdtree.cpp +++ b/modules/blackhole/rendering/kdtree.cpp @@ -6,10 +6,10 @@ #include namespace { - glm::vec3 cartesianToSpherical(glm::vec3 const& cartesian) { - float radius = glm::length(cartesian); - float theta = std::atan2(glm::sqrt(cartesian.x * cartesian.x + cartesian.y * cartesian.y), cartesian.z); - float phi = std::atan2(cartesian.y, cartesian.x); + glm::vec3 cartesianToSpherical(glm::dvec3 const& cartesian) { + double radius = glm::length(cartesian); + double theta = std::atan2(glm::sqrt(cartesian.x * cartesian.x + cartesian.y * cartesian.y), cartesian.z); + double phi = std::atan2(cartesian.y, cartesian.x); return glm::vec3(radius, theta, phi); } @@ -25,6 +25,10 @@ namespace openspace { const std::filesystem::path file{ absPath(filePath) }; dataloader::Dataset dataset = dataloader::data::loadFileWithCache(file); + // Clear without deallocating memory + _flatTrees.clear(); + _treeStartIndices.clear(); + // Convert positions to spherical coordinates #pragma omp parallel for for (auto& entry : dataset.entries) { @@ -69,8 +73,8 @@ namespace openspace { }; for (size_t i = 0; i < kdTrees.size(); ++i) { - if (kdTrees[i].empty()) continue; _treeStartIndices.push_back(_flatTrees.size()); // Mark start index of this KD-tree + if (kdTrees[i].empty()) continue; std::queue q; q.emplace(0, 0, 0, kdTrees[i].size()); diff --git a/modules/blackhole/rendering/renderableblackhole.cpp b/modules/blackhole/rendering/renderableblackhole.cpp index 7dd2a50478..8c85b19b5b 100644 --- a/modules/blackhole/rendering/renderableblackhole.cpp +++ b/modules/blackhole/rendering/renderableblackhole.cpp @@ -64,7 +64,8 @@ namespace { namespace openspace { RenderableBlackHole::RenderableBlackHole(const ghoul::Dictionary& dictionary) - : Renderable(dictionary, { .automaticallyUpdateRenderBin = false }), _solarMass(SolarMassInfo, 4.297e6f), _colorBVMapTexturePath(ColorTextureInfo) { + : Renderable(dictionary), _solarMass(SolarMassInfo, 4.297e6f), _colorBVMapTexturePath(ColorTextureInfo) { + setRenderBin(Renderable::RenderBin::Background); const Parameters p = codegen::bake(dictionary); @@ -112,18 +113,16 @@ namespace openspace { } void RenderableBlackHole::update(const UpdateData& data) { - if (data.modelTransform.translation != _lastTranslation) { - _starKDTree.build("${BASE}/sync/http/stars_du/6/stars.speck", data.modelTransform.translation, { {0, 25 }, {25, 50}, { 50, 100 } }); - _lastTranslation = data.modelTransform.translation; + if (data.modelTransform.translation != _chachedTranslation) { + _chachedTranslation = data.modelTransform.translation; + _starKDTree.build("${BASE}/sync/http/stars_du/6/stars.speck", _chachedTranslation, { {0, 25 }, {25, 50}, { 50, 100 } }); } _viewport.updateViewGrid(global::renderEngine->renderingResolution()); - glm::vec3 cameraPosition = global::navigationHandler->camera()->positionVec3(); - glm::vec3 anchorNodePosition = global::navigationHandler->anchorNode()->position(); - float distanceToAnchor = glm::distance(cameraPosition, anchorNodePosition) / static_cast(distanceconstants::LightYear); + glm::dvec3 cameraPosition = global::navigationHandler->camera()->positionVec3(); + float distanceToAnchor = static_cast(glm::distance(cameraPosition, _chachedTranslation) / distanceconstants::LightYear); if (abs(_rCamera - distanceToAnchor) > _rs * 0.01) { - _rCamera = distanceToAnchor; _rEnvmap = 2 * _rCamera; @@ -137,6 +136,9 @@ namespace openspace { void RenderableBlackHole::render(const RenderData& renderData, RendererTasks&) { _program->activate(); bindFramebuffer(); + + glDisable(GL_DEPTH_TEST); + ghoul::opengl::TextureUnit enviromentUnit; if (!bindTexture(_uniformCache.environmentTexture, enviromentUnit, _environmentTexture)) { LWARNING("UniformCache is missing 'environmentTexture'"); @@ -157,7 +159,7 @@ namespace openspace { interaction::OrbitalNavigator::CameraRotationDecomposition camRot = global::navigationHandler->orbitalNavigator().decomposeCameraRotationSurface( CameraPose{renderData.camera.positionVec3(), renderData.camera.rotationQuaternion()}, - *global::navigationHandler->anchorNode() + *parent() ); // Calculate the camera planes rotation to make sure fisheye works correcly (dcm in sgct projection.cpp) @@ -179,6 +181,8 @@ namespace openspace { drawQuad(); + glEnable(GL_DEPTH_TEST); + _program->deactivate(); glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/modules/blackhole/rendering/renderableblackhole.h b/modules/blackhole/rendering/renderableblackhole.h index 7b5506c341..a1039c3e25 100644 --- a/modules/blackhole/rendering/renderableblackhole.h +++ b/modules/blackhole/rendering/renderableblackhole.h @@ -43,7 +43,7 @@ namespace openspace { void loadEnvironmentTexture(); ghoul::opengl::ProgramObject* _program = nullptr; - glm::dvec3 _lastTranslation{}; + glm::dvec3 _chachedTranslation{}; size_t _rayCount = 1000; size_t _stepsCount = 50000; float _stepLength = 0.001f;