leocad/common/camera.cpp

1161 lines
33 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 <stdlib.h>
#include <string.h>
#include <math.h>
2013-01-27 02:22:37 +01:00
#include <float.h>
#include "lc_file.h"
2011-09-07 23:06:51 +02:00
#include "camera.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_CAMERA_POSITION_EDGE 7.5f
#define LC_CAMERA_TARGET_EDGE 7.5f
2014-08-30 21:48:36 +02:00
#define LC_CAMERA_SAVE_VERSION 7 // LeoCAD 0.80
2011-09-07 23:06:51 +02:00
2014-05-01 20:42:11 +02:00
lcCamera::lcCamera(bool Simple)
2020-04-19 04:45:21 +02:00
: lcObject(lcObjectType::Camera)
2011-09-07 23:06:51 +02:00
{
2012-08-20 06:05:56 +02:00
Initialize();
if (Simple)
mState |= LC_CAMERA_SIMPLE;
2012-08-23 20:47:37 +02:00
else
{
2014-08-30 21:48:36 +02:00
mPosition = lcVector3(-250.0f, -250.0f, 75.0f);
2012-08-23 20:47:37 +02:00
mTargetPosition = lcVector3(0.0f, 0.0f, 0.0f);
mUpVector = lcVector3(-0.2357f, -0.2357f, 0.94281f);
2014-08-31 02:53:12 +02:00
ChangeKey(mPositionKeys, mPosition, 1, true);
ChangeKey(mTargetPositionKeys, mTargetPosition, 1, true);
ChangeKey(mUpVectorKeys, mUpVector, 1, true);
2012-08-23 20:47:37 +02:00
2014-01-30 04:13:34 +01:00
UpdatePosition(1);
2012-08-23 20:47:37 +02:00
}
2011-09-07 23:06:51 +02:00
}
2014-05-01 20:42:11 +02:00
lcCamera::lcCamera(float ex, float ey, float ez, float tx, float ty, float tz)
2020-04-19 04:45:21 +02:00
: lcObject(lcObjectType::Camera)
2011-09-07 23:06:51 +02:00
{
2012-08-22 03:13:32 +02:00
// Fix the up vector
lcVector3 UpVector(0, 0, 1), FrontVector(ex - tx, ey - ty, ez - tz), SideVector;
FrontVector.Normalize();
if (FrontVector == UpVector)
SideVector = lcVector3(1, 0, 0);
else
SideVector = lcCross(FrontVector, UpVector);
UpVector = lcCross(SideVector, FrontVector);
UpVector.Normalize();
Initialize();
2014-08-31 02:53:12 +02:00
ChangeKey(mPositionKeys, lcVector3(ex, ey, ez), 1, true);
ChangeKey(mTargetPositionKeys, lcVector3(tx, ty, tz), 1, true);
ChangeKey(mUpVectorKeys, UpVector, 1, true);
2012-08-22 03:13:32 +02:00
2014-01-30 04:13:34 +01:00
UpdatePosition(1);
2011-09-07 23:06:51 +02:00
}
2014-05-01 20:42:11 +02:00
lcCamera::~lcCamera()
2011-09-07 23:06:51 +02:00
{
}
2021-01-02 00:04:27 +01:00
lcViewpoint lcCamera::GetViewpoint(const QString& ViewpointName)
{
const QLatin1String ViewpointNames[] =
{
QLatin1String("front"),
QLatin1String("back"),
QLatin1String("top"),
QLatin1String("bottom"),
QLatin1String("left"),
QLatin1String("right"),
QLatin1String("home")
};
LC_ARRAY_SIZE_CHECK(ViewpointNames, lcViewpoint::Count);
for (int ViewpointIndex = 0; ViewpointIndex < static_cast<int>(lcViewpoint::Count); ViewpointIndex++)
if (ViewpointNames[ViewpointIndex] == ViewpointName)
return static_cast<lcViewpoint>(ViewpointIndex);
return lcViewpoint::Count;
}
2014-05-01 20:42:11 +02:00
void lcCamera::Initialize()
2011-09-07 23:06:51 +02:00
{
2012-08-23 20:47:37 +02:00
m_fovy = 30.0f;
2014-08-30 21:48:36 +02:00
m_zNear = 25.0f;
m_zFar = 50000.0f;
mState = 0;
2020-12-14 01:27:21 +01:00
}
void lcCamera::SetName(const QString& Name)
{
mName = Name;
2011-09-07 23:06:51 +02:00
}
2014-08-07 17:22:33 +02:00
void lcCamera::CreateName(const lcArray<lcCamera*>& Cameras)
2012-08-22 03:13:32 +02:00
{
2020-12-14 01:27:21 +01:00
if (!mName.isEmpty())
{
bool Found = false;
2020-12-14 01:27:21 +01:00
for (const lcCamera* Camera : Cameras)
{
2020-12-14 01:27:21 +01:00
if (Camera->GetName() == mName)
{
Found = true;
break;
}
}
if (!Found)
return;
}
2020-12-14 01:27:21 +01:00
int MaxCameraNumber = 0;
const QLatin1String Prefix("Camera ");
for (const lcCamera* Camera : Cameras)
{
QString CameraName = Camera->GetName();
if (CameraName.startsWith(Prefix))
{
bool Ok = false;
2020-12-14 02:05:27 +01:00
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
2020-12-14 01:27:21 +01:00
int CameraNumber = CameraName.midRef(Prefix.size()).toInt(&Ok);
2020-12-14 02:05:27 +01:00
#else
2020-12-14 02:14:33 +01:00
int CameraNumber = CameraName.mid((int)strlen(Prefix.latin1())).toInt(&Ok);
2020-12-14 02:05:27 +01:00
#endif
2012-08-22 03:13:32 +02:00
2020-12-14 01:27:21 +01:00
if (Ok && CameraNumber > MaxCameraNumber)
MaxCameraNumber = CameraNumber;
}
}
2012-08-22 03:13:32 +02:00
2020-12-14 01:27:21 +01:00
mName = Prefix + QString::number(MaxCameraNumber + 1);
2012-08-22 03:13:32 +02:00
}
2014-09-05 02:24:28 +02:00
void lcCamera::SaveLDraw(QTextStream& Stream) const
{
2014-09-08 21:42:20 +02:00
QLatin1String LineEnding("\r\n");
2014-09-08 21:42:20 +02:00
Stream << QLatin1String("0 !LEOCAD CAMERA FOV ") << m_fovy << QLatin1String(" ZNEAR ") << m_zNear << QLatin1String(" ZFAR ") << m_zFar << LineEnding;
if (mPositionKeys.GetSize() > 1)
2014-09-05 02:24:28 +02:00
SaveKeysLDraw(Stream, mPositionKeys, "CAMERA POSITION_KEY ");
else
2014-09-08 21:42:20 +02:00
Stream << QLatin1String("0 !LEOCAD CAMERA POSITION ") << mPosition[0] << ' ' << mPosition[1] << ' ' << mPosition[2] << LineEnding;
if (mTargetPositionKeys.GetSize() > 1)
2014-09-13 08:48:51 +02:00
SaveKeysLDraw(Stream, mTargetPositionKeys, "CAMERA TARGET_POSITION_KEY ");
else
2014-09-13 08:48:51 +02:00
Stream << QLatin1String("0 !LEOCAD CAMERA TARGET_POSITION ") << mTargetPosition[0] << ' ' << mTargetPosition[1] << ' ' << mTargetPosition[2] << LineEnding;
if (mUpVectorKeys.GetSize() > 1)
2014-09-13 08:48:51 +02:00
SaveKeysLDraw(Stream, mUpVectorKeys, "CAMERA UP_VECTOR_KEY ");
else
2014-09-13 08:48:51 +02:00
Stream << QLatin1String("0 !LEOCAD CAMERA UP_VECTOR ") << mUpVector[0] << ' ' << mUpVector[1] << ' ' << mUpVector[2] << LineEnding;
2014-09-08 21:42:20 +02:00
Stream << QLatin1String("0 !LEOCAD CAMERA ");
if (IsHidden())
Stream << QLatin1String("HIDDEN");
if (IsOrtho())
Stream << QLatin1String("ORTHOGRAPHIC ");
2020-12-14 01:27:21 +01:00
Stream << QLatin1String("NAME ") << mName << LineEnding;
}
2014-09-03 16:34:53 +02:00
bool lcCamera::ParseLDrawLine(QTextStream& Stream)
{
2014-09-08 21:42:20 +02:00
while (!Stream.atEnd())
{
2014-09-08 21:42:20 +02:00
QString Token;
Stream >> Token;
if (Token == QLatin1String("HIDDEN"))
SetHidden(true);
else if (Token == QLatin1String("ORTHOGRAPHIC"))
SetOrtho(true);
else if (Token == QLatin1String("FOV"))
Stream >> m_fovy;
else if (Token == QLatin1String("ZNEAR"))
Stream >> m_zNear;
else if (Token == QLatin1String("ZFAR"))
Stream >> m_zFar;
else if (Token == QLatin1String("POSITION"))
{
2014-09-08 21:42:20 +02:00
Stream >> mPosition[0] >> mPosition[1] >> mPosition[2];
ChangeKey(mPositionKeys, mPosition, 1, true);
}
2014-09-13 08:48:51 +02:00
else if (Token == QLatin1String("TARGET_POSITION"))
{
2014-09-08 21:42:20 +02:00
Stream >> mTargetPosition[0] >> mTargetPosition[1] >> mTargetPosition[2];
ChangeKey(mTargetPositionKeys, mTargetPosition, 1, true);
}
2014-09-13 08:48:51 +02:00
else if (Token == QLatin1String("UP_VECTOR"))
{
2014-09-08 21:42:20 +02:00
Stream >> mUpVector[0] >> mUpVector[1] >> mUpVector[2];
ChangeKey(mUpVectorKeys, mUpVector, 1, true);
}
2014-09-08 21:42:20 +02:00
else if (Token == QLatin1String("POSITION_KEY"))
LoadKeysLDraw(Stream, mPositionKeys);
2014-09-13 08:48:51 +02:00
else if (Token == QLatin1String("TARGET_POSITION_KEY"))
2014-09-08 21:42:20 +02:00
LoadKeysLDraw(Stream, mTargetPositionKeys);
2014-09-13 08:48:51 +02:00
else if (Token == QLatin1String("UP_VECTOR_KEY"))
2014-09-08 21:42:20 +02:00
LoadKeysLDraw(Stream, mUpVectorKeys);
else if (Token == QLatin1String("NAME"))
{
2020-12-14 01:27:21 +01:00
mName = Stream.readAll().trimmed();
2014-09-08 21:42:20 +02:00
return true;
}
}
return false;
}
2011-09-07 23:06:51 +02:00
/////////////////////////////////////////////////////////////////////////////
// Camera save/load
2014-05-01 20:42:11 +02:00
bool lcCamera::FileLoad(lcFile& file)
2011-09-07 23:06:51 +02:00
{
2017-12-02 21:22:04 +01:00
quint8 version, ch;
2012-08-23 20:47:37 +02:00
version = file.ReadU8();
if (version > LC_CAMERA_SAVE_VERSION)
return false;
if (version > 5)
2014-08-31 02:53:12 +02:00
{
if (file.ReadU8() != 1)
2012-08-23 20:47:37 +02:00
return false;
2017-12-02 21:22:04 +01:00
quint16 time;
2014-08-31 02:53:12 +02:00
float param[4];
2017-12-02 21:22:04 +01:00
quint8 type;
quint32 n;
2014-08-31 02:53:12 +02:00
file.ReadU32(&n, 1);
while (n--)
{
file.ReadU16(&time, 1);
file.ReadFloats(param, 4);
file.ReadU8(&type, 1);
if (type == 0)
ChangeKey(mPositionKeys, lcVector3(param[0], param[1], param[2]), time, true);
else if (type == 1)
ChangeKey(mTargetPositionKeys, lcVector3(param[0], param[1], param[2]), time, true);
else if (type == 2)
ChangeKey(mUpVectorKeys, lcVector3(param[0], param[1], param[2]), time, true);
}
file.ReadU32(&n, 1);
while (n--)
{
file.ReadU16(&time, 1);
file.ReadFloats(param, 4);
file.ReadU8(&type, 1);
}
}
2012-08-23 20:47:37 +02:00
if (version == 4)
{
2020-12-14 01:27:21 +01:00
char Name[81];
file.ReadBuffer(Name, 80);
Name[80] = 0;
mName = Name;
2012-08-23 20:47:37 +02:00
}
else
{
ch = file.ReadU8();
if (ch == 0xFF)
return false; // don't read CString
2020-12-14 01:27:21 +01:00
char Name[81];
file.ReadBuffer(Name, ch);
Name[ch] = 0;
mName = Name;
2012-08-23 20:47:37 +02:00
}
if (version < 3)
{
double d[3];
float f[3];
file.ReadDoubles(d, 3);
f[0] = (float)d[0];
f[1] = (float)d[1];
f[2] = (float)d[2];
2014-08-31 02:53:12 +02:00
ChangeKey(mPositionKeys, lcVector3(f[0], f[1], f[2]), 1, true);
2012-08-23 20:47:37 +02:00
file.ReadDoubles(d, 3);
f[0] = (float)d[0];
f[1] = (float)d[1];
f[2] = (float)d[2];
2014-08-31 02:53:12 +02:00
ChangeKey(mTargetPositionKeys, lcVector3(f[0], f[1], f[2]), 1, true);
2012-08-23 20:47:37 +02:00
file.ReadDoubles(d, 3);
f[0] = (float)d[0];
f[1] = (float)d[1];
f[2] = (float)d[2];
2014-08-31 02:53:12 +02:00
ChangeKey(mUpVectorKeys, lcVector3(f[0], f[1], f[2]), 1, true);
2012-08-23 20:47:37 +02:00
}
if (version == 3)
{
ch = file.ReadU8();
while (ch--)
{
2017-12-02 21:22:04 +01:00
quint8 step;
2012-08-23 20:47:37 +02:00
double eye[3], target[3], up[3];
float f[3];
file.ReadDoubles(eye, 3);
file.ReadDoubles(target, 3);
file.ReadDoubles(up, 3);
file.ReadU8(&step, 1);
if (up[0] == 0 && up[1] == 0 && up[2] == 0)
up[2] = 1;
f[0] = (float)eye[0];
f[1] = (float)eye[1];
f[2] = (float)eye[2];
2014-08-31 02:53:12 +02:00
ChangeKey(mPositionKeys, lcVector3(f[0], f[1], f[2]), step, true);
2012-08-23 20:47:37 +02:00
f[0] = (float)target[0];
f[1] = (float)target[1];
f[2] = (float)target[2];
2014-08-31 02:53:12 +02:00
ChangeKey(mTargetPositionKeys, lcVector3(f[0], f[1], f[2]), step, true);
2012-08-23 20:47:37 +02:00
f[0] = (float)up[0];
f[1] = (float)up[1];
f[2] = (float)up[2];
2014-08-31 02:53:12 +02:00
ChangeKey(mUpVectorKeys, lcVector3(f[0], f[1], f[2]), step, true);
2012-08-23 20:47:37 +02:00
file.ReadS32(); // snapshot
file.ReadS32(); // cam
}
}
if (version < 4)
{
m_fovy = (float)file.ReadDouble();
m_zFar = (float)file.ReadDouble();
m_zNear= (float)file.ReadDouble();
}
else
{
2017-12-02 21:22:04 +01:00
qint32 n;
2012-08-23 20:47:37 +02:00
if (version < 6)
{
2017-12-02 21:22:04 +01:00
quint16 time;
2012-08-23 20:47:37 +02:00
float param[4];
2017-12-02 21:22:04 +01:00
quint8 type;
2012-08-23 20:47:37 +02:00
n = file.ReadS32();
while (n--)
{
file.ReadU16(&time, 1);
file.ReadFloats(param, 3);
file.ReadU8(&type, 1);
2014-08-31 02:53:12 +02:00
if (type == 0)
ChangeKey(mPositionKeys, lcVector3(param[0], param[1], param[2]), time, true);
else if (type == 1)
ChangeKey(mTargetPositionKeys, lcVector3(param[0], param[1], param[2]), time, true);
else if (type == 2)
ChangeKey(mUpVectorKeys, lcVector3(param[0], param[1], param[2]), time, true);
2012-08-23 20:47:37 +02:00
}
n = file.ReadS32();
while (n--)
{
file.ReadU16(&time, 1);
file.ReadFloats(param, 3);
file.ReadU8(&type, 1);
}
}
file.ReadFloats(&m_fovy, 1);
file.ReadFloats(&m_zFar, 1);
file.ReadFloats(&m_zNear, 1);
if (version < 5)
{
n = file.ReadS32();
if (n != 0)
mState |= LC_CAMERA_HIDDEN;
2012-08-23 20:47:37 +02:00
}
else
{
ch = file.ReadU8();
if (ch & 1)
mState |= LC_CAMERA_HIDDEN;
2015-01-16 03:07:31 +01:00
file.ReadU8();
2012-08-23 20:47:37 +02:00
}
}
if ((version > 1) && (version < 4))
{
2017-12-02 21:22:04 +01:00
quint32 show;
qint32 user;
2012-08-23 20:47:37 +02:00
2013-01-06 20:24:25 +01:00
file.ReadU32(&show, 1);
2012-08-23 20:47:37 +02:00
// if (version > 2)
2013-01-06 20:24:25 +01:00
file.ReadS32(&user, 1);
2012-08-23 20:47:37 +02:00
if (show == 0)
mState |= LC_CAMERA_HIDDEN;
2012-08-23 20:47:37 +02:00
}
2014-08-30 21:48:36 +02:00
if (version < 7)
{
m_zFar *= 25.0f;
m_zNear *= 25.0f;
2014-08-31 02:53:12 +02:00
for (int KeyIdx = 0; KeyIdx < mPositionKeys.GetSize(); KeyIdx++)
mPositionKeys[KeyIdx].Value *= 25.0f;
for (int KeyIdx = 0; KeyIdx < mTargetPositionKeys.GetSize(); KeyIdx++)
mTargetPositionKeys[KeyIdx].Value *= 25.0f;
2014-08-30 21:48:36 +02:00
}
2012-08-23 20:47:37 +02:00
return true;
2011-09-07 23:06:51 +02:00
}
2016-02-19 18:53:54 +01:00
void lcCamera::CompareBoundingBox(lcVector3& Min, lcVector3& Max)
2014-11-10 01:06:11 +01:00
{
const lcVector3 Points[2] =
{
mPosition, mTargetPosition
};
for (int i = 0; i < 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
}
}
2011-09-07 23:06:51 +02:00
void lcCamera::MoveSelected(lcStep Step, bool AddKey, const lcVector3& Distance)
2011-09-07 23:06:51 +02:00
{
if (IsSimple())
2014-06-22 19:39:15 +02:00
AddKey = false;
if (IsSelected(LC_CAMERA_SECTION_POSITION))
2012-05-29 01:33:22 +02:00
{
2014-06-22 19:39:15 +02:00
mPosition += Distance;
2014-08-31 02:53:12 +02:00
ChangeKey(mPositionKeys, mPosition, Step, AddKey);
2012-08-20 06:05:56 +02:00
}
2011-09-07 23:06:51 +02:00
if (IsSelected(LC_CAMERA_SECTION_TARGET))
2012-08-20 06:05:56 +02:00
{
2014-06-22 19:39:15 +02:00
mTargetPosition += Distance;
2014-08-31 02:53:12 +02:00
ChangeKey(mTargetPositionKeys, mTargetPosition, Step, AddKey);
2012-08-20 06:05:56 +02:00
}
2014-08-31 02:53:12 +02:00
else if (IsSelected(LC_CAMERA_SECTION_UPVECTOR))
2012-08-23 20:47:37 +02:00
{
2014-06-22 19:39:15 +02:00
mUpVector += Distance;
mUpVector.Normalize();
2014-08-31 02:53:12 +02:00
ChangeKey(mUpVectorKeys, mUpVector, Step, AddKey);
2012-08-23 20:47:37 +02:00
}
2011-09-07 23:06:51 +02:00
lcVector3 FrontVector(mTargetPosition - mPosition);
lcVector3 SideVector = lcCross(FrontVector, mUpVector);
2012-08-23 20:47:37 +02:00
if (fabsf(lcDot(mUpVector, SideVector)) > 0.99f)
SideVector = lcVector3(1, 0, 0);
2012-08-23 20:47:37 +02:00
mUpVector = lcCross(SideVector, FrontVector);
mUpVector.Normalize();
2011-09-07 23:06:51 +02:00
}
void lcCamera::MoveRelative(const lcVector3& Distance, lcStep Step, bool AddKey)
{
if (IsSimple())
AddKey = false;
lcVector3 Relative = lcMul30(Distance, lcMatrix44Transpose(mWorldView)) * 5.0f;
mPosition += Relative;
ChangeKey(mPositionKeys, mPosition, Step, AddKey);
mTargetPosition += Relative;
ChangeKey(mTargetPositionKeys, mTargetPosition, Step, AddKey);
UpdatePosition(Step);
}
2014-07-06 08:04:09 +02:00
void lcCamera::UpdatePosition(lcStep Step)
2011-09-07 23:06:51 +02:00
{
if (!IsSimple())
{
mPosition = CalculateKey(mPositionKeys, Step);
mTargetPosition = CalculateKey(mTargetPositionKeys, Step);
mUpVector = CalculateKey(mUpVectorKeys, Step);
}
2012-05-29 01:33:22 +02:00
lcVector3 FrontVector(mPosition - mTargetPosition);
lcVector3 SideVector = lcCross(FrontVector, mUpVector);
mUpVector = lcNormalize(lcCross(SideVector, FrontVector));
mWorldView = lcMatrix44LookAt(mPosition, mTargetPosition, mUpVector);
2011-09-07 23:06:51 +02:00
}
void lcCamera::CopyPosition(const lcCamera* Camera)
{
m_fovy = Camera->m_fovy;
m_zNear = Camera->m_zNear;
m_zFar = Camera->m_zFar;
mWorldView = Camera->mWorldView;
mPosition = Camera->mPosition;
mTargetPosition = Camera->mTargetPosition;
mUpVector = Camera->mUpVector;
mState |= (Camera->mState & LC_CAMERA_ORTHO);
}
void lcCamera::CopySettings(const lcCamera* camera)
2012-08-20 06:05:56 +02:00
{
m_fovy = camera->m_fovy;
m_zNear = camera->m_zNear;
m_zFar = camera->m_zFar;
mState |= (camera->mState & LC_CAMERA_ORTHO);
2012-08-20 06:05:56 +02:00
}
void lcCamera::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);
2015-04-19 03:10:01 +02:00
lcMatrix44 ViewWorldMatrix = lcMatrix44AffineInverse(mWorldView);
ViewWorldMatrix.SetTranslation(lcVector3(0, 0, 0));
2015-05-17 01:04:35 +02:00
lcMatrix44 CameraViewMatrix = lcMul(ViewWorldMatrix, lcMatrix44Translation(mPosition));
Context->SetWorldMatrix(CameraViewMatrix);
2015-04-19 03:10:01 +02:00
float Verts[(12 + 8 + 8 + 3 + 4) * 3];
float* CurVert = Verts;
float Length = lcLength(mPosition - mTargetPosition);
*CurVert++ = LC_CAMERA_POSITION_EDGE; *CurVert++ = LC_CAMERA_POSITION_EDGE; *CurVert++ = LC_CAMERA_POSITION_EDGE;
*CurVert++ = -LC_CAMERA_POSITION_EDGE; *CurVert++ = LC_CAMERA_POSITION_EDGE; *CurVert++ = LC_CAMERA_POSITION_EDGE;
*CurVert++ = -LC_CAMERA_POSITION_EDGE; *CurVert++ = -LC_CAMERA_POSITION_EDGE; *CurVert++ = LC_CAMERA_POSITION_EDGE;
*CurVert++ = LC_CAMERA_POSITION_EDGE; *CurVert++ = -LC_CAMERA_POSITION_EDGE; *CurVert++ = LC_CAMERA_POSITION_EDGE;
*CurVert++ = LC_CAMERA_POSITION_EDGE; *CurVert++ = LC_CAMERA_POSITION_EDGE; *CurVert++ = -LC_CAMERA_POSITION_EDGE;
*CurVert++ = -LC_CAMERA_POSITION_EDGE; *CurVert++ = LC_CAMERA_POSITION_EDGE; *CurVert++ = -LC_CAMERA_POSITION_EDGE;
*CurVert++ = -LC_CAMERA_POSITION_EDGE; *CurVert++ = -LC_CAMERA_POSITION_EDGE; *CurVert++ = -LC_CAMERA_POSITION_EDGE;
*CurVert++ = LC_CAMERA_POSITION_EDGE; *CurVert++ = -LC_CAMERA_POSITION_EDGE; *CurVert++ = -LC_CAMERA_POSITION_EDGE;
*CurVert++ = LC_CAMERA_POSITION_EDGE; *CurVert++ = LC_CAMERA_POSITION_EDGE; *CurVert++ = -LC_CAMERA_POSITION_EDGE * 2;
*CurVert++ = -LC_CAMERA_POSITION_EDGE; *CurVert++ = LC_CAMERA_POSITION_EDGE; *CurVert++ = -LC_CAMERA_POSITION_EDGE * 2;
*CurVert++ = -LC_CAMERA_POSITION_EDGE; *CurVert++ = -LC_CAMERA_POSITION_EDGE; *CurVert++ = -LC_CAMERA_POSITION_EDGE * 2;
*CurVert++ = LC_CAMERA_POSITION_EDGE; *CurVert++ = -LC_CAMERA_POSITION_EDGE; *CurVert++ = -LC_CAMERA_POSITION_EDGE * 2;
*CurVert++ = LC_CAMERA_TARGET_EDGE; *CurVert++ = LC_CAMERA_TARGET_EDGE; *CurVert++ = LC_CAMERA_TARGET_EDGE - Length;
*CurVert++ = -LC_CAMERA_TARGET_EDGE; *CurVert++ = LC_CAMERA_TARGET_EDGE; *CurVert++ = LC_CAMERA_TARGET_EDGE - Length;
*CurVert++ = -LC_CAMERA_TARGET_EDGE; *CurVert++ = -LC_CAMERA_TARGET_EDGE; *CurVert++ = LC_CAMERA_TARGET_EDGE - Length;
*CurVert++ = LC_CAMERA_TARGET_EDGE; *CurVert++ = -LC_CAMERA_TARGET_EDGE; *CurVert++ = LC_CAMERA_TARGET_EDGE - Length;
*CurVert++ = LC_CAMERA_TARGET_EDGE; *CurVert++ = LC_CAMERA_TARGET_EDGE; *CurVert++ = -LC_CAMERA_TARGET_EDGE - Length;
*CurVert++ = -LC_CAMERA_TARGET_EDGE; *CurVert++ = LC_CAMERA_TARGET_EDGE; *CurVert++ = -LC_CAMERA_TARGET_EDGE - Length;
*CurVert++ = -LC_CAMERA_TARGET_EDGE; *CurVert++ = -LC_CAMERA_TARGET_EDGE; *CurVert++ = -LC_CAMERA_TARGET_EDGE - Length;
*CurVert++ = LC_CAMERA_TARGET_EDGE; *CurVert++ = -LC_CAMERA_TARGET_EDGE; *CurVert++ = -LC_CAMERA_TARGET_EDGE - Length;
*CurVert++ = LC_CAMERA_TARGET_EDGE; *CurVert++ = LC_CAMERA_TARGET_EDGE + 25.0f; *CurVert++ = LC_CAMERA_TARGET_EDGE;
*CurVert++ = -LC_CAMERA_TARGET_EDGE; *CurVert++ = LC_CAMERA_TARGET_EDGE + 25.0f; *CurVert++ = LC_CAMERA_TARGET_EDGE;
*CurVert++ = -LC_CAMERA_TARGET_EDGE; *CurVert++ = -LC_CAMERA_TARGET_EDGE + 25.0f; *CurVert++ = LC_CAMERA_TARGET_EDGE;
*CurVert++ = LC_CAMERA_TARGET_EDGE; *CurVert++ = -LC_CAMERA_TARGET_EDGE + 25.0f; *CurVert++ = LC_CAMERA_TARGET_EDGE;
*CurVert++ = LC_CAMERA_TARGET_EDGE; *CurVert++ = LC_CAMERA_TARGET_EDGE + 25.0f; *CurVert++ = -LC_CAMERA_TARGET_EDGE;
*CurVert++ = -LC_CAMERA_TARGET_EDGE; *CurVert++ = LC_CAMERA_TARGET_EDGE + 25.0f; *CurVert++ = -LC_CAMERA_TARGET_EDGE;
*CurVert++ = -LC_CAMERA_TARGET_EDGE; *CurVert++ = -LC_CAMERA_TARGET_EDGE + 25.0f; *CurVert++ = -LC_CAMERA_TARGET_EDGE;
*CurVert++ = LC_CAMERA_TARGET_EDGE; *CurVert++ = -LC_CAMERA_TARGET_EDGE + 25.0f; *CurVert++ = -LC_CAMERA_TARGET_EDGE;
*CurVert++ = 0.0f; *CurVert++ = 0.0f; *CurVert++ = 0.0f;
*CurVert++ = 0.0f; *CurVert++ = 0.0f; *CurVert++ = -Length;
*CurVert++ = 0.0f; *CurVert++ = 25.0f; *CurVert++ = 0.0f;
const GLushort Indices[40 + 24 + 24 + 4 + 16] =
2012-05-29 01:33:22 +02:00
{
2015-04-19 03:10:01 +02:00
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,
8, 28, 9, 28, 10, 28, 11, 28,
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, 21, 22, 22, 23, 23, 20,
24, 25, 25, 26, 26, 27, 27, 24,
20, 24, 21, 25, 22, 26, 23, 27,
28, 29, 28, 30,
31, 32, 32, 33, 33, 34, 34, 31,
28, 31, 28, 32, 28, 33, 28, 34
2012-05-29 01:33:22 +02:00
};
2015-04-19 03:10:01 +02:00
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);
2015-04-19 03:10:01 +02:00
float LineWidth = lcGetPreferences().mLineWidth;
if (!IsSelected())
2012-08-23 20:47:37 +02:00
{
2014-04-14 05:20:16 +02:00
Context->SetLineWidth(LineWidth);
2015-05-04 02:51:41 +02:00
Context->SetInterfaceColor(LC_COLOR_CAMERA);
2012-08-23 20:47:37 +02:00
2015-05-17 01:04:35 +02:00
Context->DrawIndexedPrimitives(GL_LINES, 40 + 24 + 24 + 4, GL_UNSIGNED_SHORT, 0);
2012-08-23 20:47:37 +02:00
}
else
{
2015-04-19 03:10:01 +02:00
if (IsSelected(LC_CAMERA_SECTION_POSITION))
{
Context->SetLineWidth(2.0f * LineWidth);
if (IsFocused(LC_CAMERA_SECTION_POSITION))
2015-05-04 02:51:41 +02:00
Context->SetInterfaceColor(LC_COLOR_FOCUSED);
2015-04-19 03:10:01 +02:00
else
2015-05-04 02:51:41 +02:00
Context->SetInterfaceColor(LC_COLOR_SELECTED);
2015-04-19 03:10:01 +02:00
}
else
{
Context->SetLineWidth(LineWidth);
2015-05-04 02:51:41 +02:00
Context->SetInterfaceColor(LC_COLOR_CAMERA);
2015-04-19 03:10:01 +02:00
}
2014-02-16 08:23:55 +01:00
2015-05-17 01:04:35 +02:00
Context->DrawIndexedPrimitives(GL_LINES, 40, GL_UNSIGNED_SHORT, 0);
2012-08-23 20:47:37 +02:00
2015-04-19 03:10:01 +02:00
if (IsSelected(LC_CAMERA_SECTION_TARGET))
{
Context->SetLineWidth(2.0f * LineWidth);
if (IsFocused(LC_CAMERA_SECTION_TARGET))
2015-05-04 02:51:41 +02:00
Context->SetInterfaceColor(LC_COLOR_FOCUSED);
2015-04-19 03:10:01 +02:00
else
2015-05-04 02:51:41 +02:00
Context->SetInterfaceColor(LC_COLOR_SELECTED);
2015-04-19 03:10:01 +02:00
}
else
2015-04-19 03:10:01 +02:00
{
Context->SetLineWidth(LineWidth);
2015-05-04 02:51:41 +02:00
Context->SetInterfaceColor(LC_COLOR_CAMERA);
2015-04-19 03:10:01 +02:00
}
2012-08-23 20:47:37 +02:00
2015-05-17 01:04:35 +02:00
Context->DrawIndexedPrimitives(GL_LINES, 24, GL_UNSIGNED_SHORT, 40 * 2);
2015-04-19 03:10:01 +02:00
if (IsSelected(LC_CAMERA_SECTION_UPVECTOR))
{
Context->SetLineWidth(2.0f * LineWidth);
if (IsFocused(LC_CAMERA_SECTION_UPVECTOR))
2015-05-04 02:51:41 +02:00
Context->SetInterfaceColor(LC_COLOR_FOCUSED);
2015-04-19 03:10:01 +02:00
else
2015-05-04 02:51:41 +02:00
Context->SetInterfaceColor(LC_COLOR_SELECTED);
2015-04-19 03:10:01 +02:00
}
else
{
Context->SetLineWidth(LineWidth);
2015-05-04 02:51:41 +02:00
Context->SetInterfaceColor(LC_COLOR_CAMERA);
2015-04-19 03:10:01 +02:00
}
2015-05-17 01:04:35 +02:00
Context->DrawIndexedPrimitives(GL_LINES, 24, GL_UNSIGNED_SHORT, (40 + 24) * 2);
2015-05-04 02:51:41 +02:00
Context->SetInterfaceColor(LC_COLOR_CAMERA);
2015-04-19 03:10:01 +02:00
Context->SetLineWidth(LineWidth);
2012-08-23 20:47:37 +02:00
2015-04-19 03:10:01 +02:00
float SizeY = tanf(LC_DTOR * m_fovy / 2) * Length;
float SizeX = SizeY * 1.333f;
*CurVert++ = SizeX; *CurVert++ = SizeY; *CurVert++ = -Length;
*CurVert++ = -SizeX; *CurVert++ = SizeY; *CurVert++ = -Length;
*CurVert++ = -SizeX; *CurVert++ = -SizeY; *CurVert++ = -Length;
*CurVert++ = SizeX; *CurVert++ = -SizeY; *CurVert++ = -Length;
2012-05-29 01:33:22 +02:00
2015-05-17 01:04:35 +02:00
Context->DrawIndexedPrimitives(GL_LINES, 4 + 16, GL_UNSIGNED_SHORT, (40 + 24 + 24) * 2);
2011-09-07 23:06:51 +02:00
}
}
void lcCamera::RemoveKeyFrames()
{
mPositionKeys.RemoveAll();
ChangeKey(mPositionKeys, mPosition, 1, true);
mTargetPositionKeys.RemoveAll();
ChangeKey(mTargetPositionKeys, mTargetPosition, 1, true);
mUpVectorKeys.RemoveAll();
ChangeKey(mUpVectorKeys, mUpVector, 1, true);
}
2014-05-01 20:42:11 +02:00
void lcCamera::RayTest(lcObjectRayTest& ObjectRayTest) const
2011-09-07 23:06:51 +02:00
{
2014-11-24 01:43:13 +01:00
lcVector3 Min = lcVector3(-LC_CAMERA_POSITION_EDGE, -LC_CAMERA_POSITION_EDGE, -LC_CAMERA_POSITION_EDGE);
lcVector3 Max = lcVector3(LC_CAMERA_POSITION_EDGE, LC_CAMERA_POSITION_EDGE, LC_CAMERA_POSITION_EDGE);
2011-09-07 23:06:51 +02:00
lcVector3 Start = lcMul31(ObjectRayTest.Start, mWorldView);
lcVector3 End = lcMul31(ObjectRayTest.End, mWorldView);
float Distance;
if (lcBoundingBoxRayIntersectDistance(Min, Max, Start, End, &Distance, nullptr) && (Distance < ObjectRayTest.Distance))
{
2014-08-07 17:22:33 +02:00
ObjectRayTest.ObjectSection.Object = const_cast<lcCamera*>(this);
ObjectRayTest.ObjectSection.Section = LC_CAMERA_SECTION_POSITION;
ObjectRayTest.Distance = Distance;
}
2014-11-24 01:43:13 +01:00
Min = lcVector3(-LC_CAMERA_TARGET_EDGE, -LC_CAMERA_TARGET_EDGE, -LC_CAMERA_TARGET_EDGE);
Max = lcVector3(LC_CAMERA_TARGET_EDGE, LC_CAMERA_TARGET_EDGE, LC_CAMERA_TARGET_EDGE);
lcMatrix44 WorldView = mWorldView;
WorldView.SetTranslation(lcMul30(-mTargetPosition, WorldView));
Start = lcMul31(ObjectRayTest.Start, WorldView);
End = lcMul31(ObjectRayTest.End, WorldView);
2011-09-07 23:06:51 +02:00
if (lcBoundingBoxRayIntersectDistance(Min, Max, Start, End, &Distance, nullptr) && (Distance < ObjectRayTest.Distance))
2012-08-17 01:50:40 +02:00
{
2014-08-07 17:22:33 +02:00
ObjectRayTest.ObjectSection.Object = const_cast<lcCamera*>(this);
ObjectRayTest.ObjectSection.Section = LC_CAMERA_SECTION_TARGET;
ObjectRayTest.Distance = Distance;
2012-08-17 01:50:40 +02:00
}
2011-09-07 23:06:51 +02:00
lcMatrix44 ViewWorld = lcMatrix44AffineInverse(mWorldView);
2015-03-23 01:37:12 +01:00
lcVector3 UpVectorPosition = lcMul31(lcVector3(0, 25, 0), ViewWorld);
WorldView = mWorldView;
WorldView.SetTranslation(lcMul30(-UpVectorPosition, WorldView));
Start = lcMul31(ObjectRayTest.Start, WorldView);
End = lcMul31(ObjectRayTest.End, WorldView);
if (lcBoundingBoxRayIntersectDistance(Min, Max, Start, End, &Distance, nullptr) && (Distance < ObjectRayTest.Distance))
{
2014-08-07 17:22:33 +02:00
ObjectRayTest.ObjectSection.Object = const_cast<lcCamera*>(this);
ObjectRayTest.ObjectSection.Section = LC_CAMERA_SECTION_UPVECTOR;
ObjectRayTest.Distance = Distance;
}
2011-09-07 23:06:51 +02:00
}
2014-05-01 20:42:11 +02:00
void lcCamera::BoxTest(lcObjectBoxTest& ObjectBoxTest) const
2012-08-23 20:47:37 +02:00
{
2014-11-24 01:43:13 +01:00
lcVector3 Min(-LC_CAMERA_POSITION_EDGE, -LC_CAMERA_POSITION_EDGE, -LC_CAMERA_POSITION_EDGE);
lcVector3 Max(LC_CAMERA_POSITION_EDGE, LC_CAMERA_POSITION_EDGE, LC_CAMERA_POSITION_EDGE);
2012-08-23 20:47:37 +02:00
lcVector4 LocalPlanes[6];
for (int PlaneIdx = 0; PlaneIdx < 6; PlaneIdx++)
{
lcVector3 Normal = lcMul30(ObjectBoxTest.Planes[PlaneIdx], mWorldView);
LocalPlanes[PlaneIdx] = lcVector4(Normal, ObjectBoxTest.Planes[PlaneIdx][3] - lcDot3(mWorldView[3], Normal));
}
if (lcBoundingBoxIntersectsVolume(Min, Max, LocalPlanes))
{
2014-11-29 03:55:58 +01:00
ObjectBoxTest.Objects.Add(const_cast<lcCamera*>(this));
return;
}
2014-11-24 01:43:13 +01:00
Min = lcVector3(-LC_CAMERA_TARGET_EDGE, -LC_CAMERA_TARGET_EDGE, -LC_CAMERA_TARGET_EDGE);
Max = lcVector3(LC_CAMERA_TARGET_EDGE, LC_CAMERA_TARGET_EDGE, LC_CAMERA_TARGET_EDGE);
lcMatrix44 WorldView = mWorldView;
WorldView.SetTranslation(lcMul30(-mTargetPosition, WorldView));
for (int PlaneIdx = 0; PlaneIdx < 6; PlaneIdx++)
{
lcVector3 Normal = lcMul30(ObjectBoxTest.Planes[PlaneIdx], WorldView);
LocalPlanes[PlaneIdx] = lcVector4(Normal, ObjectBoxTest.Planes[PlaneIdx][3] - lcDot3(WorldView[3], Normal));
}
if (lcBoundingBoxIntersectsVolume(Min, Max, LocalPlanes))
{
2014-11-29 03:55:58 +01:00
ObjectBoxTest.Objects.Add(const_cast<lcCamera*>(this));
return;
}
lcMatrix44 ViewWorld = lcMatrix44AffineInverse(mWorldView);
2015-03-23 01:37:12 +01:00
lcVector3 UpVectorPosition = lcMul31(lcVector3(0, 25, 0), ViewWorld);
WorldView = mWorldView;
WorldView.SetTranslation(lcMul30(-UpVectorPosition, WorldView));
for (int PlaneIdx = 0; PlaneIdx < 6; PlaneIdx++)
{
lcVector3 Normal = lcMul30(ObjectBoxTest.Planes[PlaneIdx], WorldView);
LocalPlanes[PlaneIdx] = lcVector4(Normal, ObjectBoxTest.Planes[PlaneIdx][3] - lcDot3(WorldView[3], Normal));
2012-08-23 20:47:37 +02:00
}
if (lcBoundingBoxIntersectsVolume(Min, Max, LocalPlanes))
{
2014-11-29 03:55:58 +01:00
ObjectBoxTest.Objects.Add(const_cast<lcCamera*>(this));
return;
}
2012-08-23 20:47:37 +02:00
}
2014-08-31 02:53:12 +02:00
void lcCamera::InsertTime(lcStep Start, lcStep Time)
{
lcObject::InsertTime(mPositionKeys, Start, Time);
lcObject::InsertTime(mTargetPositionKeys, Start, Time);
lcObject::InsertTime(mUpVectorKeys, Start, Time);
}
void lcCamera::RemoveTime(lcStep Start, lcStep Time)
{
lcObject::RemoveTime(mPositionKeys, Start, Time);
lcObject::RemoveTime(mTargetPositionKeys, Start, Time);
lcObject::RemoveTime(mUpVectorKeys, Start, Time);
}
void lcCamera::ZoomExtents(float AspectRatio, const lcVector3& Center, const std::vector<lcVector3>& Points, lcStep Step, bool AddKey)
2012-08-20 06:05:56 +02:00
{
if (IsOrtho())
{
float MinX = FLT_MAX, MaxX = -FLT_MAX, MinY = FLT_MAX, MaxY = -FLT_MAX;
for (lcVector3 Point : Points)
{
Point = lcMul30(Point, mWorldView);
MinX = lcMin(MinX, Point.x);
MinY = lcMin(MinY, Point.y);
MaxX = lcMax(MaxX, Point.x);
MaxY = lcMax(MaxY, Point.y);
}
2021-01-03 21:19:57 +01:00
lcVector3 ViewCenter = lcMul30(Center, mWorldView);
float Width = qMax(fabsf(MaxX - Center.x), fabsf(Center.x - MinX)) * 2;
float Height = qMax(fabsf(MaxY - Center.y), fabsf(Center.y - MinY)) * 2;
2012-08-20 06:05:56 +02:00
if (Width > Height * AspectRatio)
Height = Width / AspectRatio;
2012-08-20 06:05:56 +02:00
float f = Height / (m_fovy * (LC_PI / 180.0f));
lcVector3 FrontVector(mTargetPosition - mPosition);
mPosition = Center - lcNormalize(FrontVector) * f;
mTargetPosition = Center;
}
else
{
lcVector3 Position(mPosition + Center - mTargetPosition);
lcMatrix44 ProjectionMatrix = lcMatrix44Perspective(m_fovy, AspectRatio, m_zNear, m_zFar);
std::tie(mPosition, std::ignore) = lcZoomExtents(Position, mWorldView, ProjectionMatrix, Points.data(), Points.size());
mTargetPosition = Center;
}
2012-08-20 06:05:56 +02:00
if (IsSimple())
2014-07-06 08:04:09 +02:00
AddKey = false;
2014-08-31 02:53:12 +02:00
ChangeKey(mPositionKeys, mPosition, Step, AddKey);
ChangeKey(mTargetPositionKeys, mTargetPosition, Step, AddKey);
2012-08-20 06:05:56 +02:00
2014-07-06 08:04:09 +02:00
UpdatePosition(Step);
2012-08-20 06:05:56 +02:00
}
void lcCamera::ZoomRegion(float AspectRatio, const lcVector3& Position, const lcVector3& TargetPosition, const lcVector3* Corners, lcStep Step, bool AddKey)
{
if (IsOrtho())
{
float MinX = FLT_MAX, MaxX = -FLT_MAX, MinY = FLT_MAX, MaxY = -FLT_MAX;
for (int PointIdx = 0; PointIdx < 2; PointIdx++)
{
lcVector3 Point = lcMul30(Corners[PointIdx], mWorldView);
MinX = lcMin(MinX, Point.x);
MinY = lcMin(MinY, Point.y);
MaxX = lcMax(MaxX, Point.x);
MaxY = lcMax(MaxY, Point.y);
}
float Width = MaxX - MinX;
float Height = MaxY - MinY;
if (Width > Height * AspectRatio)
Height = Width / AspectRatio;
float f = Height / (m_fovy * (LC_PI / 180.0f));
lcVector3 FrontVector(mTargetPosition - mPosition);
mPosition = TargetPosition - lcNormalize(FrontVector) * f;
mTargetPosition = TargetPosition;
}
else
{
lcMatrix44 WorldView = lcMatrix44LookAt(Position, TargetPosition, mUpVector);
lcMatrix44 ProjectionMatrix = lcMatrix44Perspective(m_fovy, AspectRatio, m_zNear, m_zFar);
std::tie(mPosition, std::ignore) = lcZoomExtents(Position, WorldView, ProjectionMatrix, Corners, 2);
mTargetPosition = TargetPosition;
}
if (IsSimple())
2014-07-06 08:04:09 +02:00
AddKey = false;
2014-08-31 02:53:12 +02:00
ChangeKey(mPositionKeys, mPosition, Step, AddKey);
ChangeKey(mTargetPositionKeys, mTargetPosition, Step, AddKey);
2014-07-06 08:04:09 +02:00
UpdatePosition(Step);
}
2014-07-06 08:04:09 +02:00
void lcCamera::Zoom(float Distance, lcStep Step, bool AddKey)
2011-09-07 23:06:51 +02:00
{
2012-05-29 01:33:22 +02:00
lcVector3 FrontVector(mPosition - mTargetPosition);
FrontVector.Normalize();
FrontVector *= -5.0f * Distance;
2012-05-29 01:33:22 +02:00
2013-12-17 03:43:16 +01:00
// Don't zoom ortho in if it would cross the ortho focal plane.
2014-05-03 03:22:24 +02:00
if (IsOrtho())
2013-12-17 03:43:16 +01:00
{
if ((Distance > 0) && (lcDot(mPosition + FrontVector - mTargetPosition, mPosition - mTargetPosition) <= 0))
2013-12-17 03:43:16 +01:00
return;
mPosition += FrontVector;
}
else
{
mPosition += FrontVector;
mTargetPosition += FrontVector;
}
2012-05-29 01:33:22 +02:00
if (IsSimple())
2014-07-06 08:04:09 +02:00
AddKey = false;
2014-08-31 02:53:12 +02:00
ChangeKey(mPositionKeys, mPosition, Step, AddKey);
ChangeKey(mTargetPositionKeys, mTargetPosition, Step, AddKey);
2012-08-20 06:05:56 +02:00
2014-07-06 08:04:09 +02:00
UpdatePosition(Step);
2011-09-07 23:06:51 +02:00
}
void lcCamera::Pan(const lcVector3& Distance, lcStep Step, bool AddKey)
2011-09-07 23:06:51 +02:00
{
mPosition += Distance;
mTargetPosition += Distance;
2012-05-29 01:33:22 +02:00
if (IsSimple())
2014-07-06 08:04:09 +02:00
AddKey = false;
2014-08-31 02:53:12 +02:00
ChangeKey(mPositionKeys, mPosition, Step, AddKey);
ChangeKey(mTargetPositionKeys, mTargetPosition, Step, AddKey);
2012-08-20 06:05:56 +02:00
2014-07-06 08:04:09 +02:00
UpdatePosition(Step);
2011-09-07 23:06:51 +02:00
}
2014-07-06 08:04:09 +02:00
void lcCamera::Orbit(float DistanceX, float DistanceY, const lcVector3& CenterPosition, lcStep Step, bool AddKey)
2011-09-07 23:06:51 +02:00
{
2012-05-29 01:33:22 +02:00
lcVector3 FrontVector(mPosition - mTargetPosition);
2013-01-27 02:22:37 +01:00
lcVector3 Z(lcNormalize(lcVector3(FrontVector[0], FrontVector[1], 0)));
2016-08-11 00:29:59 +02:00
if (qIsNaN(Z[0]) || qIsNaN(Z[1]))
2013-01-27 02:22:37 +01:00
Z = lcNormalize(lcVector3(mUpVector[0], mUpVector[1], 0));
2012-05-29 01:33:22 +02:00
2013-01-27 02:22:37 +01:00
if (mUpVector[2] < 0)
{
Z[0] = -Z[0];
Z[1] = -Z[1];
}
2013-08-09 06:57:18 +02:00
2013-01-27 02:22:37 +01:00
lcMatrix44 YRot(lcVector4(Z[0], Z[1], 0.0f, 0.0f), lcVector4(-Z[1], Z[0], 0.0f, 0.0f), lcVector4(0.0f, 0.0f, 1.0f, 0.0f), lcVector4(0.0f, 0.0f, 0.0f, 1.0f));
lcMatrix44 transform = lcMul(lcMul(lcMul(lcMatrix44AffineInverse(YRot), lcMatrix44RotationY(DistanceY)), YRot), lcMatrix44RotationZ(-DistanceX));
2013-01-27 02:22:37 +01:00
mPosition = lcMul31(mPosition - CenterPosition, transform) + CenterPosition;
mTargetPosition = lcMul31(mTargetPosition - CenterPosition, transform) + CenterPosition;
2013-12-17 03:43:16 +01:00
2013-01-27 02:22:37 +01:00
mUpVector = lcMul31(mUpVector, transform);
2012-05-29 01:33:22 +02:00
if (IsSimple())
2014-07-06 08:04:09 +02:00
AddKey = false;
2014-08-31 02:53:12 +02:00
ChangeKey(mPositionKeys, mPosition, Step, AddKey);
ChangeKey(mTargetPositionKeys, mTargetPosition, Step, AddKey);
ChangeKey(mUpVectorKeys, mUpVector, Step, AddKey);
2012-08-20 06:05:56 +02:00
2014-07-06 08:04:09 +02:00
UpdatePosition(Step);
2011-09-07 23:06:51 +02:00
}
2014-07-06 08:04:09 +02:00
void lcCamera::Roll(float Distance, lcStep Step, bool AddKey)
2011-09-07 23:06:51 +02:00
{
2012-05-29 01:33:22 +02:00
lcVector3 FrontVector(mPosition - mTargetPosition);
lcMatrix44 Rotation = lcMatrix44FromAxisAngle(FrontVector, Distance);
2011-09-07 23:06:51 +02:00
2012-05-29 01:33:22 +02:00
mUpVector = lcMul30(mUpVector, Rotation);
2012-08-20 06:05:56 +02:00
if (IsSimple())
2014-07-06 08:04:09 +02:00
AddKey = false;
2014-08-31 02:53:12 +02:00
ChangeKey(mUpVectorKeys, mUpVector, Step, AddKey);
2012-08-20 06:05:56 +02:00
2014-07-06 08:04:09 +02:00
UpdatePosition(Step);
2012-08-20 06:05:56 +02:00
}
void lcCamera::Center(const lcVector3& NewCenter, lcStep Step, bool AddKey)
2013-12-17 23:23:41 +01:00
{
const lcMatrix44 Inverse = lcMatrix44AffineInverse(mWorldView);
const lcVector3 Direction = -lcVector3(Inverse[2]);
2020-12-14 23:18:06 +01:00
// float Yaw, Pitch, Roll;
float Roll;
if (fabsf(Direction.z) < 0.9999f)
{
2020-12-14 23:18:06 +01:00
// Yaw = atan2f(Direction.y, Direction.x);
// Pitch = asinf(Direction.z);
Roll = atan2f(Inverse[0][2], Inverse[1][2]);
}
else
{
2020-12-14 23:18:06 +01:00
// Yaw = 0.0f;
// Pitch = asinf(Direction.z);
Roll = atan2f(Inverse[0][1], Inverse[1][1]);
}
mTargetPosition = NewCenter;
lcVector3 FrontVector(mPosition - mTargetPosition);
lcMatrix44 Rotation = lcMatrix44FromAxisAngle(FrontVector, Roll);
lcVector3 UpVector(0, 0, 1), SideVector;
FrontVector.Normalize();
if (fabsf(lcDot(UpVector, FrontVector)) > 0.99f)
SideVector = lcVector3(-1, 0, 0);
else
SideVector = lcCross(FrontVector, UpVector);
UpVector = lcCross(SideVector, FrontVector);
UpVector.Normalize();
mUpVector = lcMul30(UpVector, Rotation);
2013-12-17 23:23:41 +01:00
if (IsSimple())
2014-07-06 08:04:09 +02:00
AddKey = false;
2014-08-31 02:53:12 +02:00
ChangeKey(mTargetPositionKeys, mTargetPosition, Step, AddKey);
ChangeKey(mUpVectorKeys, mUpVector, Step, AddKey);
2013-12-17 23:23:41 +01:00
2014-07-06 08:04:09 +02:00
UpdatePosition(Step);
2013-12-17 23:23:41 +01:00
}
2014-11-08 02:05:17 +01:00
void lcCamera::SetViewpoint(lcViewpoint Viewpoint)
2012-08-20 06:05:56 +02:00
{
lcVector3 Positions[] =
{
2020-12-13 01:58:40 +01:00
lcVector3( 0.0f, -1250.0f, 0.0f), // lcViewpoint::Front
lcVector3( 0.0f, 1250.0f, 0.0f), // lcViewpoint::Back
lcVector3( 0.0f, 0.0f, 1250.0f), // lcViewpoint::Top
lcVector3( 0.0f, 0.0f, -1250.0f), // lcViewpoint::Bottom
lcVector3( 1250.0f, 0.0f, 0.0f), // lcViewpoint::Left
lcVector3(-1250.0f, 0.0f, 0.0f), // lcViewpoint::Right
lcVector3( 375.0f, -375.0f, 187.5f) // lcViewpoint::Home
2012-08-20 06:05:56 +02:00
};
2013-01-27 02:36:11 +01:00
lcVector3 Ups[] =
{
2017-12-10 01:35:00 +01:00
lcVector3(0.0f, 0.0f, 1.0f),
lcVector3(0.0f, 0.0f, 1.0f),
lcVector3(0.0f, 1.0f, 0.0f),
lcVector3(0.0f,-1.0f, 0.0f),
lcVector3(0.0f, 0.0f, 1.0f),
lcVector3(0.0f, 0.0f, 1.0f),
lcVector3(0.2357f, -0.2357f, 0.94281f)
2013-01-27 02:36:11 +01:00
};
2012-08-20 06:05:56 +02:00
2020-12-13 01:58:40 +01:00
mPosition = Positions[static_cast<int>(Viewpoint)];
2012-08-20 06:05:56 +02:00
mTargetPosition = lcVector3(0, 0, 0);
2020-12-13 01:58:40 +01:00
mUpVector = Ups[static_cast<int>(Viewpoint)];
2012-08-20 06:05:56 +02:00
2014-11-08 02:05:17 +01:00
ChangeKey(mPositionKeys, mPosition, 1, false);
ChangeKey(mTargetPositionKeys, mTargetPosition, 1, false);
ChangeKey(mUpVectorKeys, mUpVector, 1, false);
2012-08-20 06:05:56 +02:00
2014-11-08 02:05:17 +01:00
UpdatePosition(1);
2011-09-07 23:06:51 +02:00
}
2018-08-20 05:27:58 +02:00
void lcCamera::SetViewpoint(const lcVector3& Position)
{
mPosition = Position;
mTargetPosition = lcVector3(0, 0, 0);
lcVector3 UpVector(0, 0, 1), FrontVector(Position), SideVector;
FrontVector.Normalize();
if (fabsf(lcDot(UpVector, FrontVector)) > 0.99f)
SideVector = lcVector3(-1, 0, 0);
2018-08-20 05:27:58 +02:00
else
SideVector = lcCross(FrontVector, UpVector);
UpVector = lcCross(SideVector, FrontVector);
UpVector.Normalize();
mUpVector = UpVector;
ChangeKey(mPositionKeys, mPosition, 1, false);
ChangeKey(mTargetPositionKeys, mTargetPosition, 1, false);
ChangeKey(mUpVectorKeys, mUpVector, 1, false);
UpdatePosition(1);
}
2020-12-13 00:03:06 +01:00
void lcCamera::SetViewpoint(const lcVector3& Position, const lcVector3& Target, const lcVector3& Up)
{
mPosition = Position;
mTargetPosition = Target;
lcVector3 Direction = Target - Position;
lcVector3 UpVector, SideVector;
SideVector = lcCross(Direction, Up);
UpVector = lcCross(SideVector, Direction);
UpVector.Normalize();
mUpVector = UpVector;
2020-12-13 00:03:06 +01:00
ChangeKey(mPositionKeys, mPosition, 1, false);
ChangeKey(mTargetPositionKeys, mTargetPosition, 1, false);
ChangeKey(mUpVectorKeys, mUpVector, 1, false);
UpdatePosition(1);
}
2019-08-16 02:41:32 +02:00
void lcCamera::SetAngles(float Latitude, float Longitude, float Distance)
{
mPosition = lcVector3(0, -1, 0);
mTargetPosition = lcVector3(0, 0, 0);
mUpVector = lcVector3(0, 0, 1);
lcMatrix33 LongitudeMatrix = lcMatrix33RotationZ(LC_DTOR * Longitude);
mPosition = lcMul(mPosition, LongitudeMatrix);
lcVector3 SideVector = lcMul(lcVector3(-1, 0, 0), LongitudeMatrix);
lcMatrix33 LatitudeMatrix = lcMatrix33FromAxisAngle(SideVector, LC_DTOR * Latitude);
2019-08-16 02:41:32 +02:00
mPosition = lcMul(mPosition, LatitudeMatrix) * Distance;
mUpVector = lcMul(mUpVector, LatitudeMatrix);
ChangeKey(mPositionKeys, mPosition, 1, false);
ChangeKey(mTargetPositionKeys, mTargetPosition, 1, false);
ChangeKey(mUpVectorKeys, mUpVector, 1, false);
UpdatePosition(1);
}
2019-08-16 02:33:37 +02:00
void lcCamera::GetAngles(float& Latitude, float& Longitude, float& Distance) const
{
lcVector3 FrontVector(mPosition - mTargetPosition);
lcVector3 X(1, 0, 0);
lcVector3 Y(0, 1, 0);
lcVector3 Z(0, 0, 1);
FrontVector.Normalize();
Latitude = acos(lcDot(-FrontVector, Z)) * LC_RTOD - 90.0f;
lcVector3 CameraXY = -lcNormalize(lcVector3(FrontVector.x, FrontVector.y, 0.0f));
Longitude = acos(lcDot(CameraXY, Y)) * LC_RTOD;
if (lcDot(CameraXY, X) > 0)
Longitude = -Longitude;
Distance = lcLength(mPosition);
}