Subversion Repositories gelsvn

Rev

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

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