Subversion Repositories gelsvn

Rev

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

Rev 195 Rev 594
-
 
1
/* ----------------------------------------------------------------------- *
-
 
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
-
 
3
 * Copyright (C) the authors and DTU Informatics
-
 
4
 * For license and list of authors, see ../../doc/intro.pdf
-
 
5
 * ----------------------------------------------------------------------- */
-
 
6
 
1
#include "gel_glu.h"
7
#include "gel_glu.h"
2
#include "GLViewController.h"
8
#include "GLViewController.h"
3
#include "SimpleTrackBall.h"
9
#include "SimpleTrackBall.h"
4
 
10
 
5
using namespace std;
11
using namespace std;
6
using namespace CGLA;
12
using namespace CGLA;
7
 
13
 
8
namespace GLGraphics
14
namespace GLGraphics
9
{
15
{
10
 
16
 
11
		void SimpleTrackBall::roll_x(float dx)
17
		void SimpleTrackBall::roll_x(float dx)
12
		{
18
		{
13
				float mouse_sign = ((dx<0)?-1.0f:1.0f);
19
				float mouse_sign = ((dx<0)?-1.0f:1.0f);
14
				phi += mouse_sign * da;
20
				phi += mouse_sign * da;
15
		}
21
		}
16
 
22
 
17
		void SimpleTrackBall::roll_y(float dy)
23
		void SimpleTrackBall::roll_y(float dy)
18
		{
24
		{
19
				float sgn = (dy<0)?1.0f:-1.0f;
25
				float sgn = (dy<0)?1.0f:-1.0f;
20
				theta += sgn * da;
26
				theta += sgn * da;
21
				theta = min(float(M_PI)-0.001f, max(0.0001f, theta));
27
				theta = min(float(M_PI)-0.001f, max(0.0001f, theta));
22
		}
28
		}
23
 
29
 
24
		void SimpleTrackBall::up_axis(char up)
30
		void SimpleTrackBall::up_axis(char up)
25
		{
31
		{
26
				switch(up)
32
				switch(up)
27
				{
33
				{
28
						case 'x':
34
						case 'x':
29
						case 'X':
35
						case 'X':
30
								X=1;
36
								X=1;
31
								Y=2;
37
								Y=2;
32
								Z=0;
38
								Z=0;
33
								break;
39
								break;
34
						case 'y':
40
						case 'y':
35
						case 'Y':
41
						case 'Y':
36
								X=0;
42
								X=0;
37
								Y=1;
43
								Y=1;
38
								Z=2;
44
								Z=2;
39
								break;
45
								break;
40
						case 'z':
46
						case 'z':
41
						case 'Z':
47
						case 'Z':
42
								X=2;
48
								X=2;
43
								Y=0;
49
								Y=0;
44
								Z=1;
50
								Z=1;
45
								break;
51
								break;
46
				}
52
				}
47
				theta = static_cast<float>(M_PI_2);
53
				theta = static_cast<float>(M_PI_2);
48
				phi = 0;
54
				phi = 0;
49
		}
55
		}
50
 
56
 
51
/** Call this to set up OpenGL viewing matrix. It will also 
57
/** Call this to set up OpenGL viewing matrix. It will also 
52
		clear the view matrix. */
58
		clear the view matrix. */
53
		void SimpleTrackBall::gl_view() const
59
		void SimpleTrackBall::gl_view() const
54
		{
60
		{
55
				const Vec3f up(0,1,0);
61
				const Vec3f up(0,1,0);
56
				float x = r * sin(theta) * cos(phi);
62
				float x = r * sin(theta) * cos(phi);
57
				float y = r * cos(theta);
63
				float y = r * cos(theta);
58
				float z = r * sin(theta) * sin(phi);
64
				float z = r * sin(theta) * sin(phi);
59
				Vec3f dir(x,y,z);
65
				Vec3f dir(x,y,z);
60
				Vec3f e = center + Vec3f(dir[X], dir[Y], dir[Z]);
66
				Vec3f e = center + Vec3f(dir[X], dir[Y], dir[Z]);
61
				glLoadIdentity();
67
				glLoadIdentity();
62
				gluLookAt(e[0],e[1],e[2],
68
				gluLookAt(e[0],e[1],e[2],
63
									center[0],center[1],center[2],
69
									center[0],center[1],center[2],
64
									up[X], up[Y], up[Z]);
70
									up[X], up[Y], up[Z]);
65
		}
71
		}
66
 
72
 
67
/** Roll ball. Call with the x,y coordinates. This function is typically
73
/** Roll ball. Call with the x,y coordinates. This function is typically
68
		called from GLUT's mouse motion callback. */
74
		called from GLUT's mouse motion callback. */
69
		void SimpleTrackBall::roll(int x, int y)
75
		void SimpleTrackBall::roll(int x, int y)
70
		{
76
		{
71
				if(firsttime) 
77
				if(firsttime) 
72
						firsttime=false;
78
						firsttime=false;
73
				else
79
				else
74
				{
80
				{
75
						float dx = x - oldx;
81
						float dx = x - oldx;
76
						float dy = y - oldy;
82
						float dy = y - oldy;
77
					
83
					
78
						if(dx*dx>dy*dy)
84
						if(dx*dx>dy*dy)
79
								roll_x(dx);
85
								roll_x(dx);
80
						else
86
						else
81
								roll_y(dy);
87
								roll_y(dy);
82
					
88
					
83
				}
89
				}
84
				oldx = x;
90
				oldx = x;
85
				oldy = y;
91
				oldy = y;
86
		}
92
		}
87
 
93
 
88
}
94
}
89
 
95