667 |
khor |
1 |
/**
|
|
|
2 |
* @file SimpleTrackBall.h
|
|
|
3 |
* @brief Trackball based on azimuth, zenith angles.
|
|
|
4 |
*/
|
|
|
5 |
/* ----------------------------------------------------------------------- *
|
|
|
6 |
* This file is part of GEL, http://www.imm.dtu.dk/GEL
|
|
|
7 |
* Copyright (C) the authors and DTU Informatics
|
|
|
8 |
* For license and list of authors, see ../../doc/intro.pdf
|
|
|
9 |
* ----------------------------------------------------------------------- */
|
|
|
10 |
|
|
|
11 |
#ifndef __GLGRAPHICS_SIMPLETRACKBALL_H__
|
|
|
12 |
#define __GLGRAPHICS_SIMPLETRACKBALL_H__
|
|
|
13 |
|
|
|
14 |
#include "../CGLA/CGLA.h"
|
|
|
15 |
#include "../CGLA/Vec3f.h"
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
namespace GLGraphics
|
|
|
19 |
{
|
|
|
20 |
|
|
|
21 |
/** \brief Simple trackball class.
|
|
|
22 |
|
|
|
23 |
Use it to let the mouse movement control the viewing transformation.
|
|
|
24 |
|
|
|
25 |
Typical usage:
|
|
|
26 |
Construct the trackball as a global variable. Call the roll function
|
|
|
27 |
from the GLUT mouse motion callback. Setup an idle callback which only
|
|
|
28 |
calls glutPostRedisplay and call gl_view from the display function to
|
|
|
29 |
set up the view transform.
|
|
|
30 |
|
|
|
31 |
Deficiencies: This trackball has many shortcomings. For instance
|
|
|
32 |
you cannot pan but only zoom and rotate. Go fix those problems!
|
|
|
33 |
*/
|
|
|
34 |
class SimpleTrackBall
|
|
|
35 |
{
|
|
|
36 |
CGLA::Vec3f center;
|
|
|
37 |
|
|
|
38 |
float r; // Distance to center (origin)
|
|
|
39 |
float theta; // Horizontal angle (azimuth)
|
|
|
40 |
float phi; // vertical angle (zenith)
|
|
|
41 |
|
|
|
42 |
float da; // angle increment
|
|
|
43 |
float dr; // zoom increment
|
|
|
44 |
|
|
|
45 |
bool firsttime; // used by the roll function
|
|
|
46 |
float oldx, oldy;
|
|
|
47 |
|
|
|
48 |
int X,Y,Z;
|
|
|
49 |
|
|
|
50 |
void roll_x(float dx);
|
|
|
51 |
void roll_y(float dy);
|
|
|
52 |
|
|
|
53 |
public:
|
|
|
54 |
|
|
|
55 |
/** Constructor. Call with the distance to the center. */
|
|
|
56 |
SimpleTrackBall(const CGLA::Vec3f& _center, float _r):
|
|
|
57 |
center(_center),
|
|
|
58 |
r(_r), theta(static_cast<float>(M_PI_2)), phi(0.0f),
|
|
|
59 |
da(static_cast<float>(M_PI/100.0)), dr(r/100.0f), firsttime(true),
|
|
|
60 |
X(0), Y(1), Z(2)
|
|
|
61 |
{}
|
|
|
62 |
|
|
|
63 |
/** Callthis to set up OpenGL viewing matrix. It will also
|
|
|
64 |
clear the view matrix. */
|
|
|
65 |
void gl_view() const;
|
|
|
66 |
|
|
|
67 |
void get_view(CGLA::Vec3f& c, CGLA::Vec3f& e, CGLA::Vec3f& u)
|
|
|
68 |
{
|
|
|
69 |
const CGLA::Vec3f up(0,1,0);
|
|
|
70 |
float x = r * sin(theta) * cos(phi);
|
|
|
71 |
float y = r * cos(theta);
|
|
|
72 |
float z = r * sin(theta) * sin(phi);
|
|
|
73 |
CGLA::Vec3f dir(x,y,z);
|
|
|
74 |
CGLA::Vec3f eye = center + CGLA::Vec3f(dir[X], dir[Y], dir[Z]);
|
|
|
75 |
c = center;
|
|
|
76 |
e = eye;
|
|
|
77 |
u = CGLA::Vec3f(up[X], up[Y], up[Z]);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
void up_axis(char up);
|
|
|
81 |
|
|
|
82 |
/** Move away. Typically called from the keyboard callback */
|
|
|
83 |
void farther()
|
|
|
84 |
{
|
|
|
85 |
r += dr;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/** Move closer. Typically called from the keyboard callback */
|
|
|
89 |
void closer()
|
|
|
90 |
{
|
|
|
91 |
r = (std::max)(0.0f, r - dr);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/** Roll ball. Call with the x,y coordinates. This function is typically
|
|
|
95 |
called from GLUT's mouse motion callback. */
|
|
|
96 |
void roll(int x, int y);
|
|
|
97 |
|
|
|
98 |
void set_center(const CGLA::Vec3f& _center)
|
|
|
99 |
{
|
|
|
100 |
center = _center;
|
|
|
101 |
}
|
|
|
102 |
};
|
|
|
103 |
|
|
|
104 |
}
|
|
|
105 |
#endif
|