Environment map from spherical coordinates.

Co-Authored-By: Emil Wallberg <49481622+EmilWallberg@users.noreply.github.com>
This commit is contained in:
Wilhelm Björkström
2025-02-27 17:32:22 +01:00
parent c775ec4eb7
commit 65b89c2c8b
8 changed files with 112 additions and 36 deletions

View File

@@ -40,8 +40,8 @@ set(SOURCE_FILES
# Define Shader Files
set(SHADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/shaders/gradiant_vs.glsl
${CMAKE_CURRENT_SOURCE_DIR}/shaders/gradiant_fs.glsl
${CMAKE_CURRENT_SOURCE_DIR}/shaders/blackhole_vs.glsl
${CMAKE_CURRENT_SOURCE_DIR}/shaders/blackhole_fs.glsl
)
# Organize Files into Groups

View File

@@ -8,7 +8,6 @@
#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>
@@ -41,10 +40,12 @@ namespace openspace {
}
void RenderableBlackHole::initializeGL() {
const glm::vec2 screenSize = glm::vec2(global::renderEngine->renderingResolution());
ZoneScoped;
setupQuad();
setupShaders();
loadEnvironmentTexture();
_viewport.uploadViewGrid(screenSize);
}
void RenderableBlackHole::deinitializeGL() {
@@ -60,15 +61,27 @@ namespace openspace {
void RenderableBlackHole::render(const RenderData&, RendererTasks&) {
_program->activate();
bindFramebuffer();
bindEnvironmentTexture();
ghoul::opengl::TextureUnit enviromentUnit;
if (!bindTexture(_uniformCache.enviromentTexture, enviromentUnit, _enviromentTexture)) {
LWARNING("UniformCache is missing 'enviromentTexture'");
}
ghoul::opengl::TextureUnit viewGridUnit;
if (!bindTexture(_uniformCache.viewGrid, viewGridUnit, _viewport.viewGrid)) {
LWARNING("UniformCache is missing 'viewGrid'");
}
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";
const std::string vertexShaderPath = "${MODULE_BLACKHOLE}/shaders/blackhole_vs.glsl";
const std::string fragmentShaderPath = "${MODULE_BLACKHOLE}/shaders/blackhole_fs.glsl";
// Initialize shaders
std::string program = std::string(ProgramName);
@@ -104,6 +117,7 @@ namespace openspace {
void RenderableBlackHole::loadEnvironmentTexture() {
const std::string texturePath = "${MODULE_BLACKHOLE}/rendering/uv.png";
//const std::string texturePath = "C:/Users/wilbj602/Documents/GitHub/OpenSpace/sync/http/milkyway_textures/2/DarkUniverse_mellinger_8k.jpg";
_enviromentTexture = ghoul::io::TextureReader::ref().loadTexture(absPath(texturePath), 2);
if (_enviromentTexture) {
@@ -119,18 +133,13 @@ namespace openspace {
glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
}
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'");
}
bool RenderableBlackHole::bindTexture(GLint chacheRegistry, ghoul::opengl::TextureUnit& textureUnit, std::unique_ptr<ghoul::opengl::Texture>& texture) {
if (!texture) return false;
//invProjection, invView
textureUnit.activate();
texture->bind();
_program->setUniform(chacheRegistry, textureUnit);
return true;
}
void RenderableBlackHole::drawQuad() {

View File

@@ -4,6 +4,7 @@
#include <openspace/rendering/renderable.h>
#include <modules/blackhole/rendering/viewport.h>
#include <ghoul/opengl/uniformcache.h>
#include <ghoul/opengl/textureunit.h>
namespace openspace {
@@ -26,7 +27,7 @@ public:
private:
void bindFramebuffer();
void bindEnvironmentTexture();
bool bindTexture(GLint chacheRegistry, ghoul::opengl::TextureUnit& textureUnit, std::unique_ptr<ghoul::opengl::Texture>& texture);
void drawQuad();
void setupShaders();
void setupQuad();
@@ -39,7 +40,7 @@ private:
GLuint _quadVao = 0;
GLuint _quadVbo = 0;
UniformCache(enviromentTexture, invProjection, invView) _uniformCache;
UniformCache(enviromentTexture, viewGrid) _uniformCache;
std::unique_ptr<ghoul::opengl::Texture> _enviromentTexture;
};

View File

@@ -1,7 +1,14 @@
#include <modules/blackhole/rendering/viewport.h>
#include <glm/gtc/constants.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <ghoul/opengl/texture.h>
#include <ghoul/logging/logmanager.h>
#ifndef PI
#define PI 3.1415926535897932384626433832795f
#endif
namespace {
constexpr std::string_view _loggerCat = "BlackHoleViewPort";
}
namespace openspace {
@@ -19,4 +26,49 @@ namespace openspace {
return glm::dvec3(r, theta, phi);
}
void ViewPort::uploadViewGrid(const glm::ivec2& screenSize) {
float halfWidth = tan(fovHorizontel / 2.f);
float halfHeight = halfWidth * screenSize.y / screenSize.x;
float stepSize = (2.f * halfWidth) / screenSize.x;
std::vector<float> data(screenSize.x * screenSize.y * 2, 0.0f);
float z = -1.0f;
for (int i = 0; i < screenSize.y; i++) {
float y = (i - screenSize.y / 2) * stepSize;
for (int j = 0; j < screenSize.x; j++) {
float x = (j - screenSize.x / 2) * stepSize;
float theta = atan2(sqrt(x * x + y * y) , z);
float phi = atan2(y, x);
int index = (screenSize.x * i + j) * 2;
data[index] = phi;
data[index + 1] = theta;
}
}
updateViewGridTexture(data, screenSize);
}
void ViewPort::updateViewGridTexture(std::vector<float>& data, const glm::vec2& screenSize) {
viewGrid = std::make_unique<ghoul::opengl::Texture>(
data.data(),
glm::uvec3(screenSize.x, screenSize.y, 1),
GL_TEXTURE_2D,
ghoul::opengl::Texture::Format::RG,
GL_RG32F,
GL_FLOAT
);
if (viewGrid) {
viewGrid->uploadTexture();
}
else {
LWARNING("Failed to load view grid on update");
}
}
} // openspace namespace

View File

@@ -2,6 +2,8 @@
#define __OPENSPACE_MODULE_BLACKHOLE___BLACKHOLECAMERA___H__
#include <openspace/camera/camera.h>
#include <ghoul/opengl/uniformcache.h>
#include <ghoul/opengl/texture.h>
#include <glm/glm.hpp>
namespace openspace {
@@ -11,9 +13,12 @@ namespace openspace {
ViewPort(Camera* camera);
glm::dvec3 sphericalPosition() const;
void uploadViewGrid(const glm::ivec2& screenSize);
void updateViewGridTexture(std::vector<float>& data, const glm::vec2& screenSize);
std::unique_ptr<ghoul::opengl::Texture> viewGrid;
private:
Camera* _camera = nullptr;
float fovHorizontel = glm::radians(120.f);
};
} // namespace openspace

View File

@@ -0,0 +1,23 @@
#include "fragment.glsl"
in vec2 TexCoord;
uniform sampler2D enviromentTexture;
uniform sampler2D viewGrid;
const float PI = 3.1415926535897932384626433832795f;
vec2 sphereToUV(vec2 sphereCoords){
float u = sphereCoords.x / (2.0f * PI) + 0.5f;
float v = sphereCoords.y / PI;
return vec2(u, v);
}
Fragment getFragment() {
Fragment frag;
vec2 sphereCoords = texture(viewGrid, TexCoord).rg;
vec4 texColor = texture(enviromentTexture, sphereToUV(sphereCoords));
frag.color = texColor;
return frag;
}

View File

@@ -1,14 +0,0 @@
#include "fragment.glsl"
in vec2 TexCoord;
uniform sampler2D enviromentTexture;
Fragment getFragment() {
Fragment frag;
vec4 texColor = texture(enviromentTexture, TexCoord);
frag.color = texColor;
return frag;
}