Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
147 jab 1
#include "gel_glu.h"
2
#include "GLViewController.h"
3
 
4
using namespace std;
5
using namespace CGLA;
6
 
7
namespace Graphics
8
{
9
 
10
	void GLViewController::reset_projection()
11
	{
12
		glMatrixMode(GL_PROJECTION);
13
		glLoadIdentity();
14
		gluPerspective(FOV_DEG, aspect, znear, zfar);
15
		glMatrixMode(GL_MODELVIEW);
16
	}
17
 
18
	GLViewController::GLViewController(int _WINX, int _WINY,
19
									 const CGLA::Vec3f& _centre, float _rad):
20
		FOV_DEG(53),
21
		FOV_RAD((FOV_DEG*M_PI)/180.0f),
22
		WINX(_WINX), WINY(_WINY), 
23
		aspect(WINX/WINY),
24
		centre(_centre), rad(_rad),
25
		button_down(false),
26
		spin(false)
27
	{
28
		float view_dist = rad/sin(FOV_RAD/2.0f);
29
		ball = new QuatTrackBall(centre, view_dist, WINX, WINY);
30
		znear = view_dist - rad;
31
		zfar  = view_dist + rad;
32
 
33
		reset_projection();
34
	}
35
 
36
	void GLViewController::grab_ball(TrackBallAction action, 
37
																	 const CGLA::Vec2i& pos)
38
	{
39
		ball->grab_ball(action,pos);
40
		if(action==ZOOM_ACTION)
41
			set_near_and_far();
42
 
43
		spin = false;
44
		button_down = true;
45
		last_action = action;
46
		old_pos     = pos;
47
	}
48
 
49
	void GLViewController::roll_ball(const CGLA::Vec2i& pos)
50
	{
51
		Vec2f dir = Vec2f(pos-old_pos);
52
		float len = dir.length();
53
		if (len < TINY)
54
			return;
55
		//if ( pos == old_pos )
56
		//	return;
57
 
58
		ball->roll_ball(pos);
59
		if(last_action==ZOOM_ACTION)
60
			set_near_and_far();
61
 
62
		spin = len>=1.1f;
63
		old_pos = pos;	
64
	}
65
 
66
 
67
	void GLViewController::release_ball()
68
	{
69
		ball->release_ball();
70
		if(last_action==ZOOM_ACTION)
71
			set_near_and_far();
72
	}
73
 
74
	bool GLViewController::try_spin()
75
	{
76
		if(spin && !ball->is_grabbed()) 
77
			{
78
				ball->do_spin();
79
				return true;
80
			}
81
		return false;
82
	}
83
 
84
	void GLViewController::set_gl_modelview()
85
	{
86
		ball->set_gl_modelview();
87
	}
88
 
89
 
90
	void GLViewController::reshape(int W, int H)
91
	{
92
		WINX = W;
93
		WINY = H;
94
		aspect = WINX/static_cast<float>(WINY);
95
		glViewport(0,0,WINX,WINY);
96
		reset_projection();
97
		ball->set_screen_window(WINX, WINY);
98
	}	
99
 
100
	void GLViewController::set_near_and_far()
101
	{		
102
		Vec3f eye, centre, up;
103
		ball->get_view_param(eye, centre, up);
104
		float len = (eye-centre).length();
105
		znear = max(0.01f*rad, len-rad);
106
		zfar = len+rad;	
107
		reset_projection();
108
	}
109
 
110
	bool GLViewController::load(std::ifstream& ifs)
111
	{
112
		if(ifs)
113
			{
114
				QuatTrackBall* ball_tmp = ball;
115
				ifs.read(reinterpret_cast<char*>(this),
116
								 sizeof(GLViewController));		
117
				ball = ball_tmp;
118
				ifs.read(reinterpret_cast<char*>(ball),sizeof(QuatTrackBall));		
119
				reset_projection();
120
				ball->set_screen_window(WINX, WINY);
121
				return true;
122
			}
123
		return false;
124
	}
125
	bool GLViewController::save(std::ofstream& ofs) const
126
	{
127
		if(ofs)
128
			{
129
				ofs.write(reinterpret_cast<const char*>(this),
130
									sizeof(GLViewController));
131
				ofs.write(reinterpret_cast<const char*>(ball),sizeof(QuatTrackBall));
132
				return true;
133
			}
134
		return false;
135
 	}
136
 
137
}