Subversion Repositories gelsvn

Rev

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

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