Attempt to fix nightly build.
[AROS-Contrib.git] / gfx / povray / normal.c
blob0a9e222f6ad4fa246153503ea60455f22f6680ee
1 /****************************************************************************
2 * normal.c
4 * This module implements solid texturing functions that perturb the surface
5 * normal to create a bumpy effect.
7 * from Persistence of Vision(tm) Ray Tracer
8 * Copyright 1996,1999 Persistence of Vision Team
9 *---------------------------------------------------------------------------
10 * NOTICE: This source code file is provided so that users may experiment
11 * with enhancements to POV-Ray and to port the software to platforms other
12 * than those supported by the POV-Ray Team. There are strict rules under
13 * which you are permitted to use this file. The rules are in the file
14 * named POVLEGAL.DOC which should be distributed with this file.
15 * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
16 * Team Coordinator by email to team-coord@povray.org or visit us on the web at
17 * http://www.povray.org. The latest version of POV-Ray may be found at this site.
19 * This program is based on the popular DKB raytracer version 2.12.
20 * DKBTrace was originally written by David K. Buck.
21 * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
23 *****************************************************************************/
26 * Some texture ideas garnered from SIGGRAPH '85 Volume 19 Number 3,
27 * "An Image Synthesizer" By Ken Perlin.
29 * Further Ideas Garnered from "The RenderMan Companion" (Addison Wesley)
32 #include "frame.h"
33 #include "vector.h"
34 #include "povproto.h"
35 #include "texture.h"
36 #include "image.h"
37 #include "matrices.h"
38 #include "normal.h"
39 #include "povray.h"
40 #include "txttest.h"
41 #include "pigment.h"
45 /*****************************************************************************
46 * Local preprocessor defines
47 ******************************************************************************/
51 /*****************************************************************************
52 * Local typedefs
53 ******************************************************************************/
57 /*****************************************************************************
58 * Local constants
59 ******************************************************************************/
61 static CONST
62 VECTOR Pyramid_Vect [4]= {{ 0.942809041,-0.333333333, 0.0},
63 {-0.471404521,-0.333333333, 0.816496581},
64 {-0.471404521,-0.333333333,-0.816496581},
65 { 0.0 , 1.0 , 0.0}};
68 /*****************************************************************************
69 * Static functions
70 ******************************************************************************/
72 static void ripples (VECTOR EPoint, TNORMAL *Tnormal, VECTOR Vector);
73 static void waves (VECTOR EPoint, TNORMAL *Tnormal, VECTOR Vector);
74 static void bumps (VECTOR EPoint, TNORMAL *Tnormal, VECTOR normal);
75 static void dents (VECTOR EPoint, TNORMAL *Tnormal, VECTOR normal);
76 static void wrinkles (VECTOR EPoint, TNORMAL *Tnormal, VECTOR normal);
77 static void quilted (VECTOR EPoint, TNORMAL *Tnormal, VECTOR normal);
78 static DBL Hermite_Cubic (DBL T1,UV_VECT UV1,UV_VECT UV2);
79 static DBL Do_Slope_Map (DBL value, BLEND_MAP *Blend_Map);
80 static void Do_Average_Normals (VECTOR EPoint, TNORMAL *Tnormal, VECTOR normal);
83 /*****************************************************************************
85 * FUNCTION
87 * ripples
89 * INPUT
91 * OUTPUT
93 * RETURNS
95 * AUTHOR
97 * POV-Ray Team
99 * DESCRIPTION
101 * CHANGES
103 ******************************************************************************/
105 static void ripples (VECTOR EPoint, TNORMAL *Tnormal, VECTOR normal)
107 register unsigned int i;
108 register DBL length, scalar, index;
109 VECTOR point;
111 for (i = 0 ; i < Number_Of_Waves ; i++)
113 VSub (point, EPoint, Wave_Sources[i]);
114 VLength (length, point);
116 if (length == 0.0)
117 length = 1.0;
119 index = length * Tnormal->Frequency + Tnormal->Phase;
121 scalar = cycloidal(index) * Tnormal ->Amount;
123 VAddScaledEq(normal, scalar / (length * (DBL)Number_Of_Waves), point);
129 /*****************************************************************************
131 * FUNCTION
133 * waves
135 * INPUT
137 * OUTPUT
139 * RETURNS
141 * AUTHOR
143 * POV-Ray Team
145 * DESCRIPTION
147 * CHANGES
149 ******************************************************************************/
151 static void waves (VECTOR EPoint, TNORMAL *Tnormal, VECTOR normal)
153 register unsigned int i;
154 register DBL length, scalar, index, sinValue ;
155 VECTOR point;
157 for (i = 0 ; i < Number_Of_Waves ; i++)
159 VSub (point, EPoint, Wave_Sources[i]);
161 VLength (length, point);
163 if (length == 0.0)
165 length = 1.0;
168 index = length * Tnormal->Frequency * frequency[i] + Tnormal->Phase;
170 sinValue = cycloidal(index);
172 scalar = sinValue * Tnormal->Amount / frequency[i];
174 VAddScaledEq(normal, scalar / (length * (DBL)Number_Of_Waves), point);
180 /*****************************************************************************
182 * FUNCTION
184 * bumps
186 * INPUT
188 * OUTPUT
190 * RETURNS
192 * AUTHOR
194 * POV-Ray Team
196 * DESCRIPTION
198 * CHANGES
200 ******************************************************************************/
202 static void bumps (VECTOR EPoint, TNORMAL *Tnormal, VECTOR normal)
204 VECTOR bump_turb;
206 /* Get normal displacement value. */
208 DNoise (bump_turb, EPoint);
210 /* Displace "normal". */
212 VAddScaledEq(normal, Tnormal->Amount, bump_turb);
217 /*****************************************************************************
219 * FUNCTION
221 * INPUT
223 * OUTPUT
225 * RETURNS
227 * AUTHOR
229 * POV-Ray Team
231 * DESCRIPTION
232 * Dents is similar to bumps, but uses noise() to control the amount of
233 * dnoise() perturbation of the object normal...
235 * CHANGES
237 ******************************************************************************/
239 static void dents (VECTOR EPoint, TNORMAL *Tnormal, VECTOR normal)
241 DBL noise;
242 VECTOR stucco_turb;
244 noise = Noise (EPoint);
246 noise = noise * noise * noise * Tnormal->Amount;
248 /* Get normal displacement value. */
250 DNoise(stucco_turb, EPoint);
252 /* Displace "normal". */
254 VAddScaledEq(normal, noise, stucco_turb);
260 /*****************************************************************************
262 * FUNCTION
264 * INPUT
266 * OUTPUT
268 * RETURNS
270 * AUTHOR
272 * POV-Ray Team
274 * DESCRIPTION
276 * Wrinkles - This is my implementation of the dented() routine, using
277 * a surface iterative fractal derived from DTurbulence.
279 * This is a 3-D version (thanks to DNoise()...) of the usual version
280 * using the singular Noise()...
282 * Seems to look a lot like wrinkles, however... (hmmm)
284 * Idea garnered from the April 89 Byte Graphics Supplement on RenderMan,
285 * refined from "The RenderMan Companion, by Steve Upstill of Pixar,
286 * (C) 1990 Addison-Wesley.
288 * CHANGES
290 ******************************************************************************/
292 static void wrinkles (VECTOR EPoint, TNORMAL *Tnormal, VECTOR normal)
294 register int i;
295 register DBL scale = 1.0;
296 VECTOR result, value, value2;
298 Make_Vector(result, 0.0, 0.0, 0.0);
300 for (i = 0; i < 10; scale *= 2.0, i++)
302 VScale(value2,EPoint,scale);
303 DNoise(value, value2);
305 result[X] += fabs(value[X] / scale);
306 result[Y] += fabs(value[Y] / scale);
307 result[Z] += fabs(value[Z] / scale);
310 /* Displace "normal". */
312 VAddScaledEq(normal, Tnormal->Amount, result);
316 /*****************************************************************************
318 * FUNCTION
320 * quilted
322 * INPUT
324 * OUTPUT
326 * RETURNS
328 * AUTHOR
330 * Dan Farmer '94
332 * DESCRIPTION
334 * CHANGES
336 ******************************************************************************/
338 static void quilted (VECTOR EPoint, TNORMAL *Tnormal, VECTOR normal)
340 VECTOR value;
341 DBL t;
343 value[X] = EPoint[X]-FLOOR(EPoint[X])-0.5;
344 value[Y] = EPoint[Y]-FLOOR(EPoint[Y])-0.5;
345 value[Z] = EPoint[Z]-FLOOR(EPoint[Z])-0.5;
347 t = sqrt(value[X]*value[X]+value[Y]*value[Y]+value[Z]*value[Z]);
349 t = quilt_cubic(t, Tnormal->Vals.Quilted.Control0, Tnormal->Vals.Quilted.Control1);
351 value[X] *= t;
352 value[Y] *= t;
353 value[Z] *= t;
355 VAddScaledEq (normal, Tnormal->Amount,value);
358 /*****************************************************************************
360 * FUNCTION
362 * Create_Tnormal
364 * INPUT
366 * OUTPUT
368 * RETURNS
370 * pointer to the created Tnormal
372 * AUTHOR
374 * POV-Ray Team
376 * DESCRIPTION : Allocate memory for new Tnormal and initialize it to
377 * system default values.
379 * CHANGES
381 ******************************************************************************/
384 TNORMAL *Create_Tnormal ()
386 TNORMAL *New;
388 New = (TNORMAL *)POV_MALLOC(sizeof(TNORMAL), "normal");
390 Init_TPat_Fields((TPATTERN *)New);
392 New->Amount = 0.5;
394 return (New);
399 /*****************************************************************************
401 * FUNCTION
403 * Copy_Tnormal
405 * INPUT
407 * OUTPUT
409 * RETURNS
411 * AUTHOR
413 * POV-Ray Team
415 * DESCRIPTION
417 * CHANGES
419 ******************************************************************************/
421 TNORMAL *Copy_Tnormal (TNORMAL *Old)
423 TNORMAL *New;
425 if (Old != NULL)
427 New = Create_Tnormal();
429 Copy_TPat_Fields ((TPATTERN *)New, (TPATTERN *)Old);
431 New->Amount = Old->Amount;
433 else
435 New = NULL;
438 return (New);
443 /*****************************************************************************
445 * FUNCTION
447 * Destroy_Tnormal
449 * INPUT
451 * OUTPUT
453 * RETURNS
455 * AUTHOR
457 * POV-Ray Team
459 * DESCRIPTION
461 * CHANGES
463 ******************************************************************************/
465 void Destroy_Tnormal(TNORMAL *Tnormal)
467 if (Tnormal != NULL)
469 Destroy_TPat_Fields ((TPATTERN *)Tnormal);
471 POV_FREE(Tnormal);
477 /*****************************************************************************
479 * FUNCTION
481 * Post_Tnormal
483 * INPUT
485 * OUTPUT
487 * RETURNS
489 * AUTHOR
491 * POV-Ray Team
493 * DESCRIPTION
495 * CHANGES
497 ******************************************************************************/
499 void Post_Tnormal (TNORMAL *Tnormal)
501 int i;
502 BLEND_MAP *Map;
504 if (Tnormal != NULL)
506 if (Tnormal->Flags & POST_DONE)
508 return;
511 if (Tnormal->Type == NO_PATTERN)
513 Error("No normal type given.");
516 Tnormal->Flags |= POST_DONE;
518 if ((Map = Tnormal->Blend_Map) != NULL)
520 for (i = 0; i < Map->Number_Of_Entries; i++)
522 switch (Map->Type)
524 case PIGMENT_TYPE:
526 Post_Pigment(Map->Blend_Map_Entries[i].Vals.Pigment);
528 break;
530 case NORMAL_TYPE:
532 Post_Tnormal(Map->Blend_Map_Entries[i].Vals.Tnormal);
534 break;
536 case TEXTURE_TYPE:
538 Post_Textures(Map->Blend_Map_Entries[i].Vals.Texture);
540 break;
542 case SLOPE_TYPE:
543 case COLOUR_TYPE:
544 case PATTERN_TYPE:
546 break;
548 default:
550 Error("Unknown pattern type in Post_Tnormal.");
559 /*****************************************************************************
561 * FUNCTION
563 * Perturb_Normal
565 * INPUT
567 * OUTPUT
569 * RETURNS
571 * AUTHOR
573 * POV-Ray Team
575 * DESCRIPTION
577 * CHANGES
579 ******************************************************************************/
581 #define DELTA 0.02
583 void Perturb_Normal(VECTOR Layer_Normal, TNORMAL *Tnormal, VECTOR EPoint)
585 VECTOR TPoint,P1;
586 DBL value1,value2,Amount;
587 int i;
588 BLEND_MAP *Blend_Map;
589 BLEND_MAP_ENTRY *Prev, *Cur;
591 if (Tnormal==NULL)
593 return;
596 /* If normal_map present, use it and return */
598 if ((Blend_Map=Tnormal->Blend_Map) != NULL)
600 if ((Blend_Map->Type == NORMAL_TYPE) && (Tnormal->Type != AVERAGE_PATTERN))
602 value1 = Evaluate_TPat((TPATTERN *)Tnormal,EPoint);
604 Search_Blend_Map (value1,Blend_Map,&Prev,&Cur);
606 Assign_Vector(P1,Layer_Normal);
608 Warp_EPoint (TPoint, EPoint, (TPATTERN *)Tnormal);
609 Perturb_Normal(Layer_Normal,Cur->Vals.Tnormal,TPoint);
611 if (Prev != Cur)
613 Perturb_Normal(P1,Prev->Vals.Tnormal,TPoint);
615 value2 = (value1-Prev->value)/(Cur->value-Prev->value);
616 value1 = 1.0-value2;
618 VLinComb2(Layer_Normal,value1,P1,value2,Layer_Normal)
621 VNormalizeEq(Layer_Normal);
623 return;
627 /* No normal_map. */
629 if (Tnormal->Type <= LAST_NORM_ONLY_PATTERN)
631 Warp_EPoint (TPoint, EPoint, (TPATTERN *)Tnormal);
632 switch (Tnormal->Type)
634 case BITMAP_PATTERN: bump_map (TPoint, Tnormal, Layer_Normal); break;
635 case BUMPS_PATTERN: bumps (TPoint, Tnormal, Layer_Normal); break;
636 case DENTS_PATTERN: dents (TPoint, Tnormal, Layer_Normal); break;
637 case RIPPLES_PATTERN:ripples (TPoint, Tnormal, Layer_Normal); break;
638 case WAVES_PATTERN: waves (TPoint, Tnormal, Layer_Normal); break;
639 case WRINKLES_PATTERN:wrinkles (TPoint, Tnormal, Layer_Normal);break;
640 case QUILTED_PATTERN:quilted (TPoint, Tnormal, Layer_Normal); break;
641 case AVERAGE_PATTERN: Do_Average_Normals (TPoint, Tnormal, Layer_Normal); break;
642 default:
643 Error("Normal pattern not yet implemented.");
646 else
648 Amount=Tnormal->Amount * -5.0; /*fudge factor*/
650 /* Note, even though DELTA and Pyramid_Vect are constants, we may later
651 make DELTA a user-defined parameter. Good optimising compilers
652 should merge the constants anyway. */
654 for(i=0; i<=3; i++)
656 VAddScaled(P1,EPoint,DELTA,Pyramid_Vect[i]);
657 value1 = Do_Slope_Map(Evaluate_TPat((TPATTERN *)Tnormal,P1),Blend_Map);
658 VAddScaledEq(Layer_Normal,value1*Amount,Pyramid_Vect[i]);
663 VNormalizeEq(Layer_Normal);
668 /*****************************************************************************
670 * FUNCTION
672 * INPUT
674 * OUTPUT
676 * RETURNS
678 * AUTHOR
680 * DESCRIPTION
682 * CHANGES
684 ******************************************************************************/
686 static DBL Do_Slope_Map (DBL value,BLEND_MAP *Blend_Map)
688 DBL Result;
689 BLEND_MAP_ENTRY *Prev, *Cur;
691 if (Blend_Map == NULL)
693 return(value);
696 Search_Blend_Map (value,Blend_Map,&Prev,&Cur);
698 if (Prev == Cur)
700 return(Cur->Vals.Point_Slope[0]);
703 Result = (value-Prev->value)/(Cur->value-Prev->value);
705 return(Hermite_Cubic(Result,Prev->Vals.Point_Slope,Cur->Vals.Point_Slope));
710 /*****************************************************************************
712 * FUNCTION
714 * INPUT
716 * OUTPUT
718 * RETURNS
720 * AUTHOR
722 * DESCRIPTION
724 * CHANGES
726 ******************************************************************************/
728 #define S1 UV1[1]
729 #define S2 UV2[1]
730 #define P1 UV1[0]
731 #define P2 UV2[0]
733 static DBL Hermite_Cubic(DBL T1,UV_VECT UV1,UV_VECT UV2)
735 DBL TT=T1*T1;
736 DBL TTT=TT*T1;
737 DBL rv; /* simplified equation for poor Symantec */
739 rv = TTT*(S1+S2+2.0*(P1-P2));
740 rv += -TT*(2.0*S1+S2+3.0*(P1-P2));
741 rv += T1*S1 +P1;
743 return (rv);
748 /*****************************************************************************
750 * FUNCTION
752 * INPUT
754 * OUTPUT
756 * RETURNS
758 * AUTHOR
760 * DESCRIPTION
762 * CHANGES
764 ******************************************************************************/
766 static void Do_Average_Normals (VECTOR EPoint,TNORMAL *Tnormal,VECTOR normal)
768 int i;
769 BLEND_MAP *Map = Tnormal->Blend_Map;
770 SNGL Value;
771 SNGL Total = 0.0;
772 VECTOR V1,V2;
774 Make_Vector (V1, 0.0, 0.0, 0.0);
776 for (i = 0; i < Map->Number_Of_Entries; i++)
778 Value = Map->Blend_Map_Entries[i].value;
780 Assign_Vector(V2,normal);
782 Perturb_Normal(V2,Map->Blend_Map_Entries[i].Vals.Tnormal,EPoint);
784 VAddScaledEq(V1,Value,V2);
786 Total += Value;
789 VInverseScale(normal,V1,Total);