Files
Graphics-Rasterizer/PointLight.cpp
IDunnoDev f5fd5b6756 Week8 [16/11] - [19/11]
Added Light Base Class
Added AmbientLight, DirectionalLight and PointLight Classes
Added Constructor to the Camera Class
Added Light CoEff's to the Model Class
Added Color Variables to Polygon3D Class
Added Light Vector to hold the lights in the Rasteriser
Added Camera Vector to hold multiple Cameras and Methods to set which is the current
Added Lighting to the Rasterizer
Added DrawSolidFlat method to the Rastorizer
Added Bounds Check to the SharedTools for Int and Float values
Added Color Enum Class
Added Normalize Method to the Vector3D Class
Renamed the TransformTools to SharedTools since adding more non transform methods to it
2021-12-11 17:05:43 +00:00

137 lines
3.1 KiB
C++

#include "PointLight.h"
PointLight::PointLight(Vertex lightPosition) : Light()
{
_lightPosition = lightPosition;
SetAttenuation(0, 0, 0);
}
PointLight::PointLight(Vertex lightPosition, float a, float b, float c) : Light(255, 255, 255)
{
_lightPosition = lightPosition;
SetAttenuation(a, b, c);
}
PointLight::PointLight(Vertex lightPosition, float a, float b, float c, int red, int green, int blue) : Light(red, green, blue)
{
_lightPosition = lightPosition;
SetAttenuation(a, b, c);
}
PointLight::PointLight(const PointLight& other) : Light(other)
{
Copy(other);
}
void PointLight::SetAttenuation(float a, float b, float c)
{
_attenuationA = a;
_attenuationB = b;
_attenuationC = c;
}
void PointLight::SetAttenuationA(float value)
{
_attenuationA = value;
}
void PointLight::SetAttenuationB(float value)
{
_attenuationB = value;
}
void PointLight::SetAttenuationC(float value)
{
_attenuationC = value;
}
float PointLight::GetAttenuationA()
{
return _attenuationA;
}
float PointLight::GetAttenuationB()
{
return _attenuationB;
}
float PointLight::GetAttenuationC()
{
return _attenuationC;
}
void PointLight::SetLightPosition(float x, float y, float z)
{
SetLightPosition(Vertex(x, y, z));
}
void PointLight::SetLightPosition(Vertex Position)
{
_lightPosition = Position;
}
Vertex PointLight::GetLightPosition() const
{
return _lightPosition;
}
COLORREF PointLight::CalculateLight(const Model& currentModel, const Polygon3D& currentPolygon, COLORREF colorIn)
{
float workingRed = (float)GetRedLightIntensity();
float workingGreen = (float)GetGreenLightIntensity();
float workingBlue = (float)GetBlueLightIntensity();
float fudge = 50;
workingRed *= currentModel.GetRedReflectionCoefficient();
workingGreen *= currentModel.GetGreenReflectionCoefficient();
workingBlue *= currentModel.GetBlueReflectionCoefficient();
Vector3D lightSource = currentModel.GetVertex(currentPolygon.GetIndex(0)) - _lightPosition;
Vector3D lightSourceNormalized = lightSource;
lightSourceNormalized.Normalize();
float distance = Vector3D::DotProduct(lightSource, currentPolygon.GetNormal());
float lightDotProd = Vector3D::DotProduct(lightSourceNormalized, currentPolygon.GetNormal());
float attenuation = 1 / (GetAttenuationA() + GetAttenuationB() * distance + GetAttenuationC() * (distance * distance));
workingRed *= lightDotProd;
workingGreen *= lightDotProd;
workingBlue *= lightDotProd;
workingRed *= attenuation;
workingGreen *= attenuation;
workingBlue *= attenuation;
workingRed *= fudge;
workingGreen *= fudge;
workingBlue *= fudge;
workingRed += (float)GetRValue(colorIn);
workingGreen += (float)GetGValue(colorIn);
workingBlue += (float)GetBValue(colorIn);
int finalRed = BoundsCheck(0, 255, (int)workingRed);
int finalGreen = BoundsCheck(0, 255, (int)workingGreen);
int finalBlue = BoundsCheck(0, 255, (int)workingBlue);
COLORREF outputColor = RGB(finalRed, finalGreen, finalBlue);
return outputColor;
}
PointLight& PointLight::operator= (const PointLight& rhs)
{
if (this != &rhs)
{
Copy(rhs);
}
return *this;
}
void PointLight::Copy(const PointLight& other)
{
_lightPosition = other.GetLightPosition();
Light::Copy(other);
}