Subversion Repositories gelsvn

Rev

Rev 424 | Rev 623 | 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"
9
 
10
using namespace std;
11
using namespace CGLA;
12
 
152 jab 13
namespace GLGraphics
147 jab 14
{
15
 
424 jrf 16
  void GLViewController::reset_projection()
17
  {
18
    glMatrixMode(GL_PROJECTION);
19
    glLoadIdentity();
20
    gluPerspective(FOV_DEG, aspect, znear, zfar);
21
    glMatrixMode(GL_MODELVIEW);
22
  }
147 jab 23
 
424 jrf 24
  GLViewController::GLViewController(int _WINX, int _WINY, const CGLA::Vec3f& centre, float rad)
25
    : FOV_DEG(53),
26
      WINX(_WINX), WINY(_WINY), 
594 jab 27
      aspect(WINX/float(WINY)),
424 jrf 28
      button_down(false),
29
      spin(false),
30
      ball(centre, rad, WINX, WINY)
31
  {
32
    znear = 0.01f*rad;
33
    zfar  = 3*rad;
34
    reset_projection();
35
  }
147 jab 36
 
424 jrf 37
  void GLViewController::grab_ball(TrackBallAction action, const CGLA::Vec2i& pos)
38
  {
39
    ball.grab_ball(action,pos);
40
    if(action==ZOOM_ACTION)
41
      set_near_and_far();
147 jab 42
 
424 jrf 43
    spin = false;
44
    button_down = true;
45
    last_action = action;
46
  }
147 jab 47
 
424 jrf 48
  void GLViewController::roll_ball(const CGLA::Vec2i& pos)
49
  {
50
    static Vec2i old_pos = pos;
51
    Vec2f dir = Vec2f(pos-old_pos);
52
    float len = dir.length();
53
    if (len < TINY)
54
      return;
55
 
56
    ball.roll_ball(pos);
57
    if(last_action==ZOOM_ACTION)
58
      set_near_and_far();
59
 
60
    spin = len>=1.1f;
61
    old_pos = pos;  
62
  }
147 jab 63
 
64
 
424 jrf 65
  void GLViewController::release_ball()
66
  {
67
    ball.release_ball();
68
    if(last_action==ZOOM_ACTION)
69
      set_near_and_far();
70
  }
147 jab 71
 
424 jrf 72
  bool GLViewController::try_spin()
73
  {
74
    if(spin && !ball.is_grabbed()) 
75
    {
76
      ball.do_spin();
77
      return true;
78
    }
79
    return false;
80
  }
81
 
82
  void GLViewController::set_gl_modelview()
83
  {
84
    ball.set_gl_modelview();
85
  }
147 jab 86
 
87
 
424 jrf 88
  void GLViewController::reshape(int W, int H)
89
  {
90
    WINX = W;
91
    WINY = H;
92
    aspect = WINX/static_cast<float>(WINY);
93
    glViewport(0,0,WINX,WINY);
94
    reset_projection();
95
    ball.set_screen_window(WINX, WINY);
96
  }  
147 jab 97
 
424 jrf 98
  void GLViewController::set_near_and_far()
99
  {  
100
    float rad = ball.get_eye_dist();
101
    znear = 0.01f*rad;
102
    zfar = 3*rad;
103
    reset_projection();
104
  }
147 jab 105
 
424 jrf 106
  bool GLViewController::load(std::ifstream& ifs)
107
  {
108
    if(ifs)
109
    {
110
      ifs.read(reinterpret_cast<char*>(this), sizeof(GLViewController));
111
      reset_projection();
112
      ball.set_screen_window(WINX, WINY);
113
      return true;
114
    }
115
    return false;
116
  }
117
  bool GLViewController::save(std::ofstream& ofs) const
118
  {
119
    if(ofs)
120
    {
121
      ofs.write(reinterpret_cast<const char*>(this), sizeof(GLViewController));
122
      return true;
123
    }
124
    return false;
125
   }
147 jab 126
}