Subversion Repositories gelsvn

Rev

Rev 630 | 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
{
633 janba 23
 
147 jab 24
	enum TrackBallAction
633 janba 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
147 jab 38
	{
39
		CGLA::Vec3f centre;
40
		CGLA::Vec2i screen_centre;
633 janba 41
 
147 jab 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;
633 janba 51
 
147 jab 52
		void rotate(const CGLA::Vec2f&);
53
		void pan(const CGLA::Vec2f&);
54
		void zoom(const CGLA::Vec2f&);
633 janba 55
 
147 jab 56
		void calcRotation(const CGLA::Vec2f&);
57
		float projectToSphere(const CGLA::Vec2f&);
58
		CGLA::Vec2f scalePoint(const CGLA::Vec2i&) const;
633 janba 59
 
147 jab 60
		void set_position(const CGLA::Vec2f&);
633 janba 61
 
147 jab 62
	public:
633 janba 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. */
147 jab 68
		QuatTrackBall(const CGLA::Vec3f&, float, unsigned, unsigned);
633 janba 69
 
147 jab 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
		}
633 janba 78
 
147 jab 79
		/// set the centre point of rotation
80
		void set_centre(const CGLA::Vec3f& _centre)
81
		{
82
			centre = _centre;
83
		}
633 janba 84
 
85
		void set_screen_centre(const CGLA::Vec2i& _screen_centre)
147 jab 86
		{
87
			screen_centre[0] = _screen_centre[0];
88
			screen_centre[1] = height - _screen_centre[1];
89
		}
633 janba 90
 
91
		const CGLA::Quatf& get_rotation() const
147 jab 92
		{
93
			return qrot;
94
		}
633 janba 95
 
277 jrf 96
		void set_rotation(const CGLA::Quatf& _qrot)
147 jab 97
		{
98
			qrot = _qrot;
99
		}
633 janba 100
 
147 jab 101
		void set_eye_dist(float _eye_dist)
102
		{
103
			eye_dist = _eye_dist;
104
		}
633 janba 105
 
147 jab 106
		float get_eye_dist() const
107
		{
108
			return eye_dist;
109
		}
633 janba 110
 
111
		/// Call GL to set up viewing.
147 jab 112
		void set_gl_modelview() const;
633 janba 113
 
147 jab 114
		/** Spin. used both to spin while button is pressed and if the ball is just
633 janba 115
         spinning while program is idling. */
147 jab 116
		void do_spin();
633 janba 117
 
147 jab 118
		bool is_spinning() const;
633 janba 119
 
147 jab 120
		/// Zeroes the rotation value - makes everything stop.
121
		void stop_spin();
633 janba 122
 
123
		/// Call this function to start action when mouse button is pressed
147 jab 124
		void grab_ball(TrackBallAction,const CGLA::Vec2i&);
633 janba 125
 
147 jab 126
		/// Call this function to perform action when user drags mouse
127
		void roll_ball(const CGLA::Vec2i&);
633 janba 128
 
147 jab 129
		/// Call this function to stop action when mouse is released.
633 janba 130
		void release_ball()
147 jab 131
		{
132
			current_action = NO_ACTION;
133
		}
633 janba 134
 
147 jab 135
		/// Returns true if the ball is `grabbed' and not released yet.
633 janba 136
		bool is_grabbed() const
147 jab 137
		{
633 janba 138
			if(current_action == NO_ACTION)
147 jab 139
				return false;
140
			return true;
141
		}
633 janba 142
 
147 jab 143
		void get_view_param(CGLA::Vec3f& eye, 
633 janba 144
                            CGLA::Vec3f& _centre, CGLA::Vec3f& up) const;
145
 
147 jab 146
		TrackBallAction get_current_action() 
147
		{
148
			return current_action;
149
		}
633 janba 150
 
147 jab 151
	};
633 janba 152
 
147 jab 153
}
154
 
155
#endif