Debian Codegraph @

apps/demos/qmorf/FlashBackground.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 //
38 // Implementation of cool backgrounds from Drew Olbrich
39 // Modified by Gavin Bell
40 //
41
42 #include <cmath>
43 #include <unistd.h>
44 #include <cstdlib>
45
46 #include <Inventor/SbLinear.h>
47 #include <Inventor/nodes/SoGroup.h>
48 #include <Inventor/nodes/SoCoordinate3.h>
49 #include <Inventor/nodes/SoBaseColor.h>
50 #include <Inventor/nodes/SoMaterialBinding.h>
51 #include <Inventor/nodes/SoLightModel.h>
52 #include <Inventor/nodes/SoPickStyle.h>
53 #include <Inventor/nodes/SoQuadMesh.h>
54 #include <Inventor/nodes/SoOrthographicCamera.h>
55
56 #include "Background.h"
57
58 #define ABS(a) ((a) < 0.0 ? -(a) : (a))
59 #define CLAMP(a, b, c) ((b) > (a) ? (b) : ((a) > (c) ? (c) : (a)))
60 #define SMOOTH(a) ((a)*(a)*(3.0 - 2.0*(a)))
61
62 static const int HASH_SIZE=751; // two of my favorite prime numbers
63 static const int HASH_VALUE=911;
64
65 static const int32_t BIG_NOISE=100000; // noise repeats this often
66
67 //
68 // Constructor. The background is always the same number of rows and
69 // columns (that number is the size argument that gets passed).
70 //
71 FlashBackground::FlashBackground(int size)
72 {
73 //
74 // Must create hash table...
75 //
76 hash_table = new double[HASH_SIZE];
77
78 srand48(getpid());
79 int i;
80 for (i = 0; i < HASH_SIZE; i++)
81 hash_table[i] = drand48();
82
83 //
84 // And must create the scene graph
85 //
86 meshSize = size;
87 int nverts = (size+1)*(size+1);
88
89 // Set up a 0.0 to 1.0 view
90 SoOrthographicCamera *oc = new SoOrthographicCamera;
91 //
92 // The LEAVE_ALONE viewport mapping is used because we don't
93 // really care if the background is squished/stretched, but it
94 // MUST always fill up the entire viewport.
95 //
96 oc->viewportMapping = SoCamera::LEAVE_ALONE;
97 oc->position.setValue(0.5, 0.5, 1);
98 oc->height = 0.9;
99 oc->aspectRatio = 1.0;
100 oc->farDistance = 1001.0;
101 oc->focalDistance = 999.0;
102 // Defaults for other stuff are OK.
103 sceneGraph->addChild(oc);
104
105 coordinates = new SoCoordinate3;
106 //
107 // Pre-allocate enough room in the coordinates node for the
108 // vertices
109 //
110 coordinates->point.insertSpace(0, nverts);
111 sceneGraph->addChild(coordinates);
112
113 colors = new SoBaseColor;
114 colors->rgb.insertSpace(0, nverts);
115 sceneGraph->addChild(colors);
116
117 SoMaterialBinding *mb = new SoMaterialBinding;
118 mb->value = SoMaterialBinding::PER_VERTEX_INDEXED;
119 sceneGraph->addChild(mb);
120
121 SoLightModel *lm = new SoLightModel;
122 lm->model = SoLightModel::BASE_COLOR;
123 sceneGraph->addChild(lm);
124
125 SoPickStyle *ps = new SoPickStyle;
126 ps->style = SoPickStyle::UNPICKABLE;
127 sceneGraph->addChild(ps);
128
129 SoQuadMesh *qm = new SoQuadMesh;
130 qm->verticesPerRow = meshSize+1;
131 qm->verticesPerColumn = meshSize+1;
132 sceneGraph->addChild(qm);
133
134 //
135 // Pre-calculate some different shapes and colors. These are
136 // linearly interpolated in a random order to get the nice
137 // animation. It takes a significant time to calculate some of
138 // the shape/color functions; pre-calculating them avoids annoying
139 // jumps in the animation.
140 //
141 shapeTable = new SbVec3f*[NUM_SHAPES];
142 for (i = 0; i < NUM_SHAPES; i++)
143 {
144 shapeTable[i] = new SbVec3f[nverts];
145 calculateShape((Shape)i, shapeTable[i]);
146 }
147 schemeTable = new SbColor*[NUM_SHAPES];
148 for (i = 0; i < NUM_SCHEMES; i++)
149 {
150 schemeTable[i] = new SbColor[nverts];
151 calculateColor((Shade)(lrand48()%NUM_SHADES), (Scheme)i,
152 schemeTable[i]);
153 }
154 //
155 // Start out with a random shape and color
156 //
157 currentShape = (Shape)(lrand48()%NUM_SHAPES);
158 currentScheme = (Scheme)(lrand48()%NUM_SCHEMES);
159 nextShape = (Shape)(lrand48()%NUM_SHAPES);
160 nextScheme = (Scheme)(lrand48()%NUM_SCHEMES);
161 //
162 // And start at time 0.0
163 //
164 updateSceneShape(0.0);
165 updateSceneColor(0.0);
166 }
167
168 FlashBackground::~FlashBackground()
169 {
170 //
171 // Must get rid of all the stuff allocated or ref'ed in the
172 // constructor. Note: the sceneGraph member variable is allocated
173 // and freed in the Background base class.
174 //
175 delete[] hash_table;
176
177 int i;
178 for (i = 0; i < NUM_SHAPES; i++)
179 {
180 delete[] shapeTable[i];
181 }
182 delete[] shapeTable;
183 for (i = 0; i < NUM_SCHEMES; i++)
184 {
185 delete[] schemeTable[i];
186 }
187 delete[] schemeTable;
188 }
189
190 //
191 // Noise() - returns noise at integer boundaries
192 //
193 // Warning! This only works for positive values!
194 //
195 double
196 FlashBackground::Noise(uint32_t x, uint32_t y, uint32_t z)
197 {
198 uint32_t index;
199 double result;
200
201 index = x;
202 index = index*HASH_VALUE + y;
203 index = index*HASH_VALUE + z;
204 result = hash_table[index % HASH_SIZE];
205
206 // add a little high order noise to make it look more convincing
207 index = HASH_VALUE + x*1000;
208 index = index*HASH_VALUE + y*1000;
209 index = index*HASH_VALUE + z*1000;
210 result += hash_table[index % HASH_SIZE];
211
212 if (result >= 1.0)
213 result -= 1.0;
214
215 return result;
216 }
217
218 //
219 // Noise() - the noise function
220 //
221 double
222 FlashBackground::Noise(SbVec3f &p)
223 {
224 uint32_t x0, y0, z0;
225 uint32_t x1, y1, z1;
226 double sx, sy, sz;
227 double tx, ty, tz;
228 double n;
229 double result;
230
231 SbVec3f q = p;
232 if (q[0] < 0.0)
233 q[0] += BIG_NOISE;
234 q[0] = fmod(q[0], (double) BIG_NOISE);
235 if (q[1] < 0.0)
236 q[1] += BIG_NOISE;
237 q[1] = fmod(q[1], (double) BIG_NOISE);
238 if (q[2] < 0.0)
239 q[2] += BIG_NOISE;
240 q[2] = fmod(q[2], (double) BIG_NOISE);
241
242 x0 = (uint32_t) q[0];
243 y0 = (uint32_t) q[1];
244 z0 = (uint32_t) q[2];
245
246 x1 = (x0 + 1) % BIG_NOISE;
247 y1 = (y0 + 1) % BIG_NOISE;
248 z1 = (z0 + 1) % BIG_NOISE;
249
250 sx = SMOOTH(q[0] - x0);
251 sy = SMOOTH(q[1] - y0);
252 sz = SMOOTH(q[2] - z0);
253
254 tx = 1.0 - sx;
255 ty = 1.0 - sy;
256 tz = 1.0 - sz;
257
258 n = Noise(x0, y0, z0);
259 result = tx*ty*tz*n;
260 n = Noise(x0, y0, z1);
261 result += tx*ty*sz*n;
262 n = Noise(x0, y1, z0);
263 result += tx*sy*tz*n;
264 n = Noise(x0, y1, z1);
265 result += tx*sy*sz*n;
266 n = Noise(x1, y0, z0);
267 result += sx*ty*tz*n;
268 n = Noise(x1, y0, z1);
269 result += sx*ty*sz*n;
270 n = Noise(x1, y1, z0);
271 result += sx*sy*tz*n;
272 n = Noise(x1, y1, z1);
273 result += sx*sy*sz*n;
274
275 return result;
276 }
277
278 //
279 // Turbulence() - the turbulence function
280 //
281 // The variable 'octaves' holds the number of octaves of noise
282 // we want to make the turbulence out of.
283 //
284 double
285 FlashBackground::Turbulence(SbVec3f &p, int octaves)
286 {
287 double scale = 1.0;
288 double inv_scale = 1.0;
289 double result = 0.0;
290 double max_total = 0.0;
291 int i;
292 SbVec3f q;
293
294 for (i = 0; i < octaves; i++)
295 {
296 max_total += inv_scale;
297 q = p * scale;
298 result += Noise(q)*inv_scale;
299 if (i != octaves - 1)
300 {
301 scale *= 2.0;
302 inv_scale *= 0.5;
303 }
304 }
305
306 result /= max_total; /* scale it to [0..1] */
307
308 return result;
309 }
310
311 void
312 FlashBackground::shapeFunction(Shape shape, SbVec3f &xy)
313 {
314 double dx, dy;
315 double l;
316 double theta;
317
318 switch (shape) {
319 case SHAPE_CLOUDS :
320 xy[1] = (0.5 - xy[1])*2.0;
321 xy[1] = xy[1]*xy[1]*xy[1];
322 xy[1] = 1.0 - (xy[1]/2.0 + 0.5);
323 break;
324 case SHAPE_FLAT :
325 break;
326 case SHAPE_SWIRL :
327 dx = 0.5 - xy[0];
328 dy = 0.5 - xy[1];
329 l = sqrt(dx*dx + dy*dy);
330 if (dx != 0.0 || dy != 0.0)
331 {
332 theta = atan2(dy, dx);
333 xy[0] = 0.5 + cos(theta + l*5.0)*l*1.5;
334 xy[1] = 0.5 + sin(theta + l*5.0)*l*1.5;
335 }
336 break;
337 case SHAPE_PINCH :
338 dx = xy[0] - 0.5;
339 dy = xy[1] - 0.5;
340 l = sqrt(dx*dx + dy*dy);
341 xy[0] = 0.5 + dx*l*l*4.0;
342 xy[1] = 0.5 + dy*l*l*4.0;
343 break;
344 case SHAPE_WAVY :
345 xy[1] += sin(xy[0]*15.0)*0.06;
346 xy[1] = 0.5 + (xy[1] - 0.5)*1.3;
347 break;
348 case SHAPE_SCRUNCH :
349 xy[1] = xy[1]*xy[1]*xy[1];
350 break;
351 case SHAPE_WALLS :
352 dx = (1.0 - ABS(xy[0] - 0.5)*2.0);
353 dx = 1.0 - exp(dx);
354 if (xy[1] != 1.0)
355 xy[1] += xy[1]*dx*0.8;
356 break;
357 case SHAPE_PULL :
358 dx = (xy[0] - 0.5)*2.0;
359 dx *= 4.0;
360 dx = 1.0/(1.0 + dx*dx);
361 xy[1] += xy[1]*dx;
362 break;
363 case NUM_SHAPES:
364 fprintf(stderr, "BLEUCH: NUM_SHAPES CASE REACHED\n");
365 break;
366 }
367 }
368
369 double
370 FlashBackground::shadeFunction(Shade shade, double i, double j)
371 {
372 double gray;
373 double result;
374 SbVec3f v;
375
376 switch (shade) {
377 case SHADE_BARS :
378 j *= meshSize;
379 gray = (meshSize - j)*120/meshSize + ((int) (j*4129) % 30);
380 result = gray/255.0;
381 break;
382 case SHADE_STATIC :
383 v.setValue(i*20.0, j*20.0, 0.0);
384 result = drand48()*0.5 + 0.25;
385 result *= (1.0 - j)*0.8;
386 break;
387 case SHADE_FOG :
388 v.setValue(i*6.0, j*6.0, 0.0);
389 result = Turbulence(v, 5);
390 result *= (1.0 - j)*0.8;
391 break;
392 case SHADE_MARBLE :
393 v.setValue(i*5.0, j*5.0, 0.0);
394 result = sin(i*30.0 + 15.0*Turbulence(v, 5))*0.5 + 0.5;
395 result = result*0.5 + 0.25;
396 result *= (1.0 - j)*0.8;
397 break;
398 case SHADE_HAZY :
399 j *= meshSize;
400 gray = (meshSize - j)*120/meshSize + ((int) (j*4129) % 30);
401 result = gray/255.0;
402 v.setValue(i*4.5, j*6.0, 0.0);
403 v[0] += v[1]/3.1;
404 result *= Turbulence(v, 4);
405 break;
406 case NUM_SHADES:
407 fprintf(stderr, "BLEUCH: NUM_SHADES CASE REACHED\n");
408 break;
409 }
410 return result;
411 }
412
413 void
414 FlashBackground::colorFunction(Scheme cscheme, SbColor &c, double y)
415 {
416 switch (cscheme) {
417 case COLOR_GRAY :
418 break;
419 case COLOR_NOISE :
420 c[0] += (drand48()*2.0 - 1.0)*0.03;
421 c[1] += (drand48()*2.0 - 1.0)*0.03;
422 c[2] += (drand48()*2.0 - 1.0)*0.03;
423 break;
424 case COLOR_PURPLE :
425 c[0] = c[0];
426 c[1] = SMOOTH(c[1]);
427 c[2] = c[2];
428 break;
429 case COLOR_SUNSET :
430 c[0] = c[0]*3.0 - 0.5;
431 c[1] = c[1]*c[1]*c[1]*2.5;
432 c[2] = c[2]*(1.0 - y);
433 break;
434 case COLOR_ABYSS :
435 c[0] = c[0]*c[0]*c[0]*3.0 - 0.5;
436 c[1] = c[1]*c[1]*3.0 - 0.5;
437 c[2] = c[2]*3.0 - 0.5;
438 break;
439 case NUM_SCHEMES:
440 fprintf(stderr, "BLEUCH: NUM_SCHEMES CASE REACHED\n");
441 break;
442 }
443 c[0] = CLAMP(c[0], 0.0, 1.0);
444 c[1] = CLAMP(c[1], 0.0, 1.0);
445 c[2] = CLAMP(c[2], 0.0, 1.0);
446 }
447
448 void
449 FlashBackground::calculateShape(Shape shape,
450 SbVec3f *coords)
451 {
452 for (int i = 0; i < meshSize+1; i++)
453 {
454 double y = (double)i / (double)(meshSize);
455
456 for (int j = 0; j < meshSize+1; j++)
457 {
458 int index = i*(meshSize+1)+j;
459 double x = (double)j / (double)(meshSize);
460
461 coords[index].setValue(x, y, -999.0);
462
463 shapeFunction(shape, coords[index]);
464
465 if (j > 0 && j < meshSize)
466 coords[index][1] += drand48()*x*0.015;
467 }
468 }
469 }
470
471 void
472 FlashBackground::calculateColor(Shade shade,
473 Scheme cscheme,
474 SbColor *clrs)
475 {
476 for (int i = 0; i < meshSize+1; i++)
477 {
478 double y = (double)i / (double)(meshSize);
479
480 for (int j = 0; j < meshSize+1; j++)
481 {
482 int index = i*(meshSize+1)+j;
483 double x = (double)j / (double)(meshSize);
484
485 double gray = shadeFunction(shade, x, y);
486 clrs[index][0] = gray;
487 clrs[index][1] = gray;
488 clrs[index][2] = gray;
489
490 colorFunction(cscheme, clrs[index], 1.0 - x);
491 }
492 }
493 }
494
495 //
496 // Once we figure out which two shapes we are interpolating
497 // (currentShape and the shape after currentShape) these two routines
498 // actually do the interpolation. If we wanted to speed this up,
499 // these are the best candidates for optimization effort.
500 //
501 void
502 FlashBackground::updateSceneShape(double t)
503 {
504 SbVec3f *coords = coordinates->point.startEditing();
505
506 for (int i = 0; i < meshSize+1; i++)
507 {
508 for (int j = 0; j < meshSize+1; j++)
509 {
510 int index = i*(meshSize+1)+j;
511
512 coords[index] = (1.0 - t) * shapeTable[currentShape][index] +
513 t * shapeTable[nextShape][index];
514 }
515 }
516 coordinates->point.finishEditing();
517 }
518 void
519 FlashBackground::updateSceneColor(double t)
520 {
521 SbColor *clrs = colors->rgb.startEditing();
522
523 for (int i = 0; i < meshSize+1; i++)
524 {
525 for (int j = 0; j < meshSize+1; j++)
526 {
527 int index = i*(meshSize+1)+j;
528
529 SbVec3f workAround =
530 (1.0 - t) * schemeTable[currentScheme][index] +
531 t * schemeTable[nextScheme][index];
532 clrs[index] = workAround.getValue();
533 }
534 }
535 colors->rgb.finishEditing();
536 }
537
538 //
539 // This is called repeatedly to animate between the various shape and
540 // color schemes. It is assumed that time does not go backwards! If
541 // real-world time is fed directly to this function, shapes will
542 // change at a rate of one per second. You can multiply or divide to
543 // get faster or slower changes (I find a change every 8 seconds to be
544 // pretty nice).
545 //
546 void
547 FlashBackground::animateShape(double time)
548 {
549 static double lastTime = (-1.0);
550
551 if (lastTime == (-1.0))
552 {
553 lastTime = time;
554 }
555
556 double t = time - lastTime;
557
558 while (t > 1.0)
559 {
560 // Choose a new next shape
561 currentShape = nextShape;
562 nextShape = (Shape)(lrand48()%NUM_SHAPES);
563
564 t -= 1.0;
565 lastTime += 1.0;
566 }
567 updateSceneShape(t);
568 }
569 void
570 FlashBackground::animateColor(double time)
571 {
572 static double lastTime = (-1.0);
573
574 if (lastTime == (-1.0))
575 {
576 lastTime = time;
577 }
578
579 double t = time - lastTime;
580
581 while (t > 1.0)
582 {
583 // Choose a new next color scheme
584 currentScheme = nextScheme;
585 nextScheme = (Scheme)(lrand48()%NUM_SCHEMES);
586
587 t -= 1.0;
588 lastTime += 1.0;
589 }
590 updateSceneColor(t);
591 }