-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharcball.cpp
More file actions
43 lines (33 loc) · 898 Bytes
/
arcball.cpp
File metadata and controls
43 lines (33 loc) · 898 Bytes
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
#include "arcball.h"
#include <cmath>
// Based on math found in https://graphicsinterface.org/wp-content/uploads/gi1992-18.pdf
ArcBall::ArcBall()
{
}
void ArcBall::startMotion(float x, float y) {
startPos = mapToSphere(x, y);
}
void ArcBall::updatePos(float x, float y) {
QVector3D currentPos = mapToSphere(x, y);
QVector3D quat_xyz = QVector3D::crossProduct(startPos, currentPos);
float quat_w = QVector3D::dotProduct(startPos, currentPos);
rotation = QQuaternion(QVector4D(quat_xyz, quat_w));
}
QQuaternion ArcBall::getRotation()
{
return rotation;
}
void ArcBall::reset()
{
rotation = QQuaternion();
}
QVector3D ArcBall::mapToSphere(float x, float y) {
QVector3D result(x, y, 0.0f);
float mag = result.lengthSquared();
if (mag > 1.0f) {
result /= sqrt(mag);
} else {
result.setZ(sqrt(1.0f - mag));
}
return result;
}