Refractor of blackhole renderable

This commit is contained in:
Emil Wallberg
2025-02-18 10:36:30 +01:00
committed by Wilhelm Björkström
parent 12de1366ec
commit 7ec8e33678
2 changed files with 88 additions and 77 deletions

View File

@@ -1,7 +1,5 @@
#include <modules/blackhole/rendering/renderableblackhole.h>
#include <modules/blackhole/blackholemodule.h>
#include <openspace/engine/globals.h>
#include <openspace/engine/windowdelegate.h>
#include <openspace/rendering/framebufferrenderer.h>
@@ -10,7 +8,6 @@
#include <ghoul/opengl/openglstatecache.h>
#include <ghoul/opengl/textureunit.h>
#include <ghoul/io/texture/texturereader.h>
#include <ghoul/logging/logmanager.h>
#include <ghoul/filesystem/filesystem.h>
#include <filesystem>
@@ -18,78 +15,34 @@
namespace {
constexpr std::string_view _loggerCat = "BlackHoleModule";
constexpr std::string_view ProgramName = "BlackHoleProgram";
constexpr std::array<GLfloat, 24> QuadVtx = {
-1.f, -1.f, 0.f, 0.f,
1.f, 1.f, 1.f, 1.f,
-1.f, 1.f, 0.f, 1.f,
-1.f, -1.f, 0.f, 0.f,
1.f, -1.f, 1.f, 0.f,
1.f, 1.f, 1.f, 1.f
};
}
namespace openspace {
RenderableBlackHole::RenderableBlackHole(const ghoul::Dictionary& dictionary) : Renderable(dictionary, { .automaticallyUpdateRenderBin = false }) {}
RenderableBlackHole::RenderableBlackHole(const ghoul::Dictionary& dictionary)
: Renderable(dictionary, { .automaticallyUpdateRenderBin = false }) {
}
void RenderableBlackHole::initialize() {
}
void RenderableBlackHole::initializeGL() {
ZoneScoped;
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());
// Screen quad VAO
constexpr std::array<GLfloat, 24> QuadVtx = {
// x y s t
-1.f, -1.f, 0.f, 0.f,
1.f, 1.f, 1.f, 1.f,
-1.f, 1.f, 0.f, 1.f,
-1.f, -1.f, 0.f, 0.f,
1.f, -1.f, 1.f, 0.f,
1.f, 1.f, 1.f, 1.f
};
glGenVertexArrays(1, &_quadVao);
glBindVertexArray(_quadVao);
glGenBuffers(1, &_quadVbo);
glBindBuffer(GL_ARRAY_BUFFER, _quadVbo);
// Send Quad to Buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(QuadVtx), QuadVtx.data(), GL_STATIC_DRAW);
//Bind aPos
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr);
//Bind aTexCoord
glEnableVertexAttribArray(1);
glVertexAttribPointer(
1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), reinterpret_cast<void*>(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<ghoul::opengl::ProgramObject> {
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);
setupQuad();
setupShaders();
loadEnvironmentTexture();
}
void RenderableBlackHole::deinitializeGL() {
glDeleteFramebuffers(1, &_framebuffer);
glDeleteBuffers(1, &_quadVbo);
glDeleteVertexArrays(1, &_quadVao);
}
@@ -100,35 +53,86 @@ namespace openspace {
void RenderableBlackHole::render(const RenderData&, RendererTasks&) {
_program->activate();
//Bind Buffer
bindFramebuffer();
bindEnvironmentTexture();
drawQuad();
_program->deactivate();
glBindTexture(GL_TEXTURE_2D, 0);
}
void RenderableBlackHole::setupShaders() {
const std::string vertexShaderPath = "${MODULE_BLACKHOLE}/shaders/gradiant_vs.glsl";
const std::string fragmentShaderPath = "${MODULE_BLACKHOLE}/shaders/gradiant_fs.glsl";
// 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<ghoul::opengl::ProgramObject> {
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::setupQuad() {
glGenVertexArrays(1, &_quadVao);
glBindVertexArray(_quadVao);
glGenBuffers(1, &_quadVbo);
glBindBuffer(GL_ARRAY_BUFFER, _quadVbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(QuadVtx), QuadVtx.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), reinterpret_cast<void*>(2 * sizeof(GLfloat)));
}
void RenderableBlackHole::loadEnvironmentTexture() {
const std::string texturePath = "${MODULE_BLACKHOLE}/rendering/uv.png";
_enviromentTexture = ghoul::io::TextureReader::ref().loadTexture(absPath(texturePath), 2);
if (_enviromentTexture) {
_enviromentTexture->uploadTexture();
}
else {
LWARNING(std::format("Failed to load environment texture from path '{}'", absPath(texturePath).string()));
}
}
void RenderableBlackHole::bindFramebuffer() {
const GLint defaultFBO = ghoul::opengl::FramebufferObject::getActiveObject();
glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
}
ghoul::opengl::TextureUnit enviromentUnit;
void RenderableBlackHole::bindEnvironmentTexture() {
if (_uniformCache.enviromentTexture != -1 && _enviromentTexture) {
ghoul::opengl::TextureUnit enviromentUnit;
enviromentUnit.activate();
_enviromentTexture->bind();
_program->setUniform(_uniformCache.enviromentTexture, enviromentUnit);
}
else {
LWARNING("UniformCache is missing \'enviromentTexture\'");
LWARNING("UniformCache is missing 'enviromentTexture'");
}
}
// Draw
void RenderableBlackHole::drawQuad() {
glBindVertexArray(_quadVao);
glDrawArrays(GL_TRIANGLES, 0, 6);
_program->deactivate();
glBindTexture(GL_TEXTURE_2D, 0);
}
void RenderableBlackHole::update(const UpdateData& data)
{
}
void RenderableBlackHole::update(const UpdateData&) {}
documentation::Documentation RenderableBlackHole::Documentation()
{
documentation::Documentation RenderableBlackHole::Documentation() {
return documentation::Documentation();
}
} //openspace namespace
} // namespace openspace

View File

@@ -15,6 +15,7 @@ public:
void initializeGL() override;
void deinitializeGL() override;
bool isReady() const override;
void render(const RenderData& data, RendererTasks& rendererTask) override;
@@ -23,6 +24,12 @@ public:
static documentation::Documentation Documentation();
private:
void bindFramebuffer();
void bindEnvironmentTexture();
void drawQuad();
void setupShaders();
void setupQuad();
void loadEnvironmentTexture();
ghoul::opengl::ProgramObject* _program = nullptr;
GLuint _framebuffer = 0;