3 * Written by John Tsiombikas <nuclear@member.fsf.org>
\r
4 * (converted from the libspnav cube example)
\r
6 * Use the spaceball to move and rotate the colored cube.
\r
7 * Pressing any button will reset the cube at its original location.
\r
9 * Press escape or q to exit.
\r
15 #include <GL/freeglut.h>
\r
19 #define M_PI 3.14159265358979323846264338327950
\r
22 void draw_cube(void);
\r
26 void reshape(int x, int y);
\r
27 void keyb(unsigned char key, int x, int y);
\r
28 void sbmot(int x, int y, int z); /* spaceball translation */
\r
29 void sbrot(int x, int y, int z); /* spaceball rotation */
\r
30 void sbbut(int bn, int state); /* spaceball button */
\r
32 vec3_t pos = {0, 0, -6};
\r
33 quat_t rot = {0, 0, 0, 1};
\r
35 int main(int argc, char **argv)
\r
37 glutInit(&argc, argv);
\r
38 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
\r
39 glutCreateWindow("spaceball demo");
\r
41 glutDisplayFunc(disp);
\r
42 glutReshapeFunc(reshape);
\r
43 glutKeyboardFunc(keyb);
\r
44 glutSpaceballMotionFunc(sbmot);
\r
45 glutSpaceballRotateFunc(sbrot);
\r
46 glutSpaceballButtonFunc(sbbut);
\r
48 glEnable(GL_CULL_FACE);
\r
58 quat_to_mat(xform, rot);
\r
60 glClear(GL_COLOR_BUFFER_BIT);
\r
62 glMatrixMode(GL_MODELVIEW);
\r
64 glTranslatef(pos.x, pos.y, pos.z);
\r
65 glMultMatrixf((float*)xform);
\r
72 void draw_cube(void)
\r
76 glNormal3f(0, 0, 1);
\r
78 glVertex3f(-1, -1, 1);
\r
79 glVertex3f(1, -1, 1);
\r
80 glVertex3f(1, 1, 1);
\r
81 glVertex3f(-1, 1, 1);
\r
83 glNormal3f(1, 0, 0);
\r
85 glVertex3f(1, -1, 1);
\r
86 glVertex3f(1, -1, -1);
\r
87 glVertex3f(1, 1, -1);
\r
88 glVertex3f(1, 1, 1);
\r
90 glNormal3f(0, 0, -1);
\r
92 glVertex3f(1, -1, -1);
\r
93 glVertex3f(-1, -1, -1);
\r
94 glVertex3f(-1, 1, -1);
\r
95 glVertex3f(1, 1, -1);
\r
97 glNormal3f(-1, 0, 0);
\r
99 glVertex3f(-1, -1, -1);
\r
100 glVertex3f(-1, -1, 1);
\r
101 glVertex3f(-1, 1, 1);
\r
102 glVertex3f(-1, 1, -1);
\r
104 glNormal3f(0, 1, 0);
\r
105 glColor3f(0, 1, 1);
\r
106 glVertex3f(-1, 1, 1);
\r
107 glVertex3f(1, 1, 1);
\r
108 glVertex3f(1, 1, -1);
\r
109 glVertex3f(-1, 1, -1);
\r
111 glNormal3f(0, -1, 0);
\r
112 glColor3f(1, 0, 1);
\r
113 glVertex3f(-1, -1, -1);
\r
114 glVertex3f(1, -1, -1);
\r
115 glVertex3f(1, -1, 1);
\r
116 glVertex3f(-1, -1, 1);
\r
121 #define FOV (M_PI / 4.0)
\r
123 void reshape(int x, int y)
\r
125 float aspect = (float)x / (float)y;
\r
126 float halfy = (float)tan(FOV / 2.0);
\r
127 float halfx = halfy * aspect;
\r
129 glViewport(0, 0, x, y);
\r
131 glMatrixMode(GL_PROJECTION);
\r
133 glFrustum(-halfx, halfx, -halfy, halfy, 1.0, 1000.0);
\r
136 void keyb(unsigned char key, int x, int y)
\r
145 /* reset initial view */
\r
146 pos = v3_cons(0, 0, -6);
\r
147 rot = quat_cons(1, 0, 0, 0);
\r
148 glutPostRedisplay();
\r
155 void sbmot(int x, int y, int z)
\r
157 pos.x += x * 0.001f;
\r
158 pos.y += y * 0.001f;
\r
159 pos.z -= z * 0.001f;
\r
160 glutPostRedisplay();
\r
163 void sbrot(int x, int y, int z)
\r
165 float axis_len = (float)sqrt(x * x + y * y + z * z);
\r
166 rot = quat_rotate(rot, axis_len * 0.001f, -x / axis_len, -y / axis_len, z / axis_len);
\r
167 glutPostRedisplay();
\r
170 void sbbut(int bn, int state)
\r
172 if(state == GLUT_DOWN) {
\r
173 pos = v3_cons(0, 0, -6);
\r
174 rot = quat_cons(1, 0, 0, 0);
\r
175 glutPostRedisplay();
\r