Shadowrun: Awakened 29 September 2011 - Build 871
apigeom.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  * api.c - This file contains all of the API calls that are defined for
00060  *         external driver code to use.  
00061  * 
00062  *  $Id: apigeom.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 
00073 void rt_polycylinder(void * tex, vector * points, int numpts, apiflt rad) {
00074   vector a;
00075   int i;
00076 
00077   if ((points == NULL) || (numpts == 0)) {
00078     return;
00079   }
00080 
00081   if (numpts > 0) {
00082     rt_sphere(tex, points[0], rad);
00083     
00084     if (numpts > 1) {
00085       for (i=1; i<numpts; i++) {
00086         a.x = points[i].x - points[i-1].x;
00087         a.y = points[i].y - points[i-1].y;
00088         a.z = points[i].z - points[i-1].z;
00089         
00090         rt_fcylinder(tex, points[i-1], a, rad);
00091         rt_sphere(tex, points[i], rad);
00092       }
00093     }
00094   }
00095 }
00096 
00097 void rt_heightfield(void * tex, vector ctr, int m, int n, 
00098                     apiflt * field, apiflt wx, apiflt wy) {
00099   int xx,yy; 
00100   vector v0, v1, v2; 
00101   apiflt xoff, yoff, zoff;
00102 
00103   xoff=ctr.x - (wx / 2.0);
00104   yoff=ctr.z - (wy / 2.0);
00105   zoff=ctr.y;
00106 
00107   for (yy=0; yy<(n-1); yy++) { 
00108     for (xx=0; xx<(m-1); xx++) {
00109       v0.x=wx*(xx    )/(m*1.0) + xoff; 
00110       v0.y=field[(yy    )*m + (xx    )] + zoff;
00111       v0.z=wy*(yy    )/(n*1.0) + yoff;
00112 
00113       v1.x=wx*(xx + 1)/(m*1.0) + xoff; 
00114       v1.y=field[(yy    )*m + (xx + 1)] + zoff;
00115       v1.z=wy*(yy    )/(n*1.0) + yoff;
00116 
00117       v2.x=wx*(xx + 1)/(m*1.0) + xoff; 
00118       v2.y=field[(yy + 1)*m + (xx + 1)] + zoff;
00119       v2.z=wy*(yy + 1)/(n*1.0) + yoff;
00120 
00121       rt_tri(tex, v1, v0, v2);
00122 
00123       v0.x=wx*(xx    )/(m*1.0) + xoff;
00124       v0.y=field[(yy    )*m + (xx    )] + zoff;
00125       v0.z=wy*(yy    )/(n*1.0) + yoff;
00126 
00127       v1.x=wx*(xx    )/(m*1.0) + xoff;
00128       v1.y=field[(yy + 1)*m + (xx    )] + zoff;
00129       v1.z=wy*(yy + 1)/(n*1.0) + yoff;
00130 
00131       v2.x=wx*(xx + 1)/(m*1.0) + xoff;
00132       v2.y=field[(yy + 1)*m + (xx + 1)] + zoff;
00133       v2.z=wy*(yy + 1)/(n*1.0) + yoff;
00134  
00135       rt_tri(tex, v0, v1, v2);
00136     }
00137   } 
00138 } /* end of heightfield */
00139 
00140 
00141 static void rt_sheightfield(void * tex, vector ctr, int m, int n, 
00142                     apiflt * field, apiflt wx, apiflt wy) {
00143   vector * vertices;
00144   vector * normals;
00145   vector offset;
00146   apiflt xinc, yinc;
00147   int x, y, addr; 
00148    
00149   vertices = (vector *) malloc(m*n*sizeof(vector));
00150   normals = (vector *) malloc(m*n*sizeof(vector));
00151 
00152   offset.x = ctr.x - (wx / 2.0);
00153   offset.y = ctr.z - (wy / 2.0);
00154   offset.z = ctr.y;
00155 
00156   xinc = wx / ((apiflt) m);
00157   yinc = wy / ((apiflt) n);
00158 
00159   /* build vertex list */
00160   for (y=0; y<n; y++) { 
00161     for (x=0; x<m; x++) {
00162       addr = y*m + x;
00163       vertices[addr] = rt_vector(
00164         x * xinc + offset.x,
00165         field[addr] + offset.z,
00166         y * yinc + offset.y);
00167     }
00168   }
00169 
00170   /* build normals from vertex list */
00171   for (x=1; x<m; x++) {
00172     normals[x] = normals[(n - 1)*m + x] = rt_vector(0.0, 1.0, 0.0);
00173   }
00174   for (y=1; y<n; y++) {
00175     normals[y*m] = normals[y*m + (m-1)] = rt_vector(0.0, 1.0, 0.0);
00176   }
00177   for (y=1; y<(n-1); y++) {
00178     for (x=1; x<(m-1); x++) {
00179       addr = y*m + x;
00180 
00181       normals[addr] = rt_vector(
00182         -(field[addr + 1] - field[addr - 1]) / (2.0 * xinc), 
00183         1.0, 
00184         -(field[addr + m] - field[addr - m]) / (2.0 * yinc));
00185 
00186       MyVNorm(&normals[addr]);
00187     }
00188   }    
00189 
00190   /* generate actual triangles */
00191   for (y=0; y<(n-1); y++) {
00192     for (x=0; x<(m-1); x++) {
00193       addr = y*m + x;
00194 
00195       rt_stri(tex, vertices[addr], vertices[addr + 1 + m], vertices[addr + 1],
00196                    normals[addr], normals[addr + 1 + m], normals[addr + 1]);
00197       rt_stri(tex, vertices[addr], vertices[addr + m], vertices[addr + 1 + m],
00198                    normals[addr], normals[addr + m], normals[addr + 1 + m]);
00199     }
00200   }
00201 
00202   free(normals);
00203   free(vertices);
00204 } /* end of smoothed heightfield */
00205 
00206 
00207 static void adjust(apiflt *base, int xres, int yres, apiflt wx, apiflt wy, 
00208         int xa, int ya, int x, int y, int xb, int yb) {
00209   apiflt d, v;
00210   
00211   if (base[x + (xres*y)]==0.0) { 
00212 
00213     d=(abs(xa - xb) / (xres * 1.0))*wx + (abs(ya - yb) / (yres * 1.0))*wy; 
00214 
00215     v=(base[xa + (xres*ya)] + base[xb + (xres*yb)]) / 2.0 +
00216        (((((rand() % 1000) - 500.0)/500.0)*d) / 8.0);
00217 
00218     if (v < 0.0) v=0.0; 
00219     if (v > (xres + yres)) v=(xres + yres);
00220     base[x + (xres * y)]=v; 
00221  } 
00222 }
00223 
00224 static void subdivide(apiflt *base, int xres, int yres, apiflt wx, apiflt wy,
00225                   int x1, int y1, int x2, int y2) {
00226   long x,y;
00227 
00228   if (((x2 - x1) < 2) && ((y2 - y1) < 2)) { return; }
00229 
00230   x=(x1 + x2) / 2;
00231   y=(y1 + y2) / 2;
00232 
00233   adjust(base, xres, yres, wx, wy, x1, y1, x, y1, x2, y1);
00234   adjust(base, xres, yres, wx, wy, x2, y1, x2, y, x2, y2);
00235   adjust(base, xres, yres, wx, wy, x1, y2, x, y2, x2, y2);
00236   adjust(base, xres, yres, wx, wy, x1, y1, x1, y, x1, y2);
00237 
00238  
00239   if (base[x + xres*y]==0.0) {
00240     base[x + (xres * y)]=(base[x1 + xres*y1] + base[x2 + xres*y1] +
00241                           base[x2 + xres*y2] + base[x1 + xres*y2]   )/4.0;
00242   }
00243  
00244   subdivide(base, xres, yres, wx, wy, x1, y1 ,x ,y);
00245   subdivide(base, xres, yres, wx, wy, x, y1, x2, y);
00246   subdivide(base, xres, yres, wx, wy, x, y, x2, y2);
00247   subdivide(base, xres, yres, wx, wy, x1, y, x, y2);
00248 }
00249 
00250 void rt_landscape(void * tex, int m, int n, 
00251                 vector ctr, apiflt wx, apiflt wy) {
00252   int totalsize, x, y;
00253   apiflt * field; 
00254 
00255   totalsize=m*n;
00256 
00257   srand(totalsize);
00258 
00259   field=(apiflt *) malloc(totalsize*sizeof(apiflt));
00260 
00261   for (y=0; y<n; y++) {
00262     for (x=0; x<m; x++) {
00263        field[x + y*m]=0.0;
00264     }
00265   }
00266 
00267   field[0 + 0]=1.0 + (rand() % 100)/100.0;
00268   field[m - 1]=1.0 + (rand() % 100)/100.0;
00269   field[0     + m*(n - 1)]=1.0 + (rand() % 100)/100.0;
00270   field[m - 1 + m*(n - 1)]=1.0 + (rand() % 100)/100.0;
00271 
00272   subdivide(field, m, n, wx, wy, 0, 0, m-1, n-1);
00273 
00274   rt_sheightfield(tex, ctr, m, n, field, wx, wy);
00275 
00276   free(field);
00277 }
00278 

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