6 void reshape(int x, int y);
7 void keypress(unsigned char key, int x, int y);
8 void mouse(int bn, int st, int x, int y);
9 void motion(int x, int y);
10 void sball_motion(int x, int y, int z);
11 void sball_rotate(int rx, int ry, int rz);
12 void sball_button(int bn, int state);
14 static void vcross(float *res, const float *a, const float *b);
15 static void qmul(float *a, const float *b);
16 static void qrotation(float *q, float angle, float x, float y, float z);
17 static void qrotate(float *q, float angle, float x, float y, float z);
18 static void mrotation_quat(float *m, const float *q);
21 float cam_theta, cam_phi = 25, cam_dist = 8;
25 float torus_pos[3], torus_rot[4] = {0, 0, 0, 1};
28 int main(int argc, char **argv)
30 glutInit(&argc, argv);
31 glutInitWindowSize(1024, 768);
32 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
33 glutCreateWindow("miniglut test");
35 glutDisplayFunc(display);
36 glutReshapeFunc(reshape);
37 glutKeyboardFunc(keypress);
39 glutMotionFunc(motion);
40 glutSpaceballMotionFunc(sball_motion);
41 glutSpaceballRotateFunc(sball_rotate);
42 glutSpaceballButtonFunc(sball_button);
44 glEnable(GL_DEPTH_TEST);
45 glEnable(GL_CULL_FACE);
46 glEnable(GL_LIGHTING);
61 float lpos[] = {-1, 2, 3, 0};
62 float sbrot_xform[16];
64 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
66 glMatrixMode(GL_MODELVIEW);
68 glTranslatef(0, 0, -cam_dist);
69 glRotatef(cam_phi, 1, 0, 0);
70 glRotatef(cam_theta, 0, 1, 0);
72 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
76 tm = glutGet(GLUT_ELAPSED_TIME);
77 glRotatef(tm / 10.0f, 1, 0, 0);
78 glRotatef(tm / 10.0f, 0, 1, 0);
80 glutSolidTorus(0.3, 1, 16, 24);
83 glutSolidSphere(0.4, 16, 8);
86 glTranslatef(torus_pos[0] - 2.5, torus_pos[1], torus_pos[2]);
87 mrotation_quat(sbrot_xform, torus_rot);
88 glMultMatrixf(sbrot_xform);
93 glTranslatef(2.5, -1, 0);
94 glRotatef(-90, 1, 0, 0);
95 glutSolidCone(1.1, 2, 16, 2);
100 glVertex3f(-5, -1.3, 5);
101 glVertex3f(5, -1.3, 5);
102 glVertex3f(5, -1.3, -5);
103 glVertex3f(-5, -1.3, -5);
110 void reshape(int x, int y)
112 float vsz, aspect = (float)x / (float)y;
113 glViewport(0, 0, x, y);
114 glMatrixMode(GL_PROJECTION);
116 vsz = 0.4663f * ZNEAR;
117 glFrustum(-aspect * vsz, aspect * vsz, -vsz, vsz, 0.5, 500.0);
120 void keypress(unsigned char key, int x, int y)
123 static int prev_xsz, prev_ysz;
133 glutIdleFunc(anim ? idle : 0);
139 if(!(glutGetModifiers() & GLUT_ACTIVE_ALT)) {
145 prev_xsz = glutGet(GLUT_WINDOW_WIDTH);
146 prev_ysz = glutGet(GLUT_WINDOW_HEIGHT);
149 glutReshapeWindow(prev_xsz, prev_ysz);
155 void mouse(int bn, int st, int x, int y)
157 int bidx = bn - GLUT_LEFT_BUTTON;
158 bnstate[bidx] = st == GLUT_DOWN;
163 void motion(int x, int y)
165 int dx = x - mouse_x;
166 int dy = y - mouse_y;
170 if(!(dx | dy)) return;
173 cam_theta += dx * 0.5;
175 if(cam_phi < -90) cam_phi = -90;
176 if(cam_phi > 90) cam_phi = 90;
180 cam_dist += dy * 0.1;
181 if(cam_dist < 0) cam_dist = 0;
186 void sball_motion(int x, int y, int z)
188 torus_pos[0] += x * 0.001f;
189 torus_pos[1] += y * 0.001f;
190 torus_pos[2] -= z * 0.001f;
194 static float rsqrt(float number)
198 static const float threehalfs = 1.5f;
203 i = 0x5f3759df - (i >> 1);
205 y *= threehalfs - (x2 * y * y);
206 y *= threehalfs - (x2 * y * y);
210 void sball_rotate(int rx, int ry, int rz)
213 float s = (float)rsqrt(rx * rx + ry * ry + rz * rz);
214 qrotate(torus_rot, 0.001f / s, rx * s, ry * s, -rz * s);
219 void sball_button(int bn, int state)
221 if(state == GLUT_DOWN) {
222 torus_pos[0] = torus_pos[1] = torus_pos[2] = 0;
223 torus_rot[0] = torus_rot[1] = torus_rot[2] = 0;
230 static void vcross(float *res, const float *a, const float *b)
232 res[0] = a[1] * b[2] - a[2] * b[1];
233 res[1] = a[2] * b[0] - a[0] * b[2];
234 res[2] = a[0] * b[1] - a[1] * b[0];
237 static void qmul(float *a, const float *b)
242 dot = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
245 x = a[3] * b[0] + b[3] * a[0] + cross[0];
246 y = a[3] * b[1] + b[3] * a[1] + cross[1];
247 z = a[3] * b[2] + b[3] * a[2] + cross[2];
248 a[3] = a[3] * b[3] - dot;
254 void mglut_sincos(float angle, float *sptr, float *cptr);
255 float mglut_tan(float x);
257 static void qrotation(float *q, float angle, float x, float y, float z)
260 mglut_sincos(angle * 0.5f, &sa, &ca);
267 static void qrotate(float *q, float angle, float x, float y, float z)
270 qrotation(qrot, angle, x, y, z);
278 static void mrotation_quat(float *m, const float *q)
280 float xsq2 = 2.0f * q[0] * q[0];
281 float ysq2 = 2.0f * q[1] * q[1];
282 float zsq2 = 2.0f * q[2] * q[2];
283 float sx = 1.0f - ysq2 - zsq2;
284 float sy = 1.0f - xsq2 - zsq2;
285 float sz = 1.0f - xsq2 - ysq2;
287 m[3] = m[7] = m[11] = m[12] = m[13] = m[14] = 0.0f;
291 m[1] = 2.0f * q[0] * q[1] + 2.0f * q[3] * q[2];
292 m[2] = 2.0f * q[2] * q[0] - 2.0f * q[3] * q[1];
293 m[4] = 2.0f * q[0] * q[1] - 2.0f * q[3] * q[2];
295 m[6] = 2.0f * q[1] * q[2] + 2.0f * q[3] * q[0];
296 m[8] = 2.0f * q[2] * q[0] + 2.0f * q[3] * q[1];
297 m[9] = 2.0f * q[1] * q[2] - 2.0f * q[3] * q[0];