Subversion Repositories gelsvn

Rev

Rev 23 | Details | Compare with Previous | Last modification | View Log | RSS feed

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