Subversion Repositories gelsvn

Rev

Rev 152 | Rev 198 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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