Subversion Repositories gelsvn

Rev

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

Rev 638 Rev 647
Line 12... Line 12...
12
using namespace CGLA;
12
using namespace CGLA;
13
 
13
 
14
namespace GLGraphics
14
namespace GLGraphics
15
{
15
{
16
    
16
    
17
  void GLViewController::reset_projection()
17
    void GLViewController::reset_projection()
18
  {
18
    {
19
    glMatrixMode(GL_PROJECTION);
19
        glMatrixMode(GL_PROJECTION);
20
    glLoadIdentity();
20
        glLoadIdentity();
21
    gluPerspective(FOV_DEG, aspect, znear, zfar);
21
        gluPerspective(FOV_DEG, aspect, znear, zfar);
22
    glMatrixMode(GL_MODELVIEW);
22
        glMatrixMode(GL_MODELVIEW);
23
  }
23
    }
24
 
24
    
25
  GLViewController::GLViewController()
25
    GLViewController::GLViewController()
26
    : FOV_DEG(53),
26
    : FOV_DEG(53),
27
      WINX(500), WINY(500), 
27
    WINX(500), WINY(500),
28
      aspect(500/float(500)),
28
    aspect(500/float(500)),
29
      button_down(false),
29
    button_down(false),
30
      spin(false),
30
    spin(false),
31
      ball(CGLA::Vec3f(0.0,0.0,0.0), 0.0, 500, 500)
31
    ball(CGLA::Vec3f(0.0,0.0,0.0), 0.0, 500, 500)
32
  {
32
    {
33
    znear = 0.01f*0.0;
33
        znear = 0.01f*0.0;
34
    zfar  = 3*0.0;
34
        zfar  = 3*0.0;
35
    reset_projection();
35
        //    reset_projection();
36
  }
36
    }
37
 
37
    
38
 
38
    
39
  GLViewController::GLViewController(int _WINX, int _WINY, const CGLA::Vec3f& centre, float rad)
39
    GLViewController::GLViewController(int _WINX, int _WINY, const CGLA::Vec3f& centre, float rad)
40
    : FOV_DEG(53),
40
    : FOV_DEG(53),
41
      WINX(_WINX), WINY(_WINY), 
41
    WINX(_WINX), WINY(_WINY),
42
      aspect(WINX/float(WINY)),
42
    aspect(WINX/float(WINY)),
43
      button_down(false),
43
    button_down(false),
44
      spin(false),
44
    spin(false),
45
      ball(centre, rad, WINX, WINY)
45
    ball(centre, rad, WINX, WINY)
46
  {
-
 
47
    znear = 0.01f*rad;
-
 
48
    zfar  = 3*rad;
-
 
49
    reset_projection();
-
 
50
  }
-
 
51
 
-
 
52
  void GLViewController::grab_ball(TrackBallAction action, const CGLA::Vec2i& pos)
-
 
53
  {
-
 
54
    ball.grab_ball(action,pos);
-
 
55
    if(action==ZOOM_ACTION)
-
 
56
      set_near_and_far();
-
 
57
 
-
 
58
    spin = false;
-
 
59
    button_down = true;
-
 
60
    last_action = action;
-
 
61
  }
-
 
62
 
-
 
63
  void GLViewController::roll_ball(const CGLA::Vec2i& pos)
-
 
64
  {
-
 
65
    static Vec2i old_pos = pos;
-
 
66
    Vec2f dir = Vec2f(pos-old_pos);
-
 
67
    float len = dir.length();
-
 
68
    if (len < TINY)
-
 
69
      return;
-
 
70
    
-
 
71
    ball.roll_ball(pos);
-
 
72
    if(last_action==ZOOM_ACTION)
-
 
73
      set_near_and_far();
-
 
74
    
-
 
75
    spin = len>=1.1f;
-
 
76
    old_pos = pos;  
-
 
77
  }
-
 
78
 
-
 
79
 
-
 
80
  void GLViewController::release_ball()
-
 
81
  {
-
 
82
    ball.release_ball();
-
 
83
    if(last_action==ZOOM_ACTION)
-
 
84
      set_near_and_far();
-
 
85
  }
-
 
86
 
-
 
87
  bool GLViewController::try_spin()
-
 
88
  {
-
 
89
    if(spin && !ball.is_grabbed()) 
-
 
90
    {
46
    {
91
      ball.do_spin();
-
 
92
      return true;
-
 
93
    }
-
 
94
    return false;
-
 
95
  }
-
 
96
  
-
 
97
  void GLViewController::set_gl_modelview()
-
 
98
  {
-
 
99
    ball.set_gl_modelview();
-
 
100
  }
-
 
101
 
-
 
102
 
-
 
103
  void GLViewController::reshape(int W, int H)
-
 
104
  {
-
 
105
    WINX = W;
-
 
106
    WINY = H;
-
 
107
    aspect = WINX/static_cast<float>(WINY);
-
 
108
    glViewport(0,0,WINX,WINY);
-
 
109
    reset_projection();
-
 
110
    ball.set_screen_window(WINX, WINY);
-
 
111
  }  
-
 
112
 
-
 
113
  void GLViewController::set_near_and_far()
-
 
114
  {  
-
 
115
    float rad = ball.get_eye_dist();
-
 
116
    znear = 0.01f*rad;
47
        znear = 0.01f*rad;
117
    zfar = 3*rad;
48
        zfar  = 3*rad;
118
    reset_projection();
-
 
119
  }
-
 
120
 
-
 
121
  void GLViewController::set_view_param(const Vec3f& e, const Vec3f& c, const Vec3f& u)
-
 
122
  {
-
 
123
    // native viewing direction is the negative z-axis
-
 
124
    // while right is the x-axis and up is the y-axis
-
 
125
    Vec3f view = c - e;
-
 
126
    float eye_dist = length(view);
-
 
127
    view /= eye_dist;
-
 
128
    Vec3f right = normalize(cross(view, u));
-
 
129
    Vec3f up = cross(right, view);
-
 
130
    Mat3x3f rot(right, up, -view);
-
 
131
    rot = transpose(rot); // since matrix is row-major
-
 
132
 
-
 
133
    // convert the change-of-basis matrix to a quaternion
-
 
134
    Quatf qrot;
-
 
135
    qrot.make_rot(rot);
-
 
136
    set_rotation(qrot);
-
 
137
    set_centre(c);
-
 
138
    set_eye_dist(eye_dist);
-
 
139
  }
-
 
140
 
-
 
141
  bool GLViewController::load(std::ifstream& ifs)
-
 
142
  {
-
 
143
    if(ifs)
-
 
144
    {
-
 
145
      ifs.read(reinterpret_cast<char*>(this), sizeof(GLViewController));
-
 
146
      reset_projection();
49
        // reset_projection();
147
      ball.set_screen_window(WINX, WINY);
-
 
148
      return true;
-
 
149
    }
50
    }
-
 
51
    
-
 
52
    void GLViewController::grab_ball(TrackBallAction action, const CGLA::Vec2i& pos)
-
 
53
    {
-
 
54
        ball.grab_ball(action,pos);
-
 
55
        if(action==ZOOM_ACTION)
-
 
56
            set_near_and_far();
-
 
57
        
-
 
58
        spin = false;
-
 
59
        button_down = true;
-
 
60
        last_action = action;
-
 
61
    }
-
 
62
    
-
 
63
    void GLViewController::roll_ball(const CGLA::Vec2i& pos)
-
 
64
    {
-
 
65
        static Vec2i old_pos = pos;
-
 
66
        Vec2f dir = Vec2f(pos-old_pos);
-
 
67
        float len = dir.length();
-
 
68
        if (len < TINY)
-
 
69
            return;
-
 
70
        
-
 
71
        ball.roll_ball(pos);
-
 
72
        if(last_action==ZOOM_ACTION)
-
 
73
            set_near_and_far();
-
 
74
        
-
 
75
        spin = len>=1.1f;
-
 
76
        old_pos = pos;
-
 
77
    }
-
 
78
    
-
 
79
    
-
 
80
    void GLViewController::release_ball()
-
 
81
    {
-
 
82
        ball.release_ball();
-
 
83
        if(last_action==ZOOM_ACTION)
-
 
84
            set_near_and_far();
-
 
85
    }
-
 
86
    
-
 
87
    bool GLViewController::try_spin()
-
 
88
    {
-
 
89
        if(spin && !ball.is_grabbed())
-
 
90
        {
-
 
91
            ball.do_spin();
-
 
92
            return true;
-
 
93
        }
150
    return false;
94
        return false;
-
 
95
    }
-
 
96
    
-
 
97
    void GLViewController::set_gl_modelview()
-
 
98
    {
-
 
99
        reset_projection();
-
 
100
        ball.set_gl_modelview();
-
 
101
    }
-
 
102
    
-
 
103
    
-
 
104
    void GLViewController::reshape(int W, int H)
-
 
105
    {
-
 
106
        WINX = W;
-
 
107
        WINY = H;
-
 
108
        aspect = WINX/static_cast<float>(WINY);
-
 
109
        glViewport(0,0,WINX,WINY);
-
 
110
        reset_projection();
-
 
111
        ball.set_screen_window(WINX, WINY);
-
 
112
    }
-
 
113
    
-
 
114
    void GLViewController::set_near_and_far()
-
 
115
    {
-
 
116
        float rad = ball.get_eye_dist();
-
 
117
        znear = 0.01f*rad;
-
 
118
        zfar = 3*rad;
-
 
119
        reset_projection();
-
 
120
    }
-
 
121
    
-
 
122
    void GLViewController::set_view_param(const Vec3f& e, const Vec3f& c, const Vec3f& u)
-
 
123
    {
-
 
124
        // native viewing direction is the negative z-axis
-
 
125
        // while right is the x-axis and up is the y-axis
-
 
126
        Vec3f view = c - e;
-
 
127
        float eye_dist = length(view);
-
 
128
        view /= eye_dist;
-
 
129
        Vec3f right = normalize(cross(view, u));
-
 
130
        Vec3f up = cross(right, view);
-
 
131
        Mat3x3f rot(right, up, -view);
-
 
132
        rot = transpose(rot); // since matrix is row-major
-
 
133
        
-
 
134
        // convert the change-of-basis matrix to a quaternion
-
 
135
        Quatf qrot;
-
 
136
        qrot.make_rot(rot);
-
 
137
        set_rotation(qrot);
-
 
138
        set_centre(c);
-
 
139
        set_eye_dist(eye_dist);
151
  }
140
    }
152
 
141
    
153
  bool GLViewController::save(std::ofstream& ofs) const
142
    bool GLViewController::load(std::ifstream& ifs)
154
  {
143
    {
155
    if(ofs)
144
        if(ifs)
-
 
145
        {
-
 
146
            ifs.read(reinterpret_cast<char*>(this), sizeof(GLViewController));
-
 
147
            reset_projection();
-
 
148
            ball.set_screen_window(WINX, WINY);
-
 
149
            return true;
-
 
150
        }
-
 
151
        return false;
-
 
152
    }
-
 
153
    
-
 
154
    bool GLViewController::save(std::ofstream& ofs) const
156
    {
155
    {
-
 
156
        if(ofs)
-
 
157
        {
157
      ofs.write(reinterpret_cast<const char*>(this), sizeof(GLViewController));
158
            ofs.write(reinterpret_cast<const char*>(this), sizeof(GLViewController));
158
      return true;
159
            return true;
-
 
160
        }
-
 
161
        return false;
159
    }
162
    }
160
    return false;
-
 
161
   }
-
 
162
}
163
}