Advertisement
Bisqwit

Specular per-pixel lighting

Oct 1st, 2015
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #if SUPPORT_SPECULAR_REFLECTION
  2.             auto normal = polygon.plane.Normal();
  3.             if(polygon.normalmap >= 0)
  4.             {
  5.                 // Load sample from normalmap
  6.                 #if TEXTUREMODE==2
  7.                 xyz<float> rgb = BilinearTextureFetch<32>(polygon.normalmap, {u,v}, {u0,v0});
  8.                 #else
  9.                 unsigned c = texturedata[polygon.normalmap][ (u % txth) * txtw + (v % txtw) ];
  10.                 xyz<float> rgb = dergb(c);
  11.                 #endif
  12.                 // Convert it into a vector of 3 floats
  13.                 auto perturb = rgb * (1 / 127.5f) - 1.f;
  14.                 // Tweak the normal by that vector
  15.                 normal = normal            * perturb[2]
  16.                        - polygon.tangent   * perturb[1]
  17.                        + polygon.bitangent * perturb[0];
  18.             }
  19.             xyz<float> point = xyz<float>(prop(5), prop(6), prop(7)); // 3D location of this pixel in space
  20.             auto addlight = [&] (const Light& l)
  21.             {
  22.                 constexpr float fade_distance = 655360, fade_power = 2;
  23.                 xyz<float> light_to_point = (l.v - point);
  24.                 xyz<float> V = (observer - point).Normalized(); // point to camera
  25.                 float v = V.DotProduct(normal);
  26.                 float factor = light_to_point.Normalized().DotProduct( normal * (v+v) - V );
  27.                 if(factor > 0)
  28.                 {
  29.                     // Do phong/roughness adjustment
  30.                     factor = std::pow(factor, 12.0f);
  31.                     // Do distance fading to compensate for the point-light
  32.                     factor /= 1 + std::pow(light_to_point.Length()*2 / fade_distance, fade_power);
  33.                     pix += l.rgb * factor;
  34.                 }
  35.             };  
  36.             for(const auto& l: level.lights) addlight(l);
  37.             /*for(const auto& d: level.decorations)
  38.                 for(const auto& l: d.object->lights) addlight(l);*/
  39. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement