Subversion Repositories gelsvn

Rev

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

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