Subversion Repositories gelsvn

Rev

Rev 87 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 87 Rev 125
1
#include <iostream>
1
#include <iostream>
2
#include "CGLA/CGLA.h"
2
#include "CGLA/CGLA.h"
3
#include "QuatTrackBall.h"
3
#include "QuatTrackBall.h"
4
 
4
 
5
#ifdef WIN32
5
#ifdef WIN32
6
#include <windows.h>
6
#include <windows.h>
7
#endif
7
#endif
8
 
8
 
-
 
9
#if defined(__APPLE__) && defined(__MACH__)
-
 
10
#include <OpenGL/glu.h>
-
 
11
#else
9
#include <GL/glu.h>
12
#include <GL/glu.h>
-
 
13
#endif
-
 
14
#include "GLViewController.h"
-
 
15
 
10
 
16
 
11
using namespace std;
17
using namespace std;
12
using namespace CGLA;
18
using namespace CGLA;
13
 
19
 
14
namespace Graphics
20
namespace Graphics
15
{
21
{
16
    
22
    
17
    QuatTrackBall::QuatTrackBall(const Vec3f& _centre, 
23
    QuatTrackBall::QuatTrackBall(const Vec3f& _centre, 
18
			 float _eye_dist,
24
			 float _eye_dist,
19
			 unsigned _width, 
25
			 unsigned _width, 
20
			 unsigned _height):
26
			 unsigned _height):
21
	centre(_centre), eye_dist(_eye_dist), scale(0.5*_eye_dist), 
27
	centre(_centre), eye_dist(_eye_dist), scale(0.5*_eye_dist), 
22
	width(_width), height(_height)
28
	width(_width), height(_height)
23
	{
29
	{
24
	// This size should really be based on the distance from the center of
30
	// This size should really be based on the distance from the center of
25
	// rotation to the point on the object underneath the mouse.  That
31
	// rotation to the point on the object underneath the mouse.  That
26
	// point would then track the mouse as closely as possible.  This is a
32
	// point would then track the mouse as closely as possible.  This is a
27
	// simple example, though, so that is left as an exercise.
33
	// simple example, though, so that is left as an exercise.
28
	ballsize = 2.0f;
34
	ballsize = 2.0f;
29
	screen_centre = Vec2i(width/2, height/2);
35
	screen_centre = Vec2i(width/2, height/2);
30
	qrot = Quaternion(0.0, 0.0, 0.0, 1.0);
36
	qrot = Quaternion(0.0, 0.0, 0.0, 1.0);
31
	qinc = Quaternion(0.0, 0.0, 0.0, 1.0);
37
	qinc = Quaternion(0.0, 0.0, 0.0, 1.0);
32
	trans = Vec2f(0.0, 0.0);
38
	trans = Vec2f(0.0, 0.0);
33
    }
39
    }
34
    
40
    
35
    void QuatTrackBall::grab_ball(TrackBallAction act, const Vec2i& v)
41
    void QuatTrackBall::grab_ball(TrackBallAction act, const Vec2i& v)
36
    {
42
    {
37
	set_position(scalePoint(v));
43
	set_position(scalePoint(v));
38
	current_action = act;
44
	current_action = act;
39
    }
45
    }
40
    
46
    
41
    void QuatTrackBall::roll_ball(const Vec2i& v)
47
    void QuatTrackBall::roll_ball(const Vec2i& v)
42
    {
48
    {
43
	Vec2f w = scalePoint(v); 
49
	Vec2f w = scalePoint(v); 
44
	
50
	
45
	switch (current_action) 
51
	switch (current_action) 
46
	{
52
	{
47
			case ROTATE_ACTION:
53
			case ROTATE_ACTION:
48
	    rotate(w);
54
	    rotate(w);
49
	    break;
55
	    break;
50
	    
56
	    
51
			case PAN_ACTION:
57
			case PAN_ACTION:
52
	    pan(w);
58
	    pan(w);
53
	    break;
59
	    break;
54
	    
60
	    
55
			case ZOOM_ACTION:
61
			case ZOOM_ACTION:
56
	    zoom(w);
62
	    zoom(w);
57
	    break;
63
	    break;
58
	}
64
	}
59
	last_pos = w;	
65
	last_pos = w;	
60
    }
66
    }
61
    
67
    
62
    // Call this when the user does a mouse down.  
68
    // Call this when the user does a mouse down.  
63
    // Stop the trackball glide, then remember the mouse
69
    // Stop the trackball glide, then remember the mouse
64
    // down point (for a future rotate, pan or zoom).
70
    // down point (for a future rotate, pan or zoom).
65
    void QuatTrackBall::set_position(const Vec2f& _last_pos) 
71
    void QuatTrackBall::set_position(const Vec2f& _last_pos) 
66
    {
72
    {
67
	stop_spin();
73
	stop_spin();
68
	last_pos = _last_pos;
74
	last_pos = _last_pos;
69
    }
75
    }
70
    
76
    
71
    // Rotationaly spin the trackball by the current increment.
77
    // Rotationaly spin the trackball by the current increment.
72
    // Use this to implement rotational glide.
78
    // Use this to implement rotational glide.
73
    void QuatTrackBall::do_spin() 
79
    void QuatTrackBall::do_spin() 
74
    {
80
    {
75
	qrot = qrot*qinc;
81
	qrot = qrot*qinc;
76
    }
82
    }
77
    
83
    
78
    // Cease any rotational glide by zeroing the increment.
84
    // Cease any rotational glide by zeroing the increment.
79
    void QuatTrackBall::stop_spin() 
85
    void QuatTrackBall::stop_spin() 
80
    {
86
    {
81
	qinc.set(0.0, 0.0, 0.0, 1.0);
87
	qinc.set(0.0, 0.0, 0.0, 1.0);
82
    }
88
    }
83
    
89
    
84
    void QuatTrackBall::rotate(const Vec2f& new_v) 
90
    void QuatTrackBall::rotate(const Vec2f& new_v) 
85
    {
91
    {
86
	calcRotation(new_v);
92
	calcRotation(new_v);
87
	do_spin();	
93
	do_spin();	
88
    }
94
    }
89
    
95
    
90
    void QuatTrackBall::pan(const Vec2f& new_v) 
96
    void QuatTrackBall::pan(const Vec2f& new_v) 
91
    {
97
    {
92
	trans += (new_v - last_pos) * Vec2f(scale[0], scale[1]);
98
	trans += (new_v - last_pos) * Vec2f(scale[0], scale[1]);
93
    }
99
    }
94
    
100
    
95
    void QuatTrackBall::zoom(const Vec2f& new_v) 
101
    void QuatTrackBall::zoom(const Vec2f& new_v) 
96
    {
102
    {
97
	eye_dist += (new_v[1] - last_pos[1]) * scale[2];
103
	eye_dist += (new_v[1] - last_pos[1]) * scale[2];
98
	eye_dist = max(eye_dist, 0.01f);
104
	eye_dist = max(eye_dist, 0.01f);
99
    }
105
    }
100
    
106
    
101
    void QuatTrackBall::calcRotation(const Vec2f& new_pos) 
107
    void QuatTrackBall::calcRotation(const Vec2f& new_pos) 
102
    {
108
    {
103
	// Check for zero rotation
109
	// Check for zero rotation
104
	if (new_pos == last_pos) 
110
	if (new_pos == last_pos) 
105
	    qinc = Quaternion();
111
	    qinc = Quaternion();
106
	else
112
	else
107
	{
113
	{
108
		// Form two vectors based on input points, find rotation axis
114
		// Form two vectors based on input points, find rotation axis
109
		Vec3f p1 = Vec3f(new_pos[0], new_pos[1], projectToSphere(new_pos));
115
		Vec3f p1 = Vec3f(new_pos[0], new_pos[1], projectToSphere(new_pos));
110
		Vec3f p2 = Vec3f(last_pos[0], last_pos[1], projectToSphere(last_pos));
116
		Vec3f p2 = Vec3f(last_pos[0], last_pos[1], projectToSphere(last_pos));
111
		
117
		
112
		Vec3f q = cross(p1, p2);		/* axis of rotation from p1 and p2 */
118
		Vec3f q = cross(p1, p2);		/* axis of rotation from p1 and p2 */
113
		float L = sqrt(1.0f-dot(q,q) / (dot(p1,p1) * dot(p2,p2)));
119
		float L = sqrt(1.0f-dot(q,q) / (dot(p1,p1) * dot(p2,p2)));
114
		
120
		
115
		q.normalize();				/* q' = axis of rotation */
121
		q.normalize();				/* q' = axis of rotation */
116
		q *= sqrt((1 - L)/2);	/* q' = q' * sin(phi) */
122
		q *= sqrt((1 - L)/2);	/* q' = q' * sin(phi) */
117
		
123
		
118
		qinc.set(q[0],q[1],q[2],sqrt((1 + L)/2));
124
		qinc.set(q[0],q[1],q[2],sqrt((1 + L)/2));
119
	    }
125
	    }
120
    }
126
    }
121
    
127
    
122
    // Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
128
    // Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
123
    // if we are away from the center of the sphere.
129
    // if we are away from the center of the sphere.
124
    float QuatTrackBall::projectToSphere(const Vec2f& v) 
130
    float QuatTrackBall::projectToSphere(const Vec2f& v) 
125
    {
131
    {
126
	float d, t, z;
132
	float d, t, z;
127
	
133
	
128
	d = v.length();
134
	d = v.length();
129
	
135
	
130
	// Inside sphere 
136
	// Inside sphere 
131
	if (d < ballsize * 0.70710678118654752440) {   
137
	if (d < ballsize * 0.70710678118654752440) {   
132
	    z = sqrt(ballsize*ballsize - d*d);
138
	    z = sqrt(ballsize*ballsize - d*d);
133
	}
139
	}
134
	// On hyperbola 
140
	// On hyperbola 
135
	else {           
141
	else {           
136
	    t = ballsize / 1.41421356237309504880;
142
	    t = ballsize / 1.41421356237309504880;
137
	    z = t*t / d;
143
	    z = t*t / d;
138
	}
144
	}
139
	
145
	
140
	return z;
146
	return z;
141
    }
147
    }
142
    
148
    
143
    // Scales integer point to the range [-1, 1]
149
    // Scales integer point to the range [-1, 1]
144
    Vec2f QuatTrackBall::scalePoint(const Vec2i& v) const
150
    Vec2f QuatTrackBall::scalePoint(const Vec2i& v) const
145
    {
151
    {
146
	Vec2f w(v[0],height - v[1]);
152
	Vec2f w(v[0],height - v[1]);
147
	w -= Vec2f(screen_centre);
153
	w -= Vec2f(screen_centre);
148
	w /= Vec2f(width,height);
154
	w /= Vec2f(width,height);
149
	w = CGLA::v_min(Vec2f(1.0f), CGLA::v_max(Vec2f(-1), 2*w));
155
	w = CGLA::v_min(Vec2f(1.0f), CGLA::v_max(Vec2f(-1), 2*w));
150
	return w; 
156
	return w; 
151
    }
157
    }
152
    
158
    
153
    void QuatTrackBall::get_view_param(Vec3f& eye, Vec3f& _centre, Vec3f& up) const
159
    void QuatTrackBall::get_view_param(Vec3f& eye, Vec3f& _centre, Vec3f& up) const
154
    {
160
    {
155
	up  = qrot.apply(Vec3f(0,1,0));
161
	up  = qrot.apply(Vec3f(0,1,0));
156
	Vec3f right = qrot.apply(Vec3f(1,0,0));
162
	Vec3f right = qrot.apply(Vec3f(1,0,0));
157
	_centre = centre - up * trans[1] - right * trans[0]; 
163
	_centre = centre - up * trans[1] - right * trans[0]; 
158
	eye = qrot.apply(Vec3f(0,0,1)*eye_dist) + _centre;
164
	eye = qrot.apply(Vec3f(0,0,1)*eye_dist) + _centre;
159
    }
165
    }
160
    
166
    
161
    
167
    
162
    // Modify the current gl matrix by the trackball rotation and translation.
168
    // Modify the current gl matrix by the trackball rotation and translation.
163
    void QuatTrackBall::set_gl_modelview() const
169
    void QuatTrackBall::set_gl_modelview() const
164
    {
170
    {
165
	glMatrixMode(GL_MODELVIEW);
171
	glMatrixMode(GL_MODELVIEW);
166
	glLoadIdentity();
172
	glLoadIdentity();
167
	Vec3f eye;
173
	Vec3f eye;
168
	Vec3f _centre;
174
	Vec3f _centre;
169
	Vec3f up;
175
	Vec3f up;
170
	get_view_param(eye, _centre, up);
176
	get_view_param(eye, _centre, up);
171
	gluLookAt(eye[0], eye[1], eye[2],
177
	gluLookAt(eye[0], eye[1], eye[2],
172
		  _centre[0], _centre[1], _centre[2], 
178
		  _centre[0], _centre[1], _centre[2], 
173
		  up[0],up[1],up[2]);
179
		  up[0],up[1],up[2]);
174
    }
180
    }
175
    
181
    
176
    bool QuatTrackBall::is_spinning() const
182
    bool QuatTrackBall::is_spinning() const
177
    {
183
    {
178
	static const Quaternion null_quat(0,0,0,1);
184
	static const Quaternion null_quat(0,0,0,1);
179
	if(!(qinc == null_quat))
185
	if(!(qinc == null_quat))
180
	    return true;
186
	    return true;
181
	return false;
187
	return false;
182
    }
188
    }
183
}
189
}
184
 
190