-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
64 lines (53 loc) · 1.35 KB
/
Camera.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "Camera.h"
Camera::Camera(Node *parent) : Node(parent)
{
_aspectRatio = 45;
setName("Camera");
setType("Camera");
}
Camera::~Camera() {
}
QMatrix4x4 &Camera::projection()
{
return _projection;
}
void Camera::update(float elapsed)
{
_projection.setToIdentity();
_projection.perspective(_aspectRatio, (float)_viewportSize.width() / (float)_viewportSize.height(), 0.001, 1000.0);
Node::update(elapsed);
}
void Camera::setAspectRatio(float aspectRatio)
{
_aspectRatio = aspectRatio;
}
float Camera::aspectRatio() const
{
return _aspectRatio;
}
void Camera::setViewportSize(const QSize &viewportSize)
{
_viewportSize = viewportSize;
}
void Camera::setViewportSize(float width, float height)
{
_viewportSize.setWidth(width);
_viewportSize.setHeight(height);
}
const QSize &Camera::viewportSIze() const
{
return _viewportSize;
}
QVector3D Camera::mapPoint(const QPoint screenPos) const
{
float x = screenPos.x(),
y = screenPos.y(),
width = _viewportSize.width(),
height = _viewportSize.height(),
xrel = (x * 2 - width) / width,
yrel = -(y * 2 - height) / height;
// Reverse the projection and return the point in world co-ordinates.
QMatrix4x4 m = _world.inverted() * _projection.inverted();
QMatrix4x4 invm = m;
return invm.map(QVector3D(xrel, yrel, -1.0f));
}