![]() |
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 * cylinder.c - This file contains the functions for dealing with cylinders. 00060 * 00061 * $Id: cylinder.cpp,v 1.2 2007-02-22 17:54:15 dpoulsen Exp $ 00062 */ 00063 00064 #include "machine.h" 00065 #include "types.h" 00066 #include "macros.h" 00067 #include "vector.h" 00068 #include "intersect.h" 00069 #include "util.h" 00070 00071 #define CYLINDER_PRIVATE 00072 #include "cylinder.h" 00073 00074 static object_methods cylinder_methods = { 00075 (void (*)(void *, void *))(cylinder_intersect), 00076 (void (*)(void *, void *, void *, void *))(cylinder_normal), 00077 cylinder_bbox, 00078 free 00079 }; 00080 00081 static object_methods fcylinder_methods = { 00082 (void (*)(void *, void *))(fcylinder_intersect), 00083 (void (*)(void *, void *, void *, void *))(cylinder_normal), 00084 fcylinder_bbox, 00085 free 00086 }; 00087 00088 00089 object * newcylinder(void * tex, vector ctr, vector axis, flt rad) { 00090 cylinder * c; 00091 00092 c=(cylinder *) rt_getmem(sizeof(cylinder)); 00093 memset(c, 0, sizeof(cylinder)); 00094 c->methods = &cylinder_methods; 00095 00096 c->tex=(texture *) tex; 00097 c->ctr=ctr; 00098 c->axis=axis; 00099 c->rad=rad; 00100 return (object *) c; 00101 } 00102 00103 static int cylinder_bbox(void * obj, vector * min, vector * max) { 00104 return 0; /* infinite / unbounded object */ 00105 } 00106 00107 static void cylinder_intersect(cylinder * cyl, ray * ry) { 00108 vector rc, n, D, O; 00109 flt t, s, tin, tout, ln, d; 00110 00111 rc.x = ry->o.x - cyl->ctr.x; 00112 rc.y = ry->o.y - cyl->ctr.y; 00113 rc.z = ry->o.z - cyl->ctr.z; 00114 00115 VCross(&ry->d, &cyl->axis, &n); 00116 00117 VDOT(ln, n, n); 00118 ln=sqrt(ln); /* finish length calculation */ 00119 00120 if (ln == 0.0) { /* ray is parallel to the cylinder.. */ 00121 VDOT(d, rc, cyl->axis); 00122 D.x = rc.x - d * cyl->axis.x; 00123 D.y = rc.y - d * cyl->axis.y; 00124 D.z = rc.z - d * cyl->axis.z; 00125 VDOT(d, D, D); 00126 d = sqrt(d); 00127 tin = -FHUGE; 00128 tout = FHUGE; 00129 /* if (d <= cyl->rad) then ray is inside cylinder.. else outside */ 00130 } 00131 00132 VNorm(&n); 00133 VDOT(d, rc, n); 00134 d = fabs(d); 00135 00136 if (d <= cyl->rad) { /* ray intersects cylinder.. */ 00137 VCross(&rc, &cyl->axis, &O); 00138 VDOT(t, O, n); 00139 t = - t / ln; 00140 VCross(&n, &cyl->axis, &O); 00141 VNorm(&O); 00142 VDOT(s, ry->d, O); 00143 s = fabs(sqrt(cyl->rad*cyl->rad - d*d) / s); 00144 tin = t - s; 00145 add_intersection(tin, (object *) cyl, ry); 00146 tout = t + s; 00147 add_intersection(tout, (object *) cyl, ry); 00148 } 00149 } 00150 00151 static void cylinder_normal(cylinder * cyl, vector * pnt, ray * incident, vector * N) { 00152 vector a,b,c; 00153 flt t; 00154 00155 VSub((vector *) pnt, &(cyl->ctr), &a); 00156 00157 c=cyl->axis; 00158 00159 VNorm(&c); 00160 00161 VDOT(t, a, c); 00162 00163 b.x = c.x * t + cyl->ctr.x; 00164 b.y = c.y * t + cyl->ctr.y; 00165 b.z = c.z * t + cyl->ctr.z; 00166 00167 VSub(pnt, &b, N); 00168 VNorm(N); 00169 00170 if (VDot(N, &(incident->d)) > 0.0) { /* make cylinder double sided */ 00171 N->x=-N->x; 00172 N->y=-N->y; 00173 N->z=-N->z; 00174 } 00175 } 00176 00177 object * newfcylinder(void * tex, vector ctr, vector axis, flt rad) { 00178 cylinder * c; 00179 00180 c=(cylinder *) rt_getmem(sizeof(cylinder)); 00181 memset(c, 0, sizeof(cylinder)); 00182 c->methods = &fcylinder_methods; 00183 00184 c->tex=(texture *) tex; 00185 c->ctr=ctr; 00186 c->axis=axis; 00187 c->rad=rad; 00188 00189 return (object *) c; 00190 } 00191 00192 static int fcylinder_bbox(void * obj, vector * min, vector * max) { 00193 cylinder * c = (cylinder *) obj; 00194 vector mintmp, maxtmp; 00195 00196 mintmp.x = c->ctr.x; 00197 mintmp.y = c->ctr.y; 00198 mintmp.z = c->ctr.z; 00199 maxtmp.x = c->ctr.x + c->axis.x; 00200 maxtmp.y = c->ctr.y + c->axis.y; 00201 maxtmp.z = c->ctr.z + c->axis.z; 00202 00203 min->x = MYMIN(mintmp.x, maxtmp.x); 00204 min->y = MYMIN(mintmp.y, maxtmp.y); 00205 min->z = MYMIN(mintmp.z, maxtmp.z); 00206 min->x -= c->rad; 00207 min->y -= c->rad; 00208 min->z -= c->rad; 00209 00210 max->x = MYMAX(mintmp.x, maxtmp.x); 00211 max->y = MYMAX(mintmp.y, maxtmp.y); 00212 max->z = MYMAX(mintmp.z, maxtmp.z); 00213 max->x += c->rad; 00214 max->y += c->rad; 00215 max->z += c->rad; 00216 00217 return 1; 00218 } 00219 00220 00221 static void fcylinder_intersect(cylinder * cyl, ray * ry) { 00222 vector rc, n, O, hit, tmp2, ctmp4; 00223 flt t, s, tin, tout, ln, d, tmp, tmp3; 00224 00225 rc.x = ry->o.x - cyl->ctr.x; 00226 rc.y = ry->o.y - cyl->ctr.y; 00227 rc.z = ry->o.z - cyl->ctr.z; 00228 00229 VCross(&ry->d, &cyl->axis, &n); 00230 00231 VDOT(ln, n, n); 00232 ln=sqrt(ln); /* finish length calculation */ 00233 00234 if (ln == 0.0) { /* ray is parallel to the cylinder.. */ 00235 return; /* in this case, we want to miss or go through the "hole" */ 00236 } 00237 00238 VNorm(&n); 00239 VDOT(d, rc, n); 00240 d = fabs(d); 00241 00242 if (d <= cyl->rad) { /* ray intersects cylinder.. */ 00243 VCross(&rc, &cyl->axis, &O); 00244 VDOT(t, O, n); 00245 t = - t / ln; 00246 VCross(&n, &cyl->axis, &O); 00247 VNorm(&O); 00248 VDOT(s, ry->d, O); 00249 s = fabs(sqrt(cyl->rad*cyl->rad - d*d) / s); 00250 tin = t - s; 00251 00252 RAYPNT(hit, (*ry), tin); 00253 00254 ctmp4=cyl->axis; 00255 VNorm(&ctmp4); 00256 00257 tmp2.x = hit.x - cyl->ctr.x; 00258 tmp2.y = hit.y - cyl->ctr.y; 00259 tmp2.z = hit.z - cyl->ctr.z; 00260 00261 VDOT(tmp, tmp2, ctmp4); 00262 VDOT(tmp3, cyl->axis, cyl->axis); 00263 00264 if ((tmp > 0.0) && (tmp < sqrt(tmp3))) 00265 add_intersection(tin, (object *) cyl, ry); 00266 tout = t + s; 00267 00268 RAYPNT(hit, (*ry), tout); 00269 00270 tmp2.x = hit.x - cyl->ctr.x; 00271 tmp2.y = hit.y - cyl->ctr.y; 00272 tmp2.z = hit.z - cyl->ctr.z; 00273 00274 VDOT(tmp, tmp2, ctmp4); 00275 VDOT(tmp3, cyl->axis, cyl->axis); 00276 00277 if ((tmp > 0.0) && (tmp < sqrt(tmp3))) 00278 add_intersection(tout, (object *) cyl, ry); 00279 } 00280 } 00281
Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.