Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions engine/includes/components/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ class ENGINE_EXPORT Camera : public Component {
void drawGizmos() override;
void drawGizmosSelected() override;

void recalcProjection();

private:
Matrix4 m_projection;
mutable Matrix4 m_projection;

Vector4 m_color;

Expand All @@ -104,6 +102,8 @@ class ENGINE_EXPORT Camera : public Component {

bool m_screen;

mutable bool m_dirty;

};

#endif // CAMERA_H
64 changes: 34 additions & 30 deletions engine/includes/editor/viewport/cameracontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define CAMERACONTROLLER_H

#include <QObject>
#include <QMouseEvent>

#include <amath.h>
#include <object.h>
Expand All @@ -13,8 +12,6 @@ class Actor;
class Camera;
class Viewport;

class QMenu;

class ENGINE_EXPORT CameraController : public QObject {
Q_OBJECT

Expand All @@ -25,6 +22,18 @@ class ENGINE_EXPORT CameraController : public QObject {
Z
};

struct CameraData {
Vector3 rotation;

Vector3 position;

float focalDistance = 1.0f;

float orthoSize = 1.0f;

bool ortho = false;
};

public:
CameraController();

Expand All @@ -43,7 +52,7 @@ class ENGINE_EXPORT CameraController : public QObject {

void setFocusOn(Actor *actor, float &bottom);

void setFree(bool flag) { m_cameraFree = flag; m_cameraFreeSaved = m_cameraFree; }
void setFree(bool flag) { m_cameraFree = flag; }

bool isMovementBlocked() const { return m_blockMove; }
void blockMovement(bool flag) { m_blockMove = flag; }
Expand All @@ -63,13 +72,16 @@ class ENGINE_EXPORT CameraController : public QObject {
void setGridAxis(Axis axis) { m_gridAxis = axis; }

void restoreState(const VariantMap &state);
VariantMap saveState() const;
VariantMap saveState();

void setActiveRootObject(Object *object);

void setZoomLimits(const Vector2 &limit);

void doRotation(const Vector3 &vector);
void activateCamera(int index);

void resetCamera();

signals:
void setCursor(const QCursor &cursor);
Expand All @@ -89,42 +101,34 @@ public slots:
void drawHelpers(Object *object, bool selected);

protected:
uint8_t m_cameraMove;
Axis m_gridAxis;
std::vector<CameraData> m_cameras;

bool m_blockMove;
bool m_blockRotation;
bool m_blockPicking;
bool m_overlapPicking;

bool m_cameraFree;
bool m_cameraFreeSaved;
bool m_rotationTransfer;

bool m_cameraInMove;
CameraData m_targetCamera;

Vector2 m_screenSize;
Vector2 m_mouseSaved;
Vector2 m_mouseDelta;
Vector2 m_zoomLimit;

Vector3 m_cameraSpeed;

Vector3 m_rotation;
Vector3 m_position;

Vector3 m_rotationTarget;
Vector3 m_positionTarget;
Camera *m_activeCamera;

float m_orthoWidthTarget;
float m_focalLengthTarget;
float m_transferProgress;
Object *m_activeRootObject;

Vector2 m_delta;
Vector2 m_saved;
Axis m_gridAxis;

Camera *m_activeCamera;
float m_transferProgress;
int m_currentCamera;

Object *m_activeRootObject;
bool m_blockMove;
bool m_blockMoveOnTransfer;
bool m_blockRotation;
bool m_blockPicking;
bool m_overlapPicking;

Vector2 m_zoomLimit;
bool m_cameraFree;
bool m_cameraInMove;

};

Expand Down
78 changes: 47 additions & 31 deletions engine/src/components/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ Camera::Camera() :
m_focal(1.0f),
m_orthoSize(1.0f),
m_ortho(false),
m_screen(false) {
m_screen(false),
m_dirty(true) {

recalcProjection();
}

/*!
Expand All @@ -42,6 +42,20 @@ Matrix4 Camera::viewMatrix() const {
Returns projection matrix for the camera.
*/
Matrix4 Camera::projectionMatrix() const {
if(m_dirty) {
if(m_ortho) {
float width = m_orthoSize * m_ratio;
if(m_screen) {
m_projection = Matrix4::ortho(0, width, 0, m_orthoSize, m_near, m_far);
} else {
m_projection = Matrix4::ortho(-width / 2, width / 2, -m_orthoSize / 2, m_orthoSize / 2, m_near, m_far);
}
} else {
m_projection = Matrix4::perspective(m_fov, m_ratio, m_near, m_far);
}

m_dirty = false;
}
return m_projection;
}
/*!
Expand All @@ -51,7 +65,7 @@ Matrix4 Camera::projectionMatrix() const {
Vector3 Camera::project(const Vector3 &worldSpace) {
Vector4 in(worldSpace.x, worldSpace.y, worldSpace.z, 1.0f);
Vector4 out(viewMatrix() * in);
in = m_projection * out;
in = projectionMatrix() * out;

if(in.w == 0.0f) {
return Vector3(); // false;
Expand All @@ -68,7 +82,7 @@ Vector3 Camera::project(const Vector3 &worldSpace) {
Returns result of transformation.
*/
Vector3 Camera::unproject(const Vector3 &screenSpace) {
Matrix4 final((m_projection * viewMatrix()).inverse());
Matrix4 final((projectionMatrix() * viewMatrix()).inverse());

Vector4 in(2.0f * screenSpace.x - 1.0f,
2.0f * screenSpace.y - 1.0f,
Expand Down Expand Up @@ -117,8 +131,10 @@ float Camera::fov() const {
\note Applicable only for the perspective mode.
*/
void Camera::setFov(const float angle) {
m_fov = angle;
recalcProjection();
if(angle != m_fov) {
m_fov = angle;
m_dirty = true;
}
}
/*!
Returns a distance to near cut plane.
Expand All @@ -130,8 +146,10 @@ float Camera::nearPlane() const {
Sets a \a distance to near cut plane.
*/
void Camera::setNear(const float distance) {
m_near = distance;
recalcProjection();
if(distance != m_near) {
m_near = distance;
m_dirty = true;
}
}
/*!
Returns a distance to far cut plane.
Expand All @@ -143,8 +161,10 @@ float Camera::farPlane() const {
Sets a \a distance to far cut plane.
*/
void Camera::setFar(const float distance) {
m_far = distance;
recalcProjection();
if(distance != m_far) {
m_far = distance;
m_dirty = true;
}
}
/*!
Returns the aspect ratio (width divided by height).
Expand All @@ -156,8 +176,10 @@ float Camera::ratio() const {
Sets the aspect \a ratio (width divided by height).
*/
void Camera::setRatio(float ratio) {
m_ratio = ratio;
recalcProjection();
if(ratio != m_ratio) {
m_ratio = ratio;
m_dirty = true;
}
}
/*!
Returns a focal distance for the camera.
Expand Down Expand Up @@ -193,8 +215,11 @@ float Camera::orthoSize() const {
Sets camera \a size for orthographic mode.
*/
void Camera::setOrthoSize(const float size) {
m_orthoSize = CLAMP(size, FLT_EPSILON, 100000.0f);
recalcProjection();
float s = CLAMP(size, FLT_EPSILON, FLT_MAX);
if(s != m_orthoSize) {
m_orthoSize = s;
m_dirty = true;
}
}
/*!
Returns true for the orthographic mode; for the perspective mode, returns false.
Expand All @@ -206,8 +231,10 @@ bool Camera::orthographic() const {
Sets orthographic \a mode.
*/
void Camera::setOrthographic(const bool mode) {
m_ortho = mode;
recalcProjection();
if(m_ortho != mode) {
m_ortho = mode;
m_dirty = true;
}
}
/*!
Returns true is this camera in the screen space mode.
Expand All @@ -221,8 +248,10 @@ bool Camera::isScreenSpace() const {
Typically used for Editor.
*/
void Camera::setScreenSpace(bool mode) {
m_screen = mode;
recalcProjection();
if(mode != m_screen) {
m_screen = mode;
m_dirty = true;
}
}
/*!
Returns current active camera.
Expand Down Expand Up @@ -338,16 +367,3 @@ void Camera::drawGizmosSelected() {

Gizmos::drawLines(points, indices, Vector4(0.5f, 0.5f, 0.5f, 1.0f));
}

void Camera::recalcProjection() {
if(m_ortho) {
float width = m_orthoSize * m_ratio;
if(m_screen) {
m_projection = Matrix4::ortho(0, width, 0, m_orthoSize, m_near, m_far);
} else {
m_projection = Matrix4::ortho(-width / 2, width / 2, -m_orthoSize / 2, m_orthoSize / 2, m_near, m_far);
}
} else {
m_projection = Matrix4::perspective(m_fov, m_ratio, m_near, m_far);
}
}
Loading
Loading