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
16 changes: 10 additions & 6 deletions engine/includes/components/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<Vector3, 8> frustumCorners(const Camera &camera);
static array<Vector3, 8> frustumCorners(bool ortho, float sigma, float ratio, const Vector3 &position, const Quaternion &rotation, float nearPlane, float farPlane);
Expand All @@ -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;

Expand All @@ -89,7 +93,7 @@ class ENGINE_EXPORT Camera : public Component {

float m_orthoSize;

Vector4 m_color;
bool m_ortho;

};

Expand Down
3 changes: 0 additions & 3 deletions engine/includes/editor/viewport/handletools.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

};
Expand Down
55 changes: 33 additions & 22 deletions engine/src/components/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

}

Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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).
Expand All @@ -160,6 +158,7 @@ float Camera::ratio() const {
*/
void Camera::setRatio(float ratio) {
m_ratio = ratio;
recalcProjection();
}
/*!
Returns a focal distance for the camera.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -208,6 +209,7 @@ bool Camera::orthographic() const {
*/
void Camera::setOrthographic(const bool mode) {
m_ortho = mode;
recalcProjection();
}
/*!
Returns current active camera.
Expand Down Expand Up @@ -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);
}
}
6 changes: 0 additions & 6 deletions engine/src/editor/editortool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
26 changes: 8 additions & 18 deletions engine/src/editor/viewport/handletools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,26 @@

#include <float.h>

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);
}
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion engine/src/resources/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void Font::requestCharacters(const string &characters) {

if(isNew) {
packSheets(1);
notifyCurrentState();
//notifyCurrentState();
}
}
/*!
Expand Down
4 changes: 1 addition & 3 deletions modules/editor/grapheditor/editor/graph/graphcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down
8 changes: 1 addition & 7 deletions modules/editor/texturetools/editor/spritecontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down
10 changes: 1 addition & 9 deletions modules/uikit/src/editor/widgetcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,6 @@ void WidgetController::onSelectActor(const QList<Object *> &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;
Expand All @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion worldeditor/src/screens/scenecomposer/objectcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down