Subversion Repositories gelsvn

Rev

Rev 87 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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