Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /**
  2. * Random rendering, to check for crashes, hangs, etc.
  3. *
  4. * Brian Paul
  5. * 21 June 2007
  6. */
  7. #define GL_GLEXT_PROTOTYPES
  8. #include <assert.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <math.h>
  13. #include <GL/glut.h>
  14. static int Win;
  15. static GLboolean Anim = GL_TRUE;
  16. static int Width = 200, Height = 200;
  17. static int DB = 0;
  18. static int MinVertexCount = 0, MaxVertexCount = 1000;
  19. static int Count = 0;
  20. struct vertex
  21. {
  22. int type;
  23. float v[4];
  24. };
  25. static int BufferSize = 10000;
  26. static struct vertex *Vbuffer = NULL;
  27. static int Vcount, Vprim;
  28. enum {
  29. BEGIN,
  30. END,
  31. VERTEX2,
  32. VERTEX3,
  33. VERTEX4,
  34. COLOR3,
  35. COLOR4,
  36. TEX2,
  37. TEX3,
  38. TEX4,
  39. SECCOLOR3,
  40. NORMAL3
  41. };
  42. /**
  43. * This can be called from within gdb after a crash:
  44. * (gdb) call ReportState()
  45. */
  46. static void
  47. ReportState(void)
  48. {
  49. static const struct {
  50. GLenum token;
  51. char *str;
  52. GLenum type;
  53. } state [] = {
  54. { GL_ALPHA_TEST, "GL_ALPHA_TEST", GL_INT },
  55. { GL_BLEND, "GL_BLEND", GL_INT },
  56. { GL_CLIP_PLANE0, "GL_CLIP_PLANE0", GL_INT },
  57. { GL_DEPTH_TEST, "GL_DEPTH_TEST", GL_INT },
  58. { GL_LIGHTING, "GL_LIGHTING", GL_INT },
  59. { GL_LINE_WIDTH, "GL_LINE_WIDTH", GL_FLOAT },
  60. { GL_POINT_SIZE, "GL_POINT_SIZE", GL_FLOAT },
  61. { GL_SHADE_MODEL, "GL_SHADE_MODEL", GL_INT },
  62. { GL_SCISSOR_TEST, "GL_SCISSOR_TEST", GL_INT },
  63. { 0, NULL, 0 }
  64. };
  65. GLint i;
  66. for (i = 0; state[i].token; i++) {
  67. if (state[i].type == GL_INT) {
  68. GLint v;
  69. glGetIntegerv(state[i].token, &v);
  70. printf("%s = %d\n", state[i].str, v);
  71. }
  72. else {
  73. GLfloat v;
  74. glGetFloatv(state[i].token, &v);
  75. printf("%s = %f\n", state[i].str, v);
  76. }
  77. }
  78. }
  79. static void
  80. PrintVertex(const char *f, const struct vertex *v, int sz)
  81. {
  82. int i;
  83. printf("%s(", f);
  84. for (i = 0; i < sz; i++) {
  85. printf("%g%s", v->v[i], (i == sz-1) ? "" : ", ");
  86. }
  87. printf(");\n");
  88. }
  89. /**
  90. * This can be called from within gdb after a crash:
  91. * (gdb) call ReportState()
  92. */
  93. static void
  94. LastPrim(void)
  95. {
  96. int i;
  97. for (i = 0; i < Vcount; i++) {
  98. switch (Vbuffer[i].type) {
  99. case BEGIN:
  100. printf("glBegin(%d);\n", (int) Vbuffer[i].v[0]);
  101. break;
  102. case END:
  103. printf("glEnd();\n");
  104. break;
  105. case VERTEX2:
  106. PrintVertex("glVertex2f", Vbuffer + i, 2);
  107. break;
  108. case VERTEX3:
  109. PrintVertex("glVertex3f", Vbuffer + i, 3);
  110. break;
  111. case VERTEX4:
  112. PrintVertex("glVertex4f", Vbuffer + i, 4);
  113. break;
  114. case COLOR3:
  115. PrintVertex("glColor3f", Vbuffer + i, 3);
  116. break;
  117. case COLOR4:
  118. PrintVertex("glColor4f", Vbuffer + i, 4);
  119. break;
  120. case TEX2:
  121. PrintVertex("glTexCoord2f", Vbuffer + i, 2);
  122. break;
  123. case TEX3:
  124. PrintVertex("glTexCoord3f", Vbuffer + i, 3);
  125. break;
  126. case TEX4:
  127. PrintVertex("glTexCoord4f", Vbuffer + i, 4);
  128. break;
  129. case SECCOLOR3:
  130. PrintVertex("glSecondaryColor3f", Vbuffer + i, 3);
  131. break;
  132. case NORMAL3:
  133. PrintVertex("glNormal3f", Vbuffer + i, 3);
  134. break;
  135. default:
  136. abort();
  137. }
  138. }
  139. }
  140. static int
  141. RandomInt(int max)
  142. {
  143. if (max == 0)
  144. return 0;
  145. return rand() % max;
  146. }
  147. static float
  148. RandomFloat(float min, float max)
  149. {
  150. int k = rand() % 10000;
  151. float x = min + (max - min) * k / 10000.0;
  152. return x;
  153. }
  154. /*
  155. * Return true if random number in [0,1] is <= percentile.
  156. */
  157. static GLboolean
  158. RandomChoice(float percentile)
  159. {
  160. return RandomFloat(0.0, 1.0) <= percentile;
  161. }
  162. static void
  163. RandomStateChange(void)
  164. {
  165. int k = RandomInt(19);
  166. switch (k) {
  167. case 0:
  168. glEnable(GL_BLEND);
  169. break;
  170. case 1:
  171. glDisable(GL_BLEND);
  172. break;
  173. case 2:
  174. glEnable(GL_ALPHA_TEST);
  175. break;
  176. case 3:
  177. glEnable(GL_ALPHA_TEST);
  178. break;
  179. case 4:
  180. glEnable(GL_DEPTH_TEST);
  181. break;
  182. case 5:
  183. glEnable(GL_DEPTH_TEST);
  184. break;
  185. case 6:
  186. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  187. break;
  188. case 7:
  189. glPointSize(10.0);
  190. break;
  191. case 8:
  192. glPointSize(1.0);
  193. break;
  194. case 9:
  195. glLineWidth(10.0);
  196. break;
  197. case 10:
  198. glLineWidth(1.0);
  199. break;
  200. case 11:
  201. glEnable(GL_LIGHTING);
  202. break;
  203. case 12:
  204. glDisable(GL_LIGHTING);
  205. break;
  206. case 13:
  207. glEnable(GL_SCISSOR_TEST);
  208. break;
  209. case 14:
  210. glDisable(GL_SCISSOR_TEST);
  211. break;
  212. case 15:
  213. glEnable(GL_CLIP_PLANE0);
  214. break;
  215. case 16:
  216. glDisable(GL_CLIP_PLANE0);
  217. break;
  218. case 17:
  219. glShadeModel(GL_FLAT);
  220. break;
  221. case 18:
  222. glShadeModel(GL_SMOOTH);
  223. break;
  224. }
  225. }
  226. static void
  227. RandomPrimitive(void)
  228. {
  229. int i;
  230. int len = MinVertexCount + RandomInt(MaxVertexCount - MinVertexCount);
  231. Vprim = RandomInt(10);
  232. glBegin(Vprim);
  233. Vbuffer[Vcount].type = BEGIN;
  234. Vbuffer[Vcount].v[0] = Vprim;
  235. Vcount++;
  236. for (i = 0; i < len; i++) {
  237. Vbuffer[Vcount].v[0] = RandomFloat(-3, 3);
  238. Vbuffer[Vcount].v[1] = RandomFloat(-3, 3);
  239. Vbuffer[Vcount].v[2] = RandomFloat(-3, 3);
  240. Vbuffer[Vcount].v[3] = RandomFloat(-3, 3);
  241. int k = RandomInt(9);
  242. switch (k) {
  243. case 0:
  244. glVertex2fv(Vbuffer[Vcount].v);
  245. Vbuffer[Vcount].type = VERTEX2;
  246. break;
  247. case 1:
  248. glVertex3fv(Vbuffer[Vcount].v);
  249. Vbuffer[Vcount].type = VERTEX3;
  250. break;
  251. case 2:
  252. glVertex4fv(Vbuffer[Vcount].v);
  253. Vbuffer[Vcount].type = VERTEX4;
  254. break;
  255. case 3:
  256. glColor3fv(Vbuffer[Vcount].v);
  257. Vbuffer[Vcount].type = COLOR3;
  258. break;
  259. case 4:
  260. glColor4fv(Vbuffer[Vcount].v);
  261. Vbuffer[Vcount].type = COLOR4;
  262. break;
  263. case 5:
  264. glTexCoord2fv(Vbuffer[Vcount].v);
  265. Vbuffer[Vcount].type = TEX2;
  266. break;
  267. case 6:
  268. glTexCoord3fv(Vbuffer[Vcount].v);
  269. Vbuffer[Vcount].type = TEX3;
  270. break;
  271. case 7:
  272. glTexCoord4fv(Vbuffer[Vcount].v);
  273. Vbuffer[Vcount].type = TEX4;
  274. break;
  275. case 8:
  276. glSecondaryColor3fv(Vbuffer[Vcount].v);
  277. Vbuffer[Vcount].type = SECCOLOR3;
  278. break;
  279. case 9:
  280. glNormal3fv(Vbuffer[Vcount].v);
  281. Vbuffer[Vcount].type = NORMAL3;
  282. break;
  283. default:
  284. abort();
  285. }
  286. Vcount++;
  287. if (Vcount >= BufferSize - 2) {
  288. /* reset */
  289. Vcount = 0;
  290. }
  291. }
  292. Vbuffer[Vcount++].type = END;
  293. glEnd();
  294. }
  295. static void
  296. RandomDraw(void)
  297. {
  298. int i;
  299. GLboolean dlist = RandomChoice(0.1);
  300. if (dlist)
  301. glNewList(1, GL_COMPILE);
  302. for (i = 0; i < 3; i++) {
  303. RandomStateChange();
  304. }
  305. RandomPrimitive();
  306. if (dlist) {
  307. glEndList();
  308. glCallList(1);
  309. }
  310. }
  311. static void
  312. Idle(void)
  313. {
  314. glutPostRedisplay();
  315. }
  316. static void
  317. Draw(void)
  318. {
  319. #if 1
  320. RandomDraw();
  321. Count++;
  322. #else
  323. /* cut & paste temp code here */
  324. #endif
  325. assert(glGetError() == 0);
  326. if (DB)
  327. glutSwapBuffers();
  328. else
  329. glFinish();
  330. }
  331. static void
  332. Reshape(int width, int height)
  333. {
  334. Width = width;
  335. Height = height;
  336. glViewport(0, 0, width, height);
  337. glScissor(20, 20, Width-40, Height-40);
  338. glMatrixMode(GL_PROJECTION);
  339. glLoadIdentity();
  340. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  341. glMatrixMode(GL_MODELVIEW);
  342. glLoadIdentity();
  343. glTranslatef(0.0, 0.0, -15.0);
  344. }
  345. static void
  346. Key(unsigned char key, int x, int y)
  347. {
  348. (void) x;
  349. (void) y;
  350. switch (key) {
  351. case 27:
  352. glutDestroyWindow(Win);
  353. exit(0);
  354. break;
  355. }
  356. glutPostRedisplay();
  357. }
  358. static void
  359. Init(void)
  360. {
  361. static const GLdouble plane[4] = {1, 1, 0, 0};
  362. glDrawBuffer(GL_FRONT);
  363. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  364. glEnable(GL_LIGHT0);
  365. glClipPlane(GL_CLIP_PLANE0, plane);
  366. Vbuffer = (struct vertex *)
  367. malloc(BufferSize * sizeof(struct vertex));
  368. /* silence warnings */
  369. (void) ReportState;
  370. (void) LastPrim;
  371. }
  372. static void
  373. ParseArgs(int argc, char *argv[])
  374. {
  375. int i;
  376. for (i = 1; i < argc; i++) {
  377. if (strcmp(argv[i], "-s") == 0) {
  378. int j = atoi(argv[i + 1]);
  379. printf("Random seed value: %d\n", j);
  380. srand(j);
  381. i++;
  382. }
  383. else if (strcmp(argv[i], "-a") == 0) {
  384. i++;
  385. MinVertexCount = atoi(argv[i]);
  386. }
  387. else if (strcmp(argv[i], "-b") == 0) {
  388. i++;
  389. MaxVertexCount = atoi(argv[i]);
  390. }
  391. }
  392. }
  393. int
  394. main(int argc, char *argv[])
  395. {
  396. glutInit(&argc, argv);
  397. glutInitWindowPosition(0, 0);
  398. glutInitWindowSize(Width, Height);
  399. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  400. Win = glutCreateWindow(argv[0]);
  401. ParseArgs(argc, argv);
  402. glutReshapeFunc(Reshape);
  403. glutKeyboardFunc(Key);
  404. glutDisplayFunc(Draw);
  405. if (Anim)
  406. glutIdleFunc(Idle);
  407. Init();
  408. glutMainLoop();
  409. return 0;
  410. }