source: research/2008-rubik/colorcube/tb.c @ 3898

Last change on this file since 3898 was 2677, checked in by Sam Hocevar, 15 years ago
  • Test stuff for the Rubik's cube colour reduction.
File size: 2.1 KB
Line 
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 <math.h>
8#include <assert.h>
9
10#include <GL/glut.h>
11
12#include "tb.h"
13#include "trackball.h"
14
15/* globals */
16static GLuint    tb_lasttime;
17
18float curquat[4];
19float lastquat[4];
20int beginx, beginy;
21
22static GLuint    tb_width;
23static GLuint    tb_height;
24
25static GLint     tb_button = -1;
26static GLboolean tb_tracking = GL_FALSE;
27static GLboolean tb_animate = GL_TRUE;
28
29static void
30_tbAnimate(void)
31{
32  add_quats(lastquat, curquat, curquat);
33  glutPostRedisplay();
34}
35
36static void
37_tbStartMotion(int x, int y, int time)
38{
39  assert(tb_button != -1);
40
41  glutIdleFunc(0);
42  tb_tracking = GL_TRUE;
43  tb_lasttime = time;
44  beginx = x;
45  beginy = y;
46}
47
48static void
49_tbStopMotion(unsigned time)
50{
51  assert(tb_button != -1);
52
53  tb_tracking = GL_FALSE;
54
55  if (time == tb_lasttime && tb_animate) {
56    glutIdleFunc(_tbAnimate);
57  } else {
58    if (tb_animate) {
59      glutIdleFunc(0);
60    }
61  }
62}
63
64void
65tbAnimate(GLboolean animate)
66{
67  tb_animate = animate;
68}
69
70void
71tbInit(GLuint button)
72{
73  tb_button = button;
74  trackball(curquat, 0.0, 0.0, 0.0, 0.0);
75}
76
77void
78tbMatrix(void)
79{
80  GLfloat m[4][4];
81
82  assert(tb_button != -1);
83  build_rotmatrix(m, curquat);
84  glMultMatrixf(&m[0][0]);
85}
86
87void
88tbReshape(int width, int height)
89{
90  assert(tb_button != -1);
91
92  tb_width  = width;
93  tb_height = height;
94}
95
96void
97tbMouse(int button, int state, int x, int y)
98{
99  assert(tb_button != -1);
100
101  if (state == GLUT_DOWN && button == tb_button)
102    _tbStartMotion(x, y, glutGet(GLUT_ELAPSED_TIME));
103  else if (state == GLUT_UP && button == tb_button)
104    _tbStopMotion(glutGet(GLUT_ELAPSED_TIME));
105}
106
107void
108tbMotion(int x, int y)
109{
110  if (tb_tracking) {
111    trackball(lastquat,
112      (2.0 * beginx - tb_width) / tb_width,
113      (tb_height - 2.0 * beginy) / tb_height,
114      (2.0 * x - tb_width) / tb_width,
115      (tb_height - 2.0 * y) / tb_height
116      );
117    beginx = x;
118    beginy = y;
119    tb_animate = 1;
120    tb_lasttime = glutGet(GLUT_ELAPSED_TIME);
121    _tbAnimate();
122  }
123}
Note: See TracBrowser for help on using the repository browser.