1 /*
2  * sky.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: sky.c,v 4.0 91/07/17 14:43:43 kolb Exp Locker: kolb $
17  *
18  * $Log:	sky.c,v $
19  * Revision 4.0  91/07/17  14:43:43  kolb
20  * Initial version.
21  *
22  */
23 #include "texture.h"
24 #include "sky.h"
25 
26 Sky *
SkyCreate(scale,h,lambda,octaves,cthresh,lthresh)27 SkyCreate(scale, h, lambda, octaves, cthresh, lthresh)
28 Float h, lambda, scale, cthresh, lthresh;
29 int octaves;
30 {
31 	Sky *sky;
32 
33 	sky = (Sky *)Malloc(sizeof(Sky));
34 	sky->beta = 1. + 2 * h;
35 	sky->omega = pow(lambda, -0.5 * sky->beta);
36 	sky->lambda = lambda;
37 	sky->scale = scale;
38 	sky->cthresh = cthresh;
39 	sky->lthresh = lthresh;
40 	sky->octaves = octaves;
41 	return sky;
42 }
43 
44 void
SkyApply(sky,prim,ray,pos,norm,gnorm,surf)45 SkyApply(sky, prim, ray, pos, norm, gnorm, surf)
46 Sky *sky;
47 Geom *prim;
48 Ray *ray;
49 Vector *pos, *norm, *gnorm;
50 Surface *surf;
51 {
52 	Float It, maxval;
53 
54 	It = fBm(pos, sky->omega, sky->lambda, sky->octaves);
55 	maxval = 1. / (1. - sky->omega);
56 	/*
57 	 * Map from [-maxval,maxval] to [0,1]
58 	 */
59 	It = (maxval +  It) * 0.5/maxval;
60 
61 	It = (It - sky->lthresh) / (sky->cthresh - sky->lthresh);
62 	if (It < 0.)
63 		It = 0;
64 	else if (It > 1.)
65 		It = 1;
66 
67 	if (sky->scale != 0.)
68 		It = pow(It, sky->scale);
69 
70 	surf->transp = 1. - It;
71 
72 	ColorScale(It, surf->diff, &surf->diff);
73 	ColorScale(It, surf->amb, &surf->amb);
74 }
75