leocad/common/light.cpp

1306 lines
40 KiB
C++
Raw Normal View History

#include "lc_global.h"
2012-03-29 03:10:55 +02:00
#include "lc_math.h"
#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
#define LC_LIGHT_SUN_RADIUS 8.5f
#define LC_LIGHT_SPOT_BASE_EDGE 12.5f
2014-11-24 01:43:13 +01:00
2023-08-13 15:15:52 +02:00
static const std::array<QLatin1String, 4> gLightTypes = { QLatin1String("POINT"), QLatin1String("SPOT"), QLatin1String("DIRECTIONAL"), QLatin1String("AREA") };
2011-09-07 23:06:51 +02:00
2023-08-13 15:15:52 +02:00
lcLight::lcLight(const lcVector3& Position, const lcVector3& TargetPosition, lcLightType LightType)
: lcObject(lcObjectType::Light), mPosition(Position), mTargetPosition(TargetPosition), mLightType(LightType)
2011-09-07 23:06:51 +02:00
{
mState = 0;
2012-08-17 01:50:40 +02:00
2023-08-07 13:22:59 +02:00
mPOVRayLight = false;
mShadowless = false;
2023-08-07 11:31:33 +02:00
mEnableCutoff = false;
mTargetPosition = TargetPosition;
mAmbientColor = lcVector4(0.0f, 0.0f, 0.0f, 1.0f);
mDiffuseColor = lcVector4(0.8f, 0.8f, 0.8f, 1.0f);
mSpecularColor = lcVector4(1.0f, 1.0f, 1.0f, 1.0f);
mAttenuation = lcVector3(1.0f, 0.0f, 0.0f);
2023-08-07 13:22:59 +02:00
mLightColor = lcVector3(1.0f, 1.0f, 1.0f); /*RGB - White*/
2023-08-13 15:15:52 +02:00
mLightFactor[0] = LightType == lcLightType::Directional ? 11.4f : 0.25f;
mLightFactor[1] = LightType == lcLightType::Area ? 0.25f : LightType == lcLightType::Spot ? 0.150f : 0.0f;
2023-08-07 13:22:59 +02:00
mLightDiffuse = 1.0f;
2023-08-07 11:31:33 +02:00
mLightSpecular = 1.0f;
2023-08-07 13:22:59 +02:00
mSpotExponent = 10.0f;
mPOVRayExponent = 1.0f;
2023-08-07 11:31:33 +02:00
mSpotSize = 75.0f;
2023-08-13 15:15:52 +02:00
mSpotCutoff = LightType != lcLightType::Directional ? 40.0f : 0.0f;
2023-08-07 13:22:59 +02:00
mSpotFalloff = 45.0f;
mSpotTightness = 0;
mAreaGrid = lcVector2(10.0f, 10.0f);
mAreaSize = lcVector2(200.0f, 200.0f);
mLightShape = LC_LIGHT_SHAPE_SQUARE;
2023-08-07 11:31:33 +02:00
mPositionKeys.ChangeKey(mPosition, 1, true);
mTargetPositionKeys.ChangeKey(mTargetPosition, 1, true);
mAmbientColorKeys.ChangeKey(mAmbientColor, 1, true);
mDiffuseColorKeys.ChangeKey(mDiffuseColor, 1, true);
mSpecularColorKeys.ChangeKey(mSpecularColor, 1, true);
mAttenuationKeys.ChangeKey(mAttenuation, 1, true);
mLightColorKeys.ChangeKey(mLightColor, 1, true);
mLightFactorKeys.ChangeKey(mLightFactor, 1, true);
2023-08-07 13:22:59 +02:00
mLightDiffuseKeys.ChangeKey(mLightDiffuse, 1, true);
2023-08-07 11:31:33 +02:00
mLightSpecularKeys.ChangeKey(mLightSpecular, 1, true);
mSpotCutoffKeys.ChangeKey(mSpotCutoff, 1, true);
2023-08-07 13:22:59 +02:00
mSpotFalloffKeys.ChangeKey(mSpotFalloff, 1, true);
2023-08-07 11:31:33 +02:00
mSpotExponentKeys.ChangeKey(mSpotExponent, 1, true);
2023-08-07 13:22:59 +02:00
mSpotSizeKeys.ChangeKey(mSpotSize, 1, true);
mSpotTightnessKeys.ChangeKey(mSpotTightness, 1, true);
mAreaGridKeys.ChangeKey(mAreaGrid, 1, true);
2011-09-07 23:06:51 +02:00
2023-08-13 15:15:52 +02:00
UpdatePosition(1);
2011-09-07 23:06:51 +02:00
}
2014-09-05 02:24:28 +02:00
void lcLight::SaveLDraw(QTextStream& Stream) const
{
2023-08-07 11:31:33 +02:00
const QLatin1String LineEnding("\r\n");
2023-08-07 13:22:59 +02:00
if (mPOVRayLight)
Stream << QLatin1String("0 !LEOCAD LIGHT POV_RAY") << LineEnding;
if (mShadowless)
Stream << QLatin1String("0 !LEOCAD LIGHT SHADOWLESS") << LineEnding;
2023-08-07 11:31:33 +02:00
if (mPositionKeys.GetSize() > 1)
mPositionKeys.SaveKeysLDraw(Stream, "LIGHT POSITION_KEY ");
else
Stream << QLatin1String("0 !LEOCAD LIGHT POSITION ") << mPosition[0] << ' ' << mPosition[1] << ' ' << mPosition[2] << LineEnding;
if (mLightType != lcLightType::Point)
2023-08-07 13:22:59 +02:00
{
if (mTargetPositionKeys.GetSize() > 1)
mTargetPositionKeys.SaveKeysLDraw(Stream, "LIGHT TARGET_POSITION_KEY ");
else
Stream << QLatin1String("0 !LEOCAD LIGHT TARGET_POSITION ") << mTargetPosition[0] << ' ' << mTargetPosition[1] << ' ' << mTargetPosition[2] << LineEnding;
}
2023-08-07 11:31:33 +02:00
if (mLightColorKeys.GetSize() > 1)
mLightColorKeys.SaveKeysLDraw(Stream, "LIGHT COLOR_RGB_KEY ");
else
Stream << QLatin1String("0 !LEOCAD LIGHT COLOR_RGB ") << mLightColor[0] << ' ' << mLightColor[1] << ' ' << mLightColor[2] << LineEnding;
2023-08-07 13:22:59 +02:00
if (!mPOVRayLight)
{
if (mLightDiffuseKeys.GetSize() > 1)
mLightDiffuseKeys.SaveKeysLDraw(Stream, "LIGHT DIFFUSE_KEY ");
else
Stream << QLatin1String("0 !LEOCAD LIGHT DIFFUSE ") << mLightDiffuse << LineEnding;
if (mLightSpecularKeys.GetSize() > 1)
mLightSpecularKeys.SaveKeysLDraw(Stream, "LIGHT SPECULAR_KEY ");
else
Stream << QLatin1String("0 !LEOCAD LIGHT SPECULAR ") << mLightSpecular << LineEnding;
}
2023-08-07 11:31:33 +02:00
2023-08-13 15:15:52 +02:00
if (mSpotExponentKeys.GetSize() > 1)
mSpotExponentKeys.SaveKeysLDraw(Stream, "LIGHT POWER_KEY ");
else
Stream << QLatin1String("0 !LEOCAD LIGHT POWER ") << (mPOVRayLight ? mPOVRayExponent : mSpotExponent) << LineEnding;
if (mEnableCutoff && !mPOVRayLight)
2023-08-07 11:31:33 +02:00
{
2023-08-13 15:15:52 +02:00
if (mSpotCutoffKeys.GetSize() > 1)
mSpotCutoffKeys.SaveKeysLDraw(Stream, "LIGHT CUTOFF_DISTANCE_KEY ");
2023-08-07 11:31:33 +02:00
else
2023-08-13 15:15:52 +02:00
Stream << QLatin1String("0 !LEOCAD LIGHT CUTOFF_DISTANCE ") << mSpotCutoff << LineEnding;
}
2023-08-07 11:31:33 +02:00
2023-08-13 15:15:52 +02:00
switch (mLightType)
{
case lcLightType::Point:
2023-08-07 13:22:59 +02:00
if (!mPOVRayLight)
{
if (mLightFactorKeys.GetSize() > 1)
2023-08-13 15:15:52 +02:00
mLightFactorKeys.SaveKeysLDraw(Stream, "LIGHT RADIUS_KEY ");
2023-08-07 13:22:59 +02:00
else
2023-08-13 15:15:52 +02:00
Stream << QLatin1String("0 !LEOCAD LIGHT RADIUS ") << mLightFactor[0] << LineEnding;
2023-08-07 13:22:59 +02:00
}
2023-08-13 15:15:52 +02:00
break;
2023-08-07 11:31:33 +02:00
2023-08-13 15:15:52 +02:00
case lcLightType::Spot:
if (mPOVRayLight)
2023-08-07 11:31:33 +02:00
{
2023-08-13 15:15:52 +02:00
if (mLightFactorKeys.GetSize() > 1)
mLightFactorKeys.SaveKeysLDraw(Stream, "LIGHT RADIUS_KEY ");
else
Stream << QLatin1String("0 !LEOCAD LIGHT RADIUS ") << (mSpotSize - mSpotFalloff) << LineEnding;
if (mSpotFalloffKeys.GetSize() > 1)
mSpotFalloffKeys.SaveKeysLDraw(Stream, "LIGHT SPOT_FALLOFF_KEY ");
else
Stream << QLatin1String("0 !LEOCAD LIGHT SPOT_FALLOFF ") << mSpotFalloff << LineEnding;
if (mSpotTightnessKeys.GetSize() > 1)
mSpotTightnessKeys.SaveKeysLDraw(Stream, "SPOT_TIGHTNESS_KEY ");
2023-08-07 11:31:33 +02:00
else
2023-08-13 15:15:52 +02:00
Stream << QLatin1String("0 !LEOCAD LIGHT SPOT_TIGHTNESS ") << mSpotTightness << LineEnding;
2023-08-07 11:31:33 +02:00
}
2023-08-13 15:15:52 +02:00
else
2023-08-07 11:31:33 +02:00
{
2023-08-13 15:15:52 +02:00
if (mSpotSizeKeys.GetSize() > 1)
mSpotSizeKeys.SaveKeysLDraw(Stream, "LIGHT SPOT_SIZE_KEY ");
2023-08-07 11:31:33 +02:00
else
2023-08-13 15:15:52 +02:00
Stream << QLatin1String("0 !LEOCAD LIGHT SPOT_SIZE ") << mSpotSize << LineEnding;
2023-08-07 13:22:59 +02:00
2023-08-13 15:15:52 +02:00
if (mLightFactorKeys.GetSize() > 1)
mLightFactorKeys.SaveKeysLDraw(Stream, "LIGHT RADIUS_AND_SPOT_BLEND_KEY ");
else
2023-08-07 13:22:59 +02:00
{
2023-08-13 15:15:52 +02:00
Stream << QLatin1String("0 !LEOCAD LIGHT RADIUS ") << mLightFactor[0] << LineEnding;
Stream << QLatin1String("0 !LEOCAD LIGHT SPOT_BLEND ") << mLightFactor[1] << LineEnding;
2023-08-07 13:22:59 +02:00
}
2023-08-13 15:15:52 +02:00
}
break;
case lcLightType::Directional:
if (mSpotExponentKeys.GetSize() > 1)
mSpotExponentKeys.SaveKeysLDraw(Stream, "LIGHT STRENGTH_KEY ");
else
Stream << QLatin1String("0 !LEOCAD LIGHT STRENGTH ") << mSpotExponent << LineEnding;
if (mLightFactorKeys.GetSize() > 1)
mLightFactorKeys.SaveKeysLDraw(Stream, "LIGHT ANGLE_KEY ");
else
Stream << QLatin1String("0 !LEOCAD LIGHT ANGLE ") << mLightFactor[0] << LineEnding;
break;
case lcLightType::Area:
if (mPOVRayLight)
{
if (mAreaGridKeys.GetSize() > 1)
mAreaGridKeys.SaveKeysLDraw(Stream, "LIGHT AREA_GRID_KEY ");
else
2023-08-13 15:15:52 +02:00
Stream << QLatin1String("0 !LEOCAD LIGHT AREA_ROWS ") << mAreaGrid[0] << QLatin1String(" AREA_COLUMNS ") << mAreaGrid[1] << LineEnding;
}
if (mLightFactorKeys.GetSize() > 1)
mLightFactorKeys.SaveKeysLDraw(Stream, "LIGHT SIZE_KEY ");
else
{
if (mPOVRayLight)
{
2023-08-13 15:15:52 +02:00
Stream << QLatin1String("0 !LEOCAD LIGHT WIDTH ") << mAreaSize[0] << QLatin1String(" HEIGHT ") << mAreaSize[1] << LineEnding;
2023-08-07 11:31:33 +02:00
}
else
{
2023-08-13 15:15:52 +02:00
if (mLightShape == LC_LIGHT_SHAPE_RECTANGLE || mLightShape == LC_LIGHT_SHAPE_ELLIPSE || mLightFactor[1] > 0)
Stream << QLatin1String("0 !LEOCAD LIGHT WIDTH ") << mLightFactor[0] << QLatin1String(" HEIGHT ") << mLightFactor[1] << LineEnding;
else
Stream << QLatin1String("0 !LEOCAD LIGHT SIZE ") << mLightFactor[0] << LineEnding;
2023-08-07 11:31:33 +02:00
}
}
2023-08-13 15:15:52 +02:00
Stream << QLatin1String("0 !LEOCAD LIGHT SHAPE ");
2023-08-07 11:31:33 +02:00
2023-08-13 15:15:52 +02:00
QString Shape = QLatin1String("UNDEFINED ");
switch (mLightShape)
{
2023-08-13 15:15:52 +02:00
case LC_LIGHT_SHAPE_SQUARE:
Shape = QLatin1String("SQUARE ");
2023-08-07 11:31:33 +02:00
break;
2023-08-13 15:15:52 +02:00
case LC_LIGHT_SHAPE_DISK:
Shape = QLatin1String("DISK ");
2023-08-07 11:31:33 +02:00
break;
2023-08-13 15:15:52 +02:00
case LC_LIGHT_SHAPE_RECTANGLE:
Shape = QLatin1String("RECTANGLE ");
2023-08-07 11:31:33 +02:00
break;
2023-08-13 15:15:52 +02:00
case LC_LIGHT_SHAPE_ELLIPSE:
Shape = QLatin1String("ELLIPSE ");
2023-08-07 11:31:33 +02:00
break;
}
2023-08-13 15:15:52 +02:00
Stream << QLatin1String("0 !LEOCAD LIGHT SHAPE ") << Shape << LineEnding;
break;
2023-08-07 11:31:33 +02:00
}
2023-08-13 15:15:52 +02:00
Stream << QLatin1String("0 !LEOCAD LIGHT TYPE ") << gLightTypes[static_cast<int>(mLightType)] << QLatin1String(" NAME ") << mName << LineEnding;
}
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;
2023-08-07 11:31:33 +02:00
2023-08-13 15:15:52 +02:00
QString Prefix;
switch (mLightType)
{
case lcLightType::Point:
Prefix = QLatin1String("Pointlight ");
break;
case lcLightType::Spot:
Prefix = QLatin1String("Spotlight ");
break;
case lcLightType::Directional:
Prefix = QLatin1String("Directionallight ");
break;
case lcLightType::Area:
Prefix = QLatin1String("Arealight ");
break;
}
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
}
2023-08-07 11:31:33 +02:00
bool lcLight::ParseLDrawLine(QTextStream& Stream)
{
while (!Stream.atEnd())
{
QString Token;
Stream >> Token;
if (Token == QLatin1String("COLOR_RGB"))
{
Stream >> mLightColor[0] >> mLightColor[1] >> mLightColor[2];
mLightColorKeys.ChangeKey(mLightColor, 1, true);
}
else if (Token == QLatin1String("POWER") || Token == QLatin1String("STRENGTH"))
{
2023-08-07 13:22:59 +02:00
if (mPOVRayLight)
{
Stream >> mPOVRayExponent;
mSpotExponentKeys.ChangeKey(mPOVRayExponent, 1, true);
}
else
{
Stream >> mSpotExponent;
mSpotExponentKeys.ChangeKey(mSpotExponent, 1, true);
}
2023-08-07 11:31:33 +02:00
}
else if (Token == QLatin1String("RADIUS") || Token == QLatin1String("SIZE") || Token == QLatin1String("WIDTH") || (mHeightSet = Token == QLatin1String("HEIGHT")) || (mSpotBlendSet = Token == QLatin1String("SPOT_BLEND")) || (mAngleSet = Token == QLatin1String("ANGLE")))
{
2023-08-07 13:22:59 +02:00
if (mPOVRayLight)
{
if (Token == QLatin1String("WIDTH"))
Stream >> mAreaSize[0];
else if (Token == QLatin1String("HEIGHT"))
Stream >> mAreaSize[1];
mLightFactorKeys.ChangeKey(mAreaSize, 1, true);
}
2023-08-07 11:31:33 +02:00
else
2023-08-07 13:22:59 +02:00
{
if(Token == QLatin1String("HEIGHT") || Token == QLatin1String("SPOT_BLEND"))
Stream >> mLightFactor[1];
else
Stream >> mLightFactor[0];
mLightFactorKeys.ChangeKey(mLightFactor, 1, true);
}
}
else if (Token == QLatin1String("AREA_ROWS"))
{
mPOVRayLight = true;
Stream >> mAreaGrid[0];
mAreaGridKeys.ChangeKey(mAreaGrid, 1, true);
}
else if (Token == QLatin1String("AREA_COLUMNS"))
{
mPOVRayLight = true;
Stream >> mAreaGrid[1];
mAreaGridKeys.ChangeKey(mAreaGrid, 1, true);
}
else if (Token == QLatin1String("SPOT_FALLOFF"))
{
mPOVRayLight = true;
Stream >> mSpotFalloff;
mSpotFalloffKeys.ChangeKey(mSpotFalloff, 1, true);
}
else if (Token == QLatin1String("SPOT_TIGHTNESS"))
{
mPOVRayLight = true;
Stream >> mSpotTightness;
mSpotTightnessKeys.ChangeKey(mSpotTightness, 1, true);
2023-08-07 11:31:33 +02:00
}
else if (Token == QLatin1String("SPOT_SIZE"))
{
Stream >> mSpotSize;
2023-08-07 13:22:59 +02:00
mSpotSizeKeys.ChangeKey(mSpotSize, 1, true);
2023-08-07 11:31:33 +02:00
}
else if (Token == QLatin1String("SHAPE"))
{
QString Shape;
Stream >> Shape;
2023-08-13 15:15:52 +02:00
Shape.replace("\"", "");
if (Shape == QLatin1String("SQUARE"))
2023-08-07 11:31:33 +02:00
mLightShape = LC_LIGHT_SHAPE_SQUARE;
2023-08-13 15:15:52 +02:00
else if (Shape == QLatin1String("DISK") || Shape == QLatin1String("CIRCLE"))
2023-08-07 11:31:33 +02:00
mLightShape = LC_LIGHT_SHAPE_DISK;
2023-08-13 15:15:52 +02:00
else if (Shape == QLatin1String("RECTANGLE"))
2023-08-07 11:31:33 +02:00
mLightShape = LC_LIGHT_SHAPE_RECTANGLE;
2023-08-13 15:15:52 +02:00
else if (Shape == QLatin1String("ELLIPSE"))
2023-08-07 11:31:33 +02:00
mLightShape = LC_LIGHT_SHAPE_ELLIPSE;
}
2023-08-07 13:22:59 +02:00
else if (Token == QLatin1String("DIFFUSE"))
{
Stream >>mLightDiffuse;
mLightDiffuseKeys.ChangeKey(mLightDiffuse, 1, true);
}
2023-08-07 11:31:33 +02:00
else if (Token == QLatin1String("SPECULAR"))
{
Stream >>mLightSpecular;
mLightSpecularKeys.ChangeKey(mLightSpecular, 1, true);
}
else if ((mSpotCutoffSet = Token == QLatin1String("CUTOFF_DISTANCE")))
{
mEnableCutoff = true;
Stream >> mSpotCutoff;
mSpotCutoffKeys.ChangeKey(mSpotCutoff, 1, true);
}
else if (Token == QLatin1String("TYPE"))
{
QString Type;
Stream >> Type;
2023-08-13 15:15:52 +02:00
for (size_t TypeIndex = 0; TypeIndex < gLightTypes.size(); TypeIndex++)
{
if (Type == gLightTypes[TypeIndex])
{
mLightType = static_cast<lcLightType>(TypeIndex);
break;
}
}
2023-08-07 11:31:33 +02:00
}
else if (Token == QLatin1String("POSITION"))
{
Stream >> mPosition[0] >> mPosition[1] >> mPosition[2];
mPositionKeys.ChangeKey(mPosition, 1, true);
}
else if (Token == QLatin1String("TARGET_POSITION"))
{
Stream >> mTargetPosition[0] >> mTargetPosition[1] >> mTargetPosition[2];
mTargetPositionKeys.ChangeKey(mTargetPosition, 1, true);
}
2023-08-07 13:22:59 +02:00
else if (Token == QLatin1String("POV_RAY"))
{
mPOVRayLight = true;
}
else if (Token == QLatin1String("SHADOWLESS"))
{
mShadowless = true;
}
2023-08-07 11:31:33 +02:00
else if (Token == QLatin1String("COLOR_RGB_KEY"))
mLightColorKeys.LoadKeysLDraw(Stream);
else if ((Token == QLatin1String("POWER_KEY")) || (Token == QLatin1String("STRENGTH_KEY")))
mSpotExponentKeys.LoadKeysLDraw(Stream);
else if ((Token == QLatin1String("ANGLE_KEY")) || (Token == QLatin1String("RADIUS_KEY")) || (Token == QLatin1String("SIZE_KEY")) || (Token == QLatin1String("RADIUS_AND_SPOT_BLEND_KEY")))
mLightFactorKeys.LoadKeysLDraw(Stream);
else if (Token == QLatin1String("SPOT_SIZE_KEY"))
2023-08-07 13:22:59 +02:00
mSpotSizeKeys.LoadKeysLDraw(Stream);
else if (Token == QLatin1String("SPOT_FALLOFF_KEY"))
mSpotFalloffKeys.LoadKeysLDraw(Stream);
else if (Token == QLatin1String("SPOT_TIGHTNESS_KEY"))
mSpotTightnessKeys.LoadKeysLDraw(Stream);
else if (Token == QLatin1String("AREA_GRID_KEY"))
mAreaGridKeys.LoadKeysLDraw(Stream);
else if (Token == QLatin1String("DIFFUSE_KEY"))
mLightDiffuseKeys.LoadKeysLDraw(Stream);
2023-08-07 11:31:33 +02:00
else if (Token == QLatin1String("SPECULAR_KEY"))
mLightSpecularKeys.LoadKeysLDraw(Stream);
else if (Token == QLatin1String("CUTOFF_DISTANCE_KEY"))
mSpotCutoffKeys.LoadKeysLDraw(Stream);
else if (Token == QLatin1String("POSITION_KEY"))
mPositionKeys.LoadKeysLDraw(Stream);
else if (Token == QLatin1String("TARGET_POSITION_KEY"))
mTargetPositionKeys.LoadKeysLDraw(Stream);
else if (Token == QLatin1String("NAME"))
{
mName = Stream.readAll().trimmed();
mName.replace("\"", "");
// Set default settings per light type
2023-08-13 15:15:52 +02:00
switch (mLightType)
{
2023-08-13 15:15:52 +02:00
case lcLightType::Point:
break;
case lcLightType::Spot:
if (!mSpotBlendSet)
{
2023-08-07 11:31:33 +02:00
mLightFactor[1] = 0.15f;
mLightFactorKeys.ChangeKey(mLightFactor, 1, true);
}
2023-08-13 15:15:52 +02:00
break;
case lcLightType::Directional:
if (!mAngleSet)
{
2023-08-07 11:31:33 +02:00
mLightFactor[0] = 11.4f;
mLightFactorKeys.ChangeKey(mLightFactor, 1, true);
}
2023-08-13 15:15:52 +02:00
if (!mSpotCutoffSet)
{
2023-08-13 15:15:52 +02:00
mSpotCutoff = 0.0f;
2023-08-07 11:31:33 +02:00
mSpotCutoffKeys.ChangeKey(mSpotCutoff, 1, true);
}
2023-08-13 15:15:52 +02:00
break;
case lcLightType::Area:
if (mLightShape == LC_LIGHT_SHAPE_RECTANGLE || mLightShape == LC_LIGHT_SHAPE_ELLIPSE)
{
if (!mHeightSet)
{
mLightFactor[1] = 0.25f;
mLightFactorKeys.ChangeKey(mLightFactor, 1, true);
}
}
break;
2023-08-07 11:31:33 +02:00
}
2023-08-13 15:15:52 +02:00
2023-08-07 11:31:33 +02:00
return true;
}
}
return false;
}
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
}
}
2023-08-07 11:31:33 +02:00
void lcLight::UpdateLight(lcStep Step, lcLightProperties Props, int Property)
{
switch(Property)
{
2023-08-07 11:31:33 +02:00
case LC_LIGHT_SHAPE:
mLightShape = Props.mLightShape;
break;
case LC_LIGHT_COLOR:
mLightColor = Props.mLightColor;
mLightColorKeys.ChangeKey(mLightColor, Step, false);
break;
case LC_LIGHT_FACTOR:
2023-08-13 15:15:52 +02:00
if (Props.mPOVRayLight && mLightType == lcLightType::Area)
2023-08-07 13:22:59 +02:00
{
mAreaSize = Props.mLightFactor;
mLightFactorKeys.ChangeKey(mAreaSize, 1, true);
}
else
{
mLightFactor = Props.mLightFactor;
mLightFactorKeys.ChangeKey(mLightFactor, 1, true);
}
break;
case LC_LIGHT_DIFFUSE:
mLightDiffuse = Props.mLightDiffuse;
mLightDiffuseKeys.ChangeKey(mLightDiffuse, Step, false);
2023-08-07 11:31:33 +02:00
break;
case LC_LIGHT_SPECULAR:
mLightSpecular = Props.mLightSpecular;
mLightSpecularKeys.ChangeKey(mLightSpecular, Step, false);
break;
2023-08-07 13:22:59 +02:00
case LC_LIGHT_SHADOWLESS:
mShadowless = Props.mShadowless;
break;
2023-08-07 11:31:33 +02:00
case LC_LIGHT_EXPONENT:
2023-08-07 13:22:59 +02:00
if (Props.mPOVRayLight)
{
mPOVRayExponent = Props.mSpotExponent;
mSpotExponentKeys.ChangeKey(mPOVRayExponent, Step, false);
}
else
{
mSpotExponent = Props.mSpotExponent;
mSpotExponentKeys.ChangeKey(mSpotExponent, Step, false);
}
break;
case LC_LIGHT_AREA_GRID:
mAreaGrid = Props.mAreaGrid;
mAreaGridKeys.ChangeKey(mAreaGrid, Step, false);
2023-08-07 11:31:33 +02:00
break;
case LC_LIGHT_SPOT_SIZE:
mSpotSize = Props.mSpotSize;
2023-08-07 13:22:59 +02:00
mSpotSizeKeys.ChangeKey(mSpotSize, Step, false);
break;
case LC_LIGHT_SPOT_FALLOFF:
mSpotFalloff = Props.mSpotFalloff;
mSpotFalloffKeys.ChangeKey(mSpotFalloff, Step, false);
break;
case LC_LIGHT_SPOT_TIGHTNESS:
mSpotTightness = Props.mSpotTightness;
mSpotTightnessKeys.ChangeKey(mSpotTightness, Step, false);
2023-08-07 11:31:33 +02:00
break;
case LC_LIGHT_CUTOFF:
mSpotCutoff = Props.mSpotCutoff;
mSpotCutoffKeys.ChangeKey(mSpotCutoff, Step, false);
break;
case LC_LIGHT_USE_CUTOFF:
mEnableCutoff = Props.mEnableCutoff;
break;
2023-08-07 13:22:59 +02:00
case LC_LIGHT_POVRAY:
mPOVRayLight = Props.mPOVRayLight;
break;
2023-08-07 11:31:33 +02:00
}
UpdatePosition(Step);
}
2014-05-01 20:42:11 +02:00
void lcLight::RayTest(lcObjectRayTest& ObjectRayTest) const
2011-09-07 23:06:51 +02:00
{
if (IsPointLight())
2012-09-10 01:42:57 +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-08-07 17:22:33 +02:00
ObjectRayTest.ObjectSection.Object = const_cast<lcLight*>(this);
ObjectRayTest.ObjectSection.Section = LC_LIGHT_SECTION_POSITION;
ObjectRayTest.Distance = Distance;
2012-09-10 01:42:57 +02:00
}
return;
2012-09-10 01:42:57 +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);
lcVector3 Start = lcMul31(ObjectRayTest.Start, mWorldLight);
lcVector3 End = lcMul31(ObjectRayTest.End, mWorldLight);
float Distance;
lcVector3 Plane;
if (lcBoundingBoxRayIntersectDistance(Min, Max, Start, End, &Distance, nullptr, &Plane) && (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);
ObjectRayTest.ObjectSection.Section = LC_LIGHT_SECTION_POSITION;
ObjectRayTest.Distance = Distance;
ObjectRayTest.PieceInfoRayTest.Plane = Plane;
}
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);
lcMatrix44 WorldTarget = mWorldLight;
WorldTarget.SetTranslation(lcMul30(-mTargetPosition, WorldTarget));
Start = lcMul31(ObjectRayTest.Start, WorldTarget);
End = lcMul31(ObjectRayTest.End, WorldTarget);
if (lcBoundingBoxRayIntersectDistance(Min, Max, Start, End, &Distance, nullptr, &Plane) && (Distance < ObjectRayTest.Distance))
{
2014-08-07 17:22:33 +02:00
ObjectRayTest.ObjectSection.Object = const_cast<lcLight*>(this);
ObjectRayTest.ObjectSection.Section = LC_LIGHT_SECTION_TARGET;
ObjectRayTest.Distance = Distance;
ObjectRayTest.PieceInfoRayTest.Plane = Plane;
}
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
{
if (IsPointLight())
2012-09-10 01:42:57 +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)
return;
2012-09-10 01:42:57 +02:00
2014-11-29 03:55:58 +01:00
ObjectBoxTest.Objects.Add(const_cast<lcLight*>(this));
return;
2012-09-10 01:42:57 +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);
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);
LocalPlanes[PlaneIdx] = lcVector4(Normal, ObjectBoxTest.Planes[PlaneIdx][3] - lcDot3(mWorldLight[3], Normal));
}
2011-09-07 23:06:51 +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;
}
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
lcMatrix44 WorldTarget = mWorldLight;
WorldTarget.SetTranslation(lcMul30(-mTargetPosition, WorldTarget));
2011-09-07 23:06:51 +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);
LocalPlanes[PlaneIdx] = lcVector4(Normal, ObjectBoxTest.Planes[PlaneIdx][3] - lcDot3(WorldTarget[3], Normal));
2012-08-17 01:50:40 +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
}
void lcLight::MoveSelected(lcStep Step, bool AddKey, const lcVector3& Distance)
2011-09-07 23:06:51 +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;
mPositionKeys.ChangeKey(mPosition, Step, AddKey);
2012-06-07 02:08:59 +02:00
}
2011-09-07 23:06:51 +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;
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)
{
mPositionKeys.InsertTime(Start, Time);
mTargetPositionKeys.InsertTime(Start, Time);
mAmbientColorKeys.InsertTime(Start, Time);
mDiffuseColorKeys.InsertTime(Start, Time);
mSpecularColorKeys.InsertTime(Start, Time);
mAttenuationKeys.InsertTime(Start, Time);
2023-08-07 11:31:33 +02:00
mLightColorKeys.InsertTime(Start, Time);
mLightFactorKeys.InsertTime(Start, Time);
2023-08-07 13:22:59 +02:00
mLightDiffuseKeys.InsertTime(Start, Time);
2023-08-07 11:31:33 +02:00
mLightSpecularKeys.InsertTime(Start, Time);
2023-08-07 13:22:59 +02:00
mSpotSizeKeys.InsertTime(Start, Time);
mSpotCutoffKeys.InsertTime(Start, Time);
mSpotExponentKeys.InsertTime(Start, Time);
2023-08-07 13:22:59 +02:00
mSpotFalloffKeys.InsertTime(Start, Time);
mSpotTightnessKeys.InsertTime(Start, Time);
mAreaGridKeys.InsertTime(Start, Time);
2014-08-31 02:53:12 +02:00
}
void lcLight::RemoveTime(lcStep Start, lcStep Time)
{
mPositionKeys.RemoveTime(Start, Time);
mTargetPositionKeys.RemoveTime(Start, Time);
mAmbientColorKeys.RemoveTime(Start, Time);
mDiffuseColorKeys.RemoveTime(Start, Time);
mSpecularColorKeys.RemoveTime(Start, Time);
mAttenuationKeys.RemoveTime(Start, Time);
2023-08-07 11:31:33 +02:00
mLightColorKeys.RemoveTime(Start, Time);
mLightFactorKeys.RemoveTime(Start, Time);
2023-08-07 13:22:59 +02:00
mLightDiffuseKeys.RemoveTime(Start, Time);
2023-08-07 11:31:33 +02:00
mLightSpecularKeys.RemoveTime(Start, Time);
2023-08-07 13:22:59 +02:00
mSpotSizeKeys.RemoveTime(Start, Time);
mSpotCutoffKeys.RemoveTime(Start, Time);
mSpotExponentKeys.RemoveTime(Start, Time);
2023-08-07 13:22:59 +02:00
mSpotFalloffKeys.RemoveTime(Start, Time);
mSpotTightnessKeys.RemoveTime(Start, Time);
mAreaGridKeys.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
{
mPosition = mPositionKeys.CalculateKey(Step);
mTargetPosition = mTargetPositionKeys.CalculateKey(Step);
mAmbientColor = mAmbientColorKeys.CalculateKey(Step);
mDiffuseColor = mDiffuseColorKeys.CalculateKey(Step);
mSpecularColor = mSpecularColorKeys.CalculateKey(Step);
mAttenuation = mAttenuationKeys.CalculateKey(Step);
2023-08-07 11:31:33 +02:00
mLightColor = mLightColorKeys.CalculateKey(Step);
mLightFactor = mLightFactorKeys.CalculateKey(Step);
2023-08-07 13:22:59 +02:00
mLightDiffuse = mLightDiffuseKeys.CalculateKey(Step);
2023-08-07 11:31:33 +02:00
mLightSpecular = mLightSpecularKeys.CalculateKey(Step);
2023-08-07 13:22:59 +02:00
mSpotSize = mSpotSizeKeys.CalculateKey(Step);
mSpotCutoff = mSpotCutoffKeys.CalculateKey(Step);
mSpotExponent = mSpotExponentKeys.CalculateKey(Step);
2023-08-07 13:22:59 +02:00
mSpotFalloff = mSpotFalloffKeys.CalculateKey(Step);
mSpotTightness = mSpotTightnessKeys.CalculateKey(Step);
mAreaGrid = mAreaGridKeys.CalculateKey(Step);
2011-09-07 23:06:51 +02:00
if (IsPointLight())
2012-03-29 03:10:55 +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
if (fabs(FrontVector[0]) < fabs(FrontVector[1]))
2012-08-17 01:50:40 +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
UpVector[2] = -(UpVector[0] * FrontVector[0] + UpVector[1] * FrontVector[1]);
2012-08-17 01:50:40 +02:00
}
else
{
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
UpVector[2] = -(UpVector[0] * FrontVector[0] + UpVector[1] * FrontVector[1]);
2012-08-17 01:50:40 +02:00
}
mWorldLight = lcMatrix44LookAt(mPosition, mTargetPosition, UpVector);
2012-08-17 01:50:40 +02:00
}
2011-09-07 23:06:51 +02:00
}
void lcLight::DrawInterface(lcContext* Context, const lcScene& Scene) const
2011-09-07 23:06:51 +02:00
{
Q_UNUSED(Scene);
2020-03-22 21:44:20 +01:00
Context->SetMaterial(lcMaterialType::UnlitColor);
if (IsPointLight())
2015-05-17 01:04:35 +02:00
DrawPointLight(Context);
2015-04-19 03:10:01 +02:00
else
DrawDirectionalLight(Context);
2015-04-19 03:10:01 +02:00
}
2023-08-07 11:31:33 +02:00
void lcLight::DrawDirectionalLight(lcContext* Context) const
{
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]);
else
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));
const lcMatrix44 LightViewMatrix = lcMul(LightMatrix, lcMatrix44Translation(mPosition));
2023-08-07 11:31:33 +02:00
Context->SetWorldMatrix(LightViewMatrix);
float Length = FrontVector.Length();
2023-08-07 11:31:33 +02:00
float Verts[(20 + 8 + 2 + 16) * 3];
float* CurVert = Verts;
2023-08-13 15:15:52 +02:00
if (mLightType != lcLightType::Directional)
2023-08-07 11:31:33 +02:00
{
2023-08-13 15:15:52 +02:00
if (mLightType == lcLightType::Spot)
{
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;
}
2023-08-07 11:31:33 +02:00
*CurVert++ = -LC_LIGHT_SPOT_BASE_EDGE; *CurVert++ = -LC_LIGHT_SPOT_BASE_EDGE; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
*CurVert++ = LC_LIGHT_SPOT_BASE_EDGE; *CurVert++ = -LC_LIGHT_SPOT_BASE_EDGE; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
*CurVert++ = LC_LIGHT_SPOT_BASE_EDGE; *CurVert++ = LC_LIGHT_SPOT_BASE_EDGE; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
*CurVert++ = -LC_LIGHT_SPOT_BASE_EDGE; *CurVert++ = LC_LIGHT_SPOT_BASE_EDGE; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
}
2023-08-13 15:15:52 +02:00
else if (mLightType == lcLightType::Area)
{
const float LC_LIGHT_AREA_EDGE = 5.0f;
const float LC_LIGHT_AREA_H_EDGE = 8.5f;
const float LC_LIGHT_AREA_W_EDGE = 17.0f;
*CurVert++ = LC_LIGHT_AREA_EDGE; *CurVert++ = LC_LIGHT_AREA_EDGE; *CurVert++ = LC_LIGHT_POSITION_EDGE;
*CurVert++ = -LC_LIGHT_AREA_EDGE; *CurVert++ = LC_LIGHT_AREA_EDGE; *CurVert++ = LC_LIGHT_POSITION_EDGE;
*CurVert++ = -LC_LIGHT_AREA_EDGE; *CurVert++ = -LC_LIGHT_AREA_EDGE; *CurVert++ = LC_LIGHT_POSITION_EDGE;
*CurVert++ = LC_LIGHT_AREA_EDGE; *CurVert++ = -LC_LIGHT_AREA_EDGE; *CurVert++ = LC_LIGHT_POSITION_EDGE;
*CurVert++ = LC_LIGHT_AREA_EDGE; *CurVert++ = LC_LIGHT_AREA_EDGE; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
*CurVert++ = -LC_LIGHT_AREA_EDGE; *CurVert++ = LC_LIGHT_AREA_EDGE; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
*CurVert++ = -LC_LIGHT_AREA_EDGE; *CurVert++ = -LC_LIGHT_AREA_EDGE; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
*CurVert++ = LC_LIGHT_AREA_EDGE; *CurVert++ = -LC_LIGHT_AREA_EDGE; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
*CurVert++ = -LC_LIGHT_AREA_H_EDGE; *CurVert++ = -LC_LIGHT_AREA_W_EDGE; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
*CurVert++ = LC_LIGHT_AREA_H_EDGE; *CurVert++ = -LC_LIGHT_AREA_W_EDGE; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
*CurVert++ = LC_LIGHT_AREA_H_EDGE; *CurVert++ = LC_LIGHT_AREA_W_EDGE; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
*CurVert++ = -LC_LIGHT_AREA_H_EDGE; *CurVert++ = LC_LIGHT_AREA_W_EDGE; *CurVert++ = -LC_LIGHT_POSITION_EDGE;
}
2023-08-07 11:31:33 +02:00
*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;
2023-08-07 11:31:33 +02:00
*CurVert++ = 0.0f; *CurVert++ = 0.0f; *CurVert++ = 0.0f;
*CurVert++ = 0.0f; *CurVert++ = 0.0f; *CurVert++ = -Length;
2023-08-07 11:31:33 +02:00
Context->SetVertexBufferPointer(Verts);
Context->SetVertexFormatPosition(3);
2023-08-07 11:31:33 +02:00
}
int BaseIndices = 0;
2023-08-07 11:31:33 +02:00
2023-08-13 15:15:52 +02:00
if (mLightType == lcLightType::Spot)
{
BaseIndices = 56;
const GLushort Indices[56 + 24 + 2 + 40] =
2023-08-07 11:31:33 +02:00
{
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
2023-08-07 11:31:33 +02:00
};
Context->SetIndexBufferPointer(Indices);
}
2023-08-13 15:15:52 +02:00
else if (mLightType == lcLightType::Area)
2023-08-07 11:31:33 +02:00
{
BaseIndices = 32;
const GLushort Indices[32 + 24 + 2] =
{
0, 1, 1, 2, 2, 3, 3, 0,
4, 5, 5, 6, 6, 7, 7, 4,
0, 4, 1, 5, 2, 6, 3, 7,
8, 9, 9, 10, 10, 11, 11, 8,
12, 13, 13, 14, 14, 15, 15, 12,
16, 17, 17, 18, 18, 19, 19, 16,
12, 16, 13, 17, 14, 18, 15, 19,
20, 21
};
2023-08-07 11:31:33 +02:00
Context->SetIndexBufferPointer(Indices);
2023-08-07 11:31:33 +02:00
}
2023-08-13 15:15:52 +02:00
else if (mLightType == lcLightType::Directional)
2023-08-07 11:31:33 +02:00
{
constexpr float Radius = LC_LIGHT_SUN_RADIUS;
constexpr int Slices = 9; // longitude
constexpr int Stacks = 9; // latitude
constexpr int NumSphereVertices = 918; // Slices * 2 * 3 * 3 + (Stacks - 2) * Slices * 4 * 3
constexpr int NumSphereIndices = 306; // Slices * 2 + (Stacks - 1) * Slices * 2 * 2
2023-08-07 11:31:33 +02:00
BaseIndices = NumSphereIndices;
2023-08-07 11:31:33 +02:00
float Vertices[NumSphereVertices + 24 + 6];
float *Vert = Vertices;
quint16 Indices[NumSphereIndices + 24 + 2];
quint16 *Indx = Indices;
auto AddVertex = [&](float x, float y, float z)
2023-08-07 11:31:33 +02:00
{
*Vert++ = x;
*Vert++ = y;
*Vert++ = z;
};
auto AddLineIndex = [&](quint16 v1, quint16 v2)
2023-08-07 11:31:33 +02:00
{
*Indx++ = v1;
*Indx++ = v2;
};
2023-08-07 11:31:33 +02:00
std::vector<lcVector3> WrkVertices;
2023-08-07 11:31:33 +02:00
float Slice = LC_2PI / Slices;
float Stack = LC_PI / Stacks;
float SliceAngle, StackAngle;
2023-08-07 11:31:33 +02:00
for(int i = 0; i <= Stacks; ++i)
{
StackAngle = LC_PI / 2 - i * Stack; // starting from pi/2 to -pi/2
float xy = Radius * cosf(StackAngle);
float z = Radius * sinf(StackAngle);
2023-08-07 11:31:33 +02:00
for(int j = 0; j <= Slices; ++j) // add (Slices+1) vertices per stack
{
SliceAngle = j * Slice;
2015-04-19 03:10:01 +02:00
lcVector3 Vertex;
Vertex.x = xy * cosf(SliceAngle);
Vertex.y = xy * sinf(SliceAngle);
Vertex.z = z;
2015-04-19 03:10:01 +02:00
WrkVertices.push_back(Vertex);
}
}
2015-04-19 03:10:01 +02:00
int Index = 0;
2015-04-19 03:10:01 +02:00
lcVector3 v1, v2, v3, v4;
2015-04-19 03:10:01 +02:00
for(int i = 0; i < Stacks; ++i)
{
int vi1 = i * (Slices + 1);
int vi2 = (i + 1) * (Slices + 1);
2015-04-19 03:10:01 +02:00
for(int j = 0; j < Slices; ++j, ++vi1, ++vi2)
{
// 4 vertices per slice
// v1--v3
// | |
// v2--v4
v1 = WrkVertices[vi1];
v2 = WrkVertices[vi2];
v3 = WrkVertices[vi1 + 1];
v4 = WrkVertices[vi2 + 1];
// if first stack or last stack, store 1 triangle per slice else, store 2 triangles (1 quad) per slice
if(i == 0)
{
// first stack triangle v1-v2-v4
AddVertex(v1.x, v1.y, v1.z);
AddVertex(v2.x, v2.y, v2.z);
AddVertex(v4.x, v4.y, v4.z);
2015-04-19 03:10:01 +02:00
// only vertical lines for first stack)
AddLineIndex(Index, Index+1);
2015-04-19 03:10:01 +02:00
Index += 3;
}
else if(i == (Stacks-1))
{
// last stack inverted triangle v1-v2-v3
AddVertex(v1.x, v1.y, v1.z);
AddVertex(v2.x, v2.y, v2.z);
AddVertex(v3.x, v3.y, v3.z);
2015-04-19 03:10:01 +02:00
// both vertical and horizontal lines for last stack
AddLineIndex(Index, Index+1);
AddLineIndex(Index, Index+2);
2015-04-19 03:10:01 +02:00
Index += 3;
}
else
{
// 2 triangles (quad vertices v1-v2-v3-v4) for other stacks
AddVertex(v1.x, v1.y, v1.z);
AddVertex(v2.x, v2.y, v2.z);
AddVertex(v3.x, v3.y, v3.z);
AddVertex(v4.x, v4.y, v4.z);
2015-04-19 03:10:01 +02:00
// both vertical and horizontal lines for other stacks
AddLineIndex(Index, Index+1);
AddLineIndex(Index, Index+2);
Index += 4;
}
}
}
AddVertex( LC_LIGHT_TARGET_EDGE, LC_LIGHT_TARGET_EDGE, LC_LIGHT_TARGET_EDGE - Length);
AddVertex(-LC_LIGHT_TARGET_EDGE, LC_LIGHT_TARGET_EDGE, LC_LIGHT_TARGET_EDGE - Length);
AddVertex(-LC_LIGHT_TARGET_EDGE, -LC_LIGHT_TARGET_EDGE, LC_LIGHT_TARGET_EDGE - Length);
AddVertex( LC_LIGHT_TARGET_EDGE, -LC_LIGHT_TARGET_EDGE, LC_LIGHT_TARGET_EDGE - Length);
AddVertex( LC_LIGHT_TARGET_EDGE, LC_LIGHT_TARGET_EDGE, -LC_LIGHT_TARGET_EDGE - Length);
AddVertex(-LC_LIGHT_TARGET_EDGE, LC_LIGHT_TARGET_EDGE, -LC_LIGHT_TARGET_EDGE - Length);
AddVertex(-LC_LIGHT_TARGET_EDGE, -LC_LIGHT_TARGET_EDGE, -LC_LIGHT_TARGET_EDGE - Length);
AddVertex( LC_LIGHT_TARGET_EDGE, -LC_LIGHT_TARGET_EDGE, -LC_LIGHT_TARGET_EDGE - Length);
AddVertex(0.0f, 0.0f, 0.0f);
AddVertex(0.0f, 0.0f, -Length);
const int Idx[10] = { Index++, Index++, Index++, Index++, Index++, Index++, Index++, Index++, Index++, Index++ };
AddLineIndex(Idx[0], Idx[1]); AddLineIndex(Idx[1], Idx[2]); AddLineIndex(Idx[2], Idx[3]); AddLineIndex(Idx[3], Idx[0]);
AddLineIndex(Idx[4], Idx[5]); AddLineIndex(Idx[5], Idx[6]); AddLineIndex(Idx[6], Idx[7]); AddLineIndex(Idx[7], Idx[4]);
AddLineIndex(Idx[0], Idx[4]); AddLineIndex(Idx[1], Idx[5]); AddLineIndex(Idx[2], Idx[6]); AddLineIndex(Idx[3], Idx[7]);
AddLineIndex(Idx[8], Idx[9]);
Context->SetVertexBufferPointer(Vertices);
Context->SetVertexFormatPosition(3);
Context->SetIndexBufferPointer(Indices);
}
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
Context->DrawIndexedPrimitives(GL_LINES, BaseIndices + 24 + 2, GL_UNSIGNED_SHORT, 0);
}
else
2012-06-07 02:08:59 +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);
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
Context->DrawIndexedPrimitives(GL_LINES, BaseIndices, GL_UNSIGNED_SHORT, 0);
2014-04-14 05:20:16 +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);
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
Context->DrawIndexedPrimitives(GL_LINES, 24, GL_UNSIGNED_SHORT, BaseIndices * 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
int SpotCone = 0;
2012-09-10 01:42:57 +02:00
2023-08-13 15:15:52 +02:00
if (mLightType == lcLightType::Spot)
2012-06-07 02:08:59 +02:00
{
SpotCone = 40;
float Radius = tanf(LC_DTOR * mSpotCutoff) * Length;
for (int EdgeIdx = 0; EdgeIdx < 16; EdgeIdx++)
{
*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
Context->DrawIndexedPrimitives(GL_LINES, 2 + SpotCone, GL_UNSIGNED_SHORT, (BaseIndices + 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
}
void lcLight::RemoveKeyFrames()
{
mPositionKeys.RemoveAll();
mPositionKeys.ChangeKey(mPosition, 1, true);
mTargetPositionKeys.RemoveAll();
mTargetPositionKeys.ChangeKey(mTargetPosition, 1, true);
mAmbientColorKeys.RemoveAll();
mAmbientColorKeys.ChangeKey(mAmbientColor, 1, true);
mDiffuseColorKeys.RemoveAll();
mDiffuseColorKeys.ChangeKey(mDiffuseColor, 1, true);
mSpecularColorKeys.RemoveAll();
mSpecularColorKeys.ChangeKey(mSpecularColor, 1, true);
mAttenuationKeys.RemoveAll();
mAttenuationKeys.ChangeKey(mAttenuation, 1, true);
2023-08-07 11:31:33 +02:00
mLightColorKeys.RemoveAll();
mLightColorKeys.ChangeKey(mLightColor, 1, true);
mLightFactorKeys.RemoveAll();
mLightFactorKeys.ChangeKey(mLightFactor, 1, true);
2023-08-07 13:22:59 +02:00
mLightDiffuseKeys.RemoveAll();
mLightDiffuseKeys.ChangeKey(mLightDiffuse, 1, true);
2023-08-07 11:31:33 +02:00
mLightSpecularKeys.RemoveAll();
mLightSpecularKeys.ChangeKey(mLightSpecular, 1, true);
2023-08-07 13:22:59 +02:00
mSpotSizeKeys.RemoveAll();
mSpotSizeKeys.ChangeKey(mSpotSize, 1, false);
2023-08-07 11:31:33 +02:00
mSpotCutoffKeys.RemoveAll();
mSpotCutoffKeys.ChangeKey(mSpotCutoff, 1, true);
mSpotExponentKeys.RemoveAll();
mSpotExponentKeys.ChangeKey(mSpotExponent, 1, true);
2023-08-07 13:22:59 +02:00
mSpotFalloffKeys.RemoveAll();
mSpotFalloffKeys.ChangeKey(mSpotFalloff, 1, true);
mSpotTightnessKeys.RemoveAll();
mSpotTightnessKeys.ChangeKey(mSpotTightness, 1, true);
mAreaGridKeys.RemoveAll();
mAreaGridKeys.ChangeKey(mAreaGrid, 1, true);
}
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);
return true;
2011-09-07 23:06:51 +02:00
}