Subversion Repositories gelsvn

Rev

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

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