Subversion Repositories gelsvn

Rev

Rev 204 | Rev 299 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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