diff --git a/modules/blackhole/CMakeLists.txt b/modules/blackhole/CMakeLists.txt index a7a252da96..d612446872 100644 --- a/modules/blackhole/CMakeLists.txt +++ b/modules/blackhole/CMakeLists.txt @@ -36,6 +36,12 @@ set(SOURCE_FILES ) source_group("Source Files" FILES ${SOURCE_FILES}) +set(SHADER_FILES + shaders/gradiant_vs.glsl + shaders/gradiant_fs.glsl +) +source_group("Shader Files" FILES ${SHADER_FILES}) + create_new_module( "BlackHole" black_hole_module diff --git a/modules/blackhole/blackholemodule.cpp b/modules/blackhole/blackholemodule.cpp index 6f814ed287..381e8ee6ab 100644 --- a/modules/blackhole/blackholemodule.cpp +++ b/modules/blackhole/blackholemodule.cpp @@ -42,6 +42,7 @@ namespace { namespace openspace { ghoul::opengl::ProgramObjectManager BlackHoleModule::ProgramObjectManager; +ghoul::opengl::TextureManager BlackHoleModule::TextureManager; documentation::Documentation BlackHoleModule::Documentation() { return codegen::doc("module_black_hole"); diff --git a/modules/blackhole/blackholemodule.h b/modules/blackhole/blackholemodule.h index 692a042999..4fa341478b 100644 --- a/modules/blackhole/blackholemodule.h +++ b/modules/blackhole/blackholemodule.h @@ -29,6 +29,7 @@ #include #include +#include namespace openspace { @@ -43,6 +44,7 @@ public: static documentation::Documentation Documentation(); static ghoul::opengl::ProgramObjectManager ProgramObjectManager; + static ghoul::opengl::TextureManager TextureManager; private: void internalInitialize(const ghoul::Dictionary&) override; diff --git a/modules/blackhole/rendering/gradiant_fs.glsl b/modules/blackhole/rendering/gradiant_fs.glsl deleted file mode 100644 index f1da5d9360..0000000000 --- a/modules/blackhole/rendering/gradiant_fs.glsl +++ /dev/null @@ -1,9 +0,0 @@ -#include "fragment.glsl" -in vec2 TexCoord; - -Fragment getFragment() { - Fragment frag; - // Gradient from blue (bottom-left) to purple (top-right) - frag.color = vec4(TexCoord.x, TexCoord.y, 1.0 - TexCoord.x, 1.0); - return frag; -} diff --git a/modules/blackhole/rendering/renderableblackhole.cpp b/modules/blackhole/rendering/renderableblackhole.cpp index 20e1c5ab03..f097148195 100644 --- a/modules/blackhole/rendering/renderableblackhole.cpp +++ b/modules/blackhole/rendering/renderableblackhole.cpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include #include @@ -25,25 +27,12 @@ namespace openspace { void RenderableBlackHole::initializeGL() { ZoneScoped; - std::string _vertexShaderPath = "${MODULE_BLACKHOLE}/rendering/gradiant_vs.glsl"; - std::string _fragmentShaderPath = "${MODULE_BLACKHOLE}/rendering/gradiant_fs.glsl"; + const std::string _vertexShaderPath = "${MODULE_BLACKHOLE}/rendering/gradiant_vs.glsl"; + const std::string _fragmentShaderPath = "${MODULE_BLACKHOLE}/rendering/gradiant_fs.glsl"; + const std::string texturePath = "${MODULE_BLACKHOLE}/rendering/uv.png"; LDEBUG(absPath(_vertexShaderPath).string()); - // Initialize shaders - std::string program = std::string(ProgramName); - program += "|vs=" + _vertexShaderPath; - program += "|fs=" + _fragmentShaderPath; - _program = BlackHoleModule::ProgramObjectManager.request( - program, - [this, _fragmentShaderPath, _vertexShaderPath, program]() -> std::unique_ptr { - const std::filesystem::path vs = absPath(_vertexShaderPath); - const std::filesystem::path fs = absPath(_fragmentShaderPath); - - return global::renderEngine->buildRenderProgram(program, vs, fs); - } - ); - // Screen quad VAO constexpr std::array QuadVtx = { // x y s t @@ -73,6 +62,29 @@ namespace openspace { glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), reinterpret_cast(2 * sizeof(GLfloat)) ); + + _enviromentTexture = ghoul::io::TextureReader::ref().loadTexture(absPath(texturePath), 2); + _enviromentTexture->uploadTexture(); + + if (_enviromentTexture == nullptr) { + LWARNING(std::format("Failed to load environment texture from path '{}'", absPath(texturePath).string())); + } + + // Initialize shaders + std::string program = std::string(ProgramName); + program += "|vs=" + _vertexShaderPath; + program += "|fs=" + _fragmentShaderPath; + _program = BlackHoleModule::ProgramObjectManager.request( + program, + [this, _fragmentShaderPath, _vertexShaderPath, program]() -> std::unique_ptr { + const std::filesystem::path vs = absPath(_vertexShaderPath); + const std::filesystem::path fs = absPath(_fragmentShaderPath); + + return global::renderEngine->buildRenderProgram(program, vs, fs); + } + ); + + ghoul::opengl::updateUniformLocations(*_program, _uniformCache); } void RenderableBlackHole::deinitializeGL() { @@ -87,15 +99,27 @@ namespace openspace { } void RenderableBlackHole::render(const RenderData&, RendererTasks&) { + _program->activate(); //Bind Buffer const GLint defaultFBO = ghoul::opengl::FramebufferObject::getActiveObject(); glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO); + ghoul::opengl::TextureUnit enviromentUnit; + if (_uniformCache.enviromentTexture != -1 && _enviromentTexture) { + enviromentUnit.activate(); + _enviromentTexture->bind(); + _program->setUniform(_uniformCache.enviromentTexture, enviromentUnit); + } + else { + LWARNING("UniformCache is missing \'enviromentTexture\'"); + } + // Draw - _program->activate(); glBindVertexArray(_quadVao); glDrawArrays(GL_TRIANGLES, 0, 6); + _program->deactivate(); + glBindTexture(GL_TEXTURE_2D, 0); } void RenderableBlackHole::update(const UpdateData& data) diff --git a/modules/blackhole/rendering/renderableblackhole.h b/modules/blackhole/rendering/renderableblackhole.h index e91800114b..70369855c1 100644 --- a/modules/blackhole/rendering/renderableblackhole.h +++ b/modules/blackhole/rendering/renderableblackhole.h @@ -29,6 +29,9 @@ private: GLuint _quadVao = 0; GLuint _quadVbo = 0; + UniformCache(enviromentTexture) _uniformCache; + + std::unique_ptr _enviromentTexture; }; } // openspace namespace diff --git a/modules/blackhole/rendering/uv.png b/modules/blackhole/rendering/uv.png new file mode 100644 index 0000000000..7024c468a0 Binary files /dev/null and b/modules/blackhole/rendering/uv.png differ diff --git a/modules/blackhole/shaders/gradiant_fs.glsl b/modules/blackhole/shaders/gradiant_fs.glsl new file mode 100644 index 0000000000..39dee87350 --- /dev/null +++ b/modules/blackhole/shaders/gradiant_fs.glsl @@ -0,0 +1,14 @@ +#include "fragment.glsl" +in vec2 TexCoord; + +uniform sampler2D enviromentTexture; + +Fragment getFragment() { + Fragment frag; + + vec4 texColor = texture(enviromentTexture, TexCoord); + + frag.color = texColor; + + return frag; +} diff --git a/modules/blackhole/rendering/gradiant_vs.glsl b/modules/blackhole/shaders/gradiant_vs.glsl similarity index 100% rename from modules/blackhole/rendering/gradiant_vs.glsl rename to modules/blackhole/shaders/gradiant_vs.glsl