Subversion Repositories gelsvn

Rev

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

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