Shadowrun: Awakened 29 September 2011 - Build 871
texture.cpp
Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
00003 
00004     This file is part of Threading Building Blocks.
00005 
00006     Threading Building Blocks is free software; you can redistribute it
00007     and/or modify it under the terms of the GNU General Public License
00008     version 2 as published by the Free Software Foundation.
00009 
00010     Threading Building Blocks is distributed in the hope that it will be
00011     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00012     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Threading Building Blocks; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019     As a special exception, you may use this file as part of a free software
00020     library without restriction.  Specifically, if other files instantiate
00021     templates or use macros or inline functions from this file, or you compile
00022     this file and link it with other files to produce an executable, this
00023     file does not by itself cause the resulting executable to be covered by
00024     the GNU General Public License.  This exception does not however
00025     invalidate any other reasons why the executable file might be covered by
00026     the GNU General Public License.
00027 */
00028 
00029 /*
00030     The original source for this example is
00031     Copyright (c) 1994-2008 John E. Stone
00032     All rights reserved.
00033 
00034     Redistribution and use in source and binary forms, with or without
00035     modification, are permitted provided that the following conditions
00036     are met:
00037     1. Redistributions of source code must retain the above copyright
00038        notice, this list of conditions and the following disclaimer.
00039     2. Redistributions in binary form must reproduce the above copyright
00040        notice, this list of conditions and the following disclaimer in the
00041        documentation and/or other materials provided with the distribution.
00042     3. The name of the author may not be used to endorse or promote products
00043        derived from this software without specific prior written permission.
00044 
00045     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00046     OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00047     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00048     ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00049     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00050     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00051     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00052     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00053     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00054     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00055     SUCH DAMAGE.
00056 */
00057 
00058 /* 
00059  * texture.c - This file contains functions for implementing textures.
00060  * 
00061  *  $Id: texture.cpp,v 1.2 2007-02-22 17:54:16 dpoulsen Exp $ 
00062  */
00063 
00064 #include "machine.h"
00065 #include "types.h"
00066 #include "macros.h"
00067 #include "texture.h"
00068 #include "coordsys.h"
00069 #include "imap.h"
00070 #include "vector.h"
00071 #include "box.h"
00072 
00073 /* plain vanilla texture solely based on object color */
00074 color standard_texture(vector * hit, texture * tex, ray * ry) {
00075   return tex->col;
00076 }
00077 
00078 /* cylindrical image map */
00079 color image_cyl_texture(vector * hit, texture * tex, ray * ry) {
00080   vector rh;
00081   flt u,v;
00082  
00083   rh.x=hit->x - tex->ctr.x;
00084   rh.z=hit->y - tex->ctr.y;
00085   rh.y=hit->z - tex->ctr.z;
00086  
00087   xyztocyl(rh, 1.0, &u, &v);
00088 
00089   u = u * tex->scale.x;  
00090   u = u + tex->rot.x;
00091   u=fmod(u, 1.0);
00092   if (u < 0.0) u+=1.0; 
00093 
00094   v = v * tex->scale.y; 
00095   v = v + tex->rot.y;
00096   v=fmod(v, 1.0);
00097   if (v < 0.0) v+=1.0; 
00098 
00099   return ImageMap((rawimage *)tex->img, u, v); 
00100 }  
00101 
00102 /* spherical image map */
00103 color image_sphere_texture(vector * hit, texture * tex, ray * ry) {
00104   vector rh;
00105   flt u,v;
00106  
00107   rh.x=hit->x - tex->ctr.x;
00108   rh.y=hit->y - tex->ctr.y;
00109   rh.z=hit->z - tex->ctr.z;
00110  
00111   xyztospr(rh, &u, &v);
00112 
00113   u = u * tex->scale.x;
00114   u = u + tex->rot.x;
00115   u=fmod(u, 1.0);
00116   if (u < 0.0) u+=1.0;
00117  
00118   v = v * tex->scale.y;
00119   v = v + tex->rot.y;
00120   v=fmod(v, 1.0);
00121   if (v < 0.0) v+=1.0;
00122  
00123   return ImageMap((rawimage *)tex->img, u, v);
00124 }
00125 
00126 /* planar image map */
00127 color image_plane_texture(vector * hit, texture * tex, ray * ry) {
00128   vector pnt;
00129   flt u,v;
00130  
00131   pnt.x=hit->x - tex->ctr.x;
00132   pnt.y=hit->y - tex->ctr.y;
00133   pnt.z=hit->z - tex->ctr.z;
00134 
00135   VDOT(u, tex->uaxs, pnt);
00136 /*  VDOT(len, tex->uaxs, tex->uaxs);
00137   u = u / sqrt(len); */
00138 
00139   VDOT(v, tex->vaxs, pnt); 
00140 /*  VDOT(len, tex->vaxs, tex->vaxs);
00141   v = v / sqrt(len); */
00142     
00143 
00144   u = u * tex->scale.x;
00145   u = u + tex->rot.x;
00146   u = fmod(u, 1.0);
00147   if (u < 0.0) u += 1.0;
00148 
00149   v = v * tex->scale.y;
00150   v = v + tex->rot.y;
00151   v = fmod(v, 1.0);
00152   if (v < 0.0) v += 1.0;
00153 
00154   return ImageMap((rawimage *)tex->img, u, v);
00155 }
00156 
00157 color grit_texture(vector * hit, texture * tex, ray * ry) {
00158   int rnum;
00159   flt fnum;
00160   color col;
00161 
00162   rnum=rand() % 4096;
00163   fnum=(rnum / 4096.0 * 0.2) + 0.8;
00164 
00165   col.r=tex->col.r * fnum;
00166   col.g=tex->col.g * fnum;
00167   col.b=tex->col.b * fnum;
00168 
00169   return col;
00170 }
00171 
00172 color checker_texture(vector * hit, texture * tex, ray * ry) {
00173   long x,y,z;
00174   flt xh,yh,zh;
00175   color col;
00176 
00177   xh=hit->x - tex->ctr.x; 
00178   x=(long) ((fabs(xh) * 3) + 0.5);
00179   x=x % 2;
00180   yh=hit->y - tex->ctr.y;
00181   y=(long) ((fabs(yh) * 3) + 0.5);
00182   y=y % 2;
00183   zh=hit->z - tex->ctr.z;
00184   z=(long) ((fabs(zh) * 3) + 0.5);
00185   z=z % 2;
00186 
00187   if (((x + y + z) % 2)==1) {
00188     col.r=1.0;
00189     col.g=0.2;
00190     col.b=0.0;
00191   }
00192   else {
00193     col.r=0.0;
00194     col.g=0.2;
00195     col.b=1.0;
00196   }
00197 
00198   return col;
00199 }
00200 
00201 color cyl_checker_texture(vector * hit, texture * tex, ray * ry) {
00202   long x,y;
00203   vector rh;
00204   flt u,v;
00205   color col;
00206  
00207   rh.x=hit->x - tex->ctr.x;
00208   rh.y=hit->y - tex->ctr.y;
00209   rh.z=hit->z - tex->ctr.z;
00210 
00211   xyztocyl(rh, 1.0, &u, &v); 
00212 
00213   x=(long) (fabs(u) * 18.0);
00214   x=x % 2;
00215   y=(long) (fabs(v) * 10.0);
00216   y=y % 2;
00217  
00218   if (((x + y) % 2)==1) {
00219     col.r=1.0;
00220     col.g=0.2;
00221     col.b=0.0;
00222   }
00223   else {
00224     col.r=0.0;
00225     col.g=0.2;
00226     col.b=1.0;
00227   }
00228  
00229   return col;
00230 }
00231 
00232 
00233 color wood_texture(vector * hit, texture * tex, ray * ry) {
00234   flt radius, angle;
00235   int grain;
00236   color col;
00237   flt x,y,z;
00238 
00239   x=(hit->x - tex->ctr.x) * 1000;
00240   y=(hit->y - tex->ctr.y) * 1000;
00241   z=(hit->z - tex->ctr.z) * 1000;
00242 
00243   radius=sqrt(x*x + z*z);
00244   if (z == 0.0) 
00245     angle=3.1415926/2.0;
00246   else 
00247     angle=atan(x / z);
00248 
00249   radius=radius + 3.0 * sin(20 * angle + y / 150.0);
00250   grain=((int) (radius + 0.5)) % 60;
00251   if (grain < 40) {
00252     col.r=0.8;
00253     col.g=1.0;
00254     col.b=0.2;
00255   }
00256   else {
00257     col.r=0.0;
00258     col.g=0.0;
00259     col.b=0.0;
00260   }     
00261 
00262   return col;
00263 } 
00264 
00265 
00266 
00267 #define NMAX 28
00268 short int NoiseMatrix[NMAX][NMAX][NMAX];
00269 
00270 void InitNoise(void) {
00271   byte x,y,z,i,j,k;
00272 
00273   for (x=0; x<NMAX; x++) {
00274     for (y=0; y<NMAX; y++) {
00275       for (z=0; z<NMAX; z++) {
00276         NoiseMatrix[x][y][z]=rand() % 12000;
00277 
00278         if (x==NMAX-1) i=0; 
00279         else i=x;
00280 
00281         if (y==NMAX-1) j=0;
00282         else j=y;
00283 
00284         if (z==NMAX-1) k=0;
00285         else k=z;
00286 
00287         NoiseMatrix[x][y][z]=NoiseMatrix[i][j][k];
00288       }
00289     }
00290   }
00291 }
00292 
00293 int Noise(flt x, flt y, flt z) {
00294   byte ix, iy, iz;
00295   flt ox, oy, oz;
00296   int p000, p001, p010, p011;
00297   int p100, p101, p110, p111;
00298   int p00, p01, p10, p11;
00299   int p0, p1;
00300   int d00, d01, d10, d11;
00301   int d0, d1, d;
00302 
00303   x=fabs(x);
00304   y=fabs(y);
00305   z=fabs(z);
00306 
00307   ix=((int) x) % (NMAX-1);
00308   iy=((int) y) % (NMAX-1);
00309   iz=((int) z) % (NMAX-1);
00310 
00311   ox=(x - ((int) x));
00312   oy=(y - ((int) y));
00313   oz=(z - ((int) z));
00314 
00315   p000=NoiseMatrix[ix][iy][iz];
00316   p001=NoiseMatrix[ix][iy][iz+1];
00317   p010=NoiseMatrix[ix][iy+1][iz];
00318   p011=NoiseMatrix[ix][iy+1][iz+1];
00319   p100=NoiseMatrix[ix+1][iy][iz];
00320   p101=NoiseMatrix[ix+1][iy][iz+1];
00321   p110=NoiseMatrix[ix+1][iy+1][iz];
00322   p111=NoiseMatrix[ix+1][iy+1][iz+1];
00323 
00324   d00=p100-p000;
00325   d01=p101-p001;
00326   d10=p110-p010;
00327   d11=p111-p011;
00328 
00329   p00=(int) ((int) d00*ox) + p000;
00330   p01=(int) ((int) d01*ox) + p001;
00331   p10=(int) ((int) d10*ox) + p010;
00332   p11=(int) ((int) d11*ox) + p011;
00333   d0=p10-p00;
00334   d1=p11-p01;
00335   p0=(int) ((int) d0*oy) + p00;
00336   p1=(int) ((int) d1*oy) + p01;
00337   d=p1-p0;
00338 
00339   return (int) ((int) d*oz) + p0;
00340 }
00341 
00342 color marble_texture(vector * hit, texture * tex, ray * ry) {
00343   flt i,d;
00344   flt x,y,z;
00345   color col;
00346  
00347   x=hit->x;
00348   y=hit->y; 
00349   z=hit->z;
00350 
00351   x=x * 1.0;
00352 
00353   d=x + 0.0006 * Noise(x, (y * 1.0), (z * 1.0));
00354   d=d*(((int) d) % 25);
00355   i=0.0 + 0.10 * fabs(d - 10.0 - 20.0 * ((int) d * 0.05));
00356   if (i > 1.0) i=1.0;
00357   if (i < 0.0) i=0.0;  
00358 
00359 /*
00360   col.r=i * tex->col.r;
00361   col.g=i * tex->col.g;
00362   col.b=i * tex->col.b;
00363 */
00364 
00365   col.r = (1.0 + sin(i * 6.28)) / 2.0;
00366   col.g = (1.0 + sin(i * 16.28)) / 2.0;
00367   col.b = (1.0 + cos(i * 30.28)) / 2.0;
00368 
00369   return col;      
00370 }
00371 
00372 
00373 color gnoise_texture(vector * hit, texture * tex, ray * ry) {
00374   color col;
00375   flt f;
00376 
00377   f=Noise((hit->x - tex->ctr.x), 
00378           (hit->y - tex->ctr.y), 
00379       (hit->z - tex->ctr.z));
00380 
00381   if (f < 0.01) f=0.01;
00382   if (f > 1.0) f=1.0;
00383 
00384   col.r=tex->col.r * f;
00385   col.g=tex->col.g * f;
00386   col.b=tex->col.b * f;
00387 
00388   return col;
00389 }
00390 
00391 void InitTextures(void) {
00392   InitNoise();
00393   ResetImages();
00394 }
00395 

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net