Subversion Repositories gelsvn

Rev

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