leocad/common/camera.h

316 lines
7.9 KiB
C
Raw Normal View History

#pragma once
2011-09-07 21:06:51 +00:00
#include "object.h"
2012-05-28 23:33:22 +00:00
#include "lc_math.h"
2013-08-15 23:43:18 +00:00
#include "lc_array.h"
2011-09-07 21:06:51 +00:00
#define LC_CAMERA_HIDDEN 0x0001
#define LC_CAMERA_SIMPLE 0x0002
2014-05-03 01:22:24 +00: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-12 16:58:40 -08:00
enum class lcViewpoint
2012-08-20 04:05:56 +00:00
{
2020-12-12 16:58:40 -08:00
Front,
Back,
Top,
Bottom,
Left,
Right,
2021-01-01 15:04:27 -08:00
Home,
Count
2012-08-20 04:05:56 +00:00
};
2011-09-07 21:06:51 +00:00
enum lcCameraSection
{
LC_CAMERA_SECTION_POSITION,
LC_CAMERA_SECTION_TARGET,
LC_CAMERA_SECTION_UPVECTOR
};
2014-05-01 18:42:11 +00:00
class lcCamera : public lcObject
2011-09-07 21:06:51 +00:00
{
public:
2014-05-01 18:42:11 +00:00
lcCamera(bool Simple);
lcCamera(float ex, float ey, float ez, float tx, float ty, float tz);
~lcCamera();
2011-09-07 21:06:51 +00:00
2020-05-03 15:39:39 -07:00
lcCamera(const lcCamera&) = delete;
2021-11-14 18:34:24 -08:00
lcCamera(lcCamera&&) = delete;
2020-05-03 15:39:39 -07:00
lcCamera& operator=(const lcCamera&) = delete;
2021-11-14 18:34:24 -08:00
lcCamera& operator=(lcCamera&&) = delete;
2021-01-01 15:04:27 -08:00
static lcViewpoint GetViewpoint(const QString& ViewpointName);
2020-05-03 15:39:39 -07:00
2020-12-13 16:27:21 -08:00
QString GetName() const override
2012-08-20 04:05:56 +00:00
{
2020-12-13 16:27:21 -08:00
return mName;
2012-08-20 04:05:56 +00:00
}
2011-09-07 21:06:51 +00:00
2020-12-13 16:27:21 -08:00
void SetName(const QString& Name);
2014-08-07 15:22:33 +00:00
void CreateName(const lcArray<lcCamera*>& Cameras);
2012-08-22 01:13:32 +00:00
bool IsSimple() const
2012-08-20 04:05:56 +00:00
{
return (mState & LC_CAMERA_SIMPLE) != 0;
2012-08-20 04:05:56 +00:00
}
2011-09-07 21:06:51 +00:00
2014-05-03 01:22:24 +00: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 04:05:56 +00: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-12 22:17:14 -08:00
return ~0U;
}
quint32 GetAllowedTransforms() const override
{
2023-09-04 10:59:16 -07: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 04:05:56 +00:00
}
2011-09-07 21:06:51 +00:00
2014-09-05 00:24:28 +00:00
void SaveLDraw(QTextStream& Stream) const;
2014-09-03 14:34:53 +00:00
bool ParseLDrawLine(QTextStream& Stream);
2011-09-07 21:06:51 +00:00
public:
bool IsVisible() const
{ return (mState & LC_CAMERA_HIDDEN) == 0; }
2014-08-29 23:52:42 +00: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 21:06:51 +00:00
2014-08-31 00:53:12 +00:00
void SetPosition(const lcVector3& Position, lcStep Step, bool AddKey)
{
2023-12-28 11:32:25 -08:00
mPosition.ChangeKey(Position, Step, AddKey);
2014-08-31 00:53:12 +00:00
}
void SetTargetPosition(const lcVector3& TargetPosition, lcStep Step, bool AddKey)
{
2023-12-28 11:32:25 -08:00
mTargetPosition.ChangeKey(TargetPosition, Step, AddKey);
2014-08-31 00:53:12 +00:00
}
void SetUpVector(const lcVector3& UpVector, lcStep Step, bool AddKey)
{
2023-12-28 11:32:25 -08:00
mUpVector.ChangeKey(UpVector, Step, AddKey);
2014-08-31 00:53:12 +00:00
}
float GetOrthoHeight() const
{
// Compute the FOV/plane intersection radius.
// d d
// a = 2 atan(------) => ~ a = --- => d = af
// 2f f
2021-11-14 18:34:24 -08:00
const float f = (mPosition - mTargetPosition).Length();
return (m_fovy * f) * (LC_PI / 180.0f);
}
2011-09-07 21:06:51 +00:00
public:
void RayTest(lcObjectRayTest& ObjectRayTest) const override;
void BoxTest(lcObjectBoxTest& ObjectBoxTest) const override;
void DrawInterface(lcContext* Context, const lcScene& Scene) const override;
void RemoveKeyFrames() override;
2014-08-31 00:53:12 +00:00
void InsertTime(lcStep Start, lcStep Time);
void RemoveTime(lcStep Start, lcStep Time);
static bool FileLoad(lcFile& file);
2011-09-07 21:06:51 +00:00
2016-02-19 17:53:54 +00:00
void CompareBoundingBox(lcVector3& Min, lcVector3& Max);
2014-07-06 06:04:09 +00:00
void UpdatePosition(lcStep Step);
void CopyPosition(const lcCamera* Camera);
void CopySettings(const lcCamera* Camera);
2011-09-07 21:06:51 +00: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 06:04:09 +00:00
void Zoom(float Distance, lcStep Step, bool AddKey);
void Pan(const lcVector3& Distance, lcStep Step, bool AddKey);
2014-07-06 06:04:09 +00: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 01:05:17 +00:00
void SetViewpoint(lcViewpoint Viewpoint);
2018-08-19 20:27:58 -07:00
void SetViewpoint(const lcVector3& Position);
2020-12-12 15:03:06 -08:00
void SetViewpoint(const lcVector3& Position, const lcVector3& Target, const lcVector3& Up);
2019-08-15 17:33:37 -07:00
void GetAngles(float& Latitude, float& Longitude, float& Distance) const;
2019-08-15 17:41:32 -07:00
void SetAngles(float Latitude, float Longitude, float Distance);
2011-09-07 21:06:51 +00:00
float m_fovy;
float m_zNear;
float m_zFar;
2012-05-28 23:33:22 +00:00
lcMatrix44 mWorldView;
2023-12-28 11:32:25 -08: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-28 23:33:22 +00:00
2011-09-07 21:06:51 +00:00
protected:
void Initialize();
2020-12-13 16:27:21 -08:00
QString mName;
2017-12-02 12:22:04 -08:00
quint32 mState;
2011-09-07 21:06:51 +00:00
};