![]() |
Shadowrun: Awakened 29 September 2011 - Build 871
|
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 * triangle.c - This file contains the functions for dealing with triangles. 00060 * 00061 * $Id: triangle.cpp,v 1.3 2007-02-22 17:54:16 dpoulsen Exp $ 00062 */ 00063 00064 #include "machine.h" 00065 #include "types.h" 00066 #include "vector.h" 00067 #include "macros.h" 00068 #include "intersect.h" 00069 #include "util.h" 00070 00071 #define TRIANGLE_PRIVATE 00072 #include "triangle.h" 00073 00074 static object_methods tri_methods = { 00075 (void (*)(void *, void *))(tri_intersect), 00076 (void (*)(void *, void *, void *, void *))(tri_normal), 00077 tri_bbox, 00078 free 00079 }; 00080 00081 static object_methods stri_methods = { 00082 (void (*)(void *, void *))(tri_intersect), 00083 (void (*)(void *, void *, void *, void *))(stri_normal), 00084 tri_bbox, 00085 free 00086 }; 00087 00088 object * newtri(void * tex, vector v0, vector v1, vector v2) { 00089 tri * t; 00090 vector edge1, edge2, edge3; 00091 00092 VSub(&v1, &v0, &edge1); 00093 VSub(&v2, &v0, &edge2); 00094 VSub(&v2, &v1, &edge3); 00095 00096 /* check to see if this will be a degenerate triangle before creation */ 00097 if ((VLength(&edge1) >= EPSILON) && 00098 (VLength(&edge2) >= EPSILON) && 00099 (VLength(&edge3) >= EPSILON)) { 00100 00101 t=(tri *) rt_getmem(sizeof(tri)); 00102 00103 t->nextobj = NULL; 00104 t->methods = &tri_methods; 00105 00106 t->tex = (texture *)tex; 00107 t->v0 = v0; 00108 t->edge1 = edge1; 00109 t->edge2 = edge2; 00110 00111 return (object *) t; 00112 } 00113 00114 return NULL; /* was a degenerate triangle */ 00115 } 00116 00117 00118 object * newstri(void * tex, vector v0, vector v1, vector v2, 00119 vector n0, vector n1, vector n2) { 00120 stri * t; 00121 vector edge1, edge2, edge3; 00122 00123 VSub(&v1, &v0, &edge1); 00124 VSub(&v2, &v0, &edge2); 00125 VSub(&v2, &v1, &edge3); 00126 00127 /* check to see if this will be a degenerate triangle before creation */ 00128 if ((VLength(&edge1) >= EPSILON) && 00129 (VLength(&edge2) >= EPSILON) && 00130 (VLength(&edge3) >= EPSILON)) { 00131 00132 t=(stri *) rt_getmem(sizeof(stri)); 00133 00134 t->nextobj = NULL; 00135 t->methods = &stri_methods; 00136 00137 t->tex = (texture *)tex; 00138 t->v0 = v0; 00139 t->edge1 = edge1; 00140 t->edge2 = edge2; 00141 t->n0 = n0; 00142 t->n1 = n1; 00143 t->n2 = n2; 00144 00145 return (object *) t; 00146 } 00147 00148 return NULL; /* was a degenerate triangle */ 00149 } 00150 00151 #define CROSS(dest,v1,v2) \ 00152 dest.x=v1.y*v2.z-v1.z*v2.y; \ 00153 dest.y=v1.z*v2.x-v1.x*v2.z; \ 00154 dest.z=v1.x*v2.y-v1.y*v2.x; 00155 00156 #define DOT(v1,v2) (v1.x*v2.x+v1.y*v2.y+v1.z*v2.z) 00157 00158 #define SUB(dest,v1,v2) \ 00159 dest.x=v1.x-v2.x; \ 00160 dest.y=v1.y-v2.y; \ 00161 dest.z=v1.z-v2.z; 00162 00163 static int tri_bbox(void * obj, vector * min, vector * max) { 00164 tri * t = (tri *) obj; 00165 vector v1, v2; 00166 00167 VAdd(&t->v0, &t->edge1, &v1); 00168 VAdd(&t->v0, &t->edge2, &v2); 00169 00170 min->x = MYMIN( t->v0.x , MYMIN( v1.x , v2.x )); 00171 min->y = MYMIN( t->v0.y , MYMIN( v1.y , v2.y )); 00172 min->z = MYMIN( t->v0.z , MYMIN( v1.z , v2.z )); 00173 00174 max->x = MYMAX( t->v0.x , MYMAX( v1.x , v2.x )); 00175 max->y = MYMAX( t->v0.y , MYMAX( v1.y , v2.y )); 00176 max->z = MYMAX( t->v0.z , MYMAX( v1.z , v2.z )); 00177 00178 return 1; 00179 } 00180 00181 static void tri_intersect(tri * trn, ray * ry) { 00182 vector tvec, pvec, qvec; 00183 flt det, inv_det, t, u, v; 00184 00185 /* begin calculating determinant - also used to calculate U parameter */ 00186 CROSS(pvec, ry->d, trn->edge2); 00187 00188 /* if determinant is near zero, ray lies in plane of triangle */ 00189 det = DOT(trn->edge1, pvec); 00190 00191 if (det > -EPSILON && det < EPSILON) 00192 return; 00193 00194 inv_det = 1.0 / det; 00195 00196 /* calculate distance from vert0 to ray origin */ 00197 SUB(tvec, ry->o, trn->v0); 00198 00199 /* calculate U parameter and test bounds */ 00200 u = DOT(tvec, pvec) * inv_det; 00201 if (u < 0.0 || u > 1.0) 00202 return; 00203 00204 /* prepare to test V parameter */ 00205 CROSS(qvec, tvec, trn->edge1); 00206 00207 /* calculate V parameter and test bounds */ 00208 v = DOT(ry->d, qvec) * inv_det; 00209 if (v < 0.0 || u + v > 1.0) 00210 return; 00211 00212 /* calculate t, ray intersects triangle */ 00213 t = DOT(trn->edge2, qvec) * inv_det; 00214 00215 add_intersection(t,(object *) trn, ry); 00216 } 00217 00218 00219 static void tri_normal(tri * trn, vector * pnt, ray * incident, vector * N) { 00220 00221 CROSS((*N), trn->edge1, trn->edge2); 00222 00223 VNorm(N); 00224 00225 if (VDot(N, &(incident->d)) > 0.0) { 00226 N->x=-N->x; 00227 N->y=-N->y; 00228 N->z=-N->z; 00229 } 00230 } 00231 00232 static void stri_normal(stri * trn, vector * pnt, ray * incident, vector * N) { 00233 flt U, V, W, lensqr; 00234 vector P, tmp, norm; 00235 00236 CROSS(norm, trn->edge1, trn->edge2); 00237 lensqr = DOT(norm, norm); 00238 00239 VSUB((*pnt), trn->v0, P); 00240 00241 CROSS(tmp, P, trn->edge2); 00242 U = DOT(tmp, norm) / lensqr; 00243 00244 CROSS(tmp, trn->edge1, P); 00245 V = DOT(tmp, norm) / lensqr; 00246 00247 W = 1.0 - (U + V); 00248 00249 N->x = W*trn->n0.x + U*trn->n1.x + V*trn->n2.x; 00250 N->y = W*trn->n0.y + U*trn->n1.y + V*trn->n2.y; 00251 N->z = W*trn->n0.z + U*trn->n1.z + V*trn->n2.z; 00252 00253 VNorm(N); 00254 } 00255
Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.