Committing Intel(R) TBB 2018 source code
[platform/upstream/tbb.git] / examples / parallel_for / tachyon / src / texture.cpp
1 /*
2     Copyright (c) 2005-2017 Intel Corporation
3
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7
8         http://www.apache.org/licenses/LICENSE-2.0
9
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15
16
17
18
19 */
20
21 /*
22     The original source for this example is
23     Copyright (c) 1994-2008 John E. Stone
24     All rights reserved.
25
26     Redistribution and use in source and binary forms, with or without
27     modification, are permitted provided that the following conditions
28     are met:
29     1. Redistributions of source code must retain the above copyright
30        notice, this list of conditions and the following disclaimer.
31     2. Redistributions in binary form must reproduce the above copyright
32        notice, this list of conditions and the following disclaimer in the
33        documentation and/or other materials provided with the distribution.
34     3. The name of the author may not be used to endorse or promote products
35        derived from this software without specific prior written permission.
36
37     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
38     OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
39     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40     ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
41     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47     SUCH DAMAGE.
48 */
49
50 /* 
51  * texture.cpp - This file contains functions for implementing textures.
52  */
53
54 #include "machine.h"
55 #include "types.h"
56 #include "macros.h"
57 #include "texture.h"
58 #include "coordsys.h"
59 #include "imap.h"
60 #include "vector.h"
61 #include "box.h"
62
63 /* plain vanilla texture solely based on object color */
64 color standard_texture(vector * hit, texture * tex, ray * ry) {
65   return tex->col;
66 }
67
68 /* cylindrical image map */
69 color image_cyl_texture(vector * hit, texture * tex, ray * ry) {
70   vector rh;
71   flt u,v;
72  
73   rh.x=hit->x - tex->ctr.x;
74   rh.z=hit->y - tex->ctr.y;
75   rh.y=hit->z - tex->ctr.z;
76  
77   xyztocyl(rh, 1.0, &u, &v);
78
79   u = u * tex->scale.x;  
80   u = u + tex->rot.x;
81   u=fmod(u, 1.0);
82   if (u < 0.0) u+=1.0; 
83
84   v = v * tex->scale.y; 
85   v = v + tex->rot.y;
86   v=fmod(v, 1.0);
87   if (v < 0.0) v+=1.0; 
88
89   return ImageMap((rawimage *)tex->img, u, v); 
90 }  
91
92 /* spherical image map */
93 color image_sphere_texture(vector * hit, texture * tex, ray * ry) {
94   vector rh;
95   flt u,v;
96  
97   rh.x=hit->x - tex->ctr.x;
98   rh.y=hit->y - tex->ctr.y;
99   rh.z=hit->z - tex->ctr.z;
100  
101   xyztospr(rh, &u, &v);
102
103   u = u * tex->scale.x;
104   u = u + tex->rot.x;
105   u=fmod(u, 1.0);
106   if (u < 0.0) u+=1.0;
107  
108   v = v * tex->scale.y;
109   v = v + tex->rot.y;
110   v=fmod(v, 1.0);
111   if (v < 0.0) v+=1.0;
112  
113   return ImageMap((rawimage *)tex->img, u, v);
114 }
115
116 /* planar image map */
117 color image_plane_texture(vector * hit, texture * tex, ray * ry) {
118   vector pnt;
119   flt u,v;
120  
121   pnt.x=hit->x - tex->ctr.x;
122   pnt.y=hit->y - tex->ctr.y;
123   pnt.z=hit->z - tex->ctr.z;
124
125   VDOT(u, tex->uaxs, pnt);
126 /*  VDOT(len, tex->uaxs, tex->uaxs);
127   u = u / sqrt(len); */
128
129   VDOT(v, tex->vaxs, pnt); 
130 /*  VDOT(len, tex->vaxs, tex->vaxs);
131   v = v / sqrt(len); */
132     
133
134   u = u * tex->scale.x;
135   u = u + tex->rot.x;
136   u = fmod(u, 1.0);
137   if (u < 0.0) u += 1.0;
138
139   v = v * tex->scale.y;
140   v = v + tex->rot.y;
141   v = fmod(v, 1.0);
142   if (v < 0.0) v += 1.0;
143
144   return ImageMap((rawimage *)tex->img, u, v);
145 }
146
147 color grit_texture(vector * hit, texture * tex, ray * ry) {
148   int rnum;
149   flt fnum;
150   color col;
151
152   rnum=rand() % 4096;
153   fnum=(rnum / 4096.0 * 0.2) + 0.8;
154
155   col.r=tex->col.r * fnum;
156   col.g=tex->col.g * fnum;
157   col.b=tex->col.b * fnum;
158
159   return col;
160 }
161
162 color checker_texture(vector * hit, texture * tex, ray * ry) {
163   long x,y,z;
164   flt xh,yh,zh;
165   color col;
166
167   xh=hit->x - tex->ctr.x; 
168   x=(long) ((fabs(xh) * 3) + 0.5);
169   x=x % 2;
170   yh=hit->y - tex->ctr.y;
171   y=(long) ((fabs(yh) * 3) + 0.5);
172   y=y % 2;
173   zh=hit->z - tex->ctr.z;
174   z=(long) ((fabs(zh) * 3) + 0.5);
175   z=z % 2;
176
177   if (((x + y + z) % 2)==1) {
178     col.r=1.0;
179     col.g=0.2;
180     col.b=0.0;
181   }
182   else {
183     col.r=0.0;
184     col.g=0.2;
185     col.b=1.0;
186   }
187
188   return col;
189 }
190
191 color cyl_checker_texture(vector * hit, texture * tex, ray * ry) {
192   long x,y;
193   vector rh;
194   flt u,v;
195   color col;
196  
197   rh.x=hit->x - tex->ctr.x;
198   rh.y=hit->y - tex->ctr.y;
199   rh.z=hit->z - tex->ctr.z;
200
201   xyztocyl(rh, 1.0, &u, &v); 
202
203   x=(long) (fabs(u) * 18.0);
204   x=x % 2;
205   y=(long) (fabs(v) * 10.0);
206   y=y % 2;
207  
208   if (((x + y) % 2)==1) {
209     col.r=1.0;
210     col.g=0.2;
211     col.b=0.0;
212   }
213   else {
214     col.r=0.0;
215     col.g=0.2;
216     col.b=1.0;
217   }
218  
219   return col;
220 }
221
222
223 color wood_texture(vector * hit, texture * tex, ray * ry) {
224   flt radius, angle;
225   int grain;
226   color col;
227   flt x,y,z;
228
229   x=(hit->x - tex->ctr.x) * 1000;
230   y=(hit->y - tex->ctr.y) * 1000;
231   z=(hit->z - tex->ctr.z) * 1000;
232
233   radius=sqrt(x*x + z*z);
234   if (z == 0.0) 
235     angle=3.1415926/2.0;
236   else 
237     angle=atan(x / z);
238
239   radius=radius + 3.0 * sin(20 * angle + y / 150.0);
240   grain=((int) (radius + 0.5)) % 60;
241   if (grain < 40) {
242     col.r=0.8;
243     col.g=1.0;
244     col.b=0.2;
245   }
246   else {
247     col.r=0.0;
248     col.g=0.0;
249     col.b=0.0;
250   }     
251
252   return col;
253
254
255
256
257 #define NMAX 28
258 short int NoiseMatrix[NMAX][NMAX][NMAX];
259
260 void InitNoise(void) {
261   byte x,y,z,i,j,k;
262
263   for (x=0; x<NMAX; x++) {
264     for (y=0; y<NMAX; y++) {
265       for (z=0; z<NMAX; z++) {
266         NoiseMatrix[x][y][z]=rand() % 12000;
267
268         if (x==NMAX-1) i=0; 
269         else i=x;
270
271         if (y==NMAX-1) j=0;
272         else j=y;
273
274         if (z==NMAX-1) k=0;
275         else k=z;
276
277         NoiseMatrix[x][y][z]=NoiseMatrix[i][j][k];
278       }
279     }
280   }
281 }
282
283 int Noise(flt x, flt y, flt z) {
284   byte ix, iy, iz;
285   flt ox, oy, oz;
286   int p000, p001, p010, p011;
287   int p100, p101, p110, p111;
288   int p00, p01, p10, p11;
289   int p0, p1;
290   int d00, d01, d10, d11;
291   int d0, d1, d;
292
293   x=fabs(x);
294   y=fabs(y);
295   z=fabs(z);
296
297   ix=((int) x) % (NMAX-1);
298   iy=((int) y) % (NMAX-1);
299   iz=((int) z) % (NMAX-1);
300
301   ox=(x - ((int) x));
302   oy=(y - ((int) y));
303   oz=(z - ((int) z));
304
305   p000=NoiseMatrix[ix][iy][iz];
306   p001=NoiseMatrix[ix][iy][iz+1];
307   p010=NoiseMatrix[ix][iy+1][iz];
308   p011=NoiseMatrix[ix][iy+1][iz+1];
309   p100=NoiseMatrix[ix+1][iy][iz];
310   p101=NoiseMatrix[ix+1][iy][iz+1];
311   p110=NoiseMatrix[ix+1][iy+1][iz];
312   p111=NoiseMatrix[ix+1][iy+1][iz+1];
313
314   d00=p100-p000;
315   d01=p101-p001;
316   d10=p110-p010;
317   d11=p111-p011;
318
319   p00=(int) ((int) d00*ox) + p000;
320   p01=(int) ((int) d01*ox) + p001;
321   p10=(int) ((int) d10*ox) + p010;
322   p11=(int) ((int) d11*ox) + p011;
323   d0=p10-p00;
324   d1=p11-p01;
325   p0=(int) ((int) d0*oy) + p00;
326   p1=(int) ((int) d1*oy) + p01;
327   d=p1-p0;
328
329   return (int) ((int) d*oz) + p0;
330 }
331
332 color marble_texture(vector * hit, texture * tex, ray * ry) {
333   flt i,d;
334   flt x,y,z;
335   color col;
336  
337   x=hit->x;
338   y=hit->y; 
339   z=hit->z;
340
341   x=x * 1.0;
342
343   d=x + 0.0006 * Noise(x, (y * 1.0), (z * 1.0));
344   d=d*(((int) d) % 25);
345   i=0.0 + 0.10 * fabs(d - 10.0 - 20.0 * ((int) d * 0.05));
346   if (i > 1.0) i=1.0;
347   if (i < 0.0) i=0.0;  
348
349 /*
350   col.r=i * tex->col.r;
351   col.g=i * tex->col.g;
352   col.b=i * tex->col.b;
353 */
354
355   col.r = (1.0 + sin(i * 6.28)) / 2.0;
356   col.g = (1.0 + sin(i * 16.28)) / 2.0;
357   col.b = (1.0 + cos(i * 30.28)) / 2.0;
358
359   return col;      
360 }
361
362
363 color gnoise_texture(vector * hit, texture * tex, ray * ry) {
364   color col;
365   flt f;
366
367   f=Noise((hit->x - tex->ctr.x), 
368           (hit->y - tex->ctr.y), 
369           (hit->z - tex->ctr.z));
370
371   if (f < 0.01) f=0.01;
372   if (f > 1.0) f=1.0;
373
374   col.r=tex->col.r * f;
375   col.g=tex->col.g * f;
376   col.b=tex->col.b * f;
377
378   return col;
379 }
380
381 void InitTextures(void) {
382   InitNoise();
383   ResetImages();
384 }
385