Subversion Repositories gelsvn

Rev

Rev 594 | Rev 630 | 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
 
424 jrf 25
  GLViewController::GLViewController(int _WINX, int _WINY, const CGLA::Vec3f& centre, float rad)
26
    : FOV_DEG(53),
27
      WINX(_WINX), WINY(_WINY), 
594 jab 28
      aspect(WINX/float(WINY)),
424 jrf 29
      button_down(false),
30
      spin(false),
31
      ball(centre, rad, WINX, WINY)
32
  {
33
    znear = 0.01f*rad;
34
    zfar  = 3*rad;
35
    reset_projection();
36
  }
147 jab 37
 
424 jrf 38
  void GLViewController::grab_ball(TrackBallAction action, const CGLA::Vec2i& pos)
39
  {
40
    ball.grab_ball(action,pos);
41
    if(action==ZOOM_ACTION)
42
      set_near_and_far();
147 jab 43
 
424 jrf 44
    spin = false;
45
    button_down = true;
46
    last_action = action;
47
  }
147 jab 48
 
424 jrf 49
  void GLViewController::roll_ball(const CGLA::Vec2i& pos)
50
  {
51
    static Vec2i old_pos = pos;
52
    Vec2f dir = Vec2f(pos-old_pos);
53
    float len = dir.length();
54
    if (len < TINY)
55
      return;
56
 
57
    ball.roll_ball(pos);
58
    if(last_action==ZOOM_ACTION)
59
      set_near_and_far();
60
 
61
    spin = len>=1.1f;
62
    old_pos = pos;  
63
  }
147 jab 64
 
65
 
424 jrf 66
  void GLViewController::release_ball()
67
  {
68
    ball.release_ball();
69
    if(last_action==ZOOM_ACTION)
70
      set_near_and_far();
71
  }
147 jab 72
 
424 jrf 73
  bool GLViewController::try_spin()
74
  {
75
    if(spin && !ball.is_grabbed()) 
76
    {
77
      ball.do_spin();
78
      return true;
79
    }
80
    return false;
81
  }
82
 
83
  void GLViewController::set_gl_modelview()
84
  {
85
    ball.set_gl_modelview();
86
  }
147 jab 87
 
88
 
424 jrf 89
  void GLViewController::reshape(int W, int H)
90
  {
91
    WINX = W;
92
    WINY = H;
93
    aspect = WINX/static_cast<float>(WINY);
94
    glViewport(0,0,WINX,WINY);
95
    reset_projection();
96
    ball.set_screen_window(WINX, WINY);
97
  }  
147 jab 98
 
424 jrf 99
  void GLViewController::set_near_and_far()
100
  {  
101
    float rad = ball.get_eye_dist();
102
    znear = 0.01f*rad;
103
    zfar = 3*rad;
104
    reset_projection();
105
  }
147 jab 106
 
623 jerf 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
 
424 jrf 127
  bool GLViewController::load(std::ifstream& ifs)
128
  {
129
    if(ifs)
130
    {
131
      ifs.read(reinterpret_cast<char*>(this), sizeof(GLViewController));
132
      reset_projection();
133
      ball.set_screen_window(WINX, WINY);
134
      return true;
135
    }
136
    return false;
137
  }
623 jerf 138
 
424 jrf 139
  bool GLViewController::save(std::ofstream& ofs) const
140
  {
141
    if(ofs)
142
    {
143
      ofs.write(reinterpret_cast<const char*>(this), sizeof(GLViewController));
144
      return true;
145
    }
146
    return false;
147
   }
147 jab 148
}