Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

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