Subversion Repositories gelsvn

Rev

Rev 394 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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