-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrackball.cpp
98 lines (71 loc) · 1.77 KB
/
trackball.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "trackball.h"
#include <cmath>
using namespace glm;
namespace EZGraphics {
/* ---------------------------------------------- */
Trackball::Trackball ( int sx, int sy ) : R(), R0(), R0R(), ismousedown(false)
{
winsize[0] = sx;
winsize[1] = sy;
}
/* ---------------------------------------------- */
void Trackball::resize ( int sx, int sy )
{
winsize[0] = sx;
winsize[1] = sy;
}
/* ---------------------------------------------- */
vec3 Trackball::_ij2xyz ( float i, float j )
{
float x = 2*i/winsize[0]-1;
float y = 1-2*j/winsize[1];
float x2y2 = 1-x*x-y*y;
if (x2y2<0)
{
double l = sqrt(x*x+y*y);
x = x/l;
y = y/l;
x2y2 = 0;
}
float z = sqrt(x2y2);
return vec3(x,y,z);
}
/* ---------------------------------------------- */
void Trackball::mouseDown ( int i, int j )
{
ismousedown = true;
last = _ij2xyz(i+0.013347689,j+0.0347387583); // perturb to ensure no degeneracy
}
/* ---------------------------------------------- */
void Trackball::mouseMove ( int i, int j )
{
vec3 cur = _ij2xyz(i,j);
vec3 axis = cross(last, cur);
R0 = mat3(rotate(mat4(),float(acos(dot(last,cur))/M_PI*180.0),axis));
R0R = R0*R;
}
/* ---------------------------------------------- */
void Trackball::mouseUp ( int i, int j )
{
if (!ismousedown)
return;
ismousedown = false;
vec3 cur = _ij2xyz(i,j);
vec3 axis = cross(last, cur);
R0 = mat3(rotate(mat4(),float(acos(dot(last,cur))/M_PI*180.0),axis));
R = R0*R;
R0 = dmat3();
R0R = R;
}
/* ---------------------------------------------- */
mat3 Trackball::rotation()
{
return R0R;
}
/* ---------------------------------------------- */
bool Trackball::isActive()
{
return ismousedown;
}
/* ---------------------------------------------- */
};