GLTrackball.cc 5.92 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  OpenFlipper is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  OpenFlipper is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.
// 
//  You should have received a copy of the GNU Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision$
//   $Author$
//   $Date$
//
//=============================================================================




//=============================================================================
//
//  CLASS GLTrackball - IMPLEMENTATION
//
//=============================================================================

//== INCLUDES =================================================================


#include "GLTrackball.hh"


//== NAMESPACES ===============================================================


namespace ACG {


//== IMPLEMENTATION ========================================================== 


GLTrackball::
GLTrackball(GLState& _state) 
  : glstate_(_state), 
    center_(0,0,0) 
{
  for (int i=0; i<10; ++i)
    button_down_[i] = false;
}


//-----------------------------------------------------------------------------


void 
GLTrackball::mouse_press(int button, int x, int y)
{
  last_point_2D_ = ACG::Vec2i(x, y);
  last_point_ok_ = map_to_sphere(last_point_2D_, last_point_3D_);

  button_down_[button] = true;
}


//-----------------------------------------------------------------------------


void 
GLTrackball::mouse_release(int button, int /* x */ , int /* y */ )
{
  last_point_ok_ = false;
  button_down_[button] = false;

  // GLUT: button 3 or 4 -> mouse wheel clicked
  if (button == 3)       
    zoom(0, (int)(last_point_2D_[1] - 0.05*glstate_.viewport_width()));
  else if (button == 4)
    zoom(0, (int)(last_point_2D_[1] + 0.05*glstate_.viewport_width()));
}


//-----------------------------------------------------------------------------


void 
GLTrackball::mouse_move(int x, int y)
{
  if (button_down_[0] && button_down_[1])
    action_ = ZOOM;
  else if (button_down_[0])
    action_ = ROTATION;
  else if (button_down_[1])
    action_ = TRANSLATION;

  switch (action_)
  {
    case ROTATION:    rotation(x, y);     break;
    case TRANSLATION: translation(x, y);  break;
    case ZOOM:        zoom(x, y);         break;
  }

  last_point_2D_ = ACG::Vec2i(x, y);
  last_point_ok_ = map_to_sphere(last_point_2D_, last_point_3D_);
}


//-----------------------------------------------------------------------------


void 
GLTrackball::rotation(int x, int y)
{
  if (last_point_ok_) 
  {
    Vec2i  new_point_2D_;
    Vec3f  new_point_3D_;
    bool   new_point_ok_;

    new_point_2D_ = ACG::Vec2i(x, y);
    new_point_ok_ = map_to_sphere(new_point_2D_, new_point_3D_);
    
    if (new_point_ok_)
    {
      Vec3f axis      = (last_point_3D_ % new_point_3D_);
      float cos_angle = (last_point_3D_ | new_point_3D_);

      if (fabs(cos_angle) < 1.0) 
      {
	float angle = 2.0*acos(cos_angle) * 180.0 / M_PI;

	Vec3f t = glstate_.modelview().transform_point(center_);
	glstate_.translate(-t[0], -t[1], -t[2], MULT_FROM_LEFT);
	glstate_.rotate(angle, axis[0], axis[1], axis[2], MULT_FROM_LEFT);
	glstate_.translate( t[0],  t[1],  t[2], MULT_FROM_LEFT);
      }
    }
  }
}


//-----------------------------------------------------------------------------


void 
GLTrackball::translation(int x, int y)
{
  float dx = x - last_point_2D_[0];
  float dy = y - last_point_2D_[1];

  float z      = glstate_.modelview().transform_point(center_)[2];
  float w      = glstate_.viewport_width();
  float h      = glstate_.viewport_height();
  float fovy   = glstate_.fovy();
  float nearpl = glstate_.near_plane();

  float aspect = w / h;
  float top    = tan(fovy/2.0f*M_PI/180.f) * nearpl;
  float right  = aspect*top;

  glstate_.translate(-2.0*dx/w*right/nearpl*z, 
		     2.0*dy/h*top/nearpl*z, 
		     0.0,
		     MULT_FROM_LEFT);
}


//-----------------------------------------------------------------------------


void 
GLTrackball::zoom(int /* x */ , int y)
{
  float dy = y - last_point_2D_[1];
  float z  = glstate_.modelview().transform_point(center_)[2];
  float h  = glstate_.viewport_height();

  glstate_.translate(0.0,
		     0.0,
		     -2.0*dy/h*z, 
		     MULT_FROM_LEFT);
}


//-----------------------------------------------------------------------------


bool
GLTrackball::map_to_sphere(const Vec2i& _point, Vec3f& _result)
{
  float width  = glstate_.viewport_width();
  float height = glstate_.viewport_height();
  
  if ( (_point[0] >= 0) && (_point[0] <= width) &&
       (_point[1] >= 0) && (_point[1] <= height) ) 
  {
    double x            = (_point[0] - 0.5*width)  / width;
    double y            = (0.5*height - _point[1]) / height;
    double sinx         = sin(M_PI * x * 0.5);
    double siny         = sin(M_PI * y * 0.5);
    double sinx2siny2   = sinx * sinx + siny * siny;
    
    _result[0] = sinx;
    _result[1] = siny;
    _result[2] = sinx2siny2 < 1.0 ? sqrt(1.0 - sinx2siny2) : 0.0;
    
    return true;
  }
  else return false;
}


//=============================================================================
} // namespace ACG
//=============================================================================