Shadowrun: Awakened 29 September 2011 - Build 871
shade.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  * shade.c - This file contains the functions that perform surface shading.
00060  *
00061  *  $Id: shade.cpp,v 1.3 2007-02-22 17:54:16 dpoulsen Exp $
00062  */
00063 
00064 #include "machine.h"
00065 #include "types.h"
00066 #include "macros.h"
00067 #include "light.h"
00068 #include "intersect.h"
00069 #include "vector.h"
00070 #include "trace.h"
00071 #include "global.h"
00072 #include "shade.h"
00073 
00074 void reset_lights(void) {
00075   numlights=0;
00076 }
00077 
00078 void add_light(point_light * li) {
00079   lightlist[numlights]=li;
00080   numlights++;
00081 }
00082 
00083 color shader(ray * incident) {
00084   color col, diffuse, phongcol; 
00085   vector N, L, hit;
00086   ray shadowray;
00087   flt inten, t, Llen;
00088   object * obj;
00089   int numints, i;
00090   point_light * li;
00091 
00092 
00093   numints=closest_intersection(&t, &obj, incident->intstruct);  
00094         /* find the number of intersections */
00095                 /* and return the closest one.      */
00096 
00097   if (numints < 1) {         
00098     /* if there weren't any object intersections then return the */
00099     /* background color for the pixel color.                     */
00100     return incident->scene->background;
00101   }
00102 
00103   if (obj->tex->islight) {  /* if the current object is a light, then we  */
00104     return obj->tex->col;   /* will only use the objects ambient color    */
00105   }
00106 
00107   RAYPNT(hit, (*incident), t)       /* find the point of intersection from t */ 
00108   obj->methods->normal(obj, &hit, incident, &N);  /* find the surface normal */
00109 
00110   /* execute the object's texture function */
00111   col = obj->tex->texfunc(&hit, obj->tex, incident); 
00112 
00113   diffuse.r = 0.0; 
00114   diffuse.g = 0.0; 
00115   diffuse.b = 0.0; 
00116   phongcol = diffuse;
00117 
00118   if ((obj->tex->diffuse > 0.0) || (obj->tex->phong > 0.0)) {  
00119     for (i=0; i<numlights; i++) {   /* loop for light contributions */
00120       li=lightlist[i];              /* set li=to the current light  */
00121       VSUB(li->ctr, hit, L)         /* find the light vector        */
00122 
00123       /* calculate the distance to the light from the hit point */
00124       Llen = sqrt(L.x*L.x + L.y*L.y + L.z*L.z) + EPSILON;
00125 
00126       L.x /= Llen; /* normalize the light direction vector */
00127       L.y /= Llen;
00128       L.z /= Llen;
00129 
00130       VDOT(inten, N, L)             /* light intensity              */
00131 
00132       /* add in diffuse lighting for this light if we're facing it */ 
00133       if (inten > 0.0) {            
00134         /* test for a shadow */
00135         shadowray.intstruct = incident->intstruct;
00136         shadowray.flags = RT_RAY_SHADOW | RT_RAY_BOUNDED; 
00137         incident->serial++;
00138         shadowray.serial = incident->serial;
00139         shadowray.mbox = incident->mbox;
00140         shadowray.o   = hit;
00141         shadowray.d   = L;      
00142         shadowray.maxdist = Llen;
00143         shadowray.s   = hit;
00144         shadowray.e = li->ctr;
00145         shadowray.scene = incident->scene;
00146         reset_intersection(incident->intstruct);
00147         intersect_objects(&shadowray);
00148 
00149         if (!shadow_intersection(incident->intstruct, Llen)) {
00150           /* XXX now that opacity is in the code, have to be more careful */
00151           ColorAddS(&diffuse, &li->tex->col, inten);
00152 
00153           /* phong type specular highlights */
00154           if (obj->tex->phong > 0.0) {
00155             flt phongval;
00156             phongval = shade_phong(incident, &hit, &N, &L, obj->tex->phongexp); 
00157             if (obj->tex->phongtype) 
00158               ColorAddS(&phongcol, &col, phongval);
00159             else
00160               ColorAddS(&phongcol, &(li->tex->col), phongval);
00161           }
00162         }
00163       }  
00164     } 
00165   }
00166 
00167   ColorScale(&diffuse, obj->tex->diffuse);
00168 
00169   col.r *= (diffuse.r + obj->tex->ambient); /* do a product of the */
00170   col.g *= (diffuse.g + obj->tex->ambient); /* diffuse intensity with  */
00171   col.b *= (diffuse.b + obj->tex->ambient); /* object color + ambient  */
00172 
00173   if (obj->tex->phong > 0.0) {
00174     ColorAccum(&col, &phongcol);
00175   }
00176 
00177   /* spawn reflection rays if necessary */
00178   /* note: this will overwrite the old intersection list */
00179   if (obj->tex->specular > 0.0) {    
00180     color specol;
00181     specol = shade_reflection(incident, &hit, &N, obj->tex->specular);
00182     ColorAccum(&col, &specol);
00183   }
00184 
00185   /* spawn transmission rays / refraction */
00186   /* note: this will overwrite the old intersection list */
00187   if (obj->tex->opacity < 1.0) {      
00188     color transcol;
00189     transcol = shade_transmission(incident, &hit, 1.0 - obj->tex->opacity);
00190     ColorAccum(&col, &transcol);
00191   }
00192 
00193   return col;    /* return the color of the shaded pixel... */
00194 }
00195 
00196 
00197 color shade_reflection(ray * incident, vector * hit, vector * N, flt specular) {
00198   ray specray;
00199   color col;
00200   vector R;
00201  
00202   VAddS(-2.0 * (incident->d.x * N->x + 
00203                 incident->d.y * N->y + 
00204                 incident->d.z * N->z), N, &incident->d, &R);
00205 
00206   specray.intstruct=incident->intstruct; /* what thread are we   */
00207   specray.depth=incident->depth - 1;   /* go up a level in recursion depth */
00208   specray.flags = RT_RAY_REGULAR;      /* infinite ray, to start with */
00209   specray.serial = incident->serial + 1; /* next serial number */
00210   specray.mbox = incident->mbox; 
00211   specray.o=*hit; 
00212   specray.d=R;                 /* reflect incident ray about normal */
00213   specray.o=Raypnt(&specray, EPSILON); /* avoid numerical precision bugs */
00214   specray.maxdist = FHUGE;             /* take any intersection */
00215   specray.scene=incident->scene;       /* global scenedef info */
00216   col=trace(&specray);                 /* trace specular reflection ray */ 
00217 
00218   incident->serial = specray.serial;    /* update the serial number */
00219 
00220   ColorScale(&col, specular);
00221 
00222   return col;
00223 }
00224 
00225 
00226 color shade_transmission(ray * incident, vector * hit, flt trans) {
00227   ray transray;
00228   color col;
00229 
00230   transray.intstruct=incident->intstruct; /* what thread are we   */
00231   transray.depth=incident->depth - 1;    /* go up a level in recursion depth */
00232   transray.flags = RT_RAY_REGULAR;       /* infinite ray, to start with */
00233   transray.serial = incident->serial + 1; /* update serial number */
00234   transray.mbox = incident->mbox;
00235   transray.o=*hit; 
00236   transray.d=incident->d;                /* ray continues along incident path */
00237   transray.o=Raypnt(&transray, EPSILON); /* avoid numerical precision bugs */
00238   transray.maxdist = FHUGE;              /* take any intersection */
00239   transray.scene=incident->scene;        /* global scenedef info */
00240   col=trace(&transray);                  /* trace transmission ray */  
00241 
00242   incident->serial = transray.serial;
00243 
00244   ColorScale(&col, trans);
00245 
00246   return col;
00247 }
00248 
00249 flt shade_phong(ray * incident, vector * hit, 
00250   vector * N, vector * L, flt specpower){
00251   vector H, V;
00252   flt inten;
00253 
00254   V = incident->d;
00255   VScale(&V, -1.0);
00256   VAdd(&V, L, &H);
00257   VScale(&H, 0.5);   
00258   VNorm(&H);
00259   inten = VDot(N, &H);
00260   if (inten > 0.0) 
00261     inten = pow(inten, specpower);
00262   else 
00263     inten = 0.0;
00264 
00265   return inten;
00266 } 
00267 
00268 

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