1 /*
2  * fbm.c
3  *
4  * Copyright (C) 1989, 1991, Craig E. Kolb
5  * All rights reserved.
6  *
7  * This software may be freely copied, modified, and redistributed
8  * provided that this copyright notice is preserved on all copies.
9  *
10  * You may not distribute this software, in whole or in part, as part of
11  * any commercial product without the express consent of the authors.
12  *
13  * There is no warranty or other guarantee of fitness of this software
14  * for any purpose.  It is provided solely "as is".
15  *
16  * $Id: fbm.c,v 4.0 91/07/17 14:42:06 kolb Exp Locker: kolb $
17  *
18  * $Log:	fbm.c,v $
19  * Revision 4.0  91/07/17  14:42:06  kolb
20  * Initial version.
21  *
22  */
23 #include "texture.h"
24 #include "fbm.h"
25 
26 FBm *
FBmCreate(offset,scale,h,lambda,octaves,thresh,mapname)27 FBmCreate(offset, scale, h, lambda, octaves, thresh, mapname)
28 Float h, lambda, scale, offset, thresh;
29 int octaves;
30 char *mapname;
31 {
32 	FBm *fbm;
33 
34 	fbm = (FBm *)Malloc(sizeof(FBm));
35 
36 	fbm->beta = 1. + 2*h;
37 	fbm->omega = pow(lambda, -0.5*fbm->beta);
38 	fbm->lambda = lambda;
39 	fbm->scale = scale;
40 	fbm->offset = offset;
41 	fbm->thresh = thresh;
42 	fbm->octaves = octaves;
43 	if (mapname != (char *)NULL)
44 		fbm->colormap = ColormapRead(mapname);
45 	else
46 		fbm->colormap = (Color *)NULL;
47 	return fbm;
48 }
49 
50 void
FBmApply(fbm,prim,ray,pos,norm,gnorm,surf)51 FBmApply(fbm, prim, ray, pos, norm, gnorm, surf)
52 FBm *fbm;
53 Geom *prim;
54 Ray *ray;
55 Vector *pos, *norm, *gnorm;
56 Surface *surf;
57 {
58 	Float val;
59 	int index;
60 
61 	val = fBm(pos, fbm->omega, fbm->lambda, fbm->octaves);
62 	if (val < fbm->thresh)
63 		val = fbm->offset;
64 	else
65 		val = fbm->offset + fbm->scale*(val - fbm->thresh);
66 	if (fbm->colormap) {
67 		index = 255. * val;
68 		if (index > 255) index = 255;
69 		if (index < 0) index = 0;
70 		surf->diff.r *= fbm->colormap[index].r;
71 		surf->diff.g *= fbm->colormap[index].g;
72 		surf->diff.b *= fbm->colormap[index].b;
73 		surf->amb.r *= fbm->colormap[index].r;
74 		surf->amb.g *= fbm->colormap[index].g;
75 		surf->amb.b *= fbm->colormap[index].b;
76 	} else {
77 		ColorScale(val, surf->diff, &surf->diff);
78 		ColorScale(val, surf->amb, &surf->amb);
79 	}
80 }
81