1
0
Fork 0
2018-soft-3d-renderer/Source/Light.hpp

59 lines
1.3 KiB
C++

#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;
};