Subversion Repositories gelsvn

Rev

Rev 647 | 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
 
650 janba 7
#include "../GL/glew.h"
147 jab 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
 
647 janba 17
    void GLViewController::reset_projection()
18
    {
19
        glMatrixMode(GL_PROJECTION);
20
        glLoadIdentity();
21
        gluPerspective(FOV_DEG, aspect, znear, zfar);
22
        glMatrixMode(GL_MODELVIEW);
23
    }
24
 
25
    GLViewController::GLViewController()
638 khor 26
    : FOV_DEG(53),
647 janba 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
 
39
    GLViewController::GLViewController(int _WINX, int _WINY, const CGLA::Vec3f& centre, float rad)
424 jrf 40
    : FOV_DEG(53),
647 janba 41
    WINX(_WINX), WINY(_WINY),
42
    aspect(WINX/float(WINY)),
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
    }
424 jrf 51
 
647 janba 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();
57
 
58
        spin = false;
59
        button_down = true;
60
        last_action = action;
61
    }
424 jrf 62
 
647 janba 63
    void GLViewController::roll_ball(const CGLA::Vec2i& pos)
424 jrf 64
    {
647 janba 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;
424 jrf 77
    }
647 janba 78
 
79
 
80
    void GLViewController::release_ball()
424 jrf 81
    {
647 janba 82
        ball.release_ball();
83
        if(last_action==ZOOM_ACTION)
84
            set_near_and_far();
424 jrf 85
    }
647 janba 86
 
87
    bool GLViewController::try_spin()
424 jrf 88
    {
647 janba 89
        if(spin && !ball.is_grabbed())
90
        {
91
            ball.do_spin();
92
            return true;
93
        }
94
        return false;
424 jrf 95
    }
647 janba 96
 
97
    void GLViewController::set_gl_modelview()
98
    {
99
        reset_projection();
100
        ball.set_gl_modelview();
101
    }
102
 
103
 
104
    void GLViewController::reshape(int W, int H)
105
    {
106
        WINX = W;
107
        WINY = H;
108
        aspect = WINX/static_cast<float>(WINY);
109
        glViewport(0,0,WINX,WINY);
110
        reset_projection();
111
        ball.set_screen_window(WINX, WINY);
112
    }
113
 
114
    void GLViewController::set_near_and_far()
115
    {
116
        float rad = ball.get_eye_dist();
117
        znear = 0.01f*rad;
118
        zfar = 3*rad;
119
        reset_projection();
120
    }
121
 
122
    void GLViewController::set_view_param(const Vec3f& e, const Vec3f& c, const Vec3f& u)
123
    {
124
        // native viewing direction is the negative z-axis
125
        // while right is the x-axis and up is the y-axis
126
        Vec3f view = c - e;
127
        float eye_dist = length(view);
128
        view /= eye_dist;
129
        Vec3f right = normalize(cross(view, u));
130
        Vec3f up = cross(right, view);
131
        Mat3x3f rot(right, up, -view);
132
        rot = transpose(rot); // since matrix is row-major
133
 
134
        // convert the change-of-basis matrix to a quaternion
135
        Quatf qrot;
136
        qrot.make_rot(rot);
137
        set_rotation(qrot);
138
        set_centre(c);
139
        set_eye_dist(eye_dist);
140
    }
141
 
142
    bool GLViewController::load(std::ifstream& ifs)
143
    {
144
        if(ifs)
145
        {
146
            ifs.read(reinterpret_cast<char*>(this), sizeof(GLViewController));
147
            reset_projection();
148
            ball.set_screen_window(WINX, WINY);
149
            return true;
150
        }
151
        return false;
152
    }
153
 
154
    bool GLViewController::save(std::ofstream& ofs) const
155
    {
156
        if(ofs)
157
        {
158
            ofs.write(reinterpret_cast<const char*>(this), sizeof(GLViewController));
159
            return true;
160
        }
161
        return false;
162
    }
147 jab 163
}