Search code examples
openglmathgraphicsrgbraytracing

Computing a pixel's specular value?


I've been looking around the internet to get the formula for computing's the diffuse value of a given pixel in a raytracer (where a ray intersection has occurred of course).

The examples I found all involve multiplying colours by other colours. Nvidia's formula is as follows:

diffuse = Kd x lightColor x max(N · L, 0)

where:

- Kd is the material's diffuse color
- lightColor is the color of the incoming diffuse light
- N is the normalized surface normal
- L is the normalized vector toward the light source
- P is the point being shaded

The formula doesn't seem too hard to me, but when trying to program it I find that Kd x lightColour is the multiplication of 2 RGB values. This is confusing to me as, unless it's a case of just multiplying both R, G and B values together, there's no way to multiply values like this.

Similarly, the specular formula requires the RGB value to be raised to a power equivalent to the shininess factor of the material, which is again a multiplication of a 3-component data type by itself.

This is probably me just getting confused with the maths, but if someone could clear this up it'd be greatly appreciated.


Solution

  • Kd x lightColour is a component-wise multiplication:

      a       d       a*d
    [ b ] x [ e ] = [ b*e ]
      c       f       c*f
    

    In the formula for specular illumination (assuming you are talking about the phong or blinn-phong illumination model), there is no power operation on vectors.

    specular = ks x lightcolor x (R · V)^a
    

    The result of R · V is a float, thus the power is only applied to a single number.