Subversion Repositories gelsvn

Rev

Rev 447 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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