Subversion Repositories gelsvn

Rev

Rev 147 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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