leocad/common/camera.h

320 lines
8.3 KiB
C
Raw Normal View History

#pragma once
2011-09-07 23:06:51 +02:00
#include "object.h"
2012-05-29 01:33:22 +02:00
#include "lc_math.h"
2013-08-16 01:43:18 +02:00
#include "lc_array.h"
2011-09-07 23:06:51 +02:00
#define LC_CAMERA_HIDDEN 0x0001
#define LC_CAMERA_SIMPLE 0x0002
2014-05-03 03:22:24 +02:00
#define LC_CAMERA_ORTHO 0x0004
#define LC_CAMERA_POSITION_SELECTED 0x0010
#define LC_CAMERA_POSITION_FOCUSED 0x0020
#define LC_CAMERA_TARGET_SELECTED 0x0040
#define LC_CAMERA_TARGET_FOCUSED 0x0080
#define LC_CAMERA_UPVECTOR_SELECTED 0x0100
#define LC_CAMERA_UPVECTOR_FOCUSED 0x0200
#define LC_CAMERA_SELECTION_MASK (LC_CAMERA_POSITION_SELECTED | LC_CAMERA_TARGET_SELECTED | LC_CAMERA_UPVECTOR_SELECTED)
#define LC_CAMERA_FOCUS_MASK (LC_CAMERA_POSITION_FOCUSED | LC_CAMERA_TARGET_FOCUSED | LC_CAMERA_UPVECTOR_FOCUSED)
2020-12-13 01:58:40 +01:00
enum class lcViewpoint
2012-08-20 06:05:56 +02:00
{
2020-12-13 01:58:40 +01:00
Front,
Back,
Top,
Bottom,
Left,
Right,
2021-01-02 00:04:27 +01:00
Home,
Count
2012-08-20 06:05:56 +02:00
};
2011-09-07 23:06:51 +02:00
enum lcCameraSection
{
LC_CAMERA_SECTION_POSITION,
LC_CAMERA_SECTION_TARGET,
LC_CAMERA_SECTION_UPVECTOR
};
2014-05-01 20:42:11 +02:00
class lcCamera : public lcObject
2011-09-07 23:06:51 +02:00
{
public:
2014-05-01 20:42:11 +02:00
lcCamera(bool Simple);
lcCamera(float ex, float ey, float ez, float tx, float ty, float tz);
~lcCamera();
2011-09-07 23:06:51 +02:00
2020-05-04 00:39:39 +02:00
lcCamera(const lcCamera&) = delete;
2021-11-15 03:34:24 +01:00
lcCamera(lcCamera&&) = delete;
2020-05-04 00:39:39 +02:00
lcCamera& operator=(const lcCamera&) = delete;
2021-11-15 03:34:24 +01:00
lcCamera& operator=(lcCamera&&) = delete;
2021-01-02 00:04:27 +01:00
static lcViewpoint GetViewpoint(const QString& ViewpointName);
2020-05-04 00:39:39 +02:00
2020-12-14 01:27:21 +01:00
QString GetName() const override
2012-08-20 06:05:56 +02:00
{
2020-12-14 01:27:21 +01:00
return mName;
2012-08-20 06:05:56 +02:00
}
2011-09-07 23:06:51 +02:00
2020-12-14 01:27:21 +01:00
void SetName(const QString& Name);
2014-08-07 17:22:33 +02:00
void CreateName(const lcArray<lcCamera*>& Cameras);
2012-08-22 03:13:32 +02:00
bool IsSimple() const
2012-08-20 06:05:56 +02:00
{
return (mState & LC_CAMERA_SIMPLE) != 0;
2012-08-20 06:05:56 +02:00
}
2011-09-07 23:06:51 +02:00
2014-05-03 03:22:24 +02:00
bool IsOrtho() const
{
return (mState & LC_CAMERA_ORTHO) != 0;
}
void SetOrtho(bool Ortho)
{
if (Ortho)
mState |= LC_CAMERA_ORTHO;
else
mState &= ~LC_CAMERA_ORTHO;
}
bool IsSelected() const override
2012-08-20 06:05:56 +02:00
{
return (mState & LC_CAMERA_SELECTION_MASK) != 0;
}
bool IsSelected(quint32 Section) const override
{
switch (Section)
{
case LC_CAMERA_SECTION_POSITION:
return (mState & LC_CAMERA_POSITION_SELECTED) != 0;
break;
case LC_CAMERA_SECTION_TARGET:
return (mState & LC_CAMERA_TARGET_SELECTED) != 0;
break;
case LC_CAMERA_SECTION_UPVECTOR:
return (mState & LC_CAMERA_UPVECTOR_SELECTED) != 0;
break;
}
return false;
}
void SetSelected(bool Selected) override
{
if (Selected)
mState |= LC_CAMERA_SELECTION_MASK;
else
mState &= ~(LC_CAMERA_SELECTION_MASK | LC_CAMERA_FOCUS_MASK);
}
void SetSelected(quint32 Section, bool Selected) override
{
switch (Section)
{
case LC_CAMERA_SECTION_POSITION:
if (Selected)
mState |= LC_CAMERA_POSITION_SELECTED;
else
mState &= ~(LC_CAMERA_POSITION_SELECTED | LC_CAMERA_POSITION_FOCUSED);
break;
case LC_CAMERA_SECTION_TARGET:
if (Selected)
mState |= LC_CAMERA_TARGET_SELECTED;
else
mState &= ~(LC_CAMERA_TARGET_SELECTED | LC_CAMERA_TARGET_FOCUSED);
break;
case LC_CAMERA_SECTION_UPVECTOR:
if (Selected)
mState |= LC_CAMERA_UPVECTOR_SELECTED;
else
mState &= ~(LC_CAMERA_UPVECTOR_SELECTED | LC_CAMERA_UPVECTOR_FOCUSED);
break;
}
}
bool IsFocused() const override
{
return (mState & LC_CAMERA_FOCUS_MASK) != 0;
}
bool IsFocused(quint32 Section) const override
{
switch (Section)
{
case LC_CAMERA_SECTION_POSITION:
return (mState & LC_CAMERA_POSITION_FOCUSED) != 0;
break;
case LC_CAMERA_SECTION_TARGET:
return (mState & LC_CAMERA_TARGET_FOCUSED) != 0;
break;
case LC_CAMERA_SECTION_UPVECTOR:
return (mState & LC_CAMERA_UPVECTOR_FOCUSED) != 0;
break;
}
return false;
}
void SetFocused(quint32 Section, bool Focus) override
{
switch (Section)
{
case LC_CAMERA_SECTION_POSITION:
if (Focus)
mState |= LC_CAMERA_POSITION_SELECTED | LC_CAMERA_POSITION_FOCUSED;
else
mState &= ~(LC_CAMERA_POSITION_SELECTED | LC_CAMERA_POSITION_FOCUSED);
break;
case LC_CAMERA_SECTION_TARGET:
if (Focus)
mState |= LC_CAMERA_TARGET_SELECTED | LC_CAMERA_TARGET_FOCUSED;
else
mState &= ~(LC_CAMERA_TARGET_SELECTED | LC_CAMERA_TARGET_FOCUSED);
break;
case LC_CAMERA_SECTION_UPVECTOR:
if (Focus)
mState |= LC_CAMERA_UPVECTOR_SELECTED | LC_CAMERA_UPVECTOR_FOCUSED;
else
mState &= ~(LC_CAMERA_UPVECTOR_SELECTED | LC_CAMERA_UPVECTOR_FOCUSED);
break;
}
}
quint32 GetFocusSection() const override
{
if (mState & LC_CAMERA_POSITION_FOCUSED)
return LC_CAMERA_SECTION_POSITION;
if (mState & LC_CAMERA_TARGET_FOCUSED)
return LC_CAMERA_SECTION_TARGET;
if (mState & LC_CAMERA_UPVECTOR_FOCUSED)
return LC_CAMERA_SECTION_UPVECTOR;
2017-12-13 07:17:14 +01:00
return ~0U;
}
quint32 GetAllowedTransforms() const override
{
2023-09-04 19:59:16 +02:00
return LC_OBJECT_TRANSFORM_MOVE_XYZ;
}
lcVector3 GetSectionPosition(quint32 Section) const override
{
switch (Section)
{
case LC_CAMERA_SECTION_POSITION:
return mPosition;
case LC_CAMERA_SECTION_TARGET:
return mTargetPosition;
case LC_CAMERA_SECTION_UPVECTOR:
return lcMul31(lcVector3(0, 25, 0), lcMatrix44AffineInverse(mWorldView));
}
return lcVector3(0.0f, 0.0f, 0.0f);
2012-08-20 06:05:56 +02:00
}
2011-09-07 23:06:51 +02:00
2014-09-05 02:24:28 +02:00
void SaveLDraw(QTextStream& Stream) const;
2014-09-03 16:34:53 +02:00
bool ParseLDrawLine(QTextStream& Stream);
2011-09-07 23:06:51 +02:00
public:
bool IsVisible() const
{ return (mState & LC_CAMERA_HIDDEN) == 0; }
2014-08-30 01:52:42 +02:00
bool IsHidden() const
{
return (mState & LC_CAMERA_HIDDEN) != 0;
}
void SetHidden(bool Hidden)
{
if (Hidden)
mState |= LC_CAMERA_HIDDEN;
else
mState &= ~LC_CAMERA_HIDDEN;
}
2011-09-07 23:06:51 +02:00
2014-08-31 02:53:12 +02:00
void SetPosition(const lcVector3& Position, lcStep Step, bool AddKey)
{
2023-12-28 20:32:25 +01:00
mPosition.ChangeKey(Position, Step, AddKey);
2014-08-31 02:53:12 +02:00
}
void SetTargetPosition(const lcVector3& TargetPosition, lcStep Step, bool AddKey)
{
2023-12-28 20:32:25 +01:00
mTargetPosition.ChangeKey(TargetPosition, Step, AddKey);
2014-08-31 02:53:12 +02:00
}
void SetUpVector(const lcVector3& UpVector, lcStep Step, bool AddKey)
{
2023-12-28 20:32:25 +01:00
mUpVector.ChangeKey(UpVector, Step, AddKey);
2014-08-31 02:53:12 +02:00
}
float GetOrthoHeight() const
{
// Compute the FOV/plane intersection radius.
// d d
// a = 2 atan(------) => ~ a = --- => d = af
// 2f f
2021-11-15 03:34:24 +01:00
const float f = (mPosition - mTargetPosition).Length();
return (m_fovy * f) * (LC_PI / 180.0f);
}
2011-09-07 23:06:51 +02:00
public:
void RayTest(lcObjectRayTest& ObjectRayTest) const override;
void BoxTest(lcObjectBoxTest& ObjectBoxTest) const override;
void DrawInterface(lcContext* Context, const lcScene& Scene) const override;
2024-02-19 23:54:45 +01:00
QVariant GetPropertyValue(lcObjectPropertyId PropertyId) const override;
bool SetPropertyValue(lcObjectPropertyId PropertyId, lcStep Step, bool AddKey, QVariant Value) override;
2024-01-15 02:41:01 +01:00
bool HasKeyFrame(lcObjectPropertyId PropertyId, lcStep Time) const override;
2024-01-21 21:53:18 +01:00
bool SetKeyFrame(lcObjectPropertyId PropertyId, lcStep Time, bool KeyFrame) override;
void RemoveKeyFrames() override;
2014-08-31 02:53:12 +02:00
void InsertTime(lcStep Start, lcStep Time);
void RemoveTime(lcStep Start, lcStep Time);
static bool FileLoad(lcFile& file);
2011-09-07 23:06:51 +02:00
2016-02-19 18:53:54 +01:00
void CompareBoundingBox(lcVector3& Min, lcVector3& Max);
2024-01-21 21:53:18 +01:00
void UpdatePosition(lcStep Step) override;
void CopyPosition(const lcCamera* Camera);
void CopySettings(const lcCamera* Camera);
2011-09-07 23:06:51 +02:00
void ZoomExtents(float AspectRatio, const lcVector3& Center, const std::vector<lcVector3>& Points, lcStep Step, bool AddKey);
void ZoomRegion(float AspectRatio, const lcVector3& Position, const lcVector3& TargetPosition, const lcVector3* Corners, lcStep Step, bool AddKey);
2014-07-06 08:04:09 +02:00
void Zoom(float Distance, lcStep Step, bool AddKey);
void Pan(const lcVector3& Distance, lcStep Step, bool AddKey);
2014-07-06 08:04:09 +02:00
void Orbit(float DistanceX, float DistanceY, const lcVector3& CenterPosition, lcStep Step, bool AddKey);
void Roll(float Distance, lcStep Step, bool AddKey);
void Center(const lcVector3& NewCenter, lcStep Step, bool AddKey);
void MoveSelected(lcStep Step, bool AddKey, const lcVector3& Distance);
void MoveRelative(const lcVector3& Distance, lcStep Step, bool AddKey);
2014-11-08 02:05:17 +01:00
void SetViewpoint(lcViewpoint Viewpoint);
2018-08-20 05:27:58 +02:00
void SetViewpoint(const lcVector3& Position);
2020-12-13 00:03:06 +01:00
void SetViewpoint(const lcVector3& Position, const lcVector3& Target, const lcVector3& Up);
2019-08-16 02:33:37 +02:00
void GetAngles(float& Latitude, float& Longitude, float& Distance) const;
2019-08-16 02:41:32 +02:00
void SetAngles(float Latitude, float Longitude, float Distance);
2011-09-07 23:06:51 +02:00
float m_fovy;
float m_zNear;
float m_zFar;
2012-05-29 01:33:22 +02:00
lcMatrix44 mWorldView;
2023-12-28 20:32:25 +01:00
lcObjectProperty<lcVector3> mPosition = lcObjectProperty<lcVector3>(lcVector3(0.0f, 0.0f, 0.0f));
lcObjectProperty<lcVector3> mTargetPosition = lcObjectProperty<lcVector3>(lcVector3(0.0f, 0.0f, 0.0f));
lcObjectProperty<lcVector3> mUpVector = lcObjectProperty<lcVector3>(lcVector3(0.0f, 0.0f, 0.0f));
2012-05-29 01:33:22 +02:00
2011-09-07 23:06:51 +02:00
protected:
void Initialize();
2020-12-14 01:27:21 +01:00
QString mName;
2017-12-02 21:22:04 +01:00
quint32 mState;
2011-09-07 23:06:51 +02:00
};