82 lines
2.1 KiB
C++
82 lines
2.1 KiB
C++
#include "light.h"
|
|
|
|
|
|
Light::Light(int time, int angle): m_inclination(angle)
|
|
{
|
|
m_sunriseStart = 330;
|
|
m_sunriseEnd = 390;
|
|
m_sunsetStart = 1065;
|
|
m_sunsetEnd = 1095;
|
|
setTimeOfDay(time);
|
|
}
|
|
|
|
void Light::setTimeOfDay(int time)
|
|
{
|
|
setTimeOfDay(float(time));
|
|
}
|
|
|
|
void Light::setTimeOfDay(float time)
|
|
{
|
|
if (!qFuzzyCompare(m_timeOfDay, time)) {
|
|
m_timeOfDay = time;
|
|
updateSunlightDirection();
|
|
computeDayNightFactor();
|
|
timeOfDayChanged(int(time));
|
|
}
|
|
}
|
|
|
|
void Light::addToTimeOfDay(float timeDelta)
|
|
{
|
|
//qDebug() << timeDelta;
|
|
setTimeOfDay(m_timeOfDay + timeDelta);
|
|
}
|
|
|
|
void Light::setInclination(int angle)
|
|
{
|
|
m_inclination = angle;
|
|
updateSunlightDirection();
|
|
}
|
|
|
|
void Light::updateSunlightDirection()
|
|
{
|
|
float timeAngle = (float(m_timeOfDay) / 1440) * float(M_PI * 2) - float(M_PI);
|
|
float inclinationAngle = float(m_inclination) / 180 * float(M_PI) - float(M_PI / 2);
|
|
m_sunlightDirection = QVector4D(float(std::sin(timeAngle)),
|
|
float(std::cos(timeAngle) * std::cos(inclinationAngle)),
|
|
float(std::cos(timeAngle) * std::sin(inclinationAngle)),
|
|
0);
|
|
directionChanged(QVector3D(m_sunlightDirection));
|
|
}
|
|
|
|
void Light::computeDayNightFactor()
|
|
{
|
|
if (m_timeOfDay < m_sunriseStart || m_timeOfDay > m_sunsetEnd) {
|
|
m_dayNightFactor = 0;
|
|
|
|
} else if (m_timeOfDay > m_sunriseEnd && m_timeOfDay < m_sunsetStart) {
|
|
m_dayNightFactor = 1;
|
|
} else {
|
|
float t;
|
|
|
|
if (m_timeOfDay <= m_sunriseEnd) {
|
|
t = (m_timeOfDay - m_sunriseStart) / (m_sunriseEnd - m_sunriseStart);
|
|
} else {
|
|
t = (m_sunsetEnd - m_timeOfDay) / (m_sunsetEnd - m_sunsetStart);
|
|
}
|
|
|
|
m_dayNightFactor = 3 * t * t - 2 * t * t * t;
|
|
}
|
|
}
|
|
|
|
|
|
void Light::setUniforms(Shader* program, RenderPassType renderPass)
|
|
{
|
|
glUniform4fv(program->uniformLocation("u_lightPos"), 1, reinterpret_cast<const GLfloat *>(&m_sunlightDirection));
|
|
glUniform1f(program->uniformLocation("u_dayNightFactor"), m_dayNightFactor);
|
|
}
|
|
|
|
void Light::initGl()
|
|
{
|
|
initializeOpenGLFunctions();
|
|
}
|