You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.3 KiB
58 lines
1.3 KiB
#pragma once |
|
|
|
#include "Camera.hpp" |
|
#include "Color.hpp" |
|
#include "Geometry.hpp" |
|
#include "Point.hpp" |
|
|
|
|
|
class Light |
|
{ |
|
public: |
|
ColorF32 Compute(Point& eye, Vector& normal, Material& material, Camera& camera) |
|
{ |
|
// Point light intensity is a function of the falloff factors and the distance |
|
Vector direction = position - eye; |
|
direction.Normalize(); |
|
float distance = direction.Length(); |
|
|
|
ColorF32 totalIntensity = |
|
(color * intensity) |
|
/ (falloffConstant + (falloffLinear * distance)); |
|
|
|
|
|
// Diffuse Light = Kr * I * (alpha + (1 - alpha) * n.d) |
|
float dotNormalDirection = std::max(Vector::Dot(normal, direction), 0.0f); |
|
float alpha = 0.2f; |
|
|
|
ColorF32 diffuseLight = |
|
material.diffuse |
|
* totalIntensity |
|
* (alpha + (1.0f - alpha) * dotNormalDirection); |
|
|
|
|
|
// Specular Light = Ks * I * (r.v)^sp |
|
Vector view = camera.position - eye; |
|
view.Normalize(); |
|
Vector reflection = (normal * 2.0f * dotNormalDirection) - direction; |
|
float dotReflectionView = std::max(Vector::Dot(reflection, view), 0.0f); |
|
|
|
ColorF32 specularLight = |
|
material.specular |
|
* totalIntensity |
|
* powf(dotReflectionView, material.glossiness); |
|
|
|
|
|
// Total light is sum of all lights |
|
ColorF32 result = diffuseLight + specularLight; |
|
|
|
|
|
return result; |
|
} |
|
|
|
Point position; |
|
ColorF32 color; |
|
float intensity; |
|
float falloffConstant; |
|
float falloffLinear; |
|
};
|
|
|