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.

pointblast.c 11KB

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