Subversion Repositories gelsvn

Rev

Rev 344 | Rev 394 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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