mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-01-04 18:51:17 -06:00
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:
committed by
Wilhelm Björkström
parent
0a05bfff24
commit
12de1366ec
@@ -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
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -29,6 +29,9 @@ private:
|
||||
GLuint _quadVao = 0;
|
||||
GLuint _quadVbo = 0;
|
||||
|
||||
UniformCache(enviromentTexture) _uniformCache;
|
||||
|
||||
std::unique_ptr<ghoul::opengl::Texture> _enviromentTexture;
|
||||
};
|
||||
|
||||
} // openspace namespace
|
||||
|
||||
BIN
modules/blackhole/rendering/uv.png
Normal file
BIN
modules/blackhole/rendering/uv.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 444 KiB |
14
modules/blackhole/shaders/gradiant_fs.glsl
Normal file
14
modules/blackhole/shaders/gradiant_fs.glsl
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user