summaryrefslogtreecommitdiffhomepage
path: root/SixenseSDK/src/sixense_simple3d/progs/demos/spaceball/spaceball.c
blob: 52da93a7f43fd612589e918337027e834ad1ea73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Spaceball demo
 *
 * Written by John Tsiombikas <nuclear@member.fsf.org>
 * (converted from the libspnav cube example)
 *
 * Use the spaceball to move and rotate the colored cube.
 * Pressing any button will reset the cube at its original location.
 *
 * Press escape or q to exit.
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/freeglut.h>
#include "vmath.h"

#ifndef M_PI
#define M_PI    3.14159265358979323846264338327950
#endif

void draw_cube(void);

/* callbacks */
void disp(void);
void reshape(int x, int y);
void keyb(unsigned char key, int x, int y);
void sbmot(int x, int y, int z);  /* spaceball translation */
void sbrot(int x, int y, int z);  /* spaceball rotation */
void sbbut(int bn, int state);    /* spaceball button */

vec3_t pos = {0, 0, -6};
quat_t rot = {0, 0, 0, 1};

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  glutCreateWindow("spaceball demo");

  glutDisplayFunc(disp);
  glutReshapeFunc(reshape);
  glutKeyboardFunc(keyb);
  glutSpaceballMotionFunc(sbmot);
  glutSpaceballRotateFunc(sbrot);
  glutSpaceballButtonFunc(sbbut);

  glEnable(GL_CULL_FACE);

  glutMainLoop();
  return 0;
}

void disp(void)
{
  mat4_t xform;

  quat_to_mat(xform, rot);

  glClear(GL_COLOR_BUFFER_BIT);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(pos.x, pos.y, pos.z);
  glMultMatrixf((float*)xform);

  draw_cube();

  glutSwapBuffers();
}

void draw_cube(void)
{
  glBegin(GL_QUADS);
  /* face +Z */
  glNormal3f(0, 0, 1);
  glColor3f(1, 0, 0);
  glVertex3f(-1, -1, 1);
  glVertex3f(1, -1, 1);
  glVertex3f(1, 1, 1);
  glVertex3f(-1, 1, 1);
  /* face +X */
  glNormal3f(1, 0, 0);
  glColor3f(0, 1, 0);
  glVertex3f(1, -1, 1);
  glVertex3f(1, -1, -1);
  glVertex3f(1, 1, -1);
  glVertex3f(1, 1, 1);
  /* face -Z */
  glNormal3f(0, 0, -1);
  glColor3f(0, 0, 1);
  glVertex3f(1, -1, -1);
  glVertex3f(-1, -1, -1);
  glVertex3f(-1, 1, -1);
  glVertex3f(1, 1, -1);
  /* face -X */
  glNormal3f(-1, 0, 0);
  glColor3f(1, 1, 0);
  glVertex3f(-1, -1, -1);
  glVertex3f(-1, -1, 1);
  glVertex3f(-1, 1, 1);
  glVertex3f(-1, 1, -1);
  /* face +Y */
  glNormal3f(0, 1, 0);
  glColor3f(0, 1, 1);
  glVertex3f(-1, 1, 1);
  glVertex3f(1, 1, 1);
  glVertex3f(1, 1, -1);
  glVertex3f(-1, 1, -1);
  /* face -Y */
  glNormal3f(0, -1, 0);
  glColor3f(1, 0, 1);
  glVertex3f(-1, -1, -1);
  glVertex3f(1, -1, -1);
  glVertex3f(1, -1, 1);
  glVertex3f(-1, -1, 1);
  glEnd();
}

/* 45deg fov */
#define FOV    (M_PI / 4.0)

void reshape(int x, int y)
{
  float aspect = (float)x / (float)y;
  float halfy = (float)tan(FOV / 2.0);
  float halfx = halfy * aspect;

  glViewport(0, 0, x, y);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum(-halfx, halfx, -halfy, halfy, 1.0, 1000.0);
}

void keyb(unsigned char key, int x, int y)
{
  switch(key) {
  case 'q':
  case 'Q':
  case 27:
    exit(0);

  case ' ':
    /* reset initial view */
    pos = v3_cons(0, 0, -6);
    rot = quat_cons(1, 0, 0, 0);
    glutPostRedisplay();

  default:
    break;
  }
}

void sbmot(int x, int y, int z)
{
  pos.x += x * 0.001f;
  pos.y += y * 0.001f;
  pos.z -= z * 0.001f;
  glutPostRedisplay();
}

void sbrot(int x, int y, int z)
{
  float axis_len = (float)sqrt(x * x + y * y + z * z);
  rot = quat_rotate(rot, axis_len * 0.001f, -x / axis_len, -y / axis_len, z / axis_len);
  glutPostRedisplay();
}

void sbbut(int bn, int state)
{
  if(state == GLUT_DOWN) {
    pos = v3_cons(0, 0, -6);
    rot = quat_cons(1, 0, 0, 0);
    glutPostRedisplay();
  }
}