fixed win32 srgb/multisample context creation
[miniglut] / test.c
1 #include <string.h>
2 #include <math.h>
3 #include "miniglut.h"
4
5 void idle(void);
6 void display(void);
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);
14
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);
20
21
22 float cam_theta, cam_phi = 25, cam_dist = 8;
23 int mouse_x, mouse_y;
24 int bnstate[8];
25 int anim;
26 float torus_pos[3], torus_rot[4] = {0, 0, 0, 1};
27
28 #ifndef GL_FRAMEBUFFER_SRGB
29 #define GL_FRAMEBUFFER_SRGB     0x8db9
30 #endif
31
32 #ifndef GL_MULTISAMPLE
33 #define GL_MULTISAMPLE 0x809d
34 #endif
35
36 int main(int argc, char **argv)
37 {
38         int i, test_aa = 0, test_srgb = 0;
39         unsigned int mode = GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE;
40
41         for(i=1; i<argc; i++) {
42                 if(strcmp(argv[i], "-ms") == 0) {
43                         test_aa = 1;
44                 } else if(strcmp(argv[i], "-srgb") == 0) {
45                         test_srgb = 1;
46                 }
47         }
48
49         if(test_aa) mode |= GLUT_MULTISAMPLE;
50         if(test_srgb) mode |= GLUT_SRGB;
51
52         glutInit(&argc, argv);
53         glutInitWindowSize(1024, 768);
54         glutInitDisplayMode(mode);
55         glutCreateWindow("miniglut test");
56
57         glutDisplayFunc(display);
58         glutReshapeFunc(reshape);
59         glutKeyboardFunc(keypress);
60         glutMouseFunc(mouse);
61         glutMotionFunc(motion);
62         glutSpaceballMotionFunc(sball_motion);
63         glutSpaceballRotateFunc(sball_rotate);
64         glutSpaceballButtonFunc(sball_button);
65
66         glEnable(GL_DEPTH_TEST);
67         glEnable(GL_CULL_FACE);
68         glEnable(GL_LIGHTING);
69         glEnable(GL_LIGHT0);
70
71         if(test_aa) {
72                 glEnable(GL_MULTISAMPLE);
73         }
74         if(test_srgb) {
75                 glEnable(GL_FRAMEBUFFER_SRGB);
76         }
77
78         glutMainLoop();
79         return 0;
80 }
81
82 void idle(void)
83 {
84         glutPostRedisplay();
85 }
86
87 void display(void)
88 {
89         long tm;
90         float lpos[] = {-1, 2, 3, 0};
91         float sbrot_xform[16];
92
93         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
94
95         glMatrixMode(GL_MODELVIEW);
96         glLoadIdentity();
97         glTranslatef(0, 0, -cam_dist);
98         glRotatef(cam_phi, 1, 0, 0);
99         glRotatef(cam_theta, 0, 1, 0);
100
101         glLightfv(GL_LIGHT0, GL_POSITION, lpos);
102
103         glPushMatrix();
104         if(anim) {
105                 tm = glutGet(GLUT_ELAPSED_TIME);
106                 glRotatef(tm / 10.0f, 1, 0, 0);
107                 glRotatef(tm / 10.0f, 0, 1, 0);
108         }
109         glutSolidTorus(0.3, 1, 16, 24);
110         glPopMatrix();
111
112         glutSolidSphere(0.4, 16, 8);
113
114         glPushMatrix();
115         glTranslatef(torus_pos[0] - 2.5, torus_pos[1], torus_pos[2]);
116         mrotation_quat(sbrot_xform, torus_rot);
117         glMultMatrixf(sbrot_xform);
118         glutSolidCube(1.5);
119         glPopMatrix();
120
121         glPushMatrix();
122         glTranslatef(2.5, -1, 0);
123         glRotatef(-90, 1, 0, 0);
124         glutSolidCone(1.1, 2, 16, 2);
125         glPopMatrix();
126
127         glBegin(GL_QUADS);
128         glNormal3f(0, 1, 0);
129         glVertex3f(-5, -1.3, 5);
130         glVertex3f(5, -1.3, 5);
131         glVertex3f(5, -1.3, -5);
132         glVertex3f(-5, -1.3, -5);
133         glEnd();
134
135         glutSwapBuffers();
136 }
137
138 #define ZNEAR   0.5f
139 void reshape(int x, int y)
140 {
141         float vsz, aspect = (float)x / (float)y;
142         glViewport(0, 0, x, y);
143         glMatrixMode(GL_PROJECTION);
144         glLoadIdentity();
145         vsz = 0.4663f * ZNEAR;
146         glFrustum(-aspect * vsz, aspect * vsz, -vsz, vsz, 0.5, 500.0);
147 }
148
149 void keypress(unsigned char key, int x, int y)
150 {
151         static int fullscr;
152         static int prev_xsz, prev_ysz;
153
154         switch(key) {
155         case 27:
156         case 'q':
157                 glutExit();
158                 break;
159
160         case ' ':
161                 anim ^= 1;
162                 glutIdleFunc(anim ? idle : 0);
163                 glutPostRedisplay();
164                 break;
165
166         case '\n':
167         case '\r':
168                 if(!(glutGetModifiers() & GLUT_ACTIVE_ALT)) {
169                         break;
170                 }
171         case 'f':
172                 fullscr ^= 1;
173                 if(fullscr) {
174                         prev_xsz = glutGet(GLUT_WINDOW_WIDTH);
175                         prev_ysz = glutGet(GLUT_WINDOW_HEIGHT);
176                         glutFullScreen();
177                 } else {
178                         glutReshapeWindow(prev_xsz, prev_ysz);
179                 }
180                 break;
181         }
182 }
183
184 void mouse(int bn, int st, int x, int y)
185 {
186         int bidx = bn - GLUT_LEFT_BUTTON;
187         bnstate[bidx] = st == GLUT_DOWN;
188         mouse_x = x;
189         mouse_y = y;
190 }
191
192 void motion(int x, int y)
193 {
194         int dx = x - mouse_x;
195         int dy = y - mouse_y;
196         mouse_x = x;
197         mouse_y = y;
198
199         if(!(dx | dy)) return;
200
201         if(bnstate[0]) {
202                 cam_theta += dx * 0.5;
203                 cam_phi += dy * 0.5;
204                 if(cam_phi < -90) cam_phi = -90;
205                 if(cam_phi > 90) cam_phi = 90;
206                 glutPostRedisplay();
207         }
208         if(bnstate[2]) {
209                 cam_dist += dy * 0.1;
210                 if(cam_dist < 0) cam_dist = 0;
211                 glutPostRedisplay();
212         }
213 }
214
215 void sball_motion(int x, int y, int z)
216 {
217         torus_pos[0] += x * 0.001f;
218         torus_pos[1] += y * 0.001f;
219         torus_pos[2] -= z * 0.001f;
220         glutPostRedisplay();
221 }
222
223 static float rsqrt(float number)
224 {
225         int i;
226         float x2, y;
227         static const float threehalfs = 1.5f;
228
229         x2 = number * 0.5f;
230         y = number;
231         i = *(int*)&y;
232         i = 0x5f3759df - (i >> 1);
233         y = *(float*)&i;
234         y *= threehalfs - (x2 * y * y);
235         y *= threehalfs - (x2 * y * y);
236         return y;
237 }
238
239 void sball_rotate(int rx, int ry, int rz)
240 {
241         if(rx | ry | 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);
244                 glutPostRedisplay();
245         }
246 }
247
248 void sball_button(int bn, int state)
249 {
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;
253                 torus_rot[3] = 1;
254                 glutPostRedisplay();
255         }
256 }
257
258
259 static void vcross(float *res, const float *a, const float *b)
260 {
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];
264 }
265
266 static void qmul(float *a, const float *b)
267 {
268         float x, y, z, dot;
269         float cross[3];
270
271         dot = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
272         vcross(cross, a, b);
273
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;
278         a[0] = x;
279         a[1] = y;
280         a[2] = z;
281 }
282
283 void mglut_sincos(float angle, float *sptr, float *cptr);
284 float mglut_tan(float x);
285
286 static void qrotation(float *q, float angle, float x, float y, float z)
287 {
288         float sa, ca;
289         mglut_sincos(angle * 0.5f, &sa, &ca);
290         q[3] = ca;
291         q[0] = x * sa;
292         q[1] = y * sa;
293         q[2] = z * sa;
294 }
295
296 static void qrotate(float *q, float angle, float x, float y, float z)
297 {
298         float qrot[4];
299         qrotation(qrot, angle, x, y, z);
300         qmul(qrot, q);
301         q[0] = qrot[0];
302         q[1] = qrot[1];
303         q[2] = qrot[2];
304         q[3] = qrot[3];
305 }
306
307 static void mrotation_quat(float *m, const float *q)
308 {
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;
315
316         m[3] = m[7] = m[11] = m[12] = m[13] = m[14] = 0.0f;
317         m[15] = 1.0f;
318
319         m[0] = sx;
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];
323         m[5] = sy;
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];
327         m[10] = sz;
328 }