Subversion Repositories gelsvn

Rev

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

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