Files
directx-plane-game/Graphics2/ColorGradient.cpp
iDunnoDev bc906064e5 Added color gradient for the terrain blend maps
Added terrainBuffer struct
Added shared methods to genereate random UV's and intensities
Added water to the terrain
Added random UV's to the shader
Removed the vector3d class since DX can deal with that stuff...
2022-05-04 14:09:59 +01:00

56 lines
1.5 KiB
C++

#include "ColorGradient.h"
ColorGradient::ColorGradient(float min, float max, vector<RGBA> colorSteps)
{
_minValue = min;
_maxValue = max;
_colorSteps = colorSteps;
}
ColorGradient::~ColorGradient()
{
_colorSteps.clear();
}
// Get the RGBA value at the give point
RGBA ColorGradient::GetRGBAValue(float inputValue)
{
if (inputValue <= _minValue)
{
return _colorSteps.front();
}
else if (inputValue >= _maxValue)
{
return _colorSteps.back();
}
float range = _maxValue - _minValue;
float value = inputValue - _minValue;
float steps = range / (float)(_colorSteps.size() - 1);
int colorStepInside = (int)(value / steps);
float normalisedValue = (value - (colorStepInside * steps)) / steps;
return Interpolate(_colorSteps[colorStepInside], _colorSteps[colorStepInside + 1], normalisedValue);
}
// Method for interpolating the color between each step
RGBA ColorGradient::Interpolate(RGBA a, RGBA b, float pointValue)
{
if (pointValue <= 0.0f)
{
return a;
}
else if (pointValue >= 1.0f)
{
return b;
}
unsigned int currentRed = (unsigned int)((1.0f - pointValue) * a.red + pointValue * b.red);
unsigned int currentGreen = (unsigned int)((1.0f - pointValue) * a.green + pointValue * b.green);
unsigned int currentBlue = (unsigned int)((1.0f - pointValue) * a.blue + pointValue * b.blue);
unsigned int currentAlpha = (unsigned int)((1.0f - pointValue) * a.alpha + pointValue * b.alpha);
return RGBA{ currentRed, currentGreen, currentBlue, currentAlpha };
}