7 void reshape(int x, int y);
8 void keypress(unsigned char key, int x, int y);
9 void mouse(int bn, int st, int x, int y);
10 void motion(int x, int y);
11 void sball_motion(int x, int y, int z);
12 void sball_rotate(int rx, int ry, int rz);
13 void sball_button(int bn, int state);
15 static void vcross(float *res, const float *a, const float *b);
16 static void qmul(float *a, const float *b);
17 static void qrotation(float *q, float angle, float x, float y, float z);
18 static void qrotate(float *q, float angle, float x, float y, float z);
19 static void mrotation_quat(float *m, const float *q);
22 float cam_theta, cam_phi = 25, cam_dist = 8;
26 float torus_pos[3], torus_rot[4] = {0, 0, 0, 1};
28 #ifndef GL_FRAMEBUFFER_SRGB
29 #define GL_FRAMEBUFFER_SRGB 0x8db9
32 #ifndef GL_MULTISAMPLE
33 #define GL_MULTISAMPLE 0x809d
36 int main(int argc, char **argv)
38 int i, test_aa = 0, test_srgb = 0;
39 unsigned int mode = GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE;
41 for(i=1; i<argc; i++) {
42 if(strcmp(argv[i], "-ms") == 0) {
44 } else if(strcmp(argv[i], "-srgb") == 0) {
49 if(test_aa) mode |= GLUT_MULTISAMPLE;
50 if(test_srgb) mode |= GLUT_SRGB;
52 glutInit(&argc, argv);
53 glutInitWindowSize(1024, 768);
54 glutInitDisplayMode(mode);
55 glutCreateWindow("miniglut test");
57 glutDisplayFunc(display);
58 glutReshapeFunc(reshape);
59 glutKeyboardFunc(keypress);
61 glutMotionFunc(motion);
62 glutSpaceballMotionFunc(sball_motion);
63 glutSpaceballRotateFunc(sball_rotate);
64 glutSpaceballButtonFunc(sball_button);
66 glEnable(GL_DEPTH_TEST);
67 glEnable(GL_CULL_FACE);
68 glEnable(GL_LIGHTING);
72 glEnable(GL_MULTISAMPLE);
75 glEnable(GL_FRAMEBUFFER_SRGB);
90 float lpos[] = {-1, 2, 3, 0};
91 float sbrot_xform[16];
93 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
95 glMatrixMode(GL_MODELVIEW);
97 glTranslatef(0, 0, -cam_dist);
98 glRotatef(cam_phi, 1, 0, 0);
99 glRotatef(cam_theta, 0, 1, 0);
101 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
105 tm = glutGet(GLUT_ELAPSED_TIME);
106 glRotatef(tm / 10.0f, 1, 0, 0);
107 glRotatef(tm / 10.0f, 0, 1, 0);
109 glutSolidTorus(0.3, 1, 16, 24);
112 glutSolidSphere(0.4, 16, 8);
115 glTranslatef(torus_pos[0] - 2.5, torus_pos[1], torus_pos[2]);
116 mrotation_quat(sbrot_xform, torus_rot);
117 glMultMatrixf(sbrot_xform);
122 glTranslatef(2.5, -1, 0);
123 glRotatef(-90, 1, 0, 0);
124 glutSolidCone(1.1, 2, 16, 2);
129 glVertex3f(-5, -1.3, 5);
130 glVertex3f(5, -1.3, 5);
131 glVertex3f(5, -1.3, -5);
132 glVertex3f(-5, -1.3, -5);
139 void reshape(int x, int y)
141 float vsz, aspect = (float)x / (float)y;
142 glViewport(0, 0, x, y);
143 glMatrixMode(GL_PROJECTION);
145 vsz = 0.4663f * ZNEAR;
146 glFrustum(-aspect * vsz, aspect * vsz, -vsz, vsz, 0.5, 500.0);
149 void keypress(unsigned char key, int x, int y)
152 static int prev_xsz, prev_ysz;
162 glutIdleFunc(anim ? idle : 0);
168 if(!(glutGetModifiers() & GLUT_ACTIVE_ALT)) {
174 prev_xsz = glutGet(GLUT_WINDOW_WIDTH);
175 prev_ysz = glutGet(GLUT_WINDOW_HEIGHT);
178 glutReshapeWindow(prev_xsz, prev_ysz);
184 void mouse(int bn, int st, int x, int y)
186 int bidx = bn - GLUT_LEFT_BUTTON;
187 bnstate[bidx] = st == GLUT_DOWN;
192 void motion(int x, int y)
194 int dx = x - mouse_x;
195 int dy = y - mouse_y;
199 if(!(dx | dy)) return;
202 cam_theta += dx * 0.5;
204 if(cam_phi < -90) cam_phi = -90;
205 if(cam_phi > 90) cam_phi = 90;
209 cam_dist += dy * 0.1;
210 if(cam_dist < 0) cam_dist = 0;
215 void sball_motion(int x, int y, int z)
217 torus_pos[0] += x * 0.001f;
218 torus_pos[1] += y * 0.001f;
219 torus_pos[2] -= z * 0.001f;
223 static float rsqrt(float number)
227 static const float threehalfs = 1.5f;
232 i = 0x5f3759df - (i >> 1);
234 y *= threehalfs - (x2 * y * y);
235 y *= threehalfs - (x2 * y * y);
239 void sball_rotate(int rx, int ry, int rz)
242 float s = (float)rsqrt(rx * rx + ry * ry + rz * rz);
243 qrotate(torus_rot, 0.001f / s, rx * s, ry * s, -rz * s);
248 void sball_button(int bn, int state)
250 if(state == GLUT_DOWN) {
251 torus_pos[0] = torus_pos[1] = torus_pos[2] = 0;
252 torus_rot[0] = torus_rot[1] = torus_rot[2] = 0;
259 static void vcross(float *res, const float *a, const float *b)
261 res[0] = a[1] * b[2] - a[2] * b[1];
262 res[1] = a[2] * b[0] - a[0] * b[2];
263 res[2] = a[0] * b[1] - a[1] * b[0];
266 static void qmul(float *a, const float *b)
271 dot = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
274 x = a[3] * b[0] + b[3] * a[0] + cross[0];
275 y = a[3] * b[1] + b[3] * a[1] + cross[1];
276 z = a[3] * b[2] + b[3] * a[2] + cross[2];
277 a[3] = a[3] * b[3] - dot;
283 void mglut_sincos(float angle, float *sptr, float *cptr);
284 float mglut_tan(float x);
286 static void qrotation(float *q, float angle, float x, float y, float z)
289 mglut_sincos(angle * 0.5f, &sa, &ca);
296 static void qrotate(float *q, float angle, float x, float y, float z)
299 qrotation(qrot, angle, x, y, z);
307 static void mrotation_quat(float *m, const float *q)
309 float xsq2 = 2.0f * q[0] * q[0];
310 float ysq2 = 2.0f * q[1] * q[1];
311 float zsq2 = 2.0f * q[2] * q[2];
312 float sx = 1.0f - ysq2 - zsq2;
313 float sy = 1.0f - xsq2 - zsq2;
314 float sz = 1.0f - xsq2 - ysq2;
316 m[3] = m[7] = m[11] = m[12] = m[13] = m[14] = 0.0f;
320 m[1] = 2.0f * q[0] * q[1] + 2.0f * q[3] * q[2];
321 m[2] = 2.0f * q[2] * q[0] - 2.0f * q[3] * q[1];
322 m[4] = 2.0f * q[0] * q[1] - 2.0f * q[3] * q[2];
324 m[6] = 2.0f * q[1] * q[2] + 2.0f * q[3] * q[0];
325 m[8] = 2.0f * q[2] * q[0] + 2.0f * q[3] * q[1];
326 m[9] = 2.0f * q[1] * q[2] - 2.0f * q[3] * q[0];