r_part.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "quakedef.h"
  16. #include "r_local.h"
  17. #define MAX_PARTICLES 2048 // default max # of particles at one
  18. // time
  19. #define ABSOLUTE_MIN_PARTICLES 512 // no fewer than this no matter what's
  20. // on the command line
  21. int ramp1[8] = {0x6f, 0x6d, 0x6b, 0x69, 0x67, 0x65, 0x63, 0x61};
  22. int ramp2[8] = {0x6f, 0x6e, 0x6d, 0x6c, 0x6b, 0x6a, 0x68, 0x66};
  23. int ramp3[8] = {0x6d, 0x6b, 6, 5, 4, 3};
  24. particle_t *active_particles, *free_particles;
  25. particle_t *particles;
  26. int r_numparticles;
  27. vec3_t r_pright, r_pup, r_ppn;
  28. /*
  29. ===============
  30. R_InitParticles
  31. ===============
  32. */
  33. void R_InitParticles (void)
  34. {
  35. int i;
  36. i = COM_CheckParm ("-particles");
  37. if (i)
  38. {
  39. r_numparticles = (int)(Q_atoi(com_argv[i+1]));
  40. if (r_numparticles < ABSOLUTE_MIN_PARTICLES)
  41. r_numparticles = ABSOLUTE_MIN_PARTICLES;
  42. }
  43. else
  44. {
  45. r_numparticles = MAX_PARTICLES;
  46. }
  47. particles = (particle_t *)
  48. Hunk_AllocName (r_numparticles * sizeof(particle_t), "particles");
  49. }
  50. /*
  51. ===============
  52. R_ClearParticles
  53. ===============
  54. */
  55. void R_ClearParticles (void)
  56. {
  57. int i;
  58. free_particles = &particles[0];
  59. active_particles = NULL;
  60. for (i=0 ;i<r_numparticles ; i++)
  61. particles[i].next = &particles[i+1];
  62. particles[r_numparticles-1].next = NULL;
  63. }
  64. void R_ReadPointFile_f (void)
  65. {
  66. FILE *f;
  67. vec3_t org;
  68. int r;
  69. int c;
  70. particle_t *p;
  71. char name[MAX_OSPATH];
  72. // FIXME sprintf (name,"maps/%s.pts", sv.name);
  73. COM_FOpenFile (name, &f);
  74. if (!f)
  75. {
  76. Con_Printf ("couldn't open %s\n", name);
  77. return;
  78. }
  79. Con_Printf ("Reading %s...\n", name);
  80. c = 0;
  81. for ( ;; )
  82. {
  83. r = fscanf (f,"%f %f %f\n", &org[0], &org[1], &org[2]);
  84. if (r != 3)
  85. break;
  86. c++;
  87. if (!free_particles)
  88. {
  89. Con_Printf ("Not enough free particles\n");
  90. break;
  91. }
  92. p = free_particles;
  93. free_particles = p->next;
  94. p->next = active_particles;
  95. active_particles = p;
  96. p->die = 99999;
  97. p->color = (-c)&15;
  98. p->type = pt_static;
  99. VectorCopy (vec3_origin, p->vel);
  100. VectorCopy (org, p->org);
  101. }
  102. fclose (f);
  103. Con_Printf ("%i points read\n", c);
  104. }
  105. /*
  106. ===============
  107. R_ParticleExplosion
  108. ===============
  109. */
  110. void R_ParticleExplosion (vec3_t org)
  111. {
  112. int i, j;
  113. particle_t *p;
  114. for (i=0 ; i<1024 ; i++)
  115. {
  116. if (!free_particles)
  117. return;
  118. p = free_particles;
  119. free_particles = p->next;
  120. p->next = active_particles;
  121. active_particles = p;
  122. p->die = cl.time + 5;
  123. p->color = ramp1[0];
  124. p->ramp = rand()&3;
  125. if (i & 1)
  126. {
  127. p->type = pt_explode;
  128. for (j=0 ; j<3 ; j++)
  129. {
  130. p->org[j] = org[j] + ((rand()%32)-16);
  131. p->vel[j] = (rand()%512)-256;
  132. }
  133. }
  134. else
  135. {
  136. p->type = pt_explode2;
  137. for (j=0 ; j<3 ; j++)
  138. {
  139. p->org[j] = org[j] + ((rand()%32)-16);
  140. p->vel[j] = (rand()%512)-256;
  141. }
  142. }
  143. }
  144. }
  145. /*
  146. ===============
  147. R_BlobExplosion
  148. ===============
  149. */
  150. void R_BlobExplosion (vec3_t org)
  151. {
  152. int i, j;
  153. particle_t *p;
  154. for (i=0 ; i<1024 ; i++)
  155. {
  156. if (!free_particles)
  157. return;
  158. p = free_particles;
  159. free_particles = p->next;
  160. p->next = active_particles;
  161. active_particles = p;
  162. p->die = cl.time + 1 + (rand()&8)*0.05;
  163. if (i & 1)
  164. {
  165. p->type = pt_blob;
  166. p->color = 66 + rand()%6;
  167. for (j=0 ; j<3 ; j++)
  168. {
  169. p->org[j] = org[j] + ((rand()%32)-16);
  170. p->vel[j] = (rand()%512)-256;
  171. }
  172. }
  173. else
  174. {
  175. p->type = pt_blob2;
  176. p->color = 150 + rand()%6;
  177. for (j=0 ; j<3 ; j++)
  178. {
  179. p->org[j] = org[j] + ((rand()%32)-16);
  180. p->vel[j] = (rand()%512)-256;
  181. }
  182. }
  183. }
  184. }
  185. /*
  186. ===============
  187. R_RunParticleEffect
  188. ===============
  189. */
  190. void R_RunParticleEffect (vec3_t org, vec3_t dir, int color, int count)
  191. {
  192. int i, j;
  193. particle_t *p;
  194. int scale;
  195. if (count > 130)
  196. scale = 3;
  197. else if (count > 20)
  198. scale = 2;
  199. else
  200. scale = 1;
  201. for (i=0 ; i<count ; i++)
  202. {
  203. if (!free_particles)
  204. return;
  205. p = free_particles;
  206. free_particles = p->next;
  207. p->next = active_particles;
  208. active_particles = p;
  209. p->die = cl.time + 0.1*(rand()%5);
  210. p->color = (color&~7) + (rand()&7);
  211. p->type = pt_grav;
  212. for (j=0 ; j<3 ; j++)
  213. {
  214. p->org[j] = org[j] + scale*((rand()&15)-8);
  215. p->vel[j] = dir[j]*15;// + (rand()%300)-150;
  216. }
  217. }
  218. }
  219. /*
  220. ===============
  221. R_LavaSplash
  222. ===============
  223. */
  224. void R_LavaSplash (vec3_t org)
  225. {
  226. int i, j, k;
  227. particle_t *p;
  228. float vel;
  229. vec3_t dir;
  230. for (i=-16 ; i<16 ; i++)
  231. for (j=-16 ; j<16 ; j++)
  232. for (k=0 ; k<1 ; k++)
  233. {
  234. if (!free_particles)
  235. return;
  236. p = free_particles;
  237. free_particles = p->next;
  238. p->next = active_particles;
  239. active_particles = p;
  240. p->die = cl.time + 2 + (rand()&31) * 0.02;
  241. p->color = 224 + (rand()&7);
  242. p->type = pt_grav;
  243. dir[0] = j*8 + (rand()&7);
  244. dir[1] = i*8 + (rand()&7);
  245. dir[2] = 256;
  246. p->org[0] = org[0] + dir[0];
  247. p->org[1] = org[1] + dir[1];
  248. p->org[2] = org[2] + (rand()&63);
  249. VectorNormalize (dir);
  250. vel = 50 + (rand()&63);
  251. VectorScale (dir, vel, p->vel);
  252. }
  253. }
  254. /*
  255. ===============
  256. R_TeleportSplash
  257. ===============
  258. */
  259. void R_TeleportSplash (vec3_t org)
  260. {
  261. int i, j, k;
  262. particle_t *p;
  263. float vel;
  264. vec3_t dir;
  265. for (i=-16 ; i<16 ; i+=4)
  266. for (j=-16 ; j<16 ; j+=4)
  267. for (k=-24 ; k<32 ; k+=4)
  268. {
  269. if (!free_particles)
  270. return;
  271. p = free_particles;
  272. free_particles = p->next;
  273. p->next = active_particles;
  274. active_particles = p;
  275. p->die = cl.time + 0.2 + (rand()&7) * 0.02;
  276. p->color = 7 + (rand()&7);
  277. p->type = pt_grav;
  278. dir[0] = j*8;
  279. dir[1] = i*8;
  280. dir[2] = k*8;
  281. p->org[0] = org[0] + i + (rand()&3);
  282. p->org[1] = org[1] + j + (rand()&3);
  283. p->org[2] = org[2] + k + (rand()&3);
  284. VectorNormalize (dir);
  285. vel = 50 + (rand()&63);
  286. VectorScale (dir, vel, p->vel);
  287. }
  288. }
  289. void R_RocketTrail (vec3_t start, vec3_t end, int type)
  290. {
  291. vec3_t vec;
  292. float len;
  293. int j;
  294. particle_t *p;
  295. VectorSubtract (end, start, vec);
  296. len = VectorNormalize (vec);
  297. while (len > 0)
  298. {
  299. len -= 3;
  300. if (!free_particles)
  301. return;
  302. p = free_particles;
  303. free_particles = p->next;
  304. p->next = active_particles;
  305. active_particles = p;
  306. VectorCopy (vec3_origin, p->vel);
  307. p->die = cl.time + 2;
  308. if (type == 4)
  309. { // slight blood
  310. p->type = pt_slowgrav;
  311. p->color = 67 + (rand()&3);
  312. for (j=0 ; j<3 ; j++)
  313. p->org[j] = start[j] + ((rand()%6)-3);
  314. len -= 3;
  315. }
  316. else if (type == 2)
  317. { // blood
  318. p->type = pt_slowgrav;
  319. p->color = 67 + (rand()&3);
  320. for (j=0 ; j<3 ; j++)
  321. p->org[j] = start[j] + ((rand()%6)-3);
  322. }
  323. else if (type == 6)
  324. { // voor trail
  325. p->color = 9*16 + 8 + (rand()&3);
  326. p->type = pt_static;
  327. p->die = cl.time + 0.3;
  328. for (j=0 ; j<3 ; j++)
  329. p->org[j] = start[j] + ((rand()&15)-8);
  330. }
  331. else if (type == 1)
  332. { // smoke smoke
  333. p->ramp = (rand()&3) + 2;
  334. p->color = ramp3[(int)p->ramp];
  335. p->type = pt_fire;
  336. for (j=0 ; j<3 ; j++)
  337. p->org[j] = start[j] + ((rand()%6)-3);
  338. }
  339. else if (type == 0)
  340. { // rocket trail
  341. p->ramp = (rand()&3);
  342. p->color = ramp3[(int)p->ramp];
  343. p->type = pt_fire;
  344. for (j=0 ; j<3 ; j++)
  345. p->org[j] = start[j] + ((rand()%6)-3);
  346. }
  347. else if (type == 3 || type == 5)
  348. { // tracer
  349. static int tracercount;
  350. p->die = cl.time + 0.5;
  351. p->type = pt_static;
  352. if (type == 3)
  353. p->color = 52 + ((tracercount&4)<<1);
  354. else
  355. p->color = 230 + ((tracercount&4)<<1);
  356. tracercount++;
  357. VectorCopy (start, p->org);
  358. if (tracercount & 1)
  359. {
  360. p->vel[0] = 30*vec[1];
  361. p->vel[1] = 30*-vec[0];
  362. }
  363. else
  364. {
  365. p->vel[0] = 30*-vec[1];
  366. p->vel[1] = 30*vec[0];
  367. }
  368. }
  369. VectorAdd (start, vec, start);
  370. }
  371. }
  372. /*
  373. ===============
  374. R_DrawParticles
  375. ===============
  376. */
  377. void R_DrawParticles (void)
  378. {
  379. particle_t *p, *kill;
  380. float grav;
  381. int i;
  382. float time2, time3;
  383. float time1;
  384. float dvel;
  385. float frametime;
  386. #ifdef GLQUAKE
  387. unsigned char *at;
  388. unsigned char theAlpha;
  389. vec3_t up, right;
  390. float scale;
  391. qboolean alphaTestEnabled;
  392. GL_Bind(particletexture);
  393. alphaTestEnabled = glIsEnabled(GL_ALPHA_TEST);
  394. if (alphaTestEnabled)
  395. glDisable(GL_ALPHA_TEST);
  396. glEnable (GL_BLEND);
  397. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  398. glBegin (GL_TRIANGLES);
  399. VectorScale (vup, 1.5, up);
  400. VectorScale (vright, 1.5, right);
  401. #else
  402. D_StartParticles ();
  403. VectorScale (vright, xscaleshrink, r_pright);
  404. VectorScale (vup, yscaleshrink, r_pup);
  405. VectorCopy (vpn, r_ppn);
  406. #endif
  407. frametime = host_frametime;
  408. time3 = frametime * 15;
  409. time2 = frametime * 10; // 15;
  410. time1 = frametime * 5;
  411. grav = frametime * 800 * 0.05;
  412. dvel = 4*frametime;
  413. for ( ;; )
  414. {
  415. kill = active_particles;
  416. if (kill && kill->die < cl.time)
  417. {
  418. active_particles = kill->next;
  419. kill->next = free_particles;
  420. free_particles = kill;
  421. continue;
  422. }
  423. break;
  424. }
  425. for (p=active_particles ; p ; p=p->next)
  426. {
  427. for ( ;; )
  428. {
  429. kill = p->next;
  430. if (kill && kill->die < cl.time)
  431. {
  432. p->next = kill->next;
  433. kill->next = free_particles;
  434. free_particles = kill;
  435. continue;
  436. }
  437. break;
  438. }
  439. #ifdef GLQUAKE
  440. // hack a scale up to keep particles from disapearing
  441. scale = (p->org[0] - r_origin[0])*vpn[0] + (p->org[1] - r_origin[1])*vpn[1]
  442. + (p->org[2] - r_origin[2])*vpn[2];
  443. if (scale < 20)
  444. scale = 1;
  445. else
  446. scale = 1 + scale * 0.004;
  447. at = (byte *)&d_8to24table[(int)p->color];
  448. if (p->type==pt_fire)
  449. theAlpha = 255*(6-p->ramp)/6;
  450. // theAlpha = 192;
  451. // else if (p->type==pt_explode || p->type==pt_explode2)
  452. // theAlpha = 255*(8-p->ramp)/8;
  453. else
  454. theAlpha = 255;
  455. glColor4ub (*at, *(at+1), *(at+2), theAlpha);
  456. // glColor3ubv (at);
  457. // glColor3ubv ((byte *)&d_8to24table[(int)p->color]);
  458. glTexCoord2f (0,0);
  459. glVertex3fv (p->org);
  460. glTexCoord2f (1,0);
  461. glVertex3f (p->org[0] + up[0]*scale, p->org[1] + up[1]*scale, p->org[2] + up[2]*scale);
  462. glTexCoord2f (0,1);
  463. glVertex3f (p->org[0] + right[0]*scale, p->org[1] + right[1]*scale, p->org[2] + right[2]*scale);
  464. #else
  465. D_DrawParticle (p);
  466. #endif
  467. p->org[0] += p->vel[0]*frametime;
  468. p->org[1] += p->vel[1]*frametime;
  469. p->org[2] += p->vel[2]*frametime;
  470. switch (p->type)
  471. {
  472. case pt_static:
  473. break;
  474. case pt_fire:
  475. p->ramp += time1;
  476. if (p->ramp >= 6)
  477. p->die = -1;
  478. else
  479. p->color = ramp3[(int)p->ramp];
  480. p->vel[2] += grav;
  481. break;
  482. case pt_explode:
  483. p->ramp += time2;
  484. if (p->ramp >=8)
  485. p->die = -1;
  486. else
  487. p->color = ramp1[(int)p->ramp];
  488. for (i=0 ; i<3 ; i++)
  489. p->vel[i] += p->vel[i]*dvel;
  490. p->vel[2] -= grav;
  491. break;
  492. case pt_explode2:
  493. p->ramp += time3;
  494. if (p->ramp >=8)
  495. p->die = -1;
  496. else
  497. p->color = ramp2[(int)p->ramp];
  498. for (i=0 ; i<3 ; i++)
  499. p->vel[i] -= p->vel[i]*frametime;
  500. p->vel[2] -= grav;
  501. break;
  502. case pt_blob:
  503. for (i=0 ; i<3 ; i++)
  504. p->vel[i] += p->vel[i]*dvel;
  505. p->vel[2] -= grav;
  506. break;
  507. case pt_blob2:
  508. for (i=0 ; i<2 ; i++)
  509. p->vel[i] -= p->vel[i]*dvel;
  510. p->vel[2] -= grav;
  511. break;
  512. case pt_slowgrav:
  513. case pt_grav:
  514. p->vel[2] -= grav;
  515. break;
  516. }
  517. }
  518. #ifdef GLQUAKE
  519. glEnd ();
  520. glDisable (GL_BLEND);
  521. if (alphaTestEnabled)
  522. glEnable(GL_ALPHA_TEST);
  523. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  524. #else
  525. D_EndParticles ();
  526. #endif
  527. }