Subversion Repositories gelsvn

Rev

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

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