Subversion Repositories gelsvn

Rev

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

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