Subversion Repositories gelsvn

Rev

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

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