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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. /*
  10. * $Log: pointblast.c,v $
  11. * Revision 1.2 2000/06/27 17:04:43 brianp
  12. * fixed compiler warnings
  13. *
  14. * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
  15. * Imported sources
  16. *
  17. * Revision 3.3 1998/07/26 01:24:27 brianp
  18. * removed include of gl.h
  19. *
  20. * Revision 3.2 1998/02/14 18:51:46 brianp
  21. * fixed a small compiler warning
  22. *
  23. * Revision 3.1 1998/02/14 18:45:25 brianp
  24. * optimized to use flat shading, don't blend ground polygon
  25. *
  26. * Revision 3.0 1998/02/14 18:42:29 brianp
  27. * initial rev
  28. *
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <math.h> /* for cos(), sin(), and sqrt() */
  34. #define GL_GLEXT_LEGACY
  35. #include <GL/glut.h>
  36. /* Some <math.h> files do not define M_PI... */
  37. #ifndef M_PI
  38. #define M_PI 3.14159265
  39. #endif
  40. #if 0 /* For debugging. */
  41. #undef GL_EXT_point_parameters
  42. #endif
  43. static GLfloat angle = -150; /* in degrees */
  44. static int spin = 0;
  45. static int moving, begin;
  46. static int newModel = 1;
  47. static float theTime;
  48. static int repeat = 1;
  49. static int blend = 1;
  50. int useMipmaps = 1;
  51. int linearFiltering = 1;
  52. static GLfloat constant[3] = { 1/5.0, 0.0, 0.0 };
  53. static GLfloat linear[3] = { 0.0, 1/5.0, 0.0 };
  54. static GLfloat theQuad[3] = { 0.25, 0.0, 1/60.0 };
  55. #define MAX_POINTS 2000
  56. static int numPoints = 200;
  57. static GLfloat pointList[MAX_POINTS][3];
  58. static GLfloat pointTime[MAX_POINTS];
  59. static GLfloat pointVelocity[MAX_POINTS][2];
  60. static GLfloat pointDirection[MAX_POINTS][2];
  61. static int colorList[MAX_POINTS];
  62. static int animate = 1, motion = 0;
  63. static GLfloat colorSet[][4] = {
  64. /* Shades of red. */
  65. { 0.7, 0.2, 0.4, 0.5 },
  66. { 0.8, 0.0, 0.7, 0.5 },
  67. { 1.0, 0.0, 0.0, 0.5 },
  68. { 0.9, 0.3, 0.6, 0.5 },
  69. { 1.0, 0.4, 0.0, 0.5 },
  70. { 1.0, 0.0, 0.5, 0.5 },
  71. };
  72. #define NUM_COLORS (sizeof(colorSet)/sizeof(colorSet[0]))
  73. #define DEAD (NUM_COLORS+1)
  74. #if 0 /* drand48 might be better on Unix machines */
  75. #define RANDOM_RANGE(lo, hi) ((lo) + (hi - lo) * drand48())
  76. #else
  77. static float float_rand(void) { return rand() / (float) RAND_MAX; }
  78. #define RANDOM_RANGE(lo, hi) ((lo) + (hi - lo) * float_rand())
  79. #endif
  80. #define MEAN_VELOCITY 3.0
  81. #define GRAVITY 2.0
  82. #define TIME_DELTA 0.025 /* The speed of time. */
  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. motion = 0;
  113. for (i=0; i<numPoints; i++) {
  114. distance = pointVelocity[i][0] * theTime;
  115. /* X and Z */
  116. pointList[i][0] = pointDirection[i][0] * distance;
  117. pointList[i][2] = pointDirection[i][1] * distance;
  118. /* Z */
  119. pointList[i][1] =
  120. (pointVelocity[i][1] - 0.5 * GRAVITY * pointTime[i])*pointTime[i];
  121. /* If we hit the ground, bounce the point upward again. */
  122. if (pointList[i][1] <= 0.0) {
  123. if (distance > EDGE) {
  124. /* Particle has hit ground past the distance duration of
  125. the particles. Mark particle as dead. */
  126. colorList[i] = NUM_COLORS; /* Not moving. */
  127. continue;
  128. }
  129. pointVelocity[i][1] *= 0.8; /* 80% of previous up velocity. */
  130. pointTime[i] = 0.0; /* Reset the particles sense of up time. */
  131. }
  132. motion = 1;
  133. pointTime[i] += TIME_DELTA;
  134. }
  135. theTime += TIME_DELTA;
  136. if (!motion && !spin) {
  137. if (repeat) {
  138. makePointList();
  139. } else {
  140. glutIdleFunc(NULL);
  141. }
  142. }
  143. }
  144. static void
  145. idle(void)
  146. {
  147. updatePointList();
  148. if (spin) {
  149. angle += 0.3;
  150. newModel = 1;
  151. }
  152. glutPostRedisplay();
  153. }
  154. static void
  155. visible(int vis)
  156. {
  157. if (vis == GLUT_VISIBLE) {
  158. if (animate && (motion || spin)) {
  159. glutIdleFunc(idle);
  160. }
  161. } else {
  162. glutIdleFunc(NULL);
  163. }
  164. }
  165. static void
  166. recalcModelView(void)
  167. {
  168. glPopMatrix();
  169. glPushMatrix();
  170. glRotatef(angle, 0.0, 1.0, 0.0);
  171. newModel = 0;
  172. }
  173. static void
  174. redraw(void)
  175. {
  176. int i;
  177. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  178. if (newModel)
  179. recalcModelView();
  180. glDepthMask(GL_FALSE);
  181. /* Draw the floor. */
  182. /* glEnable(GL_TEXTURE_2D);*/
  183. glColor3f(0.5, 1.0, 0.5);
  184. glBegin(GL_QUADS);
  185. glTexCoord2f(0.0, 0.0);
  186. glVertex3f(-EDGE, -0.05, -EDGE);
  187. glTexCoord2f(20.0, 0.0);
  188. glVertex3f(EDGE, -0.05, -EDGE);
  189. glTexCoord2f(20.0, 20.0);
  190. glVertex3f(EDGE, -0.05, EDGE);
  191. glTexCoord2f(0.0, 20.0);
  192. glVertex3f(-EDGE, -0.05, EDGE);
  193. glEnd();
  194. /* Allow particles to blend with each other. */
  195. glDepthMask(GL_TRUE);
  196. if (blend)
  197. glEnable(GL_BLEND);
  198. glDisable(GL_TEXTURE_2D);
  199. glBegin(GL_POINTS);
  200. for (i=0; i<numPoints; i++) {
  201. /* Draw alive particles. */
  202. if (colorList[i] != DEAD) {
  203. glColor4fv(colorSet[colorList[i]]);
  204. glVertex3fv(pointList[i]);
  205. }
  206. }
  207. glEnd();
  208. glDisable(GL_BLEND);
  209. glutSwapBuffers();
  210. }
  211. /* ARGSUSED2 */
  212. static void
  213. mouse(int button, int state, int x, int y)
  214. {
  215. /* Scene can be spun around Y axis using left
  216. mouse button movement. */
  217. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  218. moving = 1;
  219. begin = x;
  220. }
  221. if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
  222. moving = 0;
  223. }
  224. }
  225. /* ARGSUSED1 */
  226. static void
  227. mouseMotion(int x, int y)
  228. {
  229. if (moving) {
  230. angle = angle + (x - begin);
  231. begin = x;
  232. newModel = 1;
  233. glutPostRedisplay();
  234. }
  235. }
  236. static void
  237. menu(int option)
  238. {
  239. switch (option) {
  240. case 0:
  241. makePointList();
  242. break;
  243. #if GL_EXT_point_parameters
  244. case 1:
  245. glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, constant);
  246. break;
  247. case 2:
  248. glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, linear);
  249. break;
  250. case 3:
  251. glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, theQuad);
  252. break;
  253. #endif
  254. case 4:
  255. blend = 1;
  256. break;
  257. case 5:
  258. blend = 0;
  259. break;
  260. #if GL_EXT_point_parameters
  261. case 6:
  262. glPointParameterfEXT(GL_POINT_FADE_THRESHOLD_SIZE_EXT, 1.0);
  263. break;
  264. case 7:
  265. glPointParameterfEXT(GL_POINT_FADE_THRESHOLD_SIZE_EXT, 10.0);
  266. break;
  267. #endif
  268. case 8:
  269. glEnable(GL_POINT_SMOOTH);
  270. break;
  271. case 9:
  272. glDisable(GL_POINT_SMOOTH);
  273. break;
  274. case 10:
  275. glPointSize(2.0);
  276. break;
  277. case 11:
  278. glPointSize(4.0);
  279. break;
  280. case 12:
  281. glPointSize(8.0);
  282. break;
  283. case 13:
  284. spin = 1 - spin;
  285. if (animate && (spin || motion)) {
  286. glutIdleFunc(idle);
  287. } else {
  288. glutIdleFunc(NULL);
  289. }
  290. break;
  291. case 14:
  292. numPoints = 200;
  293. break;
  294. case 15:
  295. numPoints = 500;
  296. break;
  297. case 16:
  298. numPoints = 1000;
  299. break;
  300. case 17:
  301. numPoints = 2000;
  302. break;
  303. case 666:
  304. exit(0);
  305. }
  306. glutPostRedisplay();
  307. }
  308. /* ARGSUSED1 */
  309. static void
  310. key(unsigned char c, int x, int y)
  311. {
  312. switch (c) {
  313. case 13:
  314. animate = 1 - animate; /* toggle. */
  315. if (animate && (motion || spin)) {
  316. glutIdleFunc(idle);
  317. } else {
  318. glutIdleFunc(NULL);
  319. }
  320. break;
  321. case ' ':
  322. animate = 1;
  323. makePointList();
  324. glutIdleFunc(idle);
  325. break;
  326. case 27:
  327. exit(0);
  328. }
  329. }
  330. /* Nice floor texture tiling pattern. */
  331. static char *circles[] = {
  332. "....xxxx........",
  333. "..xxxxxxxx......",
  334. ".xxxxxxxxxx.....",
  335. ".xxx....xxx.....",
  336. "xxx......xxx....",
  337. "xxx......xxx....",
  338. "xxx......xxx....",
  339. "xxx......xxx....",
  340. ".xxx....xxx.....",
  341. ".xxxxxxxxxx.....",
  342. "..xxxxxxxx......",
  343. "....xxxx........",
  344. "................",
  345. "................",
  346. "................",
  347. "................",
  348. };
  349. static void
  350. makeFloorTexture(void)
  351. {
  352. GLubyte floorTexture[16][16][3];
  353. GLubyte *loc;
  354. int s, t;
  355. /* Setup RGB image for the texture. */
  356. loc = (GLubyte*) floorTexture;
  357. for (t = 0; t < 16; t++) {
  358. for (s = 0; s < 16; s++) {
  359. if (circles[t][s] == 'x') {
  360. /* Nice blue. */
  361. loc[0] = 0x1f;
  362. loc[1] = 0x1f;
  363. loc[2] = 0x8f;
  364. } else {
  365. /* Light gray. */
  366. loc[0] = 0xca;
  367. loc[1] = 0xca;
  368. loc[2] = 0xca;
  369. }
  370. loc += 3;
  371. }
  372. }
  373. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  374. if (useMipmaps) {
  375. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  376. GL_LINEAR_MIPMAP_LINEAR);
  377. gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 16, 16,
  378. GL_RGB, GL_UNSIGNED_BYTE, floorTexture);
  379. } else {
  380. if (linearFiltering) {
  381. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  382. } else {
  383. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  384. }
  385. glTexImage2D(GL_TEXTURE_2D, 0, 3, 16, 16, 0,
  386. GL_RGB, GL_UNSIGNED_BYTE, floorTexture);
  387. }
  388. }
  389. int
  390. main(int argc, char **argv)
  391. {
  392. int i;
  393. glutInit(&argc, argv);
  394. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  395. for (i=1; i<argc; i++) {
  396. if(!strcmp("-noms", argv[i])) {
  397. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  398. printf("forcing no multisampling\n");
  399. } else if(!strcmp("-nomipmaps", argv[i])) {
  400. useMipmaps = 0;
  401. } else if(!strcmp("-nearest", argv[i])) {
  402. linearFiltering = 0;
  403. }
  404. }
  405. glutCreateWindow("point burst");
  406. glutDisplayFunc(redraw);
  407. glutMouseFunc(mouse);
  408. glutMotionFunc(mouseMotion);
  409. glutVisibilityFunc(visible);
  410. glutKeyboardFunc(key);
  411. glutCreateMenu(menu);
  412. glutAddMenuEntry("Reset time", 0);
  413. glutAddMenuEntry("Constant", 1);
  414. glutAddMenuEntry("Linear", 2);
  415. glutAddMenuEntry("Quadratic", 3);
  416. glutAddMenuEntry("Blend on", 4);
  417. glutAddMenuEntry("Blend off", 5);
  418. glutAddMenuEntry("Threshold 1", 6);
  419. glutAddMenuEntry("Threshold 10", 7);
  420. glutAddMenuEntry("Point smooth on", 8);
  421. glutAddMenuEntry("Point smooth off", 9);
  422. glutAddMenuEntry("Point size 2", 10);
  423. glutAddMenuEntry("Point size 4", 11);
  424. glutAddMenuEntry("Point size 8", 12);
  425. glutAddMenuEntry("Toggle spin", 13);
  426. glutAddMenuEntry("200 points ", 14);
  427. glutAddMenuEntry("500 points ", 15);
  428. glutAddMenuEntry("1000 points ", 16);
  429. glutAddMenuEntry("2000 points ", 17);
  430. glutAddMenuEntry("Quit", 666);
  431. glutAttachMenu(GLUT_RIGHT_BUTTON);
  432. glShadeModel(GL_FLAT);
  433. glEnable(GL_DEPTH_TEST);
  434. glEnable(GL_POINT_SMOOTH);
  435. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  436. glPointSize(8.0);
  437. #if GL_EXT_point_parameters
  438. glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, theQuad);
  439. #endif
  440. glMatrixMode(GL_PROJECTION);
  441. gluPerspective( /* field of view in degree */ 40.0,
  442. /* aspect ratio */ 1.0,
  443. /* Z near */ 0.5, /* Z far */ 40.0);
  444. glMatrixMode(GL_MODELVIEW);
  445. gluLookAt(0.0, 1.0, 8.0, /* eye location */
  446. 0.0, 1.0, 0.0, /* center is at (0,0,0) */
  447. 0.0, 1.0, 0.); /* up is in postivie Y direction */
  448. glPushMatrix(); /* dummy push so we can pop on model
  449. recalc */
  450. makePointList();
  451. makeFloorTexture();
  452. glutMainLoop();
  453. return 0; /* ANSI C requires main to return int. */
  454. }