summaryrefslogtreecommitdiff
path: root/progs/tests/random.c
blob: 4023674c05f7e5e43251801f1a549b2b9ff20cde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/**
 * Random rendering, to check for crashes, hangs, etc.
 *
 * Brian Paul
 * 21 June 2007
 */


#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glew.h>
#include <GL/glut.h>

static int Win;
static GLboolean Anim = GL_TRUE;
static int Width = 200, Height = 200;
static int DB = 0;
static int MinVertexCount = 0, MaxVertexCount = 1000;
static int Count = 0;

struct vertex
{
   int type;
   float v[4];
};

static int BufferSize = 10000;
static struct vertex *Vbuffer = NULL;
static int Vcount, Vprim;

enum {
   BEGIN,
   END,
   VERTEX2,
   VERTEX3,
   VERTEX4,
   COLOR3,
   COLOR4,
   TEX2,
   TEX3,
   TEX4,
   SECCOLOR3,
   NORMAL3
};



/**
 * This can be called from within gdb after a crash:
 * (gdb) call ReportState()
 */
static void
ReportState(void)
{
   static const struct {
      GLenum token;
      char *str;
      GLenum type;
   } state [] = {
      { GL_ALPHA_TEST, "GL_ALPHA_TEST", GL_INT },
      { GL_BLEND, "GL_BLEND", GL_INT },
      { GL_CLIP_PLANE0, "GL_CLIP_PLANE0", GL_INT },
      { GL_DEPTH_TEST, "GL_DEPTH_TEST", GL_INT },
      { GL_LIGHTING, "GL_LIGHTING", GL_INT },
      { GL_LINE_WIDTH, "GL_LINE_WIDTH", GL_FLOAT },
      { GL_POINT_SIZE, "GL_POINT_SIZE", GL_FLOAT },
      { GL_SHADE_MODEL, "GL_SHADE_MODEL", GL_INT },
      { GL_SCISSOR_TEST, "GL_SCISSOR_TEST", GL_INT },
      { 0, NULL, 0 }
   };

   GLint i;

   for (i = 0; state[i].token; i++) {
      if (state[i].type == GL_INT) {
         GLint v;
         glGetIntegerv(state[i].token, &v);
         printf("%s = %d\n", state[i].str, v);
      }
      else {
         GLfloat v;
         glGetFloatv(state[i].token, &v);
         printf("%s = %f\n", state[i].str, v);
      }
   }
}

static void
PrintVertex(const char *f, const struct vertex *v, int sz)
{
   int i;
   printf("%s(", f);
   for (i = 0; i < sz; i++) {
      printf("%g%s", v->v[i], (i == sz-1) ? "" : ", ");
   }
   printf(");\n");
}

/**
 * This can be called from within gdb after a crash:
 * (gdb) call ReportState()
 */
static void
LastPrim(void)
{
   int i;
   for (i = 0; i < Vcount; i++) {
      switch (Vbuffer[i].type) {
      case BEGIN:
         printf("glBegin(%d);\n", (int) Vbuffer[i].v[0]);
         break;
      case END:
         printf("glEnd();\n");
         break;
      case VERTEX2:
         PrintVertex("glVertex2f", Vbuffer + i, 2);
         break;
      case VERTEX3:
         PrintVertex("glVertex3f", Vbuffer + i, 3);
         break;
      case VERTEX4:
         PrintVertex("glVertex4f", Vbuffer + i, 4);
         break;
      case COLOR3:
         PrintVertex("glColor3f", Vbuffer + i, 3);
         break;
      case COLOR4:
         PrintVertex("glColor4f", Vbuffer + i, 4);
         break;
      case TEX2:
         PrintVertex("glTexCoord2f", Vbuffer + i, 2);
         break;
      case TEX3:
         PrintVertex("glTexCoord3f", Vbuffer + i, 3);
         break;
      case TEX4:
         PrintVertex("glTexCoord4f", Vbuffer + i, 4);
         break;
      case SECCOLOR3:
         PrintVertex("glSecondaryColor3f", Vbuffer + i, 3);
         break;
      case NORMAL3:
         PrintVertex("glNormal3f", Vbuffer + i, 3);
         break;
      default:
         abort();
      }
   }
}


static int
RandomInt(int max)
{
   if (max == 0)
      return 0;
   return rand() % max;
}

static float
RandomFloat(float min, float max)
{
   int k = rand() % 10000;
   float x = min + (max - min) * k / 10000.0;
   return x;
}

/*
 * Return true if random number in [0,1] is <= percentile.
 */
static GLboolean
RandomChoice(float percentile)
{
   return RandomFloat(0.0, 1.0) <= percentile;
}

static void
RandomStateChange(void)
{
   int k = RandomInt(19);
   switch (k) {
   case 0:
      glEnable(GL_BLEND);
      break;
   case 1:
      glDisable(GL_BLEND);
      break;
   case 2:
      glEnable(GL_ALPHA_TEST);
      break;
   case 3:
      glEnable(GL_ALPHA_TEST);
      break;
   case 4:
      glEnable(GL_DEPTH_TEST);
      break;
   case 5:
      glEnable(GL_DEPTH_TEST);
      break;
   case 6:
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      break;
   case 7:
      glPointSize(10.0);
      break;
   case 8:
      glPointSize(1.0);
      break;
   case 9:
      glLineWidth(10.0);
      break;
   case 10:
      glLineWidth(1.0);
      break;
   case 11:
      glEnable(GL_LIGHTING);
      break;
   case 12:
      glDisable(GL_LIGHTING);
      break;
   case 13:
      glEnable(GL_SCISSOR_TEST);
      break;
   case 14:
      glDisable(GL_SCISSOR_TEST);
      break;
   case 15:
      glEnable(GL_CLIP_PLANE0);
      break;
   case 16:
      glDisable(GL_CLIP_PLANE0);
      break;
   case 17:
      glShadeModel(GL_FLAT);
      break;
   case 18:
      glShadeModel(GL_SMOOTH);
      break;
   }
}


static void
RandomPrimitive(void)
{
   int i;
   int len = MinVertexCount + RandomInt(MaxVertexCount - MinVertexCount);

   Vprim = RandomInt(10);

   glBegin(Vprim);
   Vbuffer[Vcount].type = BEGIN;
   Vbuffer[Vcount].v[0] = Vprim;
   Vcount++;

   for (i = 0; i < len; i++) {
      Vbuffer[Vcount].v[0] = RandomFloat(-3, 3);
      Vbuffer[Vcount].v[1] = RandomFloat(-3, 3);
      Vbuffer[Vcount].v[2] = RandomFloat(-3, 3);
      Vbuffer[Vcount].v[3] = RandomFloat(-3, 3);
      int k = RandomInt(9);
      switch (k) {
      case 0:
         glVertex2fv(Vbuffer[Vcount].v);
         Vbuffer[Vcount].type = VERTEX2;
         break;
      case 1:
         glVertex3fv(Vbuffer[Vcount].v);
         Vbuffer[Vcount].type = VERTEX3;
         break;
      case 2:
         glVertex4fv(Vbuffer[Vcount].v);
         Vbuffer[Vcount].type = VERTEX4;
         break;
      case 3:
         glColor3fv(Vbuffer[Vcount].v);
         Vbuffer[Vcount].type = COLOR3;
         break;
      case 4:
         glColor4fv(Vbuffer[Vcount].v);
         Vbuffer[Vcount].type = COLOR4;
         break;
      case 5:
         glTexCoord2fv(Vbuffer[Vcount].v);
         Vbuffer[Vcount].type = TEX2;
         break;
      case 6:
         glTexCoord3fv(Vbuffer[Vcount].v);
         Vbuffer[Vcount].type = TEX3;
         break;
      case 7:
         glTexCoord4fv(Vbuffer[Vcount].v);
         Vbuffer[Vcount].type = TEX4;
         break;
      case 8:
         glSecondaryColor3fv(Vbuffer[Vcount].v);
         Vbuffer[Vcount].type = SECCOLOR3;
         break;
      case 9:
         glNormal3fv(Vbuffer[Vcount].v);
         Vbuffer[Vcount].type = NORMAL3;
         break;
      default:
         abort();
      }
      Vcount++;

      if (Vcount >= BufferSize - 2) {
         /* reset */
         Vcount = 0;
      }
   }

   Vbuffer[Vcount++].type = END;

   glEnd();
}


static void
RandomDraw(void)
{
   int i;
   GLboolean dlist = RandomChoice(0.1);
   if (dlist)
      glNewList(1, GL_COMPILE);
   for (i = 0; i < 3; i++) {
      RandomStateChange();
   }
   RandomPrimitive();

   if (dlist) {
      glEndList();
      glCallList(1);
   }
}


static void
Idle(void)
{
   glutPostRedisplay();
}


static void
Draw(void)
{
#if 1
   RandomDraw();
   Count++;
#else
   /* cut & paste temp code here */
#endif

   assert(glGetError() == 0);

   if (DB)
      glutSwapBuffers();
   else
      glFinish();
}


static void
Reshape(int width, int height)
{
   Width = width;
   Height = height;
   glViewport(0, 0, width, height);
   glScissor(20, 20, Width-40, Height-40);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glTranslatef(0.0, 0.0, -15.0);
}


static void
Key(unsigned char key, int x, int y)
{
   (void) x;
   (void) y;
   switch (key) {
      case 27:
         glutDestroyWindow(Win);
         exit(0);
         break;
   }
   glutPostRedisplay();
}


static void
Init(void)
{
   static const GLdouble plane[4] = {1, 1, 0, 0};
   glDrawBuffer(GL_FRONT);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glEnable(GL_LIGHT0);
   glClipPlane(GL_CLIP_PLANE0, plane);

   Vbuffer = (struct vertex *)
      malloc(BufferSize * sizeof(struct vertex));

   /* silence warnings */
   (void) ReportState;
   (void) LastPrim;
}


static void
ParseArgs(int argc, char *argv[])
{
   int i;
   for (i = 1; i < argc; i++) {
      if (strcmp(argv[i], "-s") == 0) {
         int j = atoi(argv[i + 1]);
         printf("Random seed value: %d\n", j);
         srand(j);
         i++;
      }
      else if (strcmp(argv[i], "-a") == 0) {
         i++;
         MinVertexCount = atoi(argv[i]);
      }
      else if (strcmp(argv[i], "-b") == 0) {
         i++;
         MaxVertexCount = atoi(argv[i]);
      }
   }
}


int
main(int argc, char *argv[])
{
   glutInit(&argc, argv);
   glutInitWindowPosition(0, 0);
   glutInitWindowSize(Width, Height);
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
   Win = glutCreateWindow(argv[0]);
   glewInit();
   ParseArgs(argc, argv);
   glutReshapeFunc(Reshape);
   glutKeyboardFunc(Key);
   glutDisplayFunc(Draw);
   if (Anim)
      glutIdleFunc(Idle);
   Init();
   glutMainLoop();
   return 0;
}