Subversion Repositories gelsvn

Rev

Rev 392 | Rev 424 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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