MythTV  master
tentacle3d.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 
3 #include "v3d.h"
4 #include "surf3d.h"
5 #include "goom_tools.h"
6 #include "goomconfig.h"
7 
8 #define D 256.0F
9 
10 #define nbgrid 6
11 #define definitionx 15
12 #define definitionz 45
13 
14 static float cycle = 0.0F;
15 static grid3d *grille[nbgrid];
16 static float *vals;
17 
18 /* Prototypes to keep gcc from spewing warnings */
19 void tentacle_free (void);
20 void tentacle_new (void);
21 void tentacle_update(int *buf, int *back, int W, int H, short data[2][512], float rapport, int drawit);
22 
23 void tentacle_free (void) {
24  free (vals);
25  for (int tmp=0;tmp<nbgrid;tmp++) {
26  grid3d_free(&(grille[tmp]));
27  }
28 }
29 
30 void tentacle_new (void) {
31  v3d center = {0,-17.0,0};
32  vals = (float*)malloc ((definitionx+20)*sizeof(float));
33 
34  for (int tmp=0;tmp<nbgrid;tmp++) {
35  // Pseudo-random is good enough. Don't need a true random.
36  // NOLINTNEXTLINE(cert-msc30-c,cert-msc50-cpp)
37  int z = 45+rand()%30;
38  // NOLINTNEXTLINE(cert-msc30-c,cert-msc50-cpp)
39  int x = 85+rand()%5;
40  center.z = z;
41  // NOLINTNEXTLINE(cert-msc30-c,cert-msc50-cpp)
42  grille[tmp] = grid3d_new (x,definitionx,z,definitionz+rand()%10,center);
43  center.y += 8;
44  }
45 }
46 
47 static inline unsigned char
48 lighten (unsigned char value, float power)
49 {
50  int val = value;
51  float t = (float) val * log10f(power) / 2.0F;
52 
53  if (t > 0) {
54  val = (int) t; // (32.0F * log (t));
55  if (val > 255)
56  val = 255;
57  if (val < 0)
58  val = 0;
59  return val;
60  }
61  return 0;
62 }
63 
64 static void
65 lightencolor (int *col, float power)
66 {
67  unsigned char *color = (unsigned char *) col;
68  *color = lighten (*color, power);
69  color++;
70  *color = lighten (*color, power);
71  color++;
72  *color = lighten (*color, power);
73  color++;
74  *color = lighten (*color, power);
75 }
76 
77 // retourne x>>s , en testant le signe de x
78 #define ShiftRight(_x,_s) (((_x)<0) ? -(-(_x)>>(_s)) : ((_x)>>(_s)))
79 
80 static
81 int evolutecolor (unsigned int src,unsigned int dest, unsigned int mask, unsigned int incr) {
82  int color = src & (~mask);
83  src &= mask;
84  dest &= mask;
85 
86  if ((src!=mask)
87  &&(src<dest))
88  src += incr;
89 
90  if (src>dest)
91  src -= incr;
92  return (src&mask)|color;
93 }
94 
95 static void pretty_move (float lcycle, float *dist,float *dist2, float *rotangle) {
96  static float s_distT = 10.0F;
97  static float s_distT2 = 0.0F;
98  static float s_rot = 0.0F; // entre 0 et 2 * M_PI
99  static int s_happens = 0;
100  static int s_rotation = 0;
101  static int s_lock = 0;
102 
103  if (s_happens)
104  s_happens -= 1;
105  else if (s_lock == 0) {
106  s_happens = iRAND(200)?0:100+iRAND(60);
107  s_lock = s_happens * 3 / 2;
108  }
109  else s_lock --;
110 // happens = 1;
111 
112  float tmp = s_happens?8.0F:0;
113  *dist2 = s_distT2 = (tmp + 15.0F*s_distT2)/16.0F;
114 
115  tmp = 30+D-90.0F*(1.0F+sinf(lcycle*19/20));
116  if (s_happens)
117  tmp *= 0.6F;
118 
119  *dist = s_distT = (tmp + 3.0F*s_distT)/4.0F;
120 
121  if (!s_happens){
122  tmp = M_PI_F*sinf(lcycle)/32+3*M_PI_F/2;
123  }
124  else {
125  s_rotation = iRAND(500)?s_rotation:iRAND(2);
126  if (s_rotation)
127  lcycle *= 2.0F*M_PI_F;
128  else
129  lcycle *= -1.0F*M_PI_F;
130  tmp = lcycle - (M_PI_F*2.0F) * floorf(lcycle/(M_PI_F*2.0F));
131  }
132 
133  if (fabsf(tmp-s_rot) > fabsf(tmp-(s_rot+2.0F*M_PI_F))) {
134  s_rot = (tmp + 15.0F*(s_rot+2*M_PI_F)) / 16.0F;
135  if (s_rot>2.0F*M_PI_F)
136  s_rot -= 2.0F*M_PI_F;
137  *rotangle = s_rot;
138  }
139  else if (fabsf(tmp-s_rot) > fabsf(tmp-(s_rot-2.0F*M_PI_F))) {
140  s_rot = (tmp + 15.0F*(s_rot-2.0F*M_PI_F)) / 16.0F;
141  if (s_rot<0.0F)
142  s_rot += 2.0F*M_PI_F;
143  *rotangle = s_rot;
144  }
145  else
146  *rotangle = s_rot = (tmp + 15.0F*s_rot) / 16.0F;
147 }
148 
149 void tentacle_update(int *buf, int *back, int W, int H, short data[2][512], float rapport, int drawit) {
150  static int s_colors[] = {
151  (0x18<<(ROUGE*8))|(0x4c<<(VERT*8))|(0x2f<<(BLEU*8)),
152  (0x48<<(ROUGE*8))|(0x2c<<(VERT*8))|(0x6f<<(BLEU*8)),
153  (0x58<<(ROUGE*8))|(0x3c<<(VERT*8))|(0x0f<<(BLEU*8))};
154 
155  static int s_col = (0x28<<(ROUGE*8))|(0x2c<<(VERT*8))|(0x5f<<(BLEU*8));
156  static int s_dstCol = 0;
157  static float s_lig = 1.15F;
158  static float s_ligs = 0.1F;
159 
160  float dist = NAN;
161  float dist2 = NAN;
162  float rotangle = NAN;
163 
164  if ((!drawit) && (s_ligs>0.0F))
165  s_ligs = -s_ligs;
166 
167  s_lig += s_ligs;
168 
169  if (s_lig > 1.01F) {
170  if ((s_lig>10.0F) | (s_lig<1.1F)) s_ligs = -s_ligs;
171 
172  if ((s_lig<6.3F)&&(iRAND(30)==0))
173  s_dstCol=iRAND(3);
174 
175  s_col = evolutecolor(s_col,s_colors[s_dstCol],0xff,0x01);
176  s_col = evolutecolor(s_col,s_colors[s_dstCol],0xff00,0x0100);
177  s_col = evolutecolor(s_col,s_colors[s_dstCol],0xff0000,0x010000);
178  s_col = evolutecolor(s_col,s_colors[s_dstCol],0xff000000,0x01000000);
179 
180  int color = s_col;
181  int colorlow = s_col;
182 
183  lightencolor(&color,s_lig * 2.0F + 2.0F);
184  lightencolor(&colorlow,(s_lig/3.0F)+0.67F);
185 
186  rapport = 1.0F + 2.0F * (rapport - 1.0F);
187  rapport *= 1.2F;
188  if (rapport > 1.12F)
189  rapport = 1.12F;
190 
191  pretty_move (cycle,&dist,&dist2,&rotangle);
192 
193  for (int tmp=0;tmp<nbgrid;tmp++) {
194  for (int tmp2=0;tmp2<definitionx;tmp2++) {
195  float val = (float)(ShiftRight(data[0][iRAND(511)],10)) * rapport;
196  vals[tmp2] = val;
197  }
198 
199  grid3d_update (grille[tmp],rotangle, vals, dist2);
200  }
201  cycle+=0.01F;
202  for (int tmp=0;tmp<nbgrid;tmp++)
203  grid3d_draw (grille[tmp],color,colorlow,dist,buf,back,W,H);
204  }
205  else {
206  s_lig = 1.05F;
207  if (s_ligs < 0.0F)
208  s_ligs = -s_ligs;
209  pretty_move (cycle,&dist,&dist2,&rotangle);
210  cycle+=0.1F;
211  if (cycle > 1000)
212  cycle = 0;
213  }
214 }
#define D
Definition: tentacle3d.c:8
void tentacle_free(void)
Definition: tentacle3d.c:23
float z
Definition: v3d.h:11
#define ROUGE
position des composantes
Definition: goomconfig.h:10
static guint32 * tmp
Definition: goom_core.c:35
static float * vals
Definition: tentacle3d.c:16
void tentacle_new(void)
Definition: tentacle3d.c:30
Definition: surf3d.h:14
static unsigned char lighten(unsigned char value, float power)
Definition: tentacle3d.c:48
void grid3d_free(grid3d **grid)
Definition: surf3d.c:6
#define M_PI_F
Definition: goom_tools.h:7
void tentacle_update(int *buf, int *back, int W, int H, short data[2][512], float rapport, int drawit)
Definition: tentacle3d.c:149
void grid3d_draw(grid3d *g, int color, int colorlow, int dist, int *buf, int *back, int W, int H)
Definition: surf3d.c:62
void grid3d_update(grid3d *g, float angle, const float *vals, float dist)
Definition: surf3d.c:98
grid3d * grid3d_new(int sizex, int defx, int sizez, int defz, v3d center)
Definition: surf3d.c:14
#define definitionz
Definition: tentacle3d.c:12
static void pretty_move(float lcycle, float *dist, float *dist2, float *rotangle)
Definition: tentacle3d.c:95
float y
Definition: v3d.h:11
#define BLEU
Definition: goomconfig.h:11
static int evolutecolor(unsigned int src, unsigned int dest, unsigned int mask, unsigned int incr)
Definition: tentacle3d.c:81
Definition: v3d.h:10
static void lightencolor(int *col, float power)
Definition: tentacle3d.c:65
#define nbgrid
Definition: tentacle3d.c:10
#define definitionx
Definition: tentacle3d.c:11
static guint32 * back
Definition: goom_core.c:34
#define iRAND(i)
Definition: goom_tools.h:33
#define VERT
Definition: goomconfig.h:12
static grid3d * grille[nbgrid]
Definition: tentacle3d.c:15
#define ShiftRight(_x, _s)
Definition: tentacle3d.c:78
static float cycle
Definition: tentacle3d.c:14