Texture rendering

Rendering texture to the camera quad

Co-Authored-By: Wilhelm Björkström <143391787+Grantallkotten@users.noreply.github.com>
This commit is contained in:
Emil Wallberg
2025-02-17 16:25:20 +01:00
committed by Wilhelm Björkström
parent 0a05bfff24
commit 12de1366ec
9 changed files with 67 additions and 26 deletions

View File

@@ -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

View File

@@ -42,6 +42,7 @@ namespace {
namespace openspace {
ghoul::opengl::ProgramObjectManager BlackHoleModule::ProgramObjectManager;
ghoul::opengl::TextureManager BlackHoleModule::TextureManager;
documentation::Documentation BlackHoleModule::Documentation() {
return codegen::doc<Parameters>("module_black_hole");

View File

@@ -29,6 +29,7 @@
#include <openspace/documentation/documentation.h>
#include <ghoul/opengl/programobjectmanager.h>
#include <ghoul/opengl/texturemanager.h>
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;

View File

@@ -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;
}

View File

@@ -8,6 +8,8 @@
#include <openspace/rendering/renderengine.h>
#include <ghoul/opengl/framebufferobject.h>
#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>
@@ -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<ghoul::opengl::ProgramObject> {
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<GLfloat, 24> QuadVtx = {
// x y s t
@@ -73,6 +62,29 @@ namespace openspace {
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);
}
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)

View File

@@ -29,6 +29,9 @@ private:
GLuint _quadVao = 0;
GLuint _quadVbo = 0;
UniformCache(enviromentTexture) _uniformCache;
std::unique_ptr<ghoul::opengl::Texture> _enviromentTexture;
};
} // openspace namespace

Binary file not shown.

After

Width:  |  Height:  |  Size: 444 KiB

View File

@@ -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;
}