2 Copyright (c) 2005-2017 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
22 The original source for this example is
23 Copyright (c) 1994-2008 John E. Stone
26 Redistribution and use in source and binary forms, with or without
27 modification, are permitted provided that the following conditions
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.
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
51 * texture.cpp - This file contains functions for implementing textures.
63 /* plain vanilla texture solely based on object color */
64 color standard_texture(vector * hit, texture * tex, ray * ry) {
68 /* cylindrical image map */
69 color image_cyl_texture(vector * hit, texture * tex, ray * ry) {
73 rh.x=hit->x - tex->ctr.x;
74 rh.z=hit->y - tex->ctr.y;
75 rh.y=hit->z - tex->ctr.z;
77 xyztocyl(rh, 1.0, &u, &v);
89 return ImageMap((rawimage *)tex->img, u, v);
92 /* spherical image map */
93 color image_sphere_texture(vector * hit, texture * tex, ray * ry) {
97 rh.x=hit->x - tex->ctr.x;
98 rh.y=hit->y - tex->ctr.y;
99 rh.z=hit->z - tex->ctr.z;
101 xyztospr(rh, &u, &v);
103 u = u * tex->scale.x;
108 v = v * tex->scale.y;
113 return ImageMap((rawimage *)tex->img, u, v);
116 /* planar image map */
117 color image_plane_texture(vector * hit, texture * tex, ray * ry) {
121 pnt.x=hit->x - tex->ctr.x;
122 pnt.y=hit->y - tex->ctr.y;
123 pnt.z=hit->z - tex->ctr.z;
125 VDOT(u, tex->uaxs, pnt);
126 /* VDOT(len, tex->uaxs, tex->uaxs);
127 u = u / sqrt(len); */
129 VDOT(v, tex->vaxs, pnt);
130 /* VDOT(len, tex->vaxs, tex->vaxs);
131 v = v / sqrt(len); */
134 u = u * tex->scale.x;
137 if (u < 0.0) u += 1.0;
139 v = v * tex->scale.y;
142 if (v < 0.0) v += 1.0;
144 return ImageMap((rawimage *)tex->img, u, v);
147 color grit_texture(vector * hit, texture * tex, ray * ry) {
153 fnum=(rnum / 4096.0 * 0.2) + 0.8;
155 col.r=tex->col.r * fnum;
156 col.g=tex->col.g * fnum;
157 col.b=tex->col.b * fnum;
162 color checker_texture(vector * hit, texture * tex, ray * ry) {
167 xh=hit->x - tex->ctr.x;
168 x=(long) ((fabs(xh) * 3) + 0.5);
170 yh=hit->y - tex->ctr.y;
171 y=(long) ((fabs(yh) * 3) + 0.5);
173 zh=hit->z - tex->ctr.z;
174 z=(long) ((fabs(zh) * 3) + 0.5);
177 if (((x + y + z) % 2)==1) {
191 color cyl_checker_texture(vector * hit, texture * tex, ray * ry) {
197 rh.x=hit->x - tex->ctr.x;
198 rh.y=hit->y - tex->ctr.y;
199 rh.z=hit->z - tex->ctr.z;
201 xyztocyl(rh, 1.0, &u, &v);
203 x=(long) (fabs(u) * 18.0);
205 y=(long) (fabs(v) * 10.0);
208 if (((x + y) % 2)==1) {
223 color wood_texture(vector * hit, texture * tex, ray * ry) {
229 x=(hit->x - tex->ctr.x) * 1000;
230 y=(hit->y - tex->ctr.y) * 1000;
231 z=(hit->z - tex->ctr.z) * 1000;
233 radius=sqrt(x*x + z*z);
239 radius=radius + 3.0 * sin(20 * angle + y / 150.0);
240 grain=((int) (radius + 0.5)) % 60;
258 short int NoiseMatrix[NMAX][NMAX][NMAX];
260 void InitNoise(void) {
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;
277 NoiseMatrix[x][y][z]=NoiseMatrix[i][j][k];
283 int Noise(flt x, flt y, flt z) {
286 int p000, p001, p010, p011;
287 int p100, p101, p110, p111;
288 int p00, p01, p10, p11;
290 int d00, d01, d10, d11;
297 ix=((int) x) % (NMAX-1);
298 iy=((int) y) % (NMAX-1);
299 iz=((int) z) % (NMAX-1);
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];
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;
325 p0=(int) ((int) d0*oy) + p00;
326 p1=(int) ((int) d1*oy) + p01;
329 return (int) ((int) d*oz) + p0;
332 color marble_texture(vector * hit, texture * tex, ray * ry) {
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));
350 col.r=i * tex->col.r;
351 col.g=i * tex->col.g;
352 col.b=i * tex->col.b;
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;
363 color gnoise_texture(vector * hit, texture * tex, ray * ry) {
367 f=Noise((hit->x - tex->ctr.x),
368 (hit->y - tex->ctr.y),
369 (hit->z - tex->ctr.z));
371 if (f < 0.01) f=0.01;
374 col.r=tex->col.r * f;
375 col.g=tex->col.g * f;
376 col.b=tex->col.b * f;
381 void InitTextures(void) {