Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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