Subversion Repositories gelsvn

Rev

Rev 90 | Details | Compare with Previous | Last modification | View Log | RSS feed

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