1
/**********************************************************************
6
--------------------------------------------------
8
Copyright (c) 1998 Paul Rademacher
10
This program is freely distributable without licensing fees and is
11
provided without guarantee or warrantee expressed or implied. This
12
program is -not- in the public domain.
14
**********************************************************************/
21
/**************************************** Arcball::Arcball() ****/
22
/* Default (void) constructor for Arcball */
24
Arcball::Arcball( void )
32
/**************************************** Arcball::Arcball() ****/
33
/* Takes as argument a mat4 to use instead of the internal rot */
35
Arcball::Arcball( mat4 *mtx )
41
/**************************************** Arcball::Arcball() ****/
42
/* A constructor that accepts the screen center and arcball radius*/
44
Arcball::Arcball( vec2 _center, float _radius )
49
set_params( _center, _radius );
53
/************************************** Arcball::set_params() ****/
55
void Arcball::set_params( vec2 _center, float _radius )
62
/*************************************** Arcball::init() **********/
64
void Arcball::init( void )
66
center.set( 0.0, 0.0 );
68
q_now = quat_identity();
69
*rot_ptr = identity3D();
70
q_increment = quat_identity();
71
rot_increment = identity3D();
72
is_mouse_down = false;
75
zero_increment = true;
79
/*********************************** Arcball::mouse_to_sphere() ****/
81
vec3 Arcball::mouse_to_sphere( vec2 p )
84
vec2 v2 = (p - center) / radius;
85
vec3 v3( v2[0], v2[1], 0.0 );
94
v3[VZ] = sqrt( 1.0 - mag );
97
/* Now we add constraints - X takes precedence over Y */
99
v3 = constrain_vector( v3, vec3( 1.0, 0.0, 0.0 ));
100
} else if ( constraint_y ) {
101
v3 = constrain_vector( v3, vec3( 0.0, 1.0, 0.0 ));
108
/************************************ Arcball::constrain_vector() ****/
110
vec3 Arcball::constrain_vector( vec3 vector, vec3 axis )
112
return (vector-(vector*axis)*axis).normalize();
115
/************************************ Arcball::mouse_down() **********/
117
void Arcball::mouse_down( int x, int y )
119
down_pt.set( (float)x, (float) y );
120
is_mouse_down = true;
122
q_increment = quat_identity();
123
rot_increment = identity3D();
124
zero_increment = true;
128
/************************************ Arcball::mouse_up() **********/
130
void Arcball::mouse_up( void )
132
q_now = q_drag * q_now;
133
is_mouse_down = false;
137
/********************************** Arcball::mouse_motion() **********/
139
void Arcball::mouse_motion( int x, int y, int shift, int ctrl, int alt )
141
/* Set the X constraint if CONTROL key is pressed, Y if ALT key */
142
set_constraints( ctrl != 0, alt != 0 );
144
vec2 new_pt( (float)x, (float) y );
145
vec3 v0 = mouse_to_sphere( down_pt );
146
vec3 v1 = mouse_to_sphere( new_pt );
150
q_drag.set( cross, v0 * v1 );
152
// *rot_ptr = (q_drag * q_now).to_mat4();
153
mat4 temp = q_drag.to_mat4();
154
*rot_ptr = *rot_ptr * temp;
158
/* We keep a copy of the current incremental rotation (= q_drag) */
159
q_increment = q_drag;
160
rot_increment = q_increment.to_mat4();
162
set_constraints( false, false );
164
if ( q_increment.s < .999999 ) {
167
zero_increment = false;
171
zero_increment = true;
176
/********************************** Arcball::mouse_motion() **********/
178
void Arcball::mouse_motion( int x, int y )
180
mouse_motion( x, y, 0, 0, 0 );
184
/***************************** Arcball::set_constraints() **********/
186
void Arcball::set_constraints( Bool _constraint_x, Bool _constraint_y )
188
constraint_x = _constraint_x;
189
constraint_y = _constraint_y;
192
/***************************** Arcball::idle() *********************/
194
void Arcball::idle( void )
196
if ( is_mouse_down ) {
198
zero_increment = true;
201
if ( damp_factor < 1.0 ) {
202
q_increment.scale_angle( 1.0 - damp_factor );
205
rot_increment = q_increment.to_mat4();
207
if ( q_increment.s >= .999999 ) {
209
zero_increment = true;
214
/************************ Arcball::set_damping() *********************/
216
void Arcball::set_damping( float d )