Shadowrun: Awakened 29 September 2011 - Build 871
api.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: api.cpp,v 1.5 2007-02-22 17:54:14 dpoulsen Exp $
00063  */
00064 
00065 #include "machine.h"
00066 #include "types.h"
00067 #include "macros.h"
00068 
00069 #include "box.h"
00070 #include "cylinder.h"
00071 #include "plane.h"
00072 #include "quadric.h"
00073 #include "ring.h"
00074 #include "sphere.h"
00075 #include "triangle.h"
00076 #include "vol.h"
00077 #include "extvol.h"
00078 
00079 #include "texture.h"
00080 #include "light.h"
00081 #include "render.h"
00082 #include "camera.h"
00083 #include "vector.h"
00084 #include "intersect.h"
00085 #include "shade.h"
00086 #include "util.h"
00087 #include "imap.h"
00088 #include "global.h"
00089 
00090 #include "video.h"
00091 
00092 typedef void * SceneHandle;
00093 #include "api.h"
00094 
00095 
00096 vector rt_vector(apiflt x, apiflt y, apiflt z) {
00097   vector v;
00098 
00099   v.x = x;
00100   v.y = y;
00101   v.z = z;
00102 
00103   return v;
00104 }
00105 
00106 color rt_color(apiflt r, apiflt g, apiflt b) {
00107   color c;
00108   
00109   c.r = r;
00110   c.g = g;
00111   c.b = b;
00112   
00113   return c;
00114 }
00115 
00116 void rt_initialize(int * argc, char ***argv) {
00117   rpcmsg msg;
00118 
00119   reset_object();
00120   reset_lights();
00121   InitTextures();
00122 
00123   if (!parinitted) {
00124     parinitted=1;
00125 
00126     msg.type=1; /* setup a ping message */
00127   }
00128 }
00129 
00130 void rt_renderscene(SceneHandle voidscene) {
00131   scenedef * scene = (scenedef *) voidscene;
00132   renderscene(*scene);
00133 }
00134 
00135 void rt_camerasetup(SceneHandle voidscene, apiflt zoom, apiflt aspectratio, 
00136     int antialiasing, int raydepth, 
00137     vector camcent, vector viewvec, vector upvec) {
00138   scenedef * scene = (scenedef *) voidscene;
00139 
00140   vector newupvec;
00141   vector newviewvec;
00142   vector newrightvec;
00143  
00144   VCross((vector *) &upvec, &viewvec, &newrightvec);
00145   VNorm(&newrightvec);
00146 
00147   VCross((vector *) &viewvec, &newrightvec, &newupvec);
00148   VNorm(&newupvec);
00149 
00150   newviewvec=viewvec;
00151   VNorm(&newviewvec);
00152 
00153 
00154   scene->camzoom=zoom; 
00155   scene->aspectratio=aspectratio;
00156   scene->antialiasing=antialiasing;
00157   scene->raydepth=raydepth; 
00158   scene->camcent=camcent;
00159   scene->camviewvec=newviewvec;
00160   scene->camrightvec=newrightvec;
00161   scene->camupvec=newupvec;
00162 }
00163 
00164 void rt_outputfile(SceneHandle voidscene, const char * outname) {
00165   scenedef * scene = (scenedef *) voidscene;
00166   strcpy((char *) &scene->outfilename, outname);
00167 }
00168 
00169 void rt_resolution(SceneHandle voidscene, int hres, int vres) {
00170   scenedef * scene = (scenedef *) voidscene;
00171   scene->hres=hres;
00172   scene->vres=vres;
00173 }
00174 
00175 void rt_verbose(SceneHandle voidscene, int v) {
00176   scenedef * scene = (scenedef *) voidscene;
00177   scene->verbosemode = v;
00178 }
00179 
00180 void rt_rawimage(SceneHandle voidscene, unsigned char *rawimage) {
00181   scenedef * scene = (scenedef *) voidscene;
00182   scene->rawimage = rawimage;
00183 }
00184 
00185 void rt_background(SceneHandle voidscene, color col) {
00186   scenedef * scene = (scenedef *) voidscene;
00187   scene->background.r = col.r;
00188   scene->background.g = col.g;
00189   scene->background.b = col.b;
00190 }
00191 
00192 void rt_boundmode(SceneHandle voidscene, int mode) {
00193   scenedef * scene = (scenedef *) voidscene;
00194   scene->boundmode = mode;
00195 }
00196 
00197 void rt_boundthresh(SceneHandle voidscene, int threshold) {
00198   scenedef * scene = (scenedef *) voidscene;
00199  
00200   if (threshold > 1) {
00201     scene->boundthresh = threshold;
00202   }
00203   else {
00204     rtmesg("Ignoring out-of-range automatic bounding threshold.\n");
00205     rtmesg("Automatic bounding threshold reset to default.\n");
00206     scene->boundthresh = MAXOCTNODES;
00207   }
00208 }
00209 
00210 void rt_displaymode(SceneHandle voidscene, int mode) {
00211   scenedef * scene = (scenedef *) voidscene;
00212   scene->displaymode = mode;
00213 }
00214 
00215 
00216 void rt_scenesetup(SceneHandle voidscene, char * outname, int hres, int vres, int verbose) {
00217   rt_outputfile(voidscene, outname);
00218   rt_resolution(voidscene, hres, vres);
00219   rt_verbose(voidscene, verbose);
00220 }
00221 
00222 SceneHandle rt_newscene(void) {
00223   scenedef * scene;
00224   SceneHandle voidscene;
00225 
00226   scene = (scenedef *) malloc(sizeof(scenedef));
00227   memset(scene, 0, sizeof(scenedef));             /* clear all valuas to 0  */
00228 
00229   voidscene = (SceneHandle) scene;
00230 
00231   rt_outputfile(voidscene, "/dev/null");   /* default output file (.tga)   */
00232   rt_resolution(voidscene, 512, 512);             /* 512x512 resolution     */
00233   rt_verbose(voidscene, 0);                       /* verbose messages off   */
00234   rt_rawimage(voidscene, NULL);                   /* raw image output off   */
00235   rt_boundmode(voidscene, RT_BOUNDING_ENABLED);   /* spatial subdivision on */
00236   rt_boundthresh(voidscene, MAXOCTNODES);         /* default threshold      */
00237   rt_displaymode(voidscene, RT_DISPLAY_ENABLED);  /* video output on        */
00238   rt_camerasetup(voidscene, 1.0, 1.0, 0, 6,
00239                  rt_vector(0.0, 0.0, 0.0),
00240                  rt_vector(0.0, 0.0, 1.0),
00241                  rt_vector(0.0, 1.0, 0.0));
00242  
00243   return scene;
00244 }
00245 
00246 void rt_deletescene(SceneHandle scene) {
00247   if (scene != NULL)
00248     free(scene);
00249 }
00250 
00251 void apitextotex(apitexture * apitex, texture * tex) {
00252   switch(apitex->texturefunc) {
00253     case 0: 
00254       tex->texfunc=(color(*)(void *, void *, void *))(standard_texture);
00255       break;
00256 
00257     case 1: 
00258       tex->texfunc=(color(*)(void *, void *, void *))(checker_texture);
00259       break;
00260 
00261     case 2: 
00262       tex->texfunc=(color(*)(void *, void *, void *))(grit_texture);
00263       break;
00264 
00265     case 3: 
00266       tex->texfunc=(color(*)(void *, void *, void *))(marble_texture);
00267       break;
00268 
00269     case 4: 
00270       tex->texfunc=(color(*)(void *, void *, void *))(wood_texture);
00271       break;
00272 
00273     case 5: 
00274       tex->texfunc=(color(*)(void *, void *, void *))(gnoise_texture);
00275       break;
00276     
00277     case 6: 
00278       tex->texfunc=(color(*)(void *, void *, void *))(cyl_checker_texture);
00279       break;
00280 
00281     case 7: 
00282       tex->texfunc=(color(*)(void *, void *, void *))(image_sphere_texture);
00283       tex->img=AllocateImage((char *)apitex->imap);
00284       break;
00285 
00286     case 8: 
00287       tex->texfunc=(color(*)(void *, void *, void *))(image_cyl_texture);
00288       tex->img=AllocateImage((char *)apitex->imap);
00289       break;
00290 
00291     case 9: 
00292       tex->texfunc=(color(*)(void *, void *, void *))(image_plane_texture);
00293       tex->img=AllocateImage((char *)apitex->imap);
00294       break;
00295 
00296     default: 
00297       tex->texfunc=(color(*)(void *, void *, void *))(standard_texture);
00298       break;
00299   }
00300 
00301        tex->ctr = apitex->ctr;
00302        tex->rot = apitex->rot;
00303      tex->scale = apitex->scale;
00304       tex->uaxs = apitex->uaxs;
00305       tex->vaxs = apitex->vaxs;
00306    tex->ambient = apitex->ambient;
00307    tex->diffuse = apitex->diffuse;
00308   tex->specular = apitex->specular;
00309    tex->opacity = apitex->opacity;
00310        tex->col = apitex->col; 
00311 
00312   tex->islight = 0;
00313   tex->shadowcast = 1;
00314   tex->phong = 0.0;
00315   tex->phongexp = 0.0;
00316   tex->phongtype = 0;
00317 }
00318 
00319 void * rt_texture(apitexture * apitex) {
00320   texture * tex;
00321 
00322   tex=(texture *)rt_getmem(sizeof(texture));
00323   apitextotex(apitex, tex); 
00324   return(tex);
00325 }
00326 
00327 void rt_tex_color(void * voidtex, color col) {
00328   texture * tex = (texture *) voidtex;
00329   tex->col = col;
00330 }
00331 
00332 void rt_tex_phong(void * voidtex, apiflt phong, apiflt phongexp, int type) {
00333   texture * tex = (texture *) voidtex;
00334   tex->phong = phong;
00335   tex->phongexp = phongexp;
00336   tex->phongtype = type;
00337 }
00338 
00339 void rt_light(void * tex, vector ctr, apiflt rad) {
00340   point_light * li;
00341 
00342   li=newlight(tex, (vector) ctr, rad);
00343 
00344   li->tex->islight=1;
00345   li->tex->shadowcast=1;
00346   li->tex->diffuse=0.0;
00347   li->tex->specular=0.0;
00348   li->tex->opacity=1.0;
00349 
00350   add_light(li);
00351   add_object((object *)li);
00352 }
00353 
00354 void rt_scalarvol(void * tex, vector min, vector max,
00355     int xs, int ys, int zs, char * fname, void * invol) {
00356   add_object((object *) newscalarvol(tex, (vector)min, (vector)max, xs, ys, zs, fname, (scalarvol *) invol));
00357 }
00358 
00359 void rt_extvol(void * tex, vector min, vector max, int samples, flt (* evaluator)(flt, flt, flt)) {
00360   add_object((object *) newextvol(tex, (vector)min, (vector)max, samples, evaluator));
00361 }
00362 
00363 void rt_box(void * tex, vector min, vector max) {
00364   add_object((object *) newbox(tex, (vector)min, (vector)max));
00365 } 
00366 
00367 void rt_cylinder(void * tex, vector ctr, vector axis, apiflt rad) {
00368   add_object(newcylinder(tex, (vector)ctr, (vector)axis, rad));
00369 }
00370 
00371 void rt_fcylinder(void * tex, vector ctr, vector axis, apiflt rad) {
00372   add_object(newfcylinder(tex, (vector)ctr, (vector)axis, rad));
00373 }
00374 
00375 void rt_plane(void * tex, vector ctr, vector norm) {
00376   add_object(newplane(tex, (vector)ctr, (vector)norm));
00377 } 
00378 
00379 void rt_ring(void * tex, vector ctr, vector norm, apiflt a, apiflt b) {
00380   add_object(newring(tex, (vector)ctr, (vector)norm, a, b));
00381 } 
00382 
00383 void rt_sphere(void * tex, vector ctr, apiflt rad) {
00384   add_object(newsphere(tex, (vector)ctr, rad));
00385 }
00386 
00387 void rt_tri(void * tex, vector v0, vector v1, vector v2) {
00388   object * trn;
00389 
00390   trn = newtri(tex, (vector)v0, (vector)v1, (vector)v2);
00391 
00392   if (trn != NULL) { 
00393     add_object(trn);
00394   }
00395 } 
00396 
00397 void rt_stri(void * tex, vector v0, vector v1, vector v2, 
00398         vector n0, vector n1, vector n2) {
00399   object * trn;
00400  
00401   trn = newstri(tex, (vector)v0, (vector)v1, (vector)v2, (vector)n0, (vector)n1, (vector)n2);
00402 
00403   if (trn != NULL) { 
00404     add_object(trn);
00405   }
00406 } 
00407 
00408 void rt_quadsphere(void * tex, vector ctr, apiflt rad) {
00409   quadric * q;
00410   flt factor;
00411   q=(quadric *) newquadric();
00412   factor= 1.0 / (rad*rad);
00413   q->tex=(texture *)tex;
00414   q->ctr=ctr;
00415  
00416   q->mat.a=factor;
00417   q->mat.b=0.0;
00418   q->mat.c=0.0;
00419   q->mat.d=0.0;
00420   q->mat.e=factor;
00421   q->mat.f=0.0;
00422   q->mat.g=0.0;
00423   q->mat.h=factor;
00424   q->mat.i=0.0;
00425   q->mat.j=-1.0;
00426  
00427   add_object((object *)q);
00428 }

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