Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

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