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

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