Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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