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 __SIMPLETRACKBALL_H__
2
#define __SIMPLETRACKBALL_H__
3
 
4
#include "CGLA/CGLA.h"
5
#include "CGLA/Vec3f.h"
6
 
7
namespace Graphics
8
{
9
 
10
/** Simple trackball class. Use it to let the mouse movement control
11
		the viewing transformation. 
12
 
13
		Typical usage:
14
		Construct the trackball as a global variable. Call the roll function
15
		from the GLUT mouse motion callback. Setup an idle callback which only
16
		calls glutPostRedisplay and call gl_view from the display function to
17
		set up the view transform.
18
 
19
		Deficiencies: This trackball has many shortcomings. For instance
20
		you cannot pan but only zoom and rotate. Go fix those problems!
21
*/
22
		class SimpleTrackBall
23
		{
24
				CGLA::Vec3f center;
25
 
26
				float r;     // Distance to center (origin)
27
				float theta; // Horizontal angle (azimuth)
28
				float phi;   // vertical angle (zenith)
29
 
30
				float da;    // angle increment
31
				float dr;    // zoom increment
32
 
33
				bool firsttime;   // used by the roll function
34
				float oldx, oldy;
35
 
36
				int X,Y,Z;
37
 
38
				void roll_x(float dx);
39
				void roll_y(float dy);
40
 
41
		public:
42
 
43
				/** Constructor. Call with the distance to the center. */
44
				SimpleTrackBall(const CGLA::Vec3f& _center, float _r): 
45
						center(_center),
46
						r(_r), theta(M_PI/2.0f), phi(0.0f), 
47
						da(M_PI/100.0f), dr(r/100.0f), firsttime(true),
48
						X(0), Y(1), Z(2)
49
						{} 
50
 
51
				/** Call this to set up OpenGL viewing matrix. It will also 
52
						clear the view matrix. */
53
				void gl_view() const;
54
 
55
				void get_view(CGLA::Vec3f& c, CGLA::Vec3f& e, CGLA::Vec3f& u)
56
						{
57
								const CGLA::Vec3f up(0,1,0);
58
								float x = r * sin(theta) * cos(phi);
59
								float y = r * cos(theta);
60
								float z = r * sin(theta) * sin(phi);
61
								CGLA::Vec3f dir(x,y,z);
62
								CGLA::Vec3f eye = center + CGLA::Vec3f(dir[X], dir[Y], dir[Z]);
63
								c = center;
64
								e = eye;
65
								u = CGLA::Vec3f(up[X], up[Y], up[Z]);
66
						}
67
 
68
				void up_axis(char up);
69
 
70
				/** Move away. Typically called from the keyboard callback */
71
				void farther()
72
						{
73
								r += dr;
74
						}
75
 
76
				/** Move closer. Typically called from the keyboard callback */
77
				void closer()
78
						{
79
								r = CGLA::s_max(0.0f, r - dr);
80
						}
81
 
82
				/** Roll ball. Call with the x,y coordinates. This function is typically
83
						called from GLUT's mouse motion callback. */
84
				void roll(int x, int y);
85
 
86
				void set_center(const CGLA::Vec3f& _center)
87
						{
88
								center = _center;
89
						}
90
		};
91
 
92
}
93
#endif