Subversion Repositories gelsvn

Rev

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

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