Subversion Repositories gelsvn

Rev

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

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