Subversion Repositories gelsvn

Rev

Rev 277 | Rev 392 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 277 Rev 299
Line 30... Line 30...
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
    if(v[0] < 0 || v[0] >= static_cast<int>(width)
-
 
36
       || v[1] < 0 || v[1] >= static_cast<int>(height))
-
 
37
      return;      
-
 
38
 
35
      set_position(scalePoint(v));
39
	set_position(scalePoint(v));
36
      current_action = act;
40
	current_action = act;
37
    }
41
    }
38
    
42
    
39
    void QuatTrackBall::roll_ball(const Vec2i& v)
43
    void QuatTrackBall::roll_ball(const Vec2i& v)
40
    {
44
    {
-
 
45
    if(v[0] < 0 || v[0] >= static_cast<int>(width)
-
 
46
       || v[1] < 0 || v[1] >= static_cast<int>(height))
-
 
47
      return;      
-
 
48
 
41
	Vec2f w = scalePoint(v); 
49
	Vec2f w = scalePoint(v); 
42
	
50
	
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
 
-
 
48
	switch (current_action) 
51
	switch (current_action) 
49
	{
52
	{
50
			case ROTATE_ACTION:
53
			case ROTATE_ACTION:
51
	    rotate(w);
54
	    rotate(w);
52
	    break;
55
	    break;
Line 109... Line 112...
109
	else
112
	else
110
	{
113
	{
111
		// Form two vectors based on input points, find rotation axis
114
		// Form two vectors based on input points, find rotation axis
112
		Vec3f p1 = Vec3f(new_pos[0], new_pos[1], projectToSphere(new_pos));
115
		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));
116
		Vec3f p2 = Vec3f(last_pos[0], last_pos[1], projectToSphere(last_pos));
-
 
117
        qinc.make_rot(normalize(p1), normalize(p2));
-
 
118
/*
-
 
119
		Vec3f q = cross(p1, p2);		// axis of rotation from p1 and p2 
-
 
120
		float L = sqrt(1.0f-dot(q,q) / (dot(p1,p1) * dot(p2,p2)));
114
		
121
		
-
 
122
		q.normalize();				// q' = axis of rotation 
-
 
123
		q *= sqrt((1 - L)/2);	// q' = q' * sin(phi)
-
 
124
		
115
		qinc.make_rot(normalize(p1), normalize(p2));
125
		qinc.set(q[0],q[1],q[2],sqrt((1 + L)/2));
-
 
126
*/
116
	}
127
	}
117
    }
128
    }
118
    
129
    
119
    // Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
130
    // 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.
131
    // if we are away from the center of the sphere.
121
    float QuatTrackBall::projectToSphere(const Vec2f& v) 
132
    float QuatTrackBall::projectToSphere(const Vec2f& v) 
122
    {
133
    {
123
	float d, t, z;
134
#ifndef M_SQRT_2
-
 
135
	  const double M_SQRT_2 = 0.707106781187;
-
 
136
#endif
124
	
137
 
125
	d = v.length();
138
      float d = v.length();
-
 
139
      float t = ballsize*M_SQRT_2;
-
 
140
      float z;
126
	
141
  
127
	// Inside sphere 
142
      // Inside sphere 
128
	if (d < ballsize * 0.70710678118654752440) {   
143
      if(d < ballsize) 
129
	    z = sqrt(ballsize*ballsize - d*d);
144
        z = sqrt(ballsize*ballsize - d*d);
-
 
145
      else if(d < t)
130
	}
146
        z = 0.0;
131
	// On hyperbola 
147
      // On hyperbola 
132
	else {           
148
      else 
133
	    t = ballsize / 1.41421356237309504880;
-
 
134
	    z = t*t / d;
149
        z = t*t/d;
135
	}
-
 
136
	
150
 
137
	return z;
151
      return z;
138
    }
152
    }
139
    
153
    
140
    // Scales integer point to the range [-1, 1]
154
    // Scales integer point to the range [-1, 1]
141
    Vec2f QuatTrackBall::scalePoint(const Vec2i& v) const
155
    Vec2f QuatTrackBall::scalePoint(const Vec2i& v) const
142
    {
156
    {
Line 171... Line 185...
171
    }
185
    }
172
    
186
    
173
    bool QuatTrackBall::is_spinning() const
187
    bool QuatTrackBall::is_spinning() const
174
    {
188
    {
175
	static const Quatf null_quat(0,0,0,1);
189
	static const Quatf null_quat(0,0,0,1);
176
	if(qinc != null_quat)
190
	if(!(qinc == null_quat))
177
	    return true;
191
	    return true;
178
	return false;
192
	return false;
179
    }
193
    }
180
}
194
}