Subversion Repositories gelsvn

Rev

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

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