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
2
\$\begingroup\$

The issue was not testing that the intersection needs to happen between the object and the light position. i.e I was raytracing past the light.

i.e the check: if(t>0.01) should instead be if(t>0.01 && t < length(object_pos - light_pos))

Or in other words, the intersection must happen between the position we are currently testing for shadowing an the light position. You also need to make sure your direction vector is normalized.

\$\endgroup\$
  • \$\begingroup\$ Can you show what code you needed to change to fix this? \$\endgroup\$ – DMGregory Sep 30 at 15:07
  • \$\begingroup\$ I don;t have the exact same code but I can provide the pseudo code \$\endgroup\$ – Makogan Sep 30 at 15:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.