Shadowrun: Awakened 29 September 2011 - Build 871
apitrigeom.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  * apitrigeom.c - This file contains code for generating triangle tesselated
00060  *                geometry, for use with OpenGL, XGL, etc.
00061  * 
00062  *  $Id: apitrigeom.cpp,v 1.2 2007-02-22 17:54:15 dpoulsen Exp $
00063  */
00064 
00065 #include "machine.h"
00066 #include "types.h"
00067 #include "api.h"
00068 #include "macros.h"
00069 #include "vector.h"
00070 
00071 #define MyVNorm(a)      VNorm ((vector *) a)
00072 #define MyVCross(a,b,c)     VCross ((vector *) a, (vector *) b, (vector *) c)
00073 #define MyVAddS(x,a,b,c)    VAddS ((flt) x, (vector *) a, (vector *) b, (vector *) c)
00074 
00075 #define CYLFACETS 36
00076 #define RINGFACETS 36
00077 #define SPHEREFACETS 25
00078 
00079 void rt_tri_fcylinder(void * tex, vector ctr, vector axis, apiflt rad) {
00080   vector x, y, z, tmp;
00081   double u, v, u2, v2;
00082   int j;
00083   vector p1, p2, p3, p4;
00084   vector n1, n2;
00085 
00086   z = axis;
00087   MyVNorm(&z);
00088   tmp.x = z.y - 2.1111111;
00089   tmp.y = -z.z + 3.14159267;
00090   tmp.z = z.x - 3.915292342341;
00091   MyVNorm(&z);
00092   MyVNorm(&tmp);
00093   MyVCross(&z, &tmp, &x);
00094   MyVNorm(&x);
00095   MyVCross(&x, &z, &y);
00096   MyVNorm(&y);
00097 
00098   for (j=0; j<CYLFACETS; j++) {
00099      u = rad * sin((6.28 * j) / (CYLFACETS - 1.0));
00100      v = rad * cos((6.28 * j) / (CYLFACETS - 1.0));
00101     u2 = rad * sin((6.28 * (j + 1.0)) / (CYLFACETS - 1.0));
00102     v2 = rad * cos((6.28 * (j + 1.0)) / (CYLFACETS - 1.0));
00103 
00104     p1.x = p1.y = p1.z = 0.0;
00105     p4 = p3 = p2 = p1;
00106 
00107     MyVAddS(u, &x, &p1, &p1);
00108     MyVAddS(v, &y, &p1, &p1);
00109     n1 = p1;
00110     MyVNorm(&n1);
00111     MyVAddS(1.0, &ctr, &p1, &p1);
00112   
00113 
00114     MyVAddS(u2, &x, &p2, &p2);
00115     MyVAddS(v2, &y, &p2, &p2);
00116     n2 = p2;
00117     MyVNorm(&n2);
00118     MyVAddS(1.0, &ctr, &p2, &p2);
00119 
00120     MyVAddS(1.0, &axis, &p1, &p3);
00121     MyVAddS(1.0, &axis, &p2, &p4);
00122 
00123     rt_stri(tex, p1, p2, p3, n1, n2, n1);
00124     rt_stri(tex, p3, p2, p4, n1, n2, n2);
00125   }
00126 }
00127 
00128 void rt_tri_cylinder(void * tex, vector ctr, vector axis, apiflt rad) {
00129   rt_fcylinder(tex, ctr, axis, rad);
00130 }
00131 
00132 void rt_tri_ring(void * tex, vector ctr, vector norm, apiflt a, apiflt b) {
00133   vector x, y, z, tmp;
00134   double u, v, u2, v2;
00135   int j;
00136   vector p1, p2, p3, p4;
00137   vector n1, n2;
00138 
00139   z = norm;
00140   MyVNorm(&z);
00141   tmp.x = z.y - 2.1111111;
00142   tmp.y = -z.z + 3.14159267;
00143   tmp.z = z.x - 3.915292342341;
00144   MyVNorm(&z);
00145   MyVNorm(&tmp);
00146   MyVCross(&z, &tmp, &x);
00147   MyVNorm(&x);
00148   MyVCross(&x, &z, &y);
00149   MyVNorm(&y);
00150 
00151   for (j=0; j<RINGFACETS; j++) {
00152      u = sin((6.28 * j) / (RINGFACETS - 1.0));
00153      v = cos((6.28 * j) / (RINGFACETS - 1.0));
00154     u2 = sin((6.28 * (j + 1.0)) / (RINGFACETS - 1.0));
00155     v2 = cos((6.28 * (j + 1.0)) / (RINGFACETS - 1.0));
00156 
00157     p1.x = p1.y = p1.z = 0.0;
00158     p4 = p3 = p2 = p1;
00159 
00160     MyVAddS(u, &x, &p1, &p1);
00161     MyVAddS(v, &y, &p1, &p1);
00162     n1 = p1;
00163     MyVNorm(&n1);
00164     MyVAddS(a, &n1, &ctr, &p1);
00165     MyVAddS(b, &n1, &ctr, &p3);
00166 
00167     MyVAddS(u2, &x, &p2, &p2);
00168     MyVAddS(v2, &y, &p2, &p2);
00169     n2 = p2;
00170     MyVNorm(&n2);
00171     MyVAddS(a, &n2, &ctr, &p2);
00172     MyVAddS(b, &n2, &ctr, &p4);
00173 
00174     rt_stri(tex, p1, p2, p3, norm, norm, norm);
00175     rt_stri(tex, p3, p2, p4, norm, norm, norm);
00176 
00177   }
00178 } 
00179 
00180 void rt_tri_box(void * tex, vector min, vector max) {
00181   /* -XY face */
00182   rt_tri(tex, rt_vector(min.x, min.y, min.z),
00183               rt_vector(min.x, max.y, min.z), 
00184               rt_vector(max.x, max.y, min.z));
00185   rt_tri(tex, rt_vector(min.x, min.y, min.z),
00186               rt_vector(max.x, max.y, min.z), 
00187               rt_vector(max.x, min.y, min.z));
00188 
00189   /* +XY face */
00190   rt_tri(tex, rt_vector(min.x, min.y, max.z),
00191               rt_vector(max.x, max.y, max.z),
00192               rt_vector(min.x, max.y, max.z)); 
00193   rt_tri(tex, rt_vector(min.x, min.y, max.z),
00194               rt_vector(max.x, min.y, max.z),
00195               rt_vector(max.x, max.y, max.z)); 
00196 
00197   /* -YZ face */
00198   rt_tri(tex, rt_vector(min.x, min.y, min.z),
00199               rt_vector(min.x, max.y, max.z),
00200               rt_vector(min.x, min.y, max.z)); 
00201   rt_tri(tex, rt_vector(min.x, min.y, min.z),
00202               rt_vector(min.x, max.y, min.z),
00203               rt_vector(min.x, max.y, max.z)); 
00204 
00205   /* +YZ face */
00206   rt_tri(tex, rt_vector(max.x, min.y, min.z),
00207               rt_vector(max.x, min.y, max.z),
00208               rt_vector(max.x, max.y, max.z));
00209   rt_tri(tex, rt_vector(max.x, min.y, min.z),
00210               rt_vector(max.x, max.y, max.z),
00211               rt_vector(max.x, max.y, min.z));
00212 
00213   /* -XZ face */
00214   rt_tri(tex, rt_vector(min.x, min.y, min.z),
00215               rt_vector(min.x, min.y, max.z), 
00216               rt_vector(max.x, min.y, max.z));
00217   rt_tri(tex, rt_vector(min.x, min.y, min.z),
00218               rt_vector(max.x, min.y, max.z), 
00219               rt_vector(max.x, min.y, min.z));
00220 
00221   /* +XZ face */
00222   rt_tri(tex, rt_vector(min.x, max.y, min.z),
00223               rt_vector(max.x, max.y, max.z),
00224               rt_vector(min.x, max.y, max.z)); 
00225   rt_tri(tex, rt_vector(min.x, max.y, min.z),
00226               rt_vector(max.x, max.y, min.z),
00227               rt_vector(max.x, max.y, max.z)); 
00228 }
00229 
00230 void rt_tri_sphere(void * tex, vector ctr, apiflt rad) {
00231 }
00232 
00233 void rt_tri_plane(void * tex, vector ctr, vector norm) {
00234   rt_tri_ring(tex, ctr, norm, 0.0, 10000.0);
00235 } 
00236 

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