Subversion Repositories gelsvn

Rev

Rev 630 | Rev 647 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
594 jab 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
6
 
147 jab 7
#include "gel_glu.h"
8
#include "GLViewController.h"
623 jerf 9
#include "../CGLA/Mat3x3f.h"
147 jab 10
 
11
using namespace std;
12
using namespace CGLA;
13
 
152 jab 14
namespace GLGraphics
147 jab 15
{
16
 
424 jrf 17
  void GLViewController::reset_projection()
18
  {
19
    glMatrixMode(GL_PROJECTION);
20
    glLoadIdentity();
21
    gluPerspective(FOV_DEG, aspect, znear, zfar);
22
    glMatrixMode(GL_MODELVIEW);
23
  }
147 jab 24
 
638 khor 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
 
424 jrf 39
  GLViewController::GLViewController(int _WINX, int _WINY, const CGLA::Vec3f& centre, float rad)
40
    : FOV_DEG(53),
41
      WINX(_WINX), WINY(_WINY), 
594 jab 42
      aspect(WINX/float(WINY)),
424 jrf 43
      button_down(false),
44
      spin(false),
45
      ball(centre, rad, WINX, WINY)
46
  {
47
    znear = 0.01f*rad;
48
    zfar  = 3*rad;
49
    reset_projection();
50
  }
147 jab 51
 
424 jrf 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();
147 jab 57
 
424 jrf 58
    spin = false;
59
    button_down = true;
60
    last_action = action;
61
  }
147 jab 62
 
424 jrf 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
  }
147 jab 78
 
79
 
424 jrf 80
  void GLViewController::release_ball()
81
  {
82
    ball.release_ball();
83
    if(last_action==ZOOM_ACTION)
84
      set_near_and_far();
85
  }
147 jab 86
 
424 jrf 87
  bool GLViewController::try_spin()
88
  {
89
    if(spin && !ball.is_grabbed()) 
90
    {
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
  }
147 jab 101
 
102
 
424 jrf 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
  }  
147 jab 112
 
424 jrf 113
  void GLViewController::set_near_and_far()
114
  {  
115
    float rad = ball.get_eye_dist();
116
    znear = 0.01f*rad;
117
    zfar = 3*rad;
118
    reset_projection();
119
  }
147 jab 120
 
623 jerf 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
 
424 jrf 141
  bool GLViewController::load(std::ifstream& ifs)
142
  {
143
    if(ifs)
144
    {
145
      ifs.read(reinterpret_cast<char*>(this), sizeof(GLViewController));
146
      reset_projection();
147
      ball.set_screen_window(WINX, WINY);
148
      return true;
149
    }
150
    return false;
151
  }
623 jerf 152
 
424 jrf 153
  bool GLViewController::save(std::ofstream& ofs) const
154
  {
155
    if(ofs)
156
    {
157
      ofs.write(reinterpret_cast<const char*>(this), sizeof(GLViewController));
158
      return true;
159
    }
160
    return false;
161
   }
147 jab 162
}