Subversion Repositories gelsvn

Rev

Rev 424 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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