| 1 |
/* |
| 2 |
* Simple trackball-like motion adapted (ripped off) from projtex.c |
| 3 |
* (written by David Yu and David Blythe). See the SIGGRAPH '96 |
| 4 |
* Advanced OpenGL course notes. |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "config.h" |
| 8 |
|
| 9 |
#include <math.h> |
| 10 |
#include <assert.h> |
| 11 |
|
| 12 |
#if defined HAVE_GL_GLUT_H |
| 13 |
# include <GL/glut.h> |
| 14 |
#elif defined HAVE_GLUT_GLUT_H |
| 15 |
# include <GLUT/glut.h> |
| 16 |
#else |
| 17 |
# error GLUT headers not present |
| 18 |
#endif |
| 19 |
|
| 20 |
#include "tb.h" |
| 21 |
#include "trackball.h" |
| 22 |
|
| 23 |
/* globals */ |
| 24 |
static GLuint tb_lasttime; |
| 25 |
|
| 26 |
float curquat[4]; |
| 27 |
float lastquat[4]; |
| 28 |
int beginx, beginy; |
| 29 |
|
| 30 |
static GLuint tb_width; |
| 31 |
static GLuint tb_height; |
| 32 |
|
| 33 |
static GLint tb_button = -1; |
| 34 |
static GLboolean tb_tracking = GL_FALSE; |
| 35 |
static GLboolean tb_animate = GL_TRUE; |
| 36 |
|
| 37 |
static void |
| 38 |
_tbAnimate(void) |
| 39 |
{ |
| 40 |
add_quats(lastquat, curquat, curquat); |
| 41 |
glutPostRedisplay(); |
| 42 |
} |
| 43 |
|
| 44 |
static void |
| 45 |
_tbStartMotion(int x, int y, int time) |
| 46 |
{ |
| 47 |
assert(tb_button != -1); |
| 48 |
|
| 49 |
glutIdleFunc(0); |
| 50 |
tb_tracking = GL_TRUE; |
| 51 |
tb_lasttime = time; |
| 52 |
beginx = x; |
| 53 |
beginy = y; |
| 54 |
} |
| 55 |
|
| 56 |
static void |
| 57 |
_tbStopMotion(unsigned time) |
| 58 |
{ |
| 59 |
assert(tb_button != -1); |
| 60 |
|
| 61 |
tb_tracking = GL_FALSE; |
| 62 |
|
| 63 |
if (time == tb_lasttime && tb_animate) { |
| 64 |
glutIdleFunc(_tbAnimate); |
| 65 |
} else { |
| 66 |
if (tb_animate) { |
| 67 |
glutIdleFunc(0); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
void |
| 73 |
tbAnimate(GLboolean animate) |
| 74 |
{ |
| 75 |
tb_animate = animate; |
| 76 |
} |
| 77 |
|
| 78 |
void |
| 79 |
tbInit(GLuint button) |
| 80 |
{ |
| 81 |
tb_button = button; |
| 82 |
trackball(curquat, 0.0, 0.0, 0.0, 0.0); |
| 83 |
} |
| 84 |
|
| 85 |
void |
| 86 |
tbMatrix(void) |
| 87 |
{ |
| 88 |
GLfloat m[4][4]; |
| 89 |
|
| 90 |
assert(tb_button != -1); |
| 91 |
build_rotmatrix(m, curquat); |
| 92 |
glMultMatrixf(&m[0][0]); |
| 93 |
} |
| 94 |
|
| 95 |
void |
| 96 |
tbReshape(int width, int height) |
| 97 |
{ |
| 98 |
assert(tb_button != -1); |
| 99 |
|
| 100 |
tb_width = width; |
| 101 |
tb_height = height; |
| 102 |
} |
| 103 |
|
| 104 |
void |
| 105 |
tbMouse(int button, int state, int x, int y) |
| 106 |
{ |
| 107 |
assert(tb_button != -1); |
| 108 |
|
| 109 |
if (state == GLUT_DOWN && button == tb_button) |
| 110 |
_tbStartMotion(x, y, glutGet(GLUT_ELAPSED_TIME)); |
| 111 |
else if (state == GLUT_UP && button == tb_button) |
| 112 |
_tbStopMotion(glutGet(GLUT_ELAPSED_TIME)); |
| 113 |
} |
| 114 |
|
| 115 |
void |
| 116 |
tbMotion(int x, int y) |
| 117 |
{ |
| 118 |
if (tb_tracking) { |
| 119 |
trackball(lastquat, |
| 120 |
(2.0 * beginx - tb_width) / tb_width, |
| 121 |
(tb_height - 2.0 * beginy) / tb_height, |
| 122 |
(2.0 * x - tb_width) / tb_width, |
| 123 |
(tb_height - 2.0 * y) / tb_height |
| 124 |
); |
| 125 |
beginx = x; |
| 126 |
beginy = y; |
| 127 |
tb_animate = 1; |
| 128 |
tb_lasttime = glutGet(GLUT_ELAPSED_TIME); |
| 129 |
_tbAnimate(); |
| 130 |
} |
| 131 |
} |