/xbmc/visualizations/Goom/goom2k4-0/src/tentacle3d.c

http://github.com/xbmc/xbmc · C · 299 lines · 234 code · 57 blank · 8 comment · 35 complexity · c1995cf2457f93525d31af127b99486a MD5 · raw file

  1. #include <stdlib.h>
  2. #include "v3d.h"
  3. #include "surf3d.h"
  4. #include "goom_tools.h"
  5. #include "goom_config.h"
  6. #include "goom_plugin_info.h"
  7. #include "tentacle3d.h"
  8. #define D 256.0f
  9. #define nbgrid 6
  10. #define definitionx 15
  11. #define definitionz 45
  12. typedef struct _TENTACLE_FX_DATA {
  13. PluginParam enabled_bp;
  14. PluginParameters params;
  15. float cycle;
  16. grid3d *grille[nbgrid];
  17. float *vals;
  18. #define NB_TENTACLE_COLORS 4
  19. int colors[NB_TENTACLE_COLORS];
  20. int col;
  21. int dstcol;
  22. float lig;
  23. float ligs;
  24. /* statics from pretty_move */
  25. float distt;
  26. float distt2;
  27. float rot; /* entre 0 et 2 * M_PI */
  28. int happens;
  29. int rotation;
  30. int lock;
  31. } TentacleFXData;
  32. static void tentacle_new (TentacleFXData *data);
  33. static void tentacle_update(PluginInfo *goomInfo, Pixel *buf, Pixel *back, int W, int H,
  34. short[2][512], float, int drawit, TentacleFXData *data);
  35. static void tentacle_free (TentacleFXData *data);
  36. /*
  37. * VisualFX wrapper for the tentacles
  38. */
  39. static void tentacle_fx_init(VisualFX *_this, PluginInfo *info) {
  40. TentacleFXData *data = (TentacleFXData*)malloc(sizeof(TentacleFXData));
  41. data->enabled_bp = secure_b_param("Enabled", 1);
  42. data->params = plugin_parameters ("3D Tentacles", 1);
  43. data->params.params[0] = &data->enabled_bp;
  44. data->cycle = 0.0f;
  45. data->col = (0x28<<(ROUGE*8))|(0x2c<<(VERT*8))|(0x5f<<(BLEU*8));
  46. data->dstcol = 0;
  47. data->lig = 1.15f;
  48. data->ligs = 0.1f;
  49. data->distt = 10.0f;
  50. data->distt2 = 0.0f;
  51. data->rot = 0.0f; /* entre 0 et 2 * M_PI */
  52. data->happens = 0;
  53. data->rotation = 0;
  54. data->lock = 0;
  55. data->colors[0] = (0x18<<(ROUGE*8))|(0x4c<<(VERT*8))|(0x2f<<(BLEU*8));
  56. data->colors[1] = (0x48<<(ROUGE*8))|(0x2c<<(VERT*8))|(0x6f<<(BLEU*8));
  57. data->colors[2] = (0x58<<(ROUGE*8))|(0x3c<<(VERT*8))|(0x0f<<(BLEU*8));
  58. data->colors[3] = (0x87<<(ROUGE*8))|(0x55<<(VERT*8))|(0x74<<(BLEU*8));
  59. tentacle_new(data);
  60. _this->params = &data->params;
  61. _this->fx_data = (void*)data;
  62. }
  63. static void tentacle_fx_apply(VisualFX *_this, Pixel *src, Pixel *dest, PluginInfo *goomInfo)
  64. {
  65. TentacleFXData *data = (TentacleFXData*)_this->fx_data;
  66. if (BVAL(data->enabled_bp)) {
  67. tentacle_update(goomInfo, dest, src, goomInfo->screen.width,
  68. goomInfo->screen.height, goomInfo->sound.samples,
  69. (float)goomInfo->sound.accelvar,
  70. goomInfo->curGState->drawTentacle, data);
  71. }
  72. }
  73. static void tentacle_fx_free(VisualFX *_this) {
  74. TentacleFXData *data = (TentacleFXData*)_this->fx_data;
  75. free(data->params.params);
  76. tentacle_free(data);
  77. free(_this->fx_data);
  78. }
  79. VisualFX tentacle_fx_create(void) {
  80. VisualFX fx;
  81. fx.init = tentacle_fx_init;
  82. fx.apply = tentacle_fx_apply;
  83. fx.free = tentacle_fx_free;
  84. return fx;
  85. }
  86. /* ----- */
  87. static void tentacle_free (TentacleFXData *data) {
  88. /* TODO : un vrai FREE GRID!! */
  89. int tmp;
  90. for (tmp=0;tmp<nbgrid;tmp++){
  91. grid3d *g = data->grille[tmp];
  92. free (g->surf.vertex);
  93. free (g->surf.svertex);
  94. free (g);
  95. }
  96. free (data->vals);
  97. }
  98. static void tentacle_new (TentacleFXData *data) {
  99. int tmp;
  100. v3d center = {0,-17.0,0};
  101. data->vals = (float*)malloc ((definitionx+20)*sizeof(float));
  102. for (tmp=0;tmp<nbgrid;tmp++) {
  103. int x,z;
  104. z = 45 + rand() % 30;
  105. x = 85 + rand() % 5;
  106. center.z = z;
  107. data->grille[tmp] = grid3d_new (x, definitionx, z, definitionz + rand() % 10, center);
  108. center.y += 8;
  109. }
  110. }
  111. static inline unsigned char lighten (unsigned char value, float power)
  112. {
  113. int val = value;
  114. float t = (float) val * log10(power) / 2.0;
  115. if (t > 0) {
  116. val = (int) t; /* (32.0f * log (t)); */
  117. if (val > 255)
  118. val = 255;
  119. if (val < 0)
  120. val = 0;
  121. return val;
  122. }
  123. else {
  124. return 0;
  125. }
  126. }
  127. static void lightencolor (int *col, float power)
  128. {
  129. unsigned char *color;
  130. color = (unsigned char *) col;
  131. *color = lighten (*color, power);
  132. color++;
  133. *color = lighten (*color, power);
  134. color++;
  135. *color = lighten (*color, power);
  136. color++;
  137. *color = lighten (*color, power);
  138. }
  139. /* retourne x>>s , en testant le signe de x */
  140. #define ShiftRight(_x,_s) ((_x<0) ? -(-_x>>_s) : (_x>>_s))
  141. static int evolutecolor (unsigned int src,unsigned int dest,
  142. unsigned int mask, unsigned int incr) {
  143. int color = src & (~mask);
  144. src &= mask;
  145. dest &= mask;
  146. if ((src!=mask)
  147. &&(src<dest))
  148. src += incr;
  149. if (src>dest)
  150. src -= incr;
  151. return (src&mask)|color;
  152. }
  153. static void pretty_move (PluginInfo *goomInfo, float cycle, float *dist, float *dist2, float *rotangle, TentacleFXData *fx_data) {
  154. float tmp;
  155. /* many magic numbers here... I don't really like that. */
  156. if (fx_data->happens)
  157. fx_data->happens -= 1;
  158. else if (fx_data->lock == 0) {
  159. fx_data->happens = goom_irand(goomInfo->gRandom,200)?0:100+goom_irand(goomInfo->gRandom,60);
  160. fx_data->lock = fx_data->happens * 3 / 2;
  161. }
  162. else fx_data->lock --;
  163. tmp = fx_data->happens?8.0f:0;
  164. *dist2 = fx_data->distt2 = (tmp + 15.0f*fx_data->distt2)/16.0f;
  165. tmp = 30+D-90.0f*(1.0f+sin(cycle*19/20));
  166. if (fx_data->happens)
  167. tmp *= 0.6f;
  168. *dist = fx_data->distt = (tmp + 3.0f*fx_data->distt)/4.0f;
  169. if (!fx_data->happens){
  170. tmp = M_PI*sin(cycle)/32+3*M_PI/2;
  171. }
  172. else {
  173. fx_data->rotation = goom_irand(goomInfo->gRandom,500)?fx_data->rotation:goom_irand(goomInfo->gRandom,2);
  174. if (fx_data->rotation)
  175. cycle *= 2.0f*M_PI;
  176. else
  177. cycle *= -1.0f*M_PI;
  178. tmp = cycle - (M_PI*2.0) * floor(cycle/(M_PI*2.0));
  179. }
  180. if (abs(tmp-fx_data->rot) > abs(tmp-(fx_data->rot+2.0*M_PI))) {
  181. fx_data->rot = (tmp + 15.0f*(fx_data->rot+2*M_PI)) / 16.0f;
  182. if (fx_data->rot>2.0*M_PI)
  183. fx_data->rot -= 2.0*M_PI;
  184. *rotangle = fx_data->rot;
  185. }
  186. else if (abs(tmp-fx_data->rot) > abs(tmp-(fx_data->rot-2.0*M_PI))) {
  187. fx_data->rot = (tmp + 15.0f*(fx_data->rot-2.0*M_PI)) / 16.0f;
  188. if (fx_data->rot<0.0f)
  189. fx_data->rot += 2.0*M_PI;
  190. *rotangle = fx_data->rot;
  191. }
  192. else
  193. *rotangle = fx_data->rot = (tmp + 15.0f*fx_data->rot) / 16.0f;
  194. }
  195. static void tentacle_update(PluginInfo *goomInfo, Pixel *buf, Pixel *back, int W, int H,
  196. short data[2][512], float rapport, int drawit, TentacleFXData *fx_data) {
  197. int tmp;
  198. int tmp2;
  199. int color;
  200. int colorlow;
  201. float dist,dist2,rotangle;
  202. if ((!drawit) && (fx_data->ligs>0.0f))
  203. fx_data->ligs = -fx_data->ligs;
  204. fx_data->lig += fx_data->ligs;
  205. if (fx_data->lig > 1.01f) {
  206. if ((fx_data->lig>10.0f) | (fx_data->lig<1.1f)) fx_data->ligs = -fx_data->ligs;
  207. if ((fx_data->lig<6.3f)&&(goom_irand(goomInfo->gRandom,30)==0))
  208. fx_data->dstcol=goom_irand(goomInfo->gRandom,NB_TENTACLE_COLORS);
  209. fx_data->col = evolutecolor(fx_data->col,fx_data->colors[fx_data->dstcol],0xff,0x01);
  210. fx_data->col = evolutecolor(fx_data->col,fx_data->colors[fx_data->dstcol],0xff00,0x0100);
  211. fx_data->col = evolutecolor(fx_data->col,fx_data->colors[fx_data->dstcol],0xff0000,0x010000);
  212. fx_data->col = evolutecolor(fx_data->col,fx_data->colors[fx_data->dstcol],0xff000000,0x01000000);
  213. color = fx_data->col;
  214. colorlow = fx_data->col;
  215. lightencolor(&color,fx_data->lig * 2.0f + 2.0f);
  216. lightencolor(&colorlow,(fx_data->lig/3.0f)+0.67f);
  217. rapport = 1.0f + 2.0f * (rapport - 1.0f);
  218. rapport *= 1.2f;
  219. if (rapport > 1.12f)
  220. rapport = 1.12f;
  221. pretty_move (goomInfo, fx_data->cycle, &dist, &dist2, &rotangle, fx_data);
  222. for (tmp=0;tmp<nbgrid;tmp++) {
  223. for (tmp2=0;tmp2<definitionx;tmp2++) {
  224. float val = (float)(ShiftRight(data[0][goom_irand(goomInfo->gRandom,511)],10)) * rapport;
  225. fx_data->vals[tmp2] = val;
  226. }
  227. grid3d_update (fx_data->grille[tmp], rotangle, fx_data->vals, dist2);
  228. }
  229. fx_data->cycle+=0.01f;
  230. for (tmp=0;tmp<nbgrid;tmp++)
  231. grid3d_draw (goomInfo, fx_data->grille[tmp],color,colorlow,dist,buf,back,W,H);
  232. }
  233. else {
  234. fx_data->lig = 1.05f;
  235. if (fx_data->ligs < 0.0f)
  236. fx_data->ligs = -fx_data->ligs;
  237. pretty_move (goomInfo, fx_data->cycle, &dist, &dist2, &rotangle, fx_data);
  238. fx_data->cycle+=0.1f;
  239. if (fx_data->cycle > 1000)
  240. fx_data->cycle = 0;
  241. }
  242. }