diff --git a/engine/includes/components/camera.h b/engine/includes/components/camera.h index 28a02994c..b7dd69fee 100644 --- a/engine/includes/components/camera.h +++ b/engine/includes/components/camera.h @@ -22,8 +22,8 @@ class ENGINE_EXPORT Camera : public Component { A_METHODS( A_METHOD(Matrix4, Camera::viewMatrix), A_METHOD(Matrix4, Camera::projectionMatrix), - A_STATIC(Vector3, Camera::project), - A_STATIC(Vector3, Camera::unproject), + A_METHOD(Vector3, Camera::project), + A_METHOD(Vector3, Camera::unproject), A_STATIC(Camera *, Camera::current), A_STATIC(void, Camera::setCurrent), A_METHOD(Ray, Camera::castRay) @@ -64,8 +64,8 @@ class ENGINE_EXPORT Camera : public Component { static Camera *current(); static void setCurrent(Camera *current); - static Vector3 project(const Vector3 &worldSpace, const Matrix4 &modelView, const Matrix4 &projection); - static Vector3 unproject(const Vector3 &screenSpace, const Matrix4 &modelView, const Matrix4 &projection); + Vector2 project(const Vector3 &worldSpace); + Vector3 unproject(const Vector3 &screenSpace); static array frustumCorners(const Camera &camera); static array frustumCorners(bool ortho, float sigma, float ratio, const Vector3 &position, const Quaternion &rotation, float nearPlane, float farPlane); @@ -74,8 +74,12 @@ class ENGINE_EXPORT Camera : public Component { void drawGizmos() override; void drawGizmosSelected() override; + void recalcProjection(); + private: - bool m_ortho; + Matrix4 m_projection; + + Vector4 m_color; float m_fov; @@ -89,7 +93,7 @@ class ENGINE_EXPORT Camera : public Component { float m_orthoSize; - Vector4 m_color; + bool m_ortho; }; diff --git a/engine/includes/editor/viewport/handletools.h b/engine/includes/editor/viewport/handletools.h index 6d144ce14..f84cca831 100644 --- a/engine/includes/editor/viewport/handletools.h +++ b/engine/includes/editor/viewport/handletools.h @@ -14,9 +14,6 @@ class ENGINE_EXPORT HandleTools { static float distanceToMesh(const Matrix4 &matrix, const IndexVector &indices, const Vector3Vector &vertices, const Vector2 &screen); public: - static Matrix4 s_View; - static Matrix4 s_Projection; - static float s_Sense; }; diff --git a/engine/src/components/camera.cpp b/engine/src/components/camera.cpp index 71432a41d..f0cd7e079 100644 --- a/engine/src/components/camera.cpp +++ b/engine/src/components/camera.cpp @@ -17,14 +17,13 @@ Camera *s_currentCamera = nullptr; */ Camera::Camera() : - m_ortho(false), - m_fov(45.0), // 2*arctan(height/(2*distance)) - m_near(0.1f), - m_far(1000.0f), - m_ratio(1.0f), - m_focal(1.0f), - m_orthoSize(1.0f), - m_color(Vector4()) { + m_fov(45.0), // 2*arctan(height/(2*distance)) + m_near(0.1f), + m_far(1000.0f), + m_ratio(1.0f), + m_focal(1.0f), + m_orthoSize(1.0f), + m_ortho(false) { } @@ -41,37 +40,33 @@ Matrix4 Camera::viewMatrix() const { Returns projection matrix for the camera. */ Matrix4 Camera::projectionMatrix() const { - if(m_ortho) { - float width = m_orthoSize * m_ratio; - return Matrix4::ortho(-width / 2, width / 2, -m_orthoSize / 2, m_orthoSize / 2, m_near, m_far); - } - return Matrix4::perspective(m_fov, m_ratio, m_near, m_far); + return m_projection; } /*! - Transforms position from \a worldSpace into screen space using \a modelView and \a projection matrices. + Transforms position from \a worldSpace into screen space. Returns result of transformation. */ -Vector3 Camera::project(const Vector3 &worldSpace, const Matrix4 &modelView, const Matrix4 &projection) { +Vector2 Camera::project(const Vector3 &worldSpace) { Vector4 in(worldSpace.x, worldSpace.y, worldSpace.z, 1.0f); - Vector4 out(modelView * in); - in = projection * out; + Vector4 out(viewMatrix() * in); + in = m_projection * out; if(in.w == 0.0f) { - return Vector3(); // false; + return Vector2(); // false; } in.w = 1.0f / in.w; in.x *= in.w; in.y *= in.w; in.z *= in.w; - return Vector3((in.x * 0.5f + 0.5f), (in.y * 0.5f + 0.5f), (1.0f + in.z) * 0.5f); + return Vector2((in.x * 0.5f + 0.5f), (in.y * 0.5f + 0.5f)); } /*! - Transforms position from \a screenSpace into world space using \a modelView and \a projection matrices. + Transforms position from \a screenSpace into world space. Returns result of transformation. */ -Vector3 Camera::unproject(const Vector3 &screenSpace, const Matrix4 &modelView, const Matrix4 &projection) { - Matrix4 final((projection * modelView).inverse()); +Vector3 Camera::unproject(const Vector3 &screenSpace) { + Matrix4 final((m_projection * viewMatrix()).inverse()); Vector4 in; in.x = (screenSpace.x) * 2.0f - 1.0f; @@ -124,6 +119,7 @@ float Camera::fov() const { */ void Camera::setFov(const float angle) { m_fov = angle; + recalcProjection(); } /*! Returns a distance to near cut plane. @@ -136,6 +132,7 @@ float Camera::nearPlane() const { */ void Camera::setNear(const float distance) { m_near = distance; + recalcProjection(); } /*! Returns a distance to far cut plane. @@ -148,6 +145,7 @@ float Camera::farPlane() const { */ void Camera::setFar(const float distance) { m_far = distance; + recalcProjection(); } /*! Returns the aspect ratio (width divided by height). @@ -160,6 +158,7 @@ float Camera::ratio() const { */ void Camera::setRatio(float ratio) { m_ratio = ratio; + recalcProjection(); } /*! Returns a focal distance for the camera. @@ -172,6 +171,7 @@ float Camera::focal() const { */ void Camera::setFocal(const float focal) { m_focal = focal; + recalcProjection(); } /*! Returns the color with which the screen will be cleared. @@ -196,6 +196,7 @@ float Camera::orthoSize() const { */ void Camera::setOrthoSize(const float size) { m_orthoSize = size; + recalcProjection(); } /*! Returns true for the orthographic mode; for the perspective mode, returns false. @@ -208,6 +209,7 @@ bool Camera::orthographic() const { */ void Camera::setOrthographic(const bool mode) { m_ortho = mode; + recalcProjection(); } /*! Returns current active camera. @@ -299,3 +301,12 @@ 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; + 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); + } +} diff --git a/engine/src/editor/editortool.cpp b/engine/src/editor/editortool.cpp index 44a463c8a..2317deccb 100644 --- a/engine/src/editor/editortool.cpp +++ b/engine/src/editor/editortool.cpp @@ -41,12 +41,6 @@ void EditorTool::update(bool center, bool local, bool snap) { A_UNUSED(center); A_UNUSED(local); A_UNUSED(snap); - - Camera *cam = Camera::current(); - if(cam) { - HandleTools::s_View = cam->viewMatrix(); - HandleTools::s_Projection = cam->projectionMatrix(); - } } void EditorTool::beginControl() { diff --git a/engine/src/editor/viewport/handletools.cpp b/engine/src/editor/viewport/handletools.cpp index a2914ed51..1a8b7fbec 100644 --- a/engine/src/editor/viewport/handletools.cpp +++ b/engine/src/editor/viewport/handletools.cpp @@ -5,32 +5,26 @@ #include -Matrix4 HandleTools::s_View; -Matrix4 HandleTools::s_Projection; - float HandleTools::s_Sense = 0.02f; float HandleTools::distanceToPoint(const Matrix4 &matrix, const Vector3 &point, const Vector2 &screen) { - Matrix4 mv = s_View * matrix; - Vector3 ssp = Camera::project(point, mv, s_Projection); + Vector2 ssp = Camera::current()->project(matrix * point); - return (screen - Vector2(ssp.x, ssp.y)).length(); + return (screen - ssp).length(); } float HandleTools::distanceToPath(const Matrix4 &matrix, const Vector3Vector &points, const Vector2 &screen) { - Matrix4 mv = s_View * matrix; float result = FLT_MAX; bool first = true; Vector2 back; for(auto &it : points) { - Vector3 ssp = Camera::project(it, mv, s_Projection); - Vector2 ss(ssp.x, ssp.y); + Vector2 ssp = Camera::current()->project(matrix * it); if(!first) { - result = std::min(Mathf::distanceToSegment(back, ss, screen), result); + result = std::min(Mathf::distanceToSegment(back, ssp, screen), result); } else { first = false; } - back = ss; + back = ssp; } return sqrtf(result); } @@ -39,17 +33,13 @@ float HandleTools::distanceToMesh(const Matrix4 &matrix, const IndexVector &indi if(indices.empty()) { return distanceToPath(matrix, vertices, screen); } - Matrix4 mv = s_View * matrix; float result = FLT_MAX; if((vertices.size() % 2) == 0) { for(uint32_t i = 0; i < indices.size() - 1; i += 2) { - Vector3 a = Camera::project(vertices[indices[i]], mv, s_Projection); - Vector3 b = Camera::project(vertices[indices[i+1]], mv, s_Projection); - - Vector2 ssa(a.x, a.y); - Vector2 ssb(b.x, b.y); + Vector2 a = Camera::current()->project(matrix * vertices[indices[i]]); + Vector2 b = Camera::current()->project(matrix * vertices[indices[i+1]]); - result = std::min(Mathf::distanceToSegment(ssa, ssb, screen), result); + result = std::min(Mathf::distanceToSegment(a, b, screen), result); } } return sqrtf(result); diff --git a/engine/src/resources/font.cpp b/engine/src/resources/font.cpp index eaf04a078..cd6603a1c 100644 --- a/engine/src/resources/font.cpp +++ b/engine/src/resources/font.cpp @@ -210,7 +210,7 @@ void Font::requestCharacters(const string &characters) { if(isNew) { packSheets(1); - notifyCurrentState(); + //notifyCurrentState(); } } /*! diff --git a/modules/editor/grapheditor/editor/graph/graphcontroller.cpp b/modules/editor/grapheditor/editor/graph/graphcontroller.cpp index 1101a8a21..bc4685ef7 100644 --- a/modules/editor/grapheditor/editor/graph/graphcontroller.cpp +++ b/modules/editor/grapheditor/editor/graph/graphcontroller.cpp @@ -52,9 +52,7 @@ Vector3 GraphController::worldPosition() { void GraphController::update() { Vector4 pos = Input::mousePosition(); - s_worldPosition = Camera::unproject(Vector3(pos.z, pos.w, 0.0f), - m_activeCamera->viewMatrix(), - m_activeCamera->projectionMatrix()); + s_worldPosition = m_activeCamera->unproject(Vector3(pos.z, pos.w, 0.0f)); if((Input::isMouseButtonUp(Input::MOUSE_RIGHT) && !m_cameraInMove) || (Input::isMouseButtonUp(Input::MOUSE_LEFT) && m_view->isCreationLink())) { diff --git a/modules/editor/texturetools/editor/spritecontroller.cpp b/modules/editor/texturetools/editor/spritecontroller.cpp index e359d9e6a..df2b0227c 100644 --- a/modules/editor/texturetools/editor/spritecontroller.cpp +++ b/modules/editor/texturetools/editor/spritecontroller.cpp @@ -160,12 +160,6 @@ void SpriteController::drawHandles() { if(m_settings) { Qt::CursorShape shape = Qt::ArrowCursor; - Camera *cam = Camera::current(); - if(cam) { - HandleTools::s_View = cam->viewMatrix(); - HandleTools::s_Projection = cam->projectionMatrix(); - } - for(auto it : m_settings->elements().keys()) { QRectF r = mapRect(m_settings->elements().value(it).m_rect); if(m_list.indexOf(it) > -1) { @@ -230,7 +224,7 @@ void SpriteController::drawHandles() { } Vector2 SpriteController::mapToScene(const Vector2 &screen) { - Vector3 world = Camera::unproject(Vector3(screen, 0.0f), m_activeCamera->viewMatrix(), m_activeCamera->projectionMatrix()); + Vector3 world = m_activeCamera->unproject(Vector3(screen, 0.0f)); world.x += SCALE * 0.5f; world.y += SCALE * 0.5f; diff --git a/modules/uikit/src/editor/widgetcontroller.cpp b/modules/uikit/src/editor/widgetcontroller.cpp index b9990befd..4722131f0 100644 --- a/modules/uikit/src/editor/widgetcontroller.cpp +++ b/modules/uikit/src/editor/widgetcontroller.cpp @@ -116,12 +116,6 @@ void WidgetController::onSelectActor(const QList &list, bool additive) void WidgetController::drawHandles() { CameraController::drawHandles(); - Camera *cam = Camera::current(); - if(cam) { - HandleTools::s_View = Matrix4(); - HandleTools::s_Projection = Matrix4::ortho(0, m_width, 0, m_height, 0.0f, 100.0f); - } - Vector4 pos(Input::mousePosition()); Handles::s_Mouse = Vector2(pos.z, pos.w); Handles::s_Screen = m_screenSize; @@ -134,9 +128,7 @@ void WidgetController::drawHandles() { void WidgetController::update() { Vector4 mouse = Input::mousePosition(); - Vector3 pos = Camera::unproject(Vector3(mouse.z, mouse.w, 0.0f), - m_activeCamera->viewMatrix(), - m_activeCamera->projectionMatrix()); + Vector3 pos = m_activeCamera->unproject(Vector3(mouse.z, mouse.w, 0.0f)); CameraController::update(); diff --git a/worldeditor/src/screens/scenecomposer/objectcontroller.cpp b/worldeditor/src/screens/scenecomposer/objectcontroller.cpp index 6b265e35e..dabbc3a5b 100644 --- a/worldeditor/src/screens/scenecomposer/objectcontroller.cpp +++ b/worldeditor/src/screens/scenecomposer/objectcontroller.cpp @@ -104,7 +104,7 @@ class ViewportRaycast : public PipelineTask { m_depth->readPixels(int32_t(mousePosition.x), int32_t(mousePosition.y), 1, 1); int pixel = m_depth->getPixel(0, 0, 0); memcpy(&screen.z, &pixel, sizeof(float)); - m_mouseWorld = Camera::unproject(screen, activeCamera->viewMatrix(), activeCamera->projectionMatrix()); + m_mouseWorld = activeCamera->unproject(screen); } else { Ray ray = activeCamera->castRay(screen.x, screen.y); m_mouseWorld = (ray.dir * 10.0f) + ray.pos;