Subversion Repositories gelsvn

Rev

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

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