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