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"
39 bj 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);
87 jab 29
		ball = new QuatTrackBall(centre, view_dist, WINX, WINY);
39 bj 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
		ball->roll_ball(pos);
52
		if(last_action==ZOOM_ACTION)
53
			set_near_and_far();
54
		Vec2f dir = Vec2f(pos-old_pos);
55
		spin = dir.length()>=1.1f;
56
		old_pos = pos;	
57
	}
58
 
59
 
60
	void GLViewController::release_ball()
61
	{
62
		ball->release_ball();
63
		if(last_action==ZOOM_ACTION)
64
			set_near_and_far();
65
	}
66
 
67
	bool GLViewController::try_spin()
68
	{
69
		if(spin && !ball->is_grabbed()) 
70
			{
71
				ball->do_spin();
72
				return true;
73
			}
74
		return false;
75
	}
76
 
77
	void GLViewController::set_gl_modelview()
78
	{
79
		ball->set_gl_modelview();
80
	}
81
 
82
 
83
	void GLViewController::reshape(int W, int H)
84
	{
85
		WINX = W;
86
		WINY = H;
87
		aspect = WINX/static_cast<float>(WINY);
88
		glViewport(0,0,WINX,WINY);
89
		reset_projection();
90
		ball->set_screen_window(WINX, WINY);
91
	}	
92
 
93
	void GLViewController::set_near_and_far()
94
	{		
95
		Vec3f eye, centre, up;
96
		ball->get_view_param(eye, centre, up);
97
		float len = (eye-centre).length();
98
		znear = max(0.01f*rad, len-rad);
99
		zfar = len+rad;	
100
		reset_projection();
101
	}
102
 
103
	bool GLViewController::load(std::ifstream& ifs)
104
	{
105
		if(ifs)
106
			{
87 jab 107
				QuatTrackBall* ball_tmp = ball;
39 bj 108
				ifs.read(reinterpret_cast<char*>(this),
109
								 sizeof(GLViewController));		
110
				ball = ball_tmp;
87 jab 111
				ifs.read(reinterpret_cast<char*>(ball),sizeof(QuatTrackBall));		
39 bj 112
				reset_projection();
113
				ball->set_screen_window(WINX, WINY);
114
				return true;
115
			}
116
		return false;
117
	}
118
	bool GLViewController::save(std::ofstream& ofs) const
119
	{
120
		if(ofs)
121
			{
122
				ofs.write(reinterpret_cast<const char*>(this),
123
									sizeof(GLViewController));
87 jab 124
				ofs.write(reinterpret_cast<const char*>(ball),sizeof(QuatTrackBall));
39 bj 125
				return true;
126
			}
127
		return false;
128
 	}
129
 
130
}