Subversion Repositories gelsvn

Rev

Rev 131 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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