Clone of mesa.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /* Copyright (c) Mark J. Kilgard, 1997. */
  2. /* This program is freely distributable without licensing fees
  3. and is provided without guarantee or warrantee expressed or
  4. implied. This program is -not- in the public domain. */
  5. /* This example demonstrates how to render particle effects
  6. with OpenGL. A cloud of pinkish/orange particles explodes with the
  7. particles bouncing off the ground. When the EXT_point_parameters
  8. is present , the particle size is attenuated based on eye distance. */
  9. /* Modified by Brian Paul to test GL_ARB_point_sprite */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <math.h> /* for cos(), sin(), and sqrt() */
  14. #ifdef _WIN32
  15. #include <windows.h>
  16. #endif
  17. #define GL_GLEXT_PROTOTYPES
  18. #include <GL/glut.h>
  19. /* Some <math.h> files do not define M_PI... */
  20. #ifndef M_PI
  21. #define M_PI 3.14159265
  22. #endif
  23. #if 0 /* For debugging. */
  24. #undef GL_EXT_point_parameters
  25. #endif
  26. static GLfloat angle = -150; /* in degrees */
  27. static int spin = 0;
  28. static int moving, begin;
  29. static float theTime;
  30. static int repeat = 1;
  31. static int blend = 1;
  32. int useMipmaps = 1;
  33. int linearFiltering = 1;
  34. static GLfloat constant[3] = { .2, 0.0, 0.0 };
  35. static GLfloat linear[3] = { .0, .1, 0.0 };
  36. static GLfloat theQuad[3] = { .005, 0.1, 1/600.0 };
  37. #define MAX_POINTS 2000
  38. static int numPoints = 200;
  39. static GLfloat pointList[MAX_POINTS][3];
  40. static GLfloat pointTime[MAX_POINTS];
  41. static GLfloat pointVelocity[MAX_POINTS][2];
  42. static GLfloat pointDirection[MAX_POINTS][2];
  43. static int colorList[MAX_POINTS];
  44. static int animate = 1, motion = 0, org = 0, sprite = 1, smooth = 1;
  45. static GLfloat colorSet[][4] = {
  46. /* Shades of red. */
  47. { 0.7, 0.2, 0.4, 0.5 },
  48. { 0.8, 0.0, 0.7, 0.5 },
  49. { 1.0, 0.0, 0.0, 0.5 },
  50. { 0.9, 0.3, 0.6, 0.5 },
  51. { 1.0, 0.4, 0.0, 0.5 },
  52. { 1.0, 0.0, 0.5, 0.5 },
  53. };
  54. #define NUM_COLORS (sizeof(colorSet)/sizeof(colorSet[0]))
  55. #define DEAD (NUM_COLORS+1)
  56. /* GL */
  57. static GLint spritePattern[16][16] = {
  58. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  59. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  60. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  61. { 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 },
  62. { 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0 },
  63. { 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0 },
  64. { 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 },
  65. { 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 },
  66. { 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0 },
  67. { 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0 },
  68. { 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0 },
  69. { 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0 },
  70. { 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0 },
  71. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  72. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  73. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  74. };
  75. #if 0 /* drand48 might be better on Unix machines */
  76. #define RANDOM_RANGE(lo, hi) ((lo) + (hi - lo) * drand48())
  77. #else
  78. static float float_rand(void) { return rand() / (float) RAND_MAX; }
  79. #define RANDOM_RANGE(lo, hi) ((lo) + (hi - lo) * float_rand())
  80. #endif
  81. #define MEAN_VELOCITY 3.0
  82. #define GRAVITY 2.0
  83. /* Modeling units of ground extent in each X and Z direction. */
  84. #define EDGE 12
  85. static void
  86. makePointList(void)
  87. {
  88. float angle, velocity, direction;
  89. int i;
  90. motion = 1;
  91. for (i=0; i<numPoints; i++) {
  92. pointList[i][0] = 0.0;
  93. pointList[i][1] = 0.0;
  94. pointList[i][2] = 0.0;
  95. pointTime[i] = 0.0;
  96. angle = (RANDOM_RANGE(60.0, 70.0)) * M_PI/180.0;
  97. direction = RANDOM_RANGE(0.0, 360.0) * M_PI/180.0;
  98. pointDirection[i][0] = cos(direction);
  99. pointDirection[i][1] = sin(direction);
  100. velocity = MEAN_VELOCITY + RANDOM_RANGE(-0.8, 1.0);
  101. pointVelocity[i][0] = velocity * cos(angle);
  102. pointVelocity[i][1] = velocity * sin(angle);
  103. colorList[i] = rand() % NUM_COLORS;
  104. }
  105. theTime = 0.0;
  106. }
  107. static void
  108. updatePointList(void)
  109. {
  110. float distance;
  111. int i;
  112. static double t0 = -1.;
  113. double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  114. if (t0 < 0.0)
  115. t0 = t;
  116. dt = t - t0;
  117. t0 = t;
  118. motion = 0;
  119. for (i=0; i<numPoints; i++) {
  120. distance = pointVelocity[i][0] * theTime;
  121. /* X and Z */
  122. pointList[i][0] = pointDirection[i][0] * distance;
  123. pointList[i][2] = pointDirection[i][1] * distance;
  124. /* Z */
  125. pointList[i][1] =
  126. (pointVelocity[i][1] - 0.5 * GRAVITY * pointTime[i])*pointTime[i];
  127. /* If we hit the ground, bounce the point upward again. */
  128. if (pointList[i][1] <= 0.0) {
  129. if (distance > EDGE) {
  130. /* Particle has hit ground past the distance duration of
  131. the particles. Mark particle as dead. */
  132. colorList[i] = NUM_COLORS; /* Not moving. */
  133. continue;
  134. }
  135. pointVelocity[i][1] *= 0.8; /* 80% of previous up velocity. */
  136. pointTime[i] = 0.0; /* Reset the particles sense of up time. */
  137. }
  138. motion = 1;
  139. pointTime[i] += dt;
  140. }
  141. theTime += dt;
  142. if (!motion && !spin) {
  143. if (repeat) {
  144. makePointList();
  145. } else {
  146. glutIdleFunc(NULL);
  147. }
  148. }
  149. }
  150. static void
  151. idle(void)
  152. {
  153. updatePointList();
  154. if (spin) {
  155. angle += 0.3;
  156. }
  157. glutPostRedisplay();
  158. }
  159. static void
  160. visible(int vis)
  161. {
  162. if (vis == GLUT_VISIBLE) {
  163. if (animate && (motion || spin)) {
  164. glutIdleFunc(idle);
  165. }
  166. } else {
  167. glutIdleFunc(NULL);
  168. }
  169. }
  170. static void
  171. redraw(void)
  172. {
  173. int i;
  174. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  175. glPushMatrix();
  176. glRotatef(15.0, 1.0, 0.0, 0.0);
  177. glRotatef(angle, 0.0, 1.0, 0.0);
  178. glDepthMask(GL_FALSE);
  179. /* Draw the floor. */
  180. /* glEnable(GL_TEXTURE_2D);*/
  181. glColor3f(0.1, 0.5, 1.0);
  182. glBegin(GL_QUADS);
  183. glTexCoord2f(0.0, 0.0);
  184. glVertex3f(-EDGE, -0.05, -EDGE);
  185. glTexCoord2f(20.0, 0.0);
  186. glVertex3f(EDGE, -0.05, -EDGE);
  187. glTexCoord2f(20.0, 20.0);
  188. glVertex3f(EDGE, -0.05, EDGE);
  189. glTexCoord2f(0.0, 20.0);
  190. glVertex3f(-EDGE, -0.05, EDGE);
  191. glEnd();
  192. /* Allow particles to blend with each other. */
  193. glDepthMask(GL_TRUE);
  194. if (blend)
  195. glEnable(GL_BLEND);
  196. if (sprite) {
  197. glEnable(GL_TEXTURE_2D);
  198. #ifdef GL_ARB_point_sprite
  199. glEnable(GL_POINT_SPRITE_ARB);
  200. #endif
  201. }
  202. glColor3f(1,1,1);
  203. glBegin(GL_POINTS);
  204. for (i=0; i<numPoints; i++) {
  205. /* Draw alive particles. */
  206. if (colorList[i] != DEAD) {
  207. if (!sprite) glColor4fv(colorSet[colorList[i]]);
  208. glVertex3fv(pointList[i]);
  209. }
  210. }
  211. glEnd();
  212. glDisable(GL_TEXTURE_2D);
  213. #ifdef GL_ARB_point_sprite
  214. glDisable(GL_POINT_SPRITE_ARB);
  215. #endif
  216. glDisable(GL_BLEND);
  217. glPopMatrix();
  218. glutSwapBuffers();
  219. }
  220. /* ARGSUSED2 */
  221. static void
  222. mouse(int button, int state, int x, int y)
  223. {
  224. /* Scene can be spun around Y axis using left
  225. mouse button movement. */
  226. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  227. moving = 1;
  228. begin = x;
  229. }
  230. if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
  231. moving = 0;
  232. }
  233. }
  234. /* ARGSUSED1 */
  235. static void
  236. mouseMotion(int x, int y)
  237. {
  238. if (moving) {
  239. angle = angle + (x - begin);
  240. begin = x;
  241. glutPostRedisplay();
  242. }
  243. }
  244. static void
  245. menu(int option)
  246. {
  247. switch (option) {
  248. case 0:
  249. makePointList();
  250. break;
  251. #ifdef GL_ARB_point_parameters
  252. case 1:
  253. glPointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION_ARB, constant);
  254. break;
  255. case 2:
  256. glPointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION_ARB, linear);
  257. break;
  258. case 3:
  259. glPointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION_ARB, theQuad);
  260. break;
  261. #endif
  262. case 4:
  263. blend = 1;
  264. break;
  265. case 5:
  266. blend = 0;
  267. break;
  268. #ifdef GL_ARB_point_parameters
  269. case 6:
  270. glPointParameterfARB(GL_POINT_FADE_THRESHOLD_SIZE_ARB, 1.0);
  271. break;
  272. case 7:
  273. glPointParameterfARB(GL_POINT_FADE_THRESHOLD_SIZE_ARB, 10.0);
  274. break;
  275. #endif
  276. case 8:
  277. glEnable(GL_POINT_SMOOTH);
  278. smooth = 1;
  279. break;
  280. case 9:
  281. glDisable(GL_POINT_SMOOTH);
  282. smooth = 0;
  283. break;
  284. case 10:
  285. glPointSize(16.0);
  286. break;
  287. case 11:
  288. glPointSize(32.0);
  289. break;
  290. case 12:
  291. glPointSize(64.0);
  292. break;
  293. case 13:
  294. spin = 1 - spin;
  295. if (animate && (spin || motion)) {
  296. glutIdleFunc(idle);
  297. } else {
  298. glutIdleFunc(NULL);
  299. }
  300. break;
  301. case 14:
  302. numPoints = 200;
  303. break;
  304. case 15:
  305. numPoints = 500;
  306. break;
  307. case 16:
  308. numPoints = 1000;
  309. break;
  310. case 17:
  311. numPoints = 2000;
  312. break;
  313. case 666:
  314. exit(0);
  315. }
  316. glutPostRedisplay();
  317. }
  318. /* ARGSUSED1 */
  319. static void
  320. key(unsigned char c, int x, int y)
  321. {
  322. switch (c) {
  323. case 13:
  324. animate = 1 - animate; /* toggle. */
  325. if (animate && (motion || spin)) {
  326. glutIdleFunc(idle);
  327. } else {
  328. glutIdleFunc(NULL);
  329. }
  330. break;
  331. case ' ':
  332. animate = 1;
  333. makePointList();
  334. glutIdleFunc(idle);
  335. break;
  336. case 'o':
  337. case 'O':
  338. org ^= 1;
  339. #ifdef GL_VERSION_2_0
  340. #ifdef GL_ARB_point_parameters
  341. glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN,
  342. org ? GL_LOWER_LEFT : GL_UPPER_LEFT);
  343. #endif
  344. #endif
  345. glutPostRedisplay();
  346. break;
  347. case 't':
  348. case 'T':
  349. sprite ^= 1;
  350. glutPostRedisplay();
  351. break;
  352. case 's':
  353. case 'S':
  354. (smooth ^= 1) ? glEnable(GL_POINT_SMOOTH) : glDisable(GL_POINT_SMOOTH);
  355. glutPostRedisplay();
  356. break;
  357. case '0':
  358. glPointSize(1.0);
  359. glutPostRedisplay();
  360. break;
  361. case '1':
  362. glPointSize(16.0);
  363. glutPostRedisplay();
  364. break;
  365. case '2':
  366. glPointSize(32.0);
  367. glutPostRedisplay();
  368. break;
  369. case '3':
  370. glPointSize(64.0);
  371. glutPostRedisplay();
  372. break;
  373. case '4':
  374. glPointSize(128.0);
  375. glutPostRedisplay();
  376. break;
  377. case 27:
  378. exit(0);
  379. }
  380. }
  381. static void
  382. makeSprite(void)
  383. {
  384. GLubyte texture[16][16][4];
  385. int i, j;
  386. if (!glutExtensionSupported("GL_ARB_point_sprite")) {
  387. printf("Sorry, this demo requires GL_ARB_point_sprite.\n");
  388. exit(0);
  389. }
  390. if (!glutExtensionSupported("GL_ARB_point_parameters")) {
  391. printf("Sorry, this demo requires GL_ARB_point_parameters.\n");
  392. exit(0);
  393. }
  394. for (i = 0; i < 16; i++) {
  395. for (j = 0; j < 16; j++) {
  396. if (spritePattern[i][j]) {
  397. texture[i][j][0] = 255;
  398. texture[i][j][1] = 255;
  399. texture[i][j][2] = 255;
  400. texture[i][j][3] = 255;
  401. }
  402. else {
  403. texture[i][j][0] = 255;
  404. texture[i][j][1] = 0;
  405. texture[i][j][2] = 0;
  406. texture[i][j][3] = 0;
  407. }
  408. }
  409. }
  410. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  411. texture);
  412. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  413. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  414. #ifdef GL_ARB_point_sprite
  415. glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
  416. #endif
  417. }
  418. static void
  419. reshape(int width, int height)
  420. {
  421. GLfloat h = (GLfloat) height / (GLfloat) width;
  422. glViewport(0, 0, (GLint) width, (GLint) height);
  423. glMatrixMode(GL_PROJECTION);
  424. glLoadIdentity();
  425. glFrustum(-1.0, 1.0, -h, h, 2.0, 30.0);
  426. glMatrixMode(GL_MODELVIEW);
  427. glLoadIdentity();
  428. glTranslatef(0.0, 0.0, -10.0);
  429. }
  430. int
  431. main(int argc, char **argv)
  432. {
  433. int i;
  434. glutInit(&argc, argv);
  435. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  436. for (i=1; i<argc; i++) {
  437. if(!strcmp("-noms", argv[i])) {
  438. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  439. printf("forcing no multisampling\n");
  440. } else if(!strcmp("-nomipmaps", argv[i])) {
  441. useMipmaps = 0;
  442. } else if(!strcmp("-nearest", argv[i])) {
  443. linearFiltering = 0;
  444. }
  445. }
  446. glutInitWindowPosition(0, 0);
  447. glutInitWindowSize(600,300);
  448. glutCreateWindow("sprite blast");
  449. glutReshapeFunc(reshape);
  450. glutDisplayFunc(redraw);
  451. glutMouseFunc(mouse);
  452. glutMotionFunc(mouseMotion);
  453. glutVisibilityFunc(visible);
  454. glutKeyboardFunc(key);
  455. glutCreateMenu(menu);
  456. glutAddMenuEntry("Reset time", 0);
  457. glutAddMenuEntry("Constant", 1);
  458. glutAddMenuEntry("Linear", 2);
  459. glutAddMenuEntry("Quadratic", 3);
  460. glutAddMenuEntry("Blend on", 4);
  461. glutAddMenuEntry("Blend off", 5);
  462. glutAddMenuEntry("Threshold 1", 6);
  463. glutAddMenuEntry("Threshold 10", 7);
  464. glutAddMenuEntry("Point smooth on", 8);
  465. glutAddMenuEntry("Point smooth off", 9);
  466. glutAddMenuEntry("Point size 16", 10);
  467. glutAddMenuEntry("Point size 32", 11);
  468. glutAddMenuEntry("Point size 64", 12);
  469. glutAddMenuEntry("Toggle spin", 13);
  470. glutAddMenuEntry("200 points ", 14);
  471. glutAddMenuEntry("500 points ", 15);
  472. glutAddMenuEntry("1000 points ", 16);
  473. glutAddMenuEntry("2000 points ", 17);
  474. glutAddMenuEntry("Quit", 666);
  475. glutAttachMenu(GLUT_RIGHT_BUTTON);
  476. makePointList();
  477. makeSprite();
  478. glShadeModel(GL_FLAT);
  479. glEnable(GL_DEPTH_TEST);
  480. glEnable(GL_POINT_SMOOTH);
  481. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  482. glPointSize(32.0);
  483. #ifdef GL_ARB_point_parameters
  484. glPointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION_ARB, theQuad);
  485. #endif
  486. glutMainLoop();
  487. return 0; /* ANSI C requires main to return int. */
  488. }