Subversion Repositories gelsvn

Rev

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

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