Subversion Repositories gelsvn

Rev

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