Subversion Repositories gelsvn

Rev

Rev 87 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 87 Rev 89
1
#ifndef _TrackBall_
1
#ifndef _TrackBall_
2
#define _TrackBall_
2
#define _TrackBall_
3
 
3
 
4
#include "CGLA/Vec2i.h"
4
#include "CGLA/Vec2i.h"
5
#include "CGLA/Vec2f.h"
5
#include "CGLA/Vec2f.h"
6
#include "CGLA/Vec3f.h"
6
#include "CGLA/Vec3f.h"
7
#include "CGLA/Vec3Hf.h"
7
#include "CGLA/Vec3Hf.h"
8
#include "CGLA/Quaternion.h"
8
#include "CGLA/Quaternion.h"
9
 
9
 
10
namespace Graphics
10
namespace Graphics
11
{
11
{
12
 
12
 
13
	enum TrackBallAction
13
	enum TrackBallAction
14
		{
14
		{
15
			NO_ACTION = 0,
15
			NO_ACTION = 0,
16
			ROTATE_ACTION,
16
			ROTATE_ACTION,
17
			PAN_ACTION,
17
			PAN_ACTION,
18
			ZOOM_ACTION
18
			ZOOM_ACTION
19
		};
19
		};
20
 
20
 
21
	/** This class represents a virtual tracball. 
21
	/** \brief This class represents a virtual tracball. 
-
 
22
 
22
			Use it in GLUT, FLTK or other OpenGL programs to allow the user to 
23
			Use it in GLUT, FLTK or other OpenGL programs to allow the user to 
23
			spin the model being rendered. It needs work to be used with non-GL
24
			spin the model being rendered. It needs work to be used with non-GL
24
			apps since it calls GL API functions. */
25
			apps since it calls GL API functions. */
25
	class QuatTrackBall 
26
	class QuatTrackBall 
26
	{
27
	{
27
		CGLA::Vec3f centre;
28
		CGLA::Vec3f centre;
28
		CGLA::Vec2i screen_centre;
29
		CGLA::Vec2i screen_centre;
29
 
30
 
30
		unsigned width, height;
31
		unsigned width, height;
31
		CGLA::Quaternion	qrot;
32
		CGLA::Quaternion	qrot;
32
		CGLA::Quaternion	qinc;
33
		CGLA::Quaternion	qinc;
33
		CGLA::Vec2f	trans;
34
		CGLA::Vec2f	trans;
34
		CGLA::Vec3f scale;
35
		CGLA::Vec3f scale;
35
		float	ballsize;
36
		float	ballsize;
36
		float eye_dist;
37
		float eye_dist;
37
		CGLA::Vec2f last_pos;
38
		CGLA::Vec2f last_pos;
38
		TrackBallAction current_action;
39
		TrackBallAction current_action;
39
 
40
 
40
		void rotate(const CGLA::Vec2f&);
41
		void rotate(const CGLA::Vec2f&);
41
		void pan(const CGLA::Vec2f&);
42
		void pan(const CGLA::Vec2f&);
42
		void zoom(const CGLA::Vec2f&);
43
		void zoom(const CGLA::Vec2f&);
43
 
44
 
44
		void calcRotation(const CGLA::Vec2f&);
45
		void calcRotation(const CGLA::Vec2f&);
45
		float projectToSphere(const CGLA::Vec2f&);
46
		float projectToSphere(const CGLA::Vec2f&);
46
		CGLA::Vec2f scalePoint(const CGLA::Vec2i&) const;
47
		CGLA::Vec2f scalePoint(const CGLA::Vec2i&) const;
47
 
48
 
48
		void set_position(const CGLA::Vec2f&);
49
		void set_position(const CGLA::Vec2f&);
49
 
50
 
50
	public:
51
	public:
51
 
52
 
52
		/** First constructor argument is the point we look at. 
53
		/** First constructor argument is the point we look at. 
53
				The second argument is the distance to eye point.
54
				The second argument is the distance to eye point.
54
				The third is the scaling factor
55
				The third is the scaling factor
55
				the last two arguments are the window dimensions. */
56
				the last two arguments are the window dimensions. */
56
		QuatTrackBall(const CGLA::Vec3f&, float, unsigned, unsigned);
57
		QuatTrackBall(const CGLA::Vec3f&, float, unsigned, unsigned);
57
 
58
 
58
		/// Set window dimensions.
59
		/// Set window dimensions.
59
		void set_screen_window(unsigned _width, unsigned _height)
60
		void set_screen_window(unsigned _width, unsigned _height)
60
		{
61
		{
61
			width = _width;
62
			width = _width;
62
			height = _height;
63
			height = _height;
63
			screen_centre[0] = static_cast<int>(width/2.0f);
64
			screen_centre[0] = static_cast<int>(width/2.0f);
64
			screen_centre[1] = static_cast<int>(height/2.0f);
65
			screen_centre[1] = static_cast<int>(height/2.0f);
65
		}
66
		}
66
	
67
	
67
		/// set the centre point of rotation
68
		/// set the centre point of rotation
68
		void set_centre(const CGLA::Vec3f& _centre)
69
		void set_centre(const CGLA::Vec3f& _centre)
69
		{
70
		{
70
			centre = _centre;
71
			centre = _centre;
71
		}
72
		}
72
 
73
 
73
		void set_screen_centre(const CGLA::Vec2i& _screen_centre) 
74
		void set_screen_centre(const CGLA::Vec2i& _screen_centre) 
74
		{
75
		{
75
			screen_centre[0] = _screen_centre[0];
76
			screen_centre[0] = _screen_centre[0];
76
			screen_centre[1] = height - _screen_centre[1];
77
			screen_centre[1] = height - _screen_centre[1];
77
		}
78
		}
78
 
79
 
79
		const CGLA::Quaternion& get_rotation() const 
80
		const CGLA::Quaternion& get_rotation() const 
80
		{
81
		{
81
			return qrot;
82
			return qrot;
82
		}
83
		}
83
 
84
 
84
		void set_rotation(const CGLA::Quaternion& _qrot)
85
		void set_rotation(const CGLA::Quaternion& _qrot)
85
		{
86
		{
86
			qrot = _qrot;
87
			qrot = _qrot;
87
		}
88
		}
88
 
89
 
89
		void set_eye_dist(float _eye_dist)
90
		void set_eye_dist(float _eye_dist)
90
		{
91
		{
91
			eye_dist = _eye_dist;
92
			eye_dist = _eye_dist;
92
		}
93
		}
93
 
94
 
94
		float get_eye_dist() const
95
		float get_eye_dist() const
95
		{
96
		{
96
			return eye_dist;
97
			return eye_dist;
97
		}
98
		}
98
 
99
 
99
		/// Call GL to set up viewing. 
100
		/// Call GL to set up viewing. 
100
		void set_gl_modelview() const;
101
		void set_gl_modelview() const;
101
 
102
 
102
		/** Spin. used both to spin while button is pressed and if the ball is just
103
		/** Spin. used both to spin while button is pressed and if the ball is just
103
				spinning while program is idling. */
104
				spinning while program is idling. */
104
		void do_spin();
105
		void do_spin();
105
 
106
 
106
		bool is_spinning() const;
107
		bool is_spinning() const;
107
	
108
	
108
		/// Zeroes the rotation value - makes everything stop.
109
		/// Zeroes the rotation value - makes everything stop.
109
		void stop_spin();
110
		void stop_spin();
110
	
111
	
111
		/// Call this function to start action when mouse button is pressed 
112
		/// Call this function to start action when mouse button is pressed 
112
		void grab_ball(TrackBallAction,const CGLA::Vec2i&);
113
		void grab_ball(TrackBallAction,const CGLA::Vec2i&);
113
 
114
 
114
		/// Call this function to perform action when user drags mouse
115
		/// Call this function to perform action when user drags mouse
115
		void roll_ball(const CGLA::Vec2i&);
116
		void roll_ball(const CGLA::Vec2i&);
116
 
117
 
117
		/// Call this function to stop action when mouse is released.
118
		/// Call this function to stop action when mouse is released.
118
		void release_ball() 
119
		void release_ball() 
119
		{
120
		{
120
			current_action = NO_ACTION;
121
			current_action = NO_ACTION;
121
		}
122
		}
122
 
123
 
123
		/// Returns true if the ball is `grabbed' and not released yet.
124
		/// Returns true if the ball is `grabbed' and not released yet.
124
		bool is_grabbed() const 
125
		bool is_grabbed() const 
125
		{
126
		{
126
			if(current_action == NO_ACTION) 
127
			if(current_action == NO_ACTION) 
127
				return false;
128
				return false;
128
			return true;
129
			return true;
129
		}
130
		}
130
 
131
 
131
		void get_view_param(CGLA::Vec3f& eye, 
132
		void get_view_param(CGLA::Vec3f& eye, 
132
												CGLA::Vec3f& _centre, CGLA::Vec3f& up) const;
133
												CGLA::Vec3f& _centre, CGLA::Vec3f& up) const;
133
 
134
 
134
		TrackBallAction get_current_action() 
135
		TrackBallAction get_current_action() 
135
		{
136
		{
136
			return current_action;
137
			return current_action;
137
		}
138
		}
138
 
139
 
139
	};
140
	};
140
 
141
 
141
}
142
}
142
namespace GFX = Graphics;
143
namespace GFX = Graphics;
143
 
144
 
144
#endif
145
#endif
145
 
146