Subversion Repositories gelsvn

Rev

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

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