Subversion Repositories gelsvn

Rev

Rev 594 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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