Moved spherical coordinates conversion to shader.

Co-Authored-By: Emil Wallberg <49481622+EmilWallberg@users.noreply.github.com>
This commit is contained in:
Wilhelm Björkström
2025-02-28 10:41:22 +01:00
parent 3a0f024d55
commit 06b7a49699
2 changed files with 25 additions and 9 deletions
+4 -7
View File
@@ -35,18 +35,15 @@ namespace openspace {
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;
data[index] = x;
data[index + 1] = y;
}
}
updateViewGridTexture(data, screenSize);
+21 -2
View File
@@ -5,6 +5,7 @@ uniform sampler2D enviromentTexture;
uniform sampler2D viewGrid;
const float PI = 3.1415926535897932384626433832795f;
const float VIEWGRIDZ = -1.0f;
vec2 sphereToUV(vec2 sphereCoords){
float u = sphereCoords.x / (2.0f * PI) + 0.5f;
@@ -13,10 +14,28 @@ vec2 sphereToUV(vec2 sphereCoords){
return vec2(u, v);
}
float atan2(float a, float b){
if (b != 0.0f) return atan(a, b);
if (a > 0.0f) return PI / 2.0f;
if (a < 0.0f) return -PI / 2.0f;
return 0.0f;
}
vec2 cartisianToSphereical(vec2 cartisian) {
float theta = atan(sqrt(cartisian.x * cartisian.x + cartisian.y * cartisian.y) , VIEWGRIDZ);
float phi = atan2(cartisian.y, cartisian.x);
return vec2(phi, theta);
}
Fragment getFragment() {
Fragment frag;
vec2 sphereCoords = texture(viewGrid, TexCoord).rg;
vec4 texColor = texture(enviromentTexture, sphereToUV(sphereCoords));
vec2 cartisianCoords = texture(viewGrid, TexCoord).xy;
vec2 sphereicaleCoords = cartisianToSphereical(cartisianCoords);
vec2 uv = sphereToUV(sphereicaleCoords);
vec4 texColor = texture(enviromentTexture, uv);
frag.color = texColor;
return frag;