2012-03-20 01:57:42 +01:00
|
|
|
#include "lc_global.h"
|
2012-03-29 03:10:55 +02:00
|
|
|
#include "lc_math.h"
|
2012-03-25 01:47:53 +01:00
|
|
|
#include "lc_colors.h"
|
2011-09-07 23:06:51 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include "light.h"
|
2014-04-14 05:20:16 +02:00
|
|
|
#include "lc_application.h"
|
|
|
|
#include "lc_context.h"
|
2011-09-07 23:06:51 +02:00
|
|
|
|
2014-11-24 01:43:13 +01:00
|
|
|
#define LC_LIGHT_POSITION_EDGE 7.5f
|
|
|
|
#define LC_LIGHT_TARGET_EDGE 5.0f
|
|
|
|
#define LC_LIGHT_SPHERE_RADIUS 5.0f
|
|
|
|
|
2012-09-10 01:42:57 +02:00
|
|
|
// New omni light.
|
2014-05-01 20:42:11 +02:00
|
|
|
lcLight::lcLight(float px, float py, float pz)
|
2020-04-19 04:45:21 +02:00
|
|
|
: lcObject(lcObjectType::Light)
|
2011-09-07 23:06:51 +02:00
|
|
|
{
|
2014-08-31 02:53:12 +02:00
|
|
|
Initialize(lcVector3(px, py, pz), lcVector3(0.0f, 0.0f, 0.0f));
|
2014-01-30 04:13:34 +01:00
|
|
|
UpdatePosition(1);
|
2011-09-07 23:06:51 +02:00
|
|
|
}
|
|
|
|
|
2012-09-10 01:42:57 +02:00
|
|
|
// New directional or spot light.
|
2014-05-01 20:42:11 +02:00
|
|
|
lcLight::lcLight(float px, float py, float pz, float tx, float ty, float tz)
|
2020-04-19 04:45:21 +02:00
|
|
|
: lcObject(lcObjectType::Light)
|
2011-09-07 23:06:51 +02:00
|
|
|
{
|
2014-08-31 02:53:12 +02:00
|
|
|
Initialize(lcVector3(px, py, pz), lcVector3(tx, ty, tz));
|
2014-11-24 01:43:13 +01:00
|
|
|
mState |= LC_LIGHT_SPOT;
|
2014-01-30 04:13:34 +01:00
|
|
|
UpdatePosition(1);
|
2011-09-07 23:06:51 +02:00
|
|
|
}
|
|
|
|
|
2014-08-31 02:53:12 +02:00
|
|
|
void lcLight::Initialize(const lcVector3& Position, const lcVector3& TargetPosition)
|
2011-09-07 23:06:51 +02:00
|
|
|
{
|
2014-05-01 16:55:12 +02:00
|
|
|
mState = 0;
|
2012-08-17 01:50:40 +02:00
|
|
|
|
2021-01-05 20:26:01 +01:00
|
|
|
mPositionKeys.ChangeKey(Position, 1, true);
|
|
|
|
mTargetPositionKeys.ChangeKey(TargetPosition, 1, true);
|
|
|
|
mAmbientColorKeys.ChangeKey(lcVector4(0.0f, 0.0f, 0.0f, 1.0f), 1, true);
|
|
|
|
mDiffuseColorKeys.ChangeKey(lcVector4(0.8f, 0.8f, 0.8f, 1.0f), 1, true);
|
|
|
|
mSpecularColorKeys.ChangeKey(lcVector4(1.0f, 1.0f, 1.0f, 1.0f), 1, true);
|
|
|
|
mAttenuationKeys.ChangeKey(lcVector3(1.0f, 0.0f, 0.0f), 1, true);
|
|
|
|
mSpotCutoffKeys.ChangeKey(30.0f, 1, true);
|
|
|
|
mSpotExponentKeys.ChangeKey(0.0f, 1, true);
|
2011-09-07 23:06:51 +02:00
|
|
|
}
|
|
|
|
|
2014-05-01 20:42:11 +02:00
|
|
|
lcLight::~lcLight()
|
2011-09-07 23:06:51 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-05 02:24:28 +02:00
|
|
|
void lcLight::SaveLDraw(QTextStream& Stream) const
|
2014-09-02 05:44:51 +02:00
|
|
|
{
|
2016-02-17 00:11:52 +01:00
|
|
|
Q_UNUSED(Stream);
|
2014-09-02 05:44:51 +02:00
|
|
|
}
|
|
|
|
|
2014-08-07 17:22:33 +02:00
|
|
|
void lcLight::CreateName(const lcArray<lcLight*>& Lights)
|
2011-09-07 23:06:51 +02:00
|
|
|
{
|
2020-12-14 01:27:21 +01:00
|
|
|
if (!mName.isEmpty())
|
2014-12-16 00:55:17 +01:00
|
|
|
{
|
|
|
|
bool Found = false;
|
2019-03-29 01:59:58 +01:00
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
for (const lcLight* Light : Lights)
|
2014-12-16 00:55:17 +01:00
|
|
|
{
|
2020-12-14 01:27:21 +01:00
|
|
|
if (Light->GetName() == mName)
|
2014-12-16 00:55:17 +01:00
|
|
|
{
|
|
|
|
Found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Found)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-14 01:27:21 +01:00
|
|
|
int MaxLightNumber = 0;
|
|
|
|
const QLatin1String Prefix("Light ");
|
2011-09-07 23:06:51 +02:00
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
for (const lcLight* Light : Lights)
|
2011-09-07 23:06:51 +02:00
|
|
|
{
|
2020-12-14 01:27:21 +01:00
|
|
|
QString LightName = Light->GetName();
|
|
|
|
|
|
|
|
if (LightName.startsWith(Prefix))
|
2011-09-07 23:06:51 +02:00
|
|
|
{
|
2020-12-14 01:27:21 +01:00
|
|
|
bool Ok = false;
|
2021-07-06 02:07:24 +02:00
|
|
|
int LightNumber = LightName.mid(Prefix.size()).toInt(&Ok);
|
2020-12-14 01:27:21 +01:00
|
|
|
|
|
|
|
if (Ok && LightNumber > MaxLightNumber)
|
|
|
|
MaxLightNumber = LightNumber;
|
2011-09-07 23:06:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 01:27:21 +01:00
|
|
|
mName = Prefix + QString::number(MaxLightNumber + 1);
|
2011-09-07 23:06:51 +02:00
|
|
|
}
|
|
|
|
|
2016-02-19 18:53:54 +01:00
|
|
|
void lcLight::CompareBoundingBox(lcVector3& Min, lcVector3& Max)
|
2014-11-10 01:06:11 +01:00
|
|
|
{
|
|
|
|
const lcVector3 Points[2] =
|
|
|
|
{
|
|
|
|
mPosition, mTargetPosition
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < (IsPointLight() ? 1 : 2); i++)
|
|
|
|
{
|
|
|
|
const lcVector3& Point = Points[i];
|
|
|
|
|
2016-02-19 18:53:54 +01:00
|
|
|
// TODO: this should check the entire mesh
|
|
|
|
|
|
|
|
Min = lcMin(Point, Min);
|
|
|
|
Max = lcMax(Point, Max);
|
2014-11-10 01:06:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-01 20:42:11 +02:00
|
|
|
void lcLight::RayTest(lcObjectRayTest& ObjectRayTest) const
|
2011-09-07 23:06:51 +02:00
|
|
|
{
|
2014-05-01 16:55:12 +02:00
|
|
|
if (IsPointLight())
|
2012-09-10 01:42:57 +02:00
|
|
|
{
|
2014-05-01 16:55:12 +02:00
|
|
|
float Distance;
|
2012-09-10 01:42:57 +02:00
|
|
|
|
2014-11-24 01:43:13 +01:00
|
|
|
if (lcSphereRayMinIntersectDistance(mPosition, LC_LIGHT_SPHERE_RADIUS, ObjectRayTest.Start, ObjectRayTest.End, &Distance))
|
2014-05-01 16:55:12 +02:00
|
|
|
{
|
2014-08-07 17:22:33 +02:00
|
|
|
ObjectRayTest.ObjectSection.Object = const_cast<lcLight*>(this);
|
2014-05-01 16:55:12 +02:00
|
|
|
ObjectRayTest.ObjectSection.Section = LC_LIGHT_SECTION_POSITION;
|
|
|
|
ObjectRayTest.Distance = Distance;
|
2012-09-10 01:42:57 +02:00
|
|
|
}
|
|
|
|
|
2014-05-01 16:55:12 +02:00
|
|
|
return;
|
2012-09-10 01:42:57 +02:00
|
|
|
}
|
2014-05-01 16:55:12 +02:00
|
|
|
|
2014-11-24 01:43:13 +01:00
|
|
|
lcVector3 Min = lcVector3(-LC_LIGHT_POSITION_EDGE, -LC_LIGHT_POSITION_EDGE, -LC_LIGHT_POSITION_EDGE);
|
|
|
|
lcVector3 Max = lcVector3(LC_LIGHT_POSITION_EDGE, LC_LIGHT_POSITION_EDGE, LC_LIGHT_POSITION_EDGE);
|
2014-05-01 16:55:12 +02:00
|
|
|
|
|
|
|
lcVector3 Start = lcMul31(ObjectRayTest.Start, mWorldLight);
|
|
|
|
lcVector3 End = lcMul31(ObjectRayTest.End, mWorldLight);
|
|
|
|
|
|
|
|
float Distance;
|
2017-04-14 02:26:40 +02:00
|
|
|
if (lcBoundingBoxRayIntersectDistance(Min, Max, Start, End, &Distance, nullptr) && (Distance < ObjectRayTest.Distance))
|
2012-09-10 01:42:57 +02:00
|
|
|
{
|
2014-08-07 17:22:33 +02:00
|
|
|
ObjectRayTest.ObjectSection.Object = const_cast<lcLight*>(this);
|
2014-05-01 16:55:12 +02:00
|
|
|
ObjectRayTest.ObjectSection.Section = LC_LIGHT_SECTION_POSITION;
|
|
|
|
ObjectRayTest.Distance = Distance;
|
|
|
|
}
|
|
|
|
|
2014-11-24 01:43:13 +01:00
|
|
|
Min = lcVector3(-LC_LIGHT_TARGET_EDGE, -LC_LIGHT_TARGET_EDGE, -LC_LIGHT_TARGET_EDGE);
|
|
|
|
Max = lcVector3(LC_LIGHT_TARGET_EDGE, LC_LIGHT_TARGET_EDGE, LC_LIGHT_TARGET_EDGE);
|
2014-05-01 16:55:12 +02:00
|
|
|
|
|
|
|
lcMatrix44 WorldTarget = mWorldLight;
|
|
|
|
WorldTarget.SetTranslation(lcMul30(-mTargetPosition, WorldTarget));
|
|
|
|
|
|
|
|
Start = lcMul31(ObjectRayTest.Start, WorldTarget);
|
|
|
|
End = lcMul31(ObjectRayTest.End, WorldTarget);
|
|
|
|
|
2017-04-14 02:26:40 +02:00
|
|
|
if (lcBoundingBoxRayIntersectDistance(Min, Max, Start, End, &Distance, nullptr) && (Distance < ObjectRayTest.Distance))
|
2014-05-01 16:55:12 +02:00
|
|
|
{
|
2014-08-07 17:22:33 +02:00
|
|
|
ObjectRayTest.ObjectSection.Object = const_cast<lcLight*>(this);
|
2014-05-01 16:55:12 +02:00
|
|
|
ObjectRayTest.ObjectSection.Section = LC_LIGHT_SECTION_TARGET;
|
|
|
|
ObjectRayTest.Distance = Distance;
|
|
|
|
}
|
2011-09-07 23:06:51 +02:00
|
|
|
}
|
|
|
|
|
2014-05-01 20:42:11 +02:00
|
|
|
void lcLight::BoxTest(lcObjectBoxTest& ObjectBoxTest) const
|
2011-09-07 23:06:51 +02:00
|
|
|
{
|
2014-05-01 16:55:12 +02:00
|
|
|
if (IsPointLight())
|
2012-09-10 01:42:57 +02:00
|
|
|
{
|
2014-05-01 16:55:12 +02:00
|
|
|
for (int PlaneIdx = 0; PlaneIdx < 6; PlaneIdx++)
|
2014-11-24 01:43:13 +01:00
|
|
|
if (lcDot3(mPosition, ObjectBoxTest.Planes[PlaneIdx]) + ObjectBoxTest.Planes[PlaneIdx][3] > LC_LIGHT_SPHERE_RADIUS)
|
2014-05-01 16:55:12 +02:00
|
|
|
return;
|
2012-09-10 01:42:57 +02:00
|
|
|
|
2014-11-29 03:55:58 +01:00
|
|
|
ObjectBoxTest.Objects.Add(const_cast<lcLight*>(this));
|
2014-05-01 16:55:12 +02:00
|
|
|
return;
|
2012-09-10 01:42:57 +02:00
|
|
|
}
|
2014-05-01 16:55:12 +02:00
|
|
|
|
2014-11-24 01:43:13 +01:00
|
|
|
lcVector3 Min(-LC_LIGHT_POSITION_EDGE, -LC_LIGHT_POSITION_EDGE, -LC_LIGHT_POSITION_EDGE);
|
|
|
|
lcVector3 Max(LC_LIGHT_POSITION_EDGE, LC_LIGHT_POSITION_EDGE, LC_LIGHT_POSITION_EDGE);
|
2014-05-01 16:55:12 +02:00
|
|
|
|
|
|
|
lcVector4 LocalPlanes[6];
|
|
|
|
|
|
|
|
for (int PlaneIdx = 0; PlaneIdx < 6; PlaneIdx++)
|
2012-09-10 01:42:57 +02:00
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
const lcVector3 Normal = lcMul30(ObjectBoxTest.Planes[PlaneIdx], mWorldLight);
|
2014-05-01 16:55:12 +02:00
|
|
|
LocalPlanes[PlaneIdx] = lcVector4(Normal, ObjectBoxTest.Planes[PlaneIdx][3] - lcDot3(mWorldLight[3], Normal));
|
|
|
|
}
|
2011-09-07 23:06:51 +02:00
|
|
|
|
2014-05-01 16:55:12 +02:00
|
|
|
if (lcBoundingBoxIntersectsVolume(Min, Max, LocalPlanes))
|
2012-08-17 01:50:40 +02:00
|
|
|
{
|
2014-11-29 03:55:58 +01:00
|
|
|
ObjectBoxTest.Objects.Add(const_cast<lcLight*>(this));
|
|
|
|
return;
|
2014-05-01 16:55:12 +02:00
|
|
|
}
|
2011-09-07 23:06:51 +02:00
|
|
|
|
2014-11-24 01:43:13 +01:00
|
|
|
Min = lcVector3(-LC_LIGHT_TARGET_EDGE, -LC_LIGHT_TARGET_EDGE, -LC_LIGHT_TARGET_EDGE);
|
|
|
|
Max = lcVector3(LC_LIGHT_TARGET_EDGE, LC_LIGHT_TARGET_EDGE, LC_LIGHT_TARGET_EDGE);
|
2011-09-07 23:06:51 +02:00
|
|
|
|
2014-05-01 16:55:12 +02:00
|
|
|
lcMatrix44 WorldTarget = mWorldLight;
|
|
|
|
WorldTarget.SetTranslation(lcMul30(-mTargetPosition, WorldTarget));
|
2011-09-07 23:06:51 +02:00
|
|
|
|
2014-05-01 16:55:12 +02:00
|
|
|
for (int PlaneIdx = 0; PlaneIdx < 6; PlaneIdx++)
|
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
const lcVector3 Normal = lcMul30(ObjectBoxTest.Planes[PlaneIdx], WorldTarget);
|
2014-05-01 16:55:12 +02:00
|
|
|
LocalPlanes[PlaneIdx] = lcVector4(Normal, ObjectBoxTest.Planes[PlaneIdx][3] - lcDot3(WorldTarget[3], Normal));
|
2012-08-17 01:50:40 +02:00
|
|
|
}
|
2014-05-01 16:55:12 +02:00
|
|
|
|
|
|
|
if (lcBoundingBoxIntersectsVolume(Min, Max, LocalPlanes))
|
2012-08-17 01:50:40 +02:00
|
|
|
{
|
2014-11-29 03:55:58 +01:00
|
|
|
ObjectBoxTest.Objects.Add(const_cast<lcLight*>(this));
|
|
|
|
return;
|
2012-08-17 01:50:40 +02:00
|
|
|
}
|
2011-09-07 23:06:51 +02:00
|
|
|
}
|
|
|
|
|
2018-01-15 20:35:15 +01:00
|
|
|
void lcLight::MoveSelected(lcStep Step, bool AddKey, const lcVector3& Distance)
|
2011-09-07 23:06:51 +02:00
|
|
|
{
|
2014-05-01 16:55:12 +02:00
|
|
|
if (IsSelected(LC_LIGHT_SECTION_POSITION))
|
2012-06-07 02:08:59 +02:00
|
|
|
{
|
2014-06-22 19:39:15 +02:00
|
|
|
mPosition += Distance;
|
2021-01-05 20:26:01 +01:00
|
|
|
mPositionKeys.ChangeKey(mPosition, Step, AddKey);
|
2012-06-07 02:08:59 +02:00
|
|
|
}
|
2011-09-07 23:06:51 +02:00
|
|
|
|
2014-05-01 16:55:12 +02:00
|
|
|
if (IsSelected(LC_LIGHT_SECTION_TARGET))
|
2012-06-07 02:08:59 +02:00
|
|
|
{
|
2014-06-22 19:39:15 +02:00
|
|
|
mTargetPosition += Distance;
|
2021-01-05 20:26:01 +01:00
|
|
|
mTargetPositionKeys.ChangeKey(mTargetPosition, Step, AddKey);
|
2012-06-07 02:08:59 +02:00
|
|
|
}
|
2011-09-07 23:06:51 +02:00
|
|
|
}
|
|
|
|
|
2014-08-31 02:53:12 +02:00
|
|
|
void lcLight::InsertTime(lcStep Start, lcStep Time)
|
|
|
|
{
|
2021-01-05 20:26:01 +01:00
|
|
|
mPositionKeys.InsertTime(Start, Time);
|
|
|
|
mTargetPositionKeys.InsertTime(Start, Time);
|
|
|
|
mAmbientColorKeys.InsertTime(Start, Time);
|
|
|
|
mDiffuseColorKeys.InsertTime(Start, Time);
|
|
|
|
mSpecularColorKeys.InsertTime(Start, Time);
|
|
|
|
mAttenuationKeys.InsertTime(Start, Time);
|
|
|
|
mSpotCutoffKeys.InsertTime(Start, Time);
|
|
|
|
mSpotExponentKeys.InsertTime(Start, Time);
|
2014-08-31 02:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void lcLight::RemoveTime(lcStep Start, lcStep Time)
|
|
|
|
{
|
2021-01-05 20:26:01 +01:00
|
|
|
mPositionKeys.RemoveTime(Start, Time);
|
|
|
|
mTargetPositionKeys.RemoveTime(Start, Time);
|
|
|
|
mAmbientColorKeys.RemoveTime(Start, Time);
|
|
|
|
mDiffuseColorKeys.RemoveTime(Start, Time);
|
|
|
|
mSpecularColorKeys.RemoveTime(Start, Time);
|
|
|
|
mAttenuationKeys.RemoveTime(Start, Time);
|
|
|
|
mSpotCutoffKeys.RemoveTime(Start, Time);
|
|
|
|
mSpotExponentKeys.RemoveTime(Start, Time);
|
2014-08-31 02:53:12 +02:00
|
|
|
}
|
|
|
|
|
2014-07-06 08:04:09 +02:00
|
|
|
void lcLight::UpdatePosition(lcStep Step)
|
2011-09-07 23:06:51 +02:00
|
|
|
{
|
2021-01-05 20:26:01 +01:00
|
|
|
mPosition = mPositionKeys.CalculateKey(Step);
|
|
|
|
mTargetPosition = mTargetPositionKeys.CalculateKey(Step);
|
|
|
|
mAmbientColor = mAmbientColorKeys.CalculateKey(Step);
|
|
|
|
mDiffuseColor = mDiffuseColorKeys.CalculateKey(Step);
|
|
|
|
mSpecularColor = mSpecularColorKeys.CalculateKey(Step);
|
|
|
|
mAttenuation = mAttenuationKeys.CalculateKey(Step);
|
|
|
|
mSpotCutoff = mSpotCutoffKeys.CalculateKey(Step);
|
|
|
|
mSpotExponent = mSpotExponentKeys.CalculateKey(Step);
|
2011-09-07 23:06:51 +02:00
|
|
|
|
2014-05-01 16:55:12 +02:00
|
|
|
if (IsPointLight())
|
2012-03-29 03:10:55 +02:00
|
|
|
{
|
2014-05-01 16:55:12 +02:00
|
|
|
mWorldLight = lcMatrix44Identity();
|
|
|
|
mWorldLight.SetTranslation(-mPosition);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lcVector3 FrontVector(mTargetPosition - mPosition);
|
|
|
|
lcVector3 UpVector(1, 1, 1);
|
2011-09-07 23:06:51 +02:00
|
|
|
|
2014-05-01 16:55:12 +02:00
|
|
|
if (fabs(FrontVector[0]) < fabs(FrontVector[1]))
|
2012-08-17 01:50:40 +02:00
|
|
|
{
|
2014-05-01 16:55:12 +02:00
|
|
|
if (fabs(FrontVector[0]) < fabs(FrontVector[2]))
|
|
|
|
UpVector[0] = -(UpVector[1] * FrontVector[1] + UpVector[2] * FrontVector[2]);
|
2012-08-17 01:50:40 +02:00
|
|
|
else
|
2014-05-01 16:55:12 +02:00
|
|
|
UpVector[2] = -(UpVector[0] * FrontVector[0] + UpVector[1] * FrontVector[1]);
|
2012-08-17 01:50:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-01 16:55:12 +02:00
|
|
|
if (fabs(FrontVector[1]) < fabs(FrontVector[2]))
|
|
|
|
UpVector[1] = -(UpVector[0] * FrontVector[0] + UpVector[2] * FrontVector[2]);
|
2012-08-17 01:50:40 +02:00
|
|
|
else
|
2014-05-01 16:55:12 +02:00
|
|
|
UpVector[2] = -(UpVector[0] * FrontVector[0] + UpVector[1] * FrontVector[1]);
|
2012-08-17 01:50:40 +02:00
|
|
|
}
|
|
|
|
|
2014-05-01 16:55:12 +02:00
|
|
|
mWorldLight = lcMatrix44LookAt(mPosition, mTargetPosition, UpVector);
|
2012-08-17 01:50:40 +02:00
|
|
|
}
|
2011-09-07 23:06:51 +02:00
|
|
|
}
|
|
|
|
|
2018-04-08 02:17:32 +02:00
|
|
|
void lcLight::DrawInterface(lcContext* Context, const lcScene& Scene) const
|
2011-09-07 23:06:51 +02:00
|
|
|
{
|
2018-04-08 02:17:32 +02:00
|
|
|
Q_UNUSED(Scene);
|
2020-03-22 21:44:20 +01:00
|
|
|
Context->SetMaterial(lcMaterialType::UnlitColor);
|
2015-12-11 02:46:30 +01:00
|
|
|
|
2014-05-01 16:55:12 +02:00
|
|
|
if (IsPointLight())
|
2015-05-17 01:04:35 +02:00
|
|
|
DrawPointLight(Context);
|
2015-04-19 03:10:01 +02:00
|
|
|
else
|
2015-05-17 01:04:35 +02:00
|
|
|
DrawSpotLight(Context);
|
2015-04-19 03:10:01 +02:00
|
|
|
}
|
2014-05-01 16:55:12 +02:00
|
|
|
|
2015-05-17 01:04:35 +02:00
|
|
|
void lcLight::DrawSpotLight(lcContext* Context) const
|
2015-04-19 03:10:01 +02:00
|
|
|
{
|
|
|
|
lcVector3 FrontVector(mTargetPosition - mPosition);
|
|
|
|
lcVector3 UpVector(1, 1, 1);
|
|
|
|
|
|
|
|
if (fabs(FrontVector[0]) < fabs(FrontVector[1]))
|
|
|
|
{
|
|
|
|
if (fabs(FrontVector[0]) < fabs(FrontVector[2]))
|
|
|
|
UpVector[0] = -(UpVector[1] * FrontVector[1] + UpVector[2] * FrontVector[2]);
|
2014-05-01 16:55:12 +02:00
|
|
|
else
|
2015-04-19 03:10:01 +02:00
|
|
|
UpVector[2] = -(UpVector[0] * FrontVector[0] + UpVector[1] * FrontVector[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (fabs(FrontVector[1]) < fabs(FrontVector[2]))
|
|
|
|
UpVector[1] = -(UpVector[0] * FrontVector[0] + UpVector[2] * FrontVector[2]);
|
|
|
|
else
|
|
|
|
UpVector[2] = -(UpVector[0] * FrontVector[0] + UpVector[1] * FrontVector[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
lcMatrix44 LightMatrix = lcMatrix44LookAt(mPosition, mTargetPosition, UpVector);
|
|
|
|
LightMatrix = lcMatrix44AffineInverse(LightMatrix);
|
|
|
|
LightMatrix.SetTranslation(lcVector3(0, 0, 0));
|
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
const lcMatrix44 LightViewMatrix = lcMul(LightMatrix, lcMatrix44Translation(mPosition));
|
2015-05-17 01:04:35 +02:00
|
|
|
Context->SetWorldMatrix(LightViewMatrix);
|
2015-04-19 03:10:01 +02:00
|
|
|
|
|
|
|
float Verts[(20 + 8 + 2 + 16) * 3];
|
|
|
|
float* CurVert = Verts;
|
|
|
|
|
|
|
|
for (int EdgeIdx = 0; EdgeIdx < 8; EdgeIdx++)
|
|
|
|
{
|
|
|
|
float c = cosf((float)EdgeIdx / 4 * LC_PI) * LC_LIGHT_POSITION_EDGE;
|
|
|
|
float s = sinf((float)EdgeIdx / 4 * LC_PI) * LC_LIGHT_POSITION_EDGE;
|
|
|
|
|
|
|
|
*CurVert++ = c;
|
|
|
|
*CurVert++ = s;
|
|
|
|
*CurVert++ = LC_LIGHT_POSITION_EDGE;
|
|
|
|
*CurVert++ = c;
|
|
|
|
*CurVert++ = s;
|
|
|
|
*CurVert++ = -LC_LIGHT_POSITION_EDGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*CurVert++ = -12.5f; *CurVert++ = -12.5f; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
|
|
|
|
*CurVert++ = 12.5f; *CurVert++ = -12.5f; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
|
|
|
|
*CurVert++ = 12.5f; *CurVert++ = 12.5f; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
|
|
|
|
*CurVert++ = -12.5f; *CurVert++ = 12.5f; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
|
|
|
|
|
|
|
|
float Length = FrontVector.Length();
|
|
|
|
|
|
|
|
*CurVert++ = LC_LIGHT_TARGET_EDGE; *CurVert++ = LC_LIGHT_TARGET_EDGE; *CurVert++ = LC_LIGHT_TARGET_EDGE - Length;
|
|
|
|
*CurVert++ = -LC_LIGHT_TARGET_EDGE; *CurVert++ = LC_LIGHT_TARGET_EDGE; *CurVert++ = LC_LIGHT_TARGET_EDGE - Length;
|
|
|
|
*CurVert++ = -LC_LIGHT_TARGET_EDGE; *CurVert++ = -LC_LIGHT_TARGET_EDGE; *CurVert++ = LC_LIGHT_TARGET_EDGE - Length;
|
|
|
|
*CurVert++ = LC_LIGHT_TARGET_EDGE; *CurVert++ = -LC_LIGHT_TARGET_EDGE; *CurVert++ = LC_LIGHT_TARGET_EDGE - Length;
|
|
|
|
*CurVert++ = LC_LIGHT_TARGET_EDGE; *CurVert++ = LC_LIGHT_TARGET_EDGE; *CurVert++ = -LC_LIGHT_TARGET_EDGE - Length;
|
|
|
|
*CurVert++ = -LC_LIGHT_TARGET_EDGE; *CurVert++ = LC_LIGHT_TARGET_EDGE; *CurVert++ = -LC_LIGHT_TARGET_EDGE - Length;
|
|
|
|
*CurVert++ = -LC_LIGHT_TARGET_EDGE; *CurVert++ = -LC_LIGHT_TARGET_EDGE; *CurVert++ = -LC_LIGHT_TARGET_EDGE - Length;
|
|
|
|
*CurVert++ = LC_LIGHT_TARGET_EDGE; *CurVert++ = -LC_LIGHT_TARGET_EDGE; *CurVert++ = -LC_LIGHT_TARGET_EDGE - Length;
|
|
|
|
|
|
|
|
*CurVert++ = 0.0f; *CurVert++ = 0.0f; *CurVert++ = 0.0f;
|
|
|
|
*CurVert++ = 0.0f; *CurVert++ = 0.0f; *CurVert++ = -Length;
|
|
|
|
|
|
|
|
const GLushort Indices[56 + 24 + 2 + 40] =
|
|
|
|
{
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
|
|
|
0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14, 0,
|
|
|
|
1, 3, 3, 5, 5, 7, 7, 9, 9, 11, 11, 13, 13, 15, 15, 1,
|
|
|
|
16, 17, 17, 18, 18, 19, 19, 16,
|
|
|
|
20, 21, 21, 22, 22, 23, 23, 20,
|
|
|
|
24, 25, 25, 26, 26, 27, 27, 24,
|
|
|
|
20, 24, 21, 25, 22, 26, 23, 27,
|
|
|
|
28, 29,
|
|
|
|
30, 31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38,
|
|
|
|
38, 39, 39, 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 45, 45, 30,
|
|
|
|
28, 30, 28, 34, 28, 38, 28, 42
|
|
|
|
};
|
|
|
|
|
|
|
|
Context->SetVertexBufferPointer(Verts);
|
2017-03-24 17:34:53 +01:00
|
|
|
Context->SetVertexFormatPosition(3);
|
2015-05-17 01:04:35 +02:00
|
|
|
Context->SetIndexBufferPointer(Indices);
|
2014-05-01 16:55:12 +02:00
|
|
|
|
2021-11-25 00:20:08 +01:00
|
|
|
const lcPreferences& Preferences = lcGetPreferences();
|
|
|
|
const float LineWidth = Preferences.mLineWidth;
|
|
|
|
const lcVector4 SelectedColor = lcVector4FromColor(Preferences.mObjectSelectedColor);
|
|
|
|
const lcVector4 FocusedColor = lcVector4FromColor(Preferences.mObjectFocusedColor);
|
|
|
|
const lcVector4 LightColor = lcVector4FromColor(Preferences.mLightColor);
|
2015-04-19 03:10:01 +02:00
|
|
|
|
|
|
|
if (!IsSelected())
|
|
|
|
{
|
|
|
|
Context->SetLineWidth(LineWidth);
|
2021-11-25 00:20:08 +01:00
|
|
|
Context->SetColor(LightColor);
|
2015-04-19 03:10:01 +02:00
|
|
|
|
2015-05-17 01:04:35 +02:00
|
|
|
Context->DrawIndexedPrimitives(GL_LINES, 56 + 24 + 2, GL_UNSIGNED_SHORT, 0);
|
2014-05-01 16:55:12 +02:00
|
|
|
}
|
|
|
|
else
|
2012-06-07 02:08:59 +02:00
|
|
|
{
|
2014-05-01 16:55:12 +02:00
|
|
|
if (IsSelected(LC_LIGHT_SECTION_POSITION))
|
2012-06-07 02:08:59 +02:00
|
|
|
{
|
2014-04-14 05:20:16 +02:00
|
|
|
Context->SetLineWidth(2.0f * LineWidth);
|
2014-05-01 16:55:12 +02:00
|
|
|
if (IsFocused(LC_LIGHT_SECTION_POSITION))
|
2021-11-25 00:20:08 +01:00
|
|
|
Context->SetColor(FocusedColor);
|
2012-06-07 02:08:59 +02:00
|
|
|
else
|
2021-11-25 00:20:08 +01:00
|
|
|
Context->SetColor(SelectedColor);
|
2012-06-07 02:08:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-14 05:20:16 +02:00
|
|
|
Context->SetLineWidth(LineWidth);
|
2021-11-25 00:20:08 +01:00
|
|
|
Context->SetColor(LightColor);
|
2012-06-07 02:08:59 +02:00
|
|
|
}
|
2011-09-07 23:06:51 +02:00
|
|
|
|
2015-05-17 01:04:35 +02:00
|
|
|
Context->DrawIndexedPrimitives(GL_LINES, 56, GL_UNSIGNED_SHORT, 0);
|
2014-04-14 05:20:16 +02:00
|
|
|
|
2014-05-01 16:55:12 +02:00
|
|
|
if (IsSelected(LC_LIGHT_SECTION_TARGET))
|
2012-06-07 02:08:59 +02:00
|
|
|
{
|
2014-04-14 05:20:16 +02:00
|
|
|
Context->SetLineWidth(2.0f * LineWidth);
|
2014-05-01 16:55:12 +02:00
|
|
|
if (IsFocused(LC_LIGHT_SECTION_TARGET))
|
2021-11-25 00:20:08 +01:00
|
|
|
Context->SetColor(FocusedColor);
|
2012-06-07 02:08:59 +02:00
|
|
|
else
|
2021-11-25 00:20:08 +01:00
|
|
|
Context->SetColor(SelectedColor);
|
2012-06-07 02:08:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-14 05:20:16 +02:00
|
|
|
Context->SetLineWidth(LineWidth);
|
2021-11-25 00:20:08 +01:00
|
|
|
Context->SetColor(LightColor);
|
2012-06-07 02:08:59 +02:00
|
|
|
}
|
2011-09-07 23:06:51 +02:00
|
|
|
|
2015-05-17 01:04:35 +02:00
|
|
|
Context->DrawIndexedPrimitives(GL_LINES, 24, GL_UNSIGNED_SHORT, 56 * 2);
|
2012-09-10 01:42:57 +02:00
|
|
|
|
2014-04-14 05:20:16 +02:00
|
|
|
Context->SetLineWidth(LineWidth);
|
2021-11-25 00:20:08 +01:00
|
|
|
Context->SetColor(LightColor);
|
2012-09-10 01:42:57 +02:00
|
|
|
|
2015-04-19 03:10:01 +02:00
|
|
|
float Radius = tanf(LC_DTOR * mSpotCutoff) * Length;
|
2012-09-10 01:42:57 +02:00
|
|
|
|
2015-04-19 03:10:01 +02:00
|
|
|
for (int EdgeIdx = 0; EdgeIdx < 16; EdgeIdx++)
|
2012-06-07 02:08:59 +02:00
|
|
|
{
|
2015-04-19 03:10:01 +02:00
|
|
|
*CurVert++ = cosf((float)EdgeIdx / 16 * LC_2PI) * Radius;
|
|
|
|
*CurVert++ = sinf((float)EdgeIdx / 16 * LC_2PI) * Radius;
|
|
|
|
*CurVert++ = -Length;
|
2012-06-07 02:08:59 +02:00
|
|
|
}
|
2012-09-10 01:42:57 +02:00
|
|
|
|
2015-05-17 01:04:35 +02:00
|
|
|
Context->DrawIndexedPrimitives(GL_LINES, 2 + 40, GL_UNSIGNED_SHORT, (56 + 24) * 2);
|
2012-09-10 01:42:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-17 01:04:35 +02:00
|
|
|
void lcLight::DrawPointLight(lcContext* Context) const
|
2012-09-10 01:42:57 +02:00
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
constexpr int Slices = 6;
|
|
|
|
constexpr int NumIndices = 3 * Slices + 6 * Slices * (Slices - 2) + 3 * Slices;
|
|
|
|
constexpr int NumVertices = (Slices - 1) * Slices + 2;
|
|
|
|
constexpr float Radius = LC_LIGHT_SPHERE_RADIUS;
|
2012-09-10 01:42:57 +02:00
|
|
|
lcVector3 Vertices[NumVertices];
|
2017-12-02 21:22:04 +01:00
|
|
|
quint16 Indices[NumIndices];
|
2012-09-10 01:42:57 +02:00
|
|
|
|
|
|
|
lcVector3* Vertex = Vertices;
|
2017-12-02 21:22:04 +01:00
|
|
|
quint16* Index = Indices;
|
2012-09-10 01:42:57 +02:00
|
|
|
|
|
|
|
*Vertex++ = lcVector3(0, 0, Radius);
|
|
|
|
|
|
|
|
for (int i = 1; i < Slices; i++ )
|
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
const float r0 = Radius * sinf(i * (LC_PI / Slices));
|
|
|
|
const float z0 = Radius * cosf(i * (LC_PI / Slices));
|
2012-09-10 01:42:57 +02:00
|
|
|
|
|
|
|
for (int j = 0; j < Slices; j++)
|
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
const float x0 = r0 * sinf(j * (LC_2PI / Slices));
|
|
|
|
const float y0 = r0 * cosf(j * (LC_2PI / Slices));
|
2012-09-10 01:42:57 +02:00
|
|
|
|
|
|
|
*Vertex++ = lcVector3(x0, y0, z0);
|
2012-06-07 02:08:59 +02:00
|
|
|
}
|
2012-09-10 01:42:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*Vertex++ = lcVector3(0, 0, -Radius);
|
2011-09-07 23:06:51 +02:00
|
|
|
|
2021-07-06 02:00:41 +02:00
|
|
|
for (quint16 i = 0; i < Slices - 1; i++ )
|
2012-09-10 01:42:57 +02:00
|
|
|
{
|
|
|
|
*Index++ = 0;
|
|
|
|
*Index++ = 1 + i;
|
|
|
|
*Index++ = 1 + i + 1;
|
2012-06-07 02:08:59 +02:00
|
|
|
}
|
2012-09-10 01:42:57 +02:00
|
|
|
|
|
|
|
*Index++ = 0;
|
|
|
|
*Index++ = 1;
|
|
|
|
*Index++ = 1 + Slices - 1;
|
|
|
|
|
2021-07-06 02:00:41 +02:00
|
|
|
for (quint16 i = 0; i < Slices - 2; i++ )
|
2012-09-10 01:42:57 +02:00
|
|
|
{
|
2021-07-06 02:00:41 +02:00
|
|
|
quint16 Row1 = 1 + i * Slices;
|
|
|
|
quint16 Row2 = 1 + (i + 1) * Slices;
|
2012-09-10 01:42:57 +02:00
|
|
|
|
2021-07-06 02:00:41 +02:00
|
|
|
for (quint16 j = 0; j < Slices - 1; j++ )
|
2012-09-10 01:42:57 +02:00
|
|
|
{
|
|
|
|
*Index++ = Row1 + j;
|
|
|
|
*Index++ = Row2 + j + 1;
|
|
|
|
*Index++ = Row2 + j;
|
|
|
|
|
|
|
|
*Index++ = Row1 + j;
|
|
|
|
*Index++ = Row1 + j + 1;
|
|
|
|
*Index++ = Row2 + j + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*Index++ = Row1 + Slices - 1;
|
|
|
|
*Index++ = Row2 + 0;
|
|
|
|
*Index++ = Row2 + Slices - 1;
|
|
|
|
|
|
|
|
*Index++ = Row1 + Slices - 1;
|
|
|
|
*Index++ = Row2 + 0;
|
|
|
|
*Index++ = Row1 + 0;
|
|
|
|
}
|
|
|
|
|
2021-07-06 02:00:41 +02:00
|
|
|
for (quint16 i = 0; i < Slices - 1; i++ )
|
2012-09-10 01:42:57 +02:00
|
|
|
{
|
|
|
|
*Index++ = (Slices - 1) * Slices + 1;
|
|
|
|
*Index++ = (Slices - 1) * (Slices - 1) + i;
|
|
|
|
*Index++ = (Slices - 1) * (Slices - 1) + i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*Index++ = (Slices - 1) * Slices + 1;
|
|
|
|
*Index++ = (Slices - 1) * (Slices - 1) + (Slices - 2) + 1;
|
|
|
|
*Index++ = (Slices - 1) * (Slices - 1);
|
|
|
|
|
2015-05-17 01:04:35 +02:00
|
|
|
Context->SetWorldMatrix(lcMatrix44Translation(mPosition));
|
2015-04-19 03:10:01 +02:00
|
|
|
|
2021-11-25 00:20:08 +01:00
|
|
|
const lcPreferences& Preferences = lcGetPreferences();
|
|
|
|
|
2015-04-19 03:10:01 +02:00
|
|
|
if (IsFocused(LC_LIGHT_SECTION_POSITION))
|
2021-11-25 00:20:08 +01:00
|
|
|
{
|
|
|
|
const lcVector4 FocusedColor = lcVector4FromColor(Preferences.mObjectFocusedColor);
|
|
|
|
Context->SetColor(FocusedColor);
|
|
|
|
}
|
2015-04-19 03:10:01 +02:00
|
|
|
else if (IsSelected(LC_LIGHT_SECTION_POSITION))
|
2021-11-25 00:20:08 +01:00
|
|
|
{
|
|
|
|
const lcVector4 SelectedColor = lcVector4FromColor(Preferences.mObjectSelectedColor);
|
|
|
|
Context->SetColor(SelectedColor);
|
|
|
|
}
|
2015-04-19 03:10:01 +02:00
|
|
|
else
|
2021-11-25 00:20:08 +01:00
|
|
|
{
|
|
|
|
const lcVector4 LightColor = lcVector4FromColor(Preferences.mLightColor);
|
|
|
|
Context->SetColor(LightColor);
|
|
|
|
}
|
2015-04-19 03:10:01 +02:00
|
|
|
|
|
|
|
Context->SetVertexBufferPointer(Vertices);
|
2017-03-24 17:34:53 +01:00
|
|
|
Context->SetVertexFormatPosition(3);
|
2015-05-17 01:04:35 +02:00
|
|
|
Context->SetIndexBufferPointer(Indices);
|
|
|
|
Context->DrawIndexedPrimitives(GL_TRIANGLES, NumIndices, GL_UNSIGNED_SHORT, 0);
|
2011-09-07 23:06:51 +02:00
|
|
|
}
|
|
|
|
|
2018-02-24 21:47:19 +01:00
|
|
|
void lcLight::RemoveKeyFrames()
|
|
|
|
{
|
|
|
|
mPositionKeys.RemoveAll();
|
2021-01-05 20:26:01 +01:00
|
|
|
mPositionKeys.ChangeKey(mPosition, 1, true);
|
2018-02-24 21:47:19 +01:00
|
|
|
|
|
|
|
mTargetPositionKeys.RemoveAll();
|
2021-01-05 20:26:01 +01:00
|
|
|
mTargetPositionKeys.ChangeKey(mTargetPosition, 1, true);
|
2018-02-24 21:47:19 +01:00
|
|
|
|
|
|
|
mAmbientColorKeys.RemoveAll();
|
2021-01-05 20:26:01 +01:00
|
|
|
mAmbientColorKeys.ChangeKey(mAmbientColor, 1, true);
|
2018-02-24 21:47:19 +01:00
|
|
|
|
|
|
|
mDiffuseColorKeys.RemoveAll();
|
2021-01-05 20:26:01 +01:00
|
|
|
mDiffuseColorKeys.ChangeKey(mDiffuseColor, 1, true);
|
2018-02-24 21:47:19 +01:00
|
|
|
|
|
|
|
mSpecularColorKeys.RemoveAll();
|
2021-01-05 20:26:01 +01:00
|
|
|
mSpecularColorKeys.ChangeKey(mSpecularColor, 1, true);
|
2018-02-24 21:47:19 +01:00
|
|
|
|
|
|
|
mAttenuationKeys.RemoveAll();
|
2021-01-05 20:26:01 +01:00
|
|
|
mAttenuationKeys.ChangeKey(mAttenuation, 1, true);
|
2018-02-24 21:47:19 +01:00
|
|
|
|
|
|
|
mSpotCutoffKeys.RemoveAll();
|
2021-01-05 20:26:01 +01:00
|
|
|
mSpotCutoffKeys.ChangeKey(mSpotCutoff, 1, true);
|
2018-02-24 21:47:19 +01:00
|
|
|
|
|
|
|
mSpotExponentKeys.RemoveAll();
|
2021-01-05 20:26:01 +01:00
|
|
|
mSpotExponentKeys.ChangeKey(mSpotExponent, 1, true);
|
2018-02-24 21:47:19 +01:00
|
|
|
}
|
|
|
|
|
2014-05-01 20:42:11 +02:00
|
|
|
bool lcLight::Setup(int LightIndex)
|
2011-09-07 23:06:51 +02:00
|
|
|
{
|
2017-04-27 07:24:54 +02:00
|
|
|
Q_UNUSED(LightIndex);
|
2014-05-01 16:55:12 +02:00
|
|
|
|
|
|
|
return true;
|
2011-09-07 23:06:51 +02:00
|
|
|
}
|