Subversion Repositories gelsvn

Rev

Rev 125 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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