4
\$\begingroup\$

I am attempting to add volumetric shadowing to an application I am working on to test some things. For this effect I wrote the following fragment

shader:#version 450

in float visible;
in vec3 normal;//Normal to the vertex
in vec3 vertexPos;//True position of the vertex (i.e it's location in space)

in vec2 texture_coord;

out vec4 outColor;//Final color of the pixel

uniform sampler2D text;

uniform vec4 color = vec4(1);//Default color
uniform vec3 lum = vec3(80,70,0.1);//A unique light position
uniform vec3 cameraPos = vec3(0);//The position of the camera in the world
uniform vec3 cameraDir = vec3(0);

float sphereIntersection(vec3 ray, vec3 origin, vec3 center, float radius)
{        

    float a = dot(ray,ray);
    float b = -2*dot(center, ray)+2*dot(ray,origin);
    float c = -2*dot(origin,center)+dot(center,center)
              -radius*radius+dot(origin,origin);

    float discriminant =b*b - 4*a*c;

    float t, t1, t2;
    if(discriminant < 0)
    {
        return -1;
    }

    else
    {
        t1=(-b-sqrt(discriminant))/(2*a);
        t2=(-b+sqrt(discriminant))/(2*a);
    }

    if(t1<0 && t2>=0)
        t1=t2;
    else if (t1>=0 && t2<0)
        t2=t1;

    return min(t1,t2);
}

void main()
{
  vec3 l = vec3(lum-vertexPos);
  if(length(l)>0)
    l = normalize(l);
  vec3 c = vec3(texture(text,abs(texture_coord)));
  vec3 n = normalize(normal);
  vec3 e = cameraPos-vertexPos;
  e = normalize(e);
  vec3 h = normalize(e+l);

  outColor = vec4(c*(vec3(0.5)+0.5*max(0,dot(n,l))) + vec3(0.1)*max(0,pow(dot(h,n), 100)), 1);

  float t = sphereIntersection(vec3(90,70,1)-vertexPos, vertexPos, vec3(80,80,3), 6);

  if(t>0.01)
  {
    outColor = vec4(t/100,0,0,0);
  }      
}

The issue is as follows, in the next image there's 2 shadows:

enter image description here

However as you can see, there's only one sphere and only one source of light and they are both hard coded, so I do not understand where the second shadow comes from (the one that does not have the circle).

\$\endgroup\$
  • \$\begingroup\$ From the picture, are you testing the the ray cast dot is > 0. It looks like you are getting an inverted situation where the ray cast originates (im assuming that the light is pointing to the left hand corner of this image). If its a point light instead of directional light, then it could be you are getting a intersection on the vector length < 0. All I got, sorry. \$\endgroup\$ – ErnieDingo Feb 5 '18 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.