OpenGL Atmospheric scattering from space problem

I hope this is the correct group to post a GLSL question. I’m developing a space shooter 3D game and I have a problem with some shader that I found online. The shader is supposed to create an atmosphere around a planet. Everything works fine when the planet is on position 0.0.0 but when I move the planet around it looks like the scattering is still calculated on 0.0.0 here are some pictures :

and here is the problem :

Here is the vertex shader :

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
layout (location = 3) in vec3 aTangent;
layout (location = 4) in vec3 aBitangent;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;

out vec4 fPosition;
out vec3 fNormal;

void main()
{	
fPosition = model * vec4(aPos, 1.f);
fNormal = mat3(model) * aNormal;
gl_Position = projection * view * fPosition;
}

and here is the fragment shader :

#version 330 core
layout (location = 0) out vec4 FragColor;
layout (location = 1) out vec4 BrightColor;
layout (location = 2) out vec4 GodRaysColor;

uniform vec3 camPosition;
uniform vec3 lightPos;
uniform float fInnerRadius;
uniform float fOuterRadius;

in vec4 fPosition;
in vec3 fNormal;

const float PI = 3.14159265359;
const float degToRad = PI / 180.0;
const float MAX = 100000.0;

const float DEG_TO_RAD = PI / 180.0;
float K_R = 0.166;
const float K_M = 0.0025;
const float E = 14.3;
const vec3 C_R = vec3(0.3, 0.7, 1.0);
const float G_M = -0.85;

float SCALE_H = 4.0 / (fOuterRadius - fInnerRadius);
float SCALE_L = 1.0 / (fOuterRadius - fInnerRadius);

const int numOutScatter = 10;
const float fNumOutScatter = 10.0;
const int numInScatter = 10;
const float fNumInScatter = 10.0;

vec3 rayDirection(vec3 camPos) {
vec4 ray = fPosition - vec4(camPos, 1.0);
return normalize(vec3(ray));
}

vec2 rayIntersection(vec3 p, vec3 dir, float radius ) {
float b = dot( p, dir );
float c = dot( p, p ) - radius * radius;

float d = b * b - c;
if ( d < 0.0 ) {
	return vec2( MAX, -MAX );
}
d = sqrt( d );

float near = -b - d;
float far = -b + d;

return vec2(near, far);
}

// Mie
// g : ( -0.75, -0.999 )
//      3 * ( 1 - g^2 )               1 + c^2
// F = ----------------- * -------------------------------
//      2 * ( 2 + g^2 )     ( 1 + g^2 - 2 * g * c )^(3/2)
float miePhase( float g, float c, float cc ) {
float gg = g * g;

float a = ( 1.0 - gg ) * ( 1.0 + cc );

float b = 1.0 + gg - 2.0 * g * c;
b *= sqrt( b );
b *= 2.0 + gg;	

return 1.5 * a / b;
}

// Reyleigh
// g : 0
// F = 3/4 * ( 1 + c^2 )
float rayleighPhase( float cc ) {
return 0.75 * ( 1.0 + cc );
}

float density(vec3 p) {
return exp(-(length(p) - fInnerRadius) * SCALE_H);
}

float optic(vec3 p, vec3 q) {
vec3 step = (q - p) / fNumOutScatter;
vec3 v = p + step * 0.5;

float sum = 0.0;
for(int i = 0; i < numOutScatter; i++) {
	sum += density(v);
	v += step;
}
sum *= length(step)*SCALE_L;
return sum;
}

vec3 inScatter(vec3 o, vec3 dir, vec2 e, vec3 l) {
float len = (e.y - e.x) / fNumInScatter;
vec3 step = dir * len;
vec3 p = o + dir * e.x;
vec3 v = p + dir * (len * 0.5);
vec3 sum = vec3(0.0);

for(int i = 0; i < numInScatter; i++) {
	vec2 f = rayIntersection(v, l, fOuterRadius);
	vec3 u = v + l * f.y;
	float n = (optic(p, v) + optic(v, u))*(PI * 4.0);
	sum += density(v)* exp(-n * ( K_R * C_R + K_M ));
	v += step;
}

sum *= len * SCALE_L;
float c = dot(dir, -l);
float cc = c * c;
return sum * ( K_R * C_R * rayleighPhase( cc ) + K_M * miePhase( G_M, c, cc ) ) * E;
}

void main (void)
{
vec3 dir = rayDirection(camPosition);
vec3 eye = camPosition;

vec3 l = normalize(lightPos - fPosition.xyz);

vec2 e = rayIntersection(eye, dir, fOuterRadius);

vec2 f = rayIntersection(eye, dir, fInnerRadius);
e.y = min(e.y, f.x);

vec3 I = inScatter(eye, dir, e, l);

FragColor = vec4(I, 0.5);

    float brightness = dot(FragColor.rgb, vec3(0.2126, 0.7152, 0.0722));
    if(brightness > 1.0)
        BrightColor = FragColor;
    else
        BrightColor = vec4(0.0, 0.0, 0.0, 1.0);

    GodRaysColor = vec4(0.f, 0.f, 0.f, 1.f);
}

Can anyone identify what seems to be the problem when I move the earth to the negative z and negative y that creates that off place effect? I can also post a video if the problem is not visible enough. I can’t see in the fragment shader something that indicates that the center of the atmosphere is on 0.0.0. Thanks for reading

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.