Subversion Repositories gelsvn

Rev

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

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