src/glcamera.cpp

Thu, 23 Feb 2017 20:18:39 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 23 Feb 2017 20:18:39 +0200
changeset 1159
6ad8cdcd88d9
parent 1151
0eddb5bcf25b
child 1177
8661b9237ed5
permissions
-rw-r--r--

print() is no longer a global function but is tied to HierarchyElement.

1129
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2013 - 2017 Teemu Piippo
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #include "glcamera.h"
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 #include "grid.h"
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 #include "miscallenous.h"
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 * Constructs a fixed camera from parameters.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 */
1130
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
26 GLCamera::GLCamera(QString name, FixedCameraParameters&& bag) :
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
27 m_name {name},
1151
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1140
diff changeset
28 m_rotationMatrix {bag.rotationMatrix},
1130
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
29 m_localX {bag.localX},
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
30 m_localY {bag.localY},
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
31 m_negatedX {bag.negatedX},
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
32 m_negatedY {bag.negatedY},
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
33 m_negatedDepth {bag.negatedZ} {}
1129
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 * Constructs a free camera.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 */
1140
c5791a3aa1ba Removed the GLCamera::FreeToken name
Teemu Piippo <teemu@hecknology.net>
parents: 1134
diff changeset
38 GLCamera::GLCamera(QString name, decltype(FreeCamera)) :
1130
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
39 m_name {name},
1129
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 m_isFree {true} {}
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 * Returns whether or not the given axis is negated on this camera.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 bool GLCamera::isAxisNegated(Axis axis) const
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 switch (axis)
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 case X:
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 return m_negatedX;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 case Y:
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 return m_negatedY;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 case Z:
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 return m_negatedDepth;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 default:
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 return false;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 * Returns the 3D axis that is on the X axis in this camera.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 Axis GLCamera::axisX() const
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 return m_localX;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 * Returns the 3D axis that is on the Y axis in this camera.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 Axis GLCamera::axisY() const
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 return m_localY;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 * Returns the 3D axis that is on the Z axis in this camera (inwards).
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 Axis GLCamera::axisZ() const
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 return static_cast<Axis>(3 - m_localX - m_localY);
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 * This converts a 2D point on the screen to a 3D point in the model. If 'snap' is true, the 3D point will snap to the current grid.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 Vertex GLCamera::convert2dTo3d(const QPoint& position2d, Grid* grid) const
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 if (m_isFree)
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 return {0, 0, 0};
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 else
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 Vertex position3d;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 int signX = m_negatedX ? -1 : 1;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 int signY = m_negatedY ? -1 : 1;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102 // Calculate cx and cy - these are the LDraw unit coords the cursor is at.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103 double cx = -m_virtualSize.width() + (2 * position2d.x() * m_virtualSize.width() / m_size.width()) - m_panningX;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 double cy = m_virtualSize.height() - (2 * position2d.y() * m_virtualSize.height() / m_size.height()) - m_panningY;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 // If a grid was passed, snap coordinates to it.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 if (grid)
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 cx = grid->snap(cx, Grid::Coordinate);
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
110 cy = grid->snap(cy, Grid::Coordinate);
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
111 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 roundToDecimals(cx, 4);
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
114 roundToDecimals(cy, 4);
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
115
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
116 // Create the vertex from the coordinates
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
117 position3d.setCoordinate(axisX(), cx * signX);
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
118 position3d.setCoordinate(axisY(), cy * signY);
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
119 position3d.setCoordinate(axisZ(), m_depth);
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
120 return position3d;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
121 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
122 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
123
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
124 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
125 * Inverse operation for the above - convert a 3D position to a 2D screen position.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
126 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
127 QPoint GLCamera::convert3dTo2d(const Vertex& position3d) const
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
128 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
129 if (m_isFree)
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
130 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
131 return {0, 0};
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
132 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
133 else
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
134 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
135 int signX = m_negatedX ? -1 : 1;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
136 int signY = m_negatedY ? -1 : 1;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
137 int rx = (position3d[axisX()] * signX + m_virtualSize.width() + m_panningX) * m_size.width() / 2 / m_virtualSize.width();
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
138 int ry = (position3d[axisY()] * signY - m_virtualSize.height() + m_panningY) * m_size.height() / 2 / m_virtualSize.height();
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
139 return {rx, -ry};
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
140 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
141 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
142
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
143 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
144 * Resizes the camera when the renderer is resized.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
145 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
146 void GLCamera::rendererResized(int width, int height)
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
147 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
148 m_size = {width, height};
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
149 m_virtualSize = {m_zoom, height * m_zoom / width};
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
150 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
151
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
152 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
153 * Returns the "virtual size" of the camera. Used to zoom in while keeping proportions.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
154 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
155 const QSizeF& GLCamera::virtualSize() const
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
156 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
157 return m_virtualSize;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
158 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
159
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
160 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
161 * Returns the "z depth" of the camera. Since the camera provides 2D editing, this value fills in the value for the
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
162 * third dimension for 3D vertices.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
163 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
164 double GLCamera::depth() const
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
165 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
166 return m_depth;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
167 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
168
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
169 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
170 * Returns the X-panning of this camera.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
171 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
172 double GLCamera::panningX() const
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
173 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
174 return m_panningX;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
175 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
176
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
177 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
178 * Returns the Y-panning of this camera.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
179 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
180 double GLCamera::panningY() const
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
181 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
182 return m_panningY;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
183 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
184
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
185 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
186 * Returns the zoom level of this camera.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
187 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
188 double GLCamera::zoom() const
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
189 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
190 return m_zoom;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
191 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
192
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
193 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
194 * Explicitly sets the panning of this camera.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
195 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
196 void GLCamera::setPanning(double x, double y)
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
197 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
198 m_panningX = x;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
199 m_panningY = y;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
200 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
201
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
202 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
203 * Makes the camera pan by the provided mouse move input.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
204 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
205 void GLCamera::pan(int xMove, int yMove)
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
206 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
207 m_panningX += 0.03f * xMove * zoom() / 7.5f;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
208 m_panningY -= 0.03f * yMove * zoom() / 7.5f;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
209 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
210
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
211 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
212 * Zooms the camera in one notch (e.g. by mousewheel).
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
213 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
214 void GLCamera::zoomNotch (bool inward)
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
215 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
216 m_zoom *= inward ? 0.833f : 1.2f;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
217 m_zoom = qBound(0.01, zoom(), 10000.0);
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
218 rendererResized(m_size.width(), m_size.height());
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
219 }
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
220
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
221 /*
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
222 * Explicitly sets the zoom of this camera.
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
223 */
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
224 void GLCamera::setZoom(double zoom)
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
225 {
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
226 m_zoom = zoom;
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
227 rendererResized(m_size.width(), m_size.height());
2b20261dc511 Exported GLRenderer cameras into a new class, GLCamera.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
228 }
1130
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
229
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
230 /*
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
231 * Returns the name of the camera
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
232 */
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
233 const QString& GLCamera::name() const
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
234 {
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
235 return m_name;
17f92eb2a30c Moved camera names to GLCamera, other adjustments
Teemu Piippo <teemu@hecknology.net>
parents: 1129
diff changeset
236 }
1134
daa9808d626e Moved more stuff to GLCamera
Teemu Piippo <teemu@hecknology.net>
parents: 1130
diff changeset
237
1151
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1140
diff changeset
238 const GLRotationMatrix& GLCamera::transformationMatrix() const
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1140
diff changeset
239 {
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1140
diff changeset
240 return m_rotationMatrix;
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1140
diff changeset
241 }
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1140
diff changeset
242
1134
daa9808d626e Moved more stuff to GLCamera
Teemu Piippo <teemu@hecknology.net>
parents: 1130
diff changeset
243 /*
daa9808d626e Moved more stuff to GLCamera
Teemu Piippo <teemu@hecknology.net>
parents: 1130
diff changeset
244 * Returns the camera's transformation matrix, scaled by the given scale value.
daa9808d626e Moved more stuff to GLCamera
Teemu Piippo <teemu@hecknology.net>
parents: 1130
diff changeset
245 */
1151
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1140
diff changeset
246 GLRotationMatrix GLCamera::transformationMatrix(double scale) const
1134
daa9808d626e Moved more stuff to GLCamera
Teemu Piippo <teemu@hecknology.net>
parents: 1130
diff changeset
247 {
1151
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1140
diff changeset
248 GLRotationMatrix matrix = m_rotationMatrix;
1134
daa9808d626e Moved more stuff to GLCamera
Teemu Piippo <teemu@hecknology.net>
parents: 1130
diff changeset
249
1151
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1140
diff changeset
250 for (int i = 0; i < 4; ++i)
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1140
diff changeset
251 for (int j = 0; j < 4; ++j)
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1140
diff changeset
252 matrix(i, j) *= scale;
1134
daa9808d626e Moved more stuff to GLCamera
Teemu Piippo <teemu@hecknology.net>
parents: 1130
diff changeset
253
1151
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1140
diff changeset
254 return matrix;
1134
daa9808d626e Moved more stuff to GLCamera
Teemu Piippo <teemu@hecknology.net>
parents: 1130
diff changeset
255 }

mercurial