Cleanup in fragment shader and start conditions

This commit is contained in:
Wilhelm Björkström
2025-03-06 17:30:45 +01:00
parent f0a5f948cc
commit 5321026626
5 changed files with 65 additions and 33 deletions
+4 -4
View File
@@ -10,13 +10,13 @@
"position": {
"x": 0,
"y": 0,
"z": -30
"z": 30
},
"type": "setNavigationState",
"up": {
"x": -0.3976079901497721,
"y": 0.9175553858863555,
"z": 0
"x": 0.0,
"y": 1.0,
"z": 0.0
},
"yaw": 0.0
},
+1 -1
View File
@@ -36,7 +36,7 @@ __global__ void solveGeodesicKernel(float rs, float envmap_r, float u_0, float*
float r = 1.0f / u;
float r_0 = 1.0f / u_0;
float a = r * sin(phi);
float b = r_0 - r * cos(phi);
float b = r * cos(phi) - r_0;
// Store starting angle (angle is local to camera)
angles_out[idx * 2] = atan2(a, b);
@@ -71,8 +71,8 @@ namespace openspace {
bindFramebuffer();
ghoul::opengl::TextureUnit enviromentUnit;
if (!bindTexture(_uniformCache.enviromentTexture, enviromentUnit, _enviromentTexture)) {
LWARNING("UniformCache is missing 'enviromentTexture'");
if (!bindTexture(_uniformCache.environmentTexture, enviromentUnit, _environmentTexture)) {
LWARNING("UniformCache is missing 'environmentTexture'");
}
ghoul::opengl::TextureUnit viewGridUnit;
@@ -84,7 +84,7 @@ namespace openspace {
_program->setUniform(
_uniformCache.cameraRotationMatrix,
glm::mat4(global::navigationHandler->camera()->combinedViewMatrix())
glm::mat4(global::navigationHandler->camera()->viewRotationMatrix())
);
drawQuad();
@@ -151,10 +151,10 @@ namespace openspace {
//const std::string texturePath = "C:/Users/wilbj602/Documents/GitHub/OpenSpace/sync/http/milkyway_textures/2/DarkUniverse_mellinger_8k.jpg";
const std::string texturePath = "C:/Users/wilbj602/Downloads/img.jpg";
_enviromentTexture = ghoul::io::TextureReader::ref().loadTexture(absPath(texturePath), 2);
_environmentTexture = ghoul::io::TextureReader::ref().loadTexture(absPath(texturePath), 2);
if (_enviromentTexture) {
_enviromentTexture->uploadTexture();
if (_environmentTexture) {
_environmentTexture->uploadTexture();
}
else {
LWARNING(std::format("Failed to load environment texture from path '{}'", absPath(texturePath).string()));
@@ -52,9 +52,9 @@ namespace openspace {
GLuint _quadVbo = 0;
GLuint _ssboData = 0;
GLuint modelMatricesBuffer;
UniformCache(enviromentTexture, viewGrid, cameraRotationMatrix, schwarzschildWarpTable) _uniformCache;
UniformCache(environmentTexture, viewGrid, cameraRotationMatrix, schwarzschildWarpTable) _uniformCache;
std::unique_ptr<ghoul::opengl::Texture> _enviromentTexture;
std::unique_ptr<ghoul::opengl::Texture> _environmentTexture;
};
} // openspace namespace
+52 -20
View File
@@ -1,7 +1,11 @@
#include "fragment.glsl"
in vec2 TexCoord;
uniform sampler2D enviromentTexture;
#define SHOW_BLACK_HOLE 1
uniform sampler2D environmentTexture;
uniform sampler2D viewGrid;
uniform mat4 cameraRotationMatrix;
@@ -9,18 +13,12 @@ layout (std430) buffer ssbo_warp_table {
float schwarzschildWarpTable[];
};
const float PI = 3.1415926535897932384626433832795f;
const float VIEWGRIDZ = -1.0f;
const float INF = 1.0f/0.0f;
vec2 sphereToUV(vec2 sphereCoords){
float u = sphereCoords.x / (2.0f * PI) + 0.5f;
float v = sphereCoords.y / PI;
return vec2(u, v);
}
float getEndAngleFromTable(float phi){
float endPhiWdithLeastDistance;
float currentDistance = INF;
@@ -31,13 +29,13 @@ float getEndAngleFromTable(float phi){
float phiDistance = abs(schwarzschildWarpTable[i] - phi);
if(phiDistance < currentDistance){
currentDistance = phiDistance;
endPhiWdithLeastDistance = schwarzschildWarpTable[i+1];
endPhiWdithLeastDistance = schwarzschildWarpTable[i + 1];
}
}
return endPhiWdithLeastDistance;
}
vec2 localToEnvSphereCoords(vec2 cameraOutSphereCoords){
vec2 applyBlackHoleWarp(vec2 cameraOutSphereCoords){
float phi = cameraOutSphereCoords.x;
float theta = cameraOutSphereCoords.y;
theta = getEndAngleFromTable(theta);
@@ -53,30 +51,64 @@ float atan2(float a, float b){
return 0.0f;
}
vec2 cartisianToSphereical(vec3 cartisian) {
vec2 cartesianToSpherical(vec3 cartisian) {
float theta = atan2(sqrt(cartisian.x * cartisian.x + cartisian.y * cartisian.y) , cartisian.z);
float phi = atan2(cartisian.y, cartisian.x);
return vec2(phi, theta);
}
vec3 sphericalToCartesian(float phi, float theta){
float x = sin(theta)*cos(phi);
float y = sin(theta)*sin(phi);
float z = cos(theta);
return vec3(x, y, z);
}
vec2 sphericalToUV(vec2 sphereCoords){
float u = sphereCoords.x / (2.0f * PI) + 0.5f;
float v = mod(sphereCoords.y, PI) / PI;
return vec2(u, v);
}
Fragment getFragment() {
Fragment frag;
vec4 cartisianCoords = normalize(vec4(texture(viewGrid, TexCoord).xy, VIEWGRIDZ, 0.0f));
cartisianCoords = cameraRotationMatrix * cartisianCoords;
vec4 viewCoords = normalize(vec4(texture(viewGrid, TexCoord).xy, VIEWGRIDZ, 0.0f));
vec4 rotatedViewCoords = cameraRotationMatrix * viewCoords;
vec2 sphereicaleCoords = cartisianToSphereical(cartisianCoords.xyz);
vec2 envSphereCoords = localToEnvSphereCoords(sphereicaleCoords);
vec2 sphericalCoords = cartesianToSpherical(rotatedViewCoords.xyz);
if (isnan(envSphereCoords.y)) {
frag.color = vec4(0);
vec2 envMapSphericalCoords;
#if SHOW_BLACK_HOLE == 1
// Apply black hole warping to spherical coordinates
envMapSphericalCoords = applyBlackHoleWarp(sphericalCoords);
if (isnan(envMapSphericalCoords.y)) {
// If inside the event horizon
frag.color = vec4(0.0f);
return frag;
}
#else
envMapSphericalCoords = sphericalCoords;
#endif
vec2 uv = sphereToUV(envSphereCoords);
vec4 texColor = texture(enviromentTexture, uv);
// Init rotation of the black hole
vec4 envMapCoords = vec4(sphericalToCartesian(envMapSphericalCoords.x, envMapSphericalCoords.y), 0.0f);
float rotationAngle = PI/2;
mat4 rotationMatrixX = mat4(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, cos(rotationAngle), -sin(rotationAngle), 0.0f,
0.0f, sin(rotationAngle), cos(rotationAngle), 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
envMapCoords = rotationMatrixX * envMapCoords;
sphericalCoords = cartesianToSpherical(envMapCoords.xyz);
vec2 uv = sphericalToUV(sphericalCoords);
vec4 texColor = texture(environmentTexture, uv);
frag.color = texColor;
return frag;
}
}