Subversion Repositories gelsvn

Rev

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

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