Search code examples
graphicsopengl-esglslraytracing

Issue with implementing phong reflectance in glsl fragment shader


I've written a fragment shader that shades a sphere using ray tracing.I'm now trying to implement the phong model for specular reflectance which uses one equation: R = 2 (N • L) N - L where L is the light direction and N is the surface normal. The value of reflection vector R is then plugged into: s_rgb max(0, E • R)^p where E is the eye direction of the viewer (in this case it is -w or the negative direction of the ray), p is the specular power and s_rgb is the color of the specular reflection. In my current implementation of the function used to determine the color of the sphere, I'm getting errors with the max and power functions. Here is the code for the function:

vec3 shadeSphere(vec3 point, vec4 sphere, vec3 material,vec3 eyeDir) {
      vec3 color = vec3(1.,2.,4.);
      vec3 N = (point - sphere.xyz) / sphere.w;
      float diffuse = max(dot(Ldir, N), 0.0);
      vec3 ambient = material/5.0;
      vec3 R = 2.*(dot(N, Ldir))*(N - Ldir);
      float reflect = max(0.0,dot(eyeDir,R));
      float phong= pow(reflect, 2);
      color = phong + ambient + Lrgb  *diffuse *  max(0.0, dot(N , Ldir));
      return color;
   }

For some reason, the pow() function is returning the error
ERROR: 0:49: 'pow' : no matching overloaded function found``` I think that the second parameter of the max function might be causing the problem but it seems like it should work. Can anyone spot the problem with the function?


Solution

  • It kinda looks like you're just missing the dot. "There's no overload" for pow that takes a float with an int.

    float phong= pow(reflect, 2);   // nope
    float phong= pow(reflect, 2.);  // ok