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
66 /* This is STRICTLY a place-holder until we get this working properly for windows.
\r
67 * I do not pretend that this is equivalent to the non-Windows equivalent. I just
\r
68 * want the thing to build without errors.
\r
70 glMultMatrixf((float*)xform);
\r
72 glMultTransposeMatrixf((float*)xform);
\r
80 void draw_cube(void)
\r
84 glNormal3f(0, 0, 1);
\r
86 glVertex3f(-1, -1, 1);
\r
87 glVertex3f(1, -1, 1);
\r
88 glVertex3f(1, 1, 1);
\r
89 glVertex3f(-1, 1, 1);
\r
91 glNormal3f(1, 0, 0);
\r
93 glVertex3f(1, -1, 1);
\r
94 glVertex3f(1, -1, -1);
\r
95 glVertex3f(1, 1, -1);
\r
96 glVertex3f(1, 1, 1);
\r
98 glNormal3f(0, 0, -1);
\r
100 glVertex3f(1, -1, -1);
\r
101 glVertex3f(-1, -1, -1);
\r
102 glVertex3f(-1, 1, -1);
\r
103 glVertex3f(1, 1, -1);
\r
105 glNormal3f(-1, 0, 0);
\r
106 glColor3f(1, 1, 0);
\r
107 glVertex3f(-1, -1, -1);
\r
108 glVertex3f(-1, -1, 1);
\r
109 glVertex3f(-1, 1, 1);
\r
110 glVertex3f(-1, 1, -1);
\r
112 glNormal3f(0, 1, 0);
\r
113 glColor3f(0, 1, 1);
\r
114 glVertex3f(-1, 1, 1);
\r
115 glVertex3f(1, 1, 1);
\r
116 glVertex3f(1, 1, -1);
\r
117 glVertex3f(-1, 1, -1);
\r
119 glNormal3f(0, -1, 0);
\r
120 glColor3f(1, 0, 1);
\r
121 glVertex3f(-1, -1, -1);
\r
122 glVertex3f(1, -1, -1);
\r
123 glVertex3f(1, -1, 1);
\r
124 glVertex3f(-1, -1, 1);
\r
129 #define FOV (M_PI / 4.0)
\r
131 void reshape(int x, int y)
\r
133 float aspect = (float)x / (float)y;
\r
134 float halfy = (float)tan(FOV / 2.0);
\r
135 float halfx = halfy * aspect;
\r
137 glViewport(0, 0, x, y);
\r
139 glMatrixMode(GL_PROJECTION);
\r
141 glFrustum(-halfx, halfx, -halfy, halfy, 1.0, 1000.0);
\r
144 void keyb(unsigned char key, int x, int y)
\r
153 /* reset initial view */
\r
154 pos = v3_cons(0, 0, -6);
\r
155 rot = quat_cons(1, 0, 0, 0);
\r
156 glutPostRedisplay();
\r
163 void sbmot(int x, int y, int z)
\r
165 pos.x += x * 0.001f;
\r
166 pos.y += y * 0.001f;
\r
167 pos.z -= z * 0.001f;
\r
168 glutPostRedisplay();
\r
171 void sbrot(int x, int y, int z)
\r
173 float axis_len = (float)sqrt(x * x + y * y + z * z);
\r
174 rot = quat_rotate(rot, axis_len * 0.001f, -x / axis_len, -y / axis_len, z / axis_len);
\r
175 glutPostRedisplay();
\r
178 void sbbut(int bn, int state)
\r
180 if(state == GLUT_DOWN) {
\r
181 pos = v3_cons(0, 0, -6);
\r
182 rot = quat_cons(1, 0, 0, 0);
\r
183 glutPostRedisplay();
\r