Shadowrun: Awakened 29 September 2011 - Build 871
parse.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  * parse.c - an UltraLame (tm) parser for simple data files...
00060  *
00061  *  $Id: parse.cpp,v 1.3 2007-02-22 17:54:15 dpoulsen Exp $
00062  */
00063 
00064 #include <stdio.h>
00065 #include <math.h>
00066 #include <string.h>
00067 #include <stdlib.h>
00068 #include <ctype.h> /* needed for toupper(), macro.. */
00069 
00070 #include "types.h"
00071 #include "api.h"      /* rendering API */
00072 
00073 #define PARSE_INTERNAL
00074 #include "parse.h" /* self protos */
00075 #undef PARSE_INTERNAL
00076 
00077 static texentry textable[NUMTEXS]; /* texture lookup table */
00078 static texentry defaulttex;     /* The default texture when a lookup fails */
00079 static int numtextures;         /* number of TEXDEF textures               */
00080 static int numobjectsparsed;    /* total number of objects parsed so far   */
00081 static color scenebackcol;   /* scene background color                  */
00082 
00083 static int stringcmp(const char * a, const char * b) {
00084   int i, s, l;
00085 
00086   s=strlen(a);
00087   l=strlen(b);
00088 
00089   if (s != l) 
00090     return 1;
00091 
00092   for (i=0; i<s; i++) {
00093     if (toupper(a[i]) != toupper(b[i])) {
00094       return 1;
00095     }
00096   }
00097   return 0;
00098 }
00099 
00100 static void reset_tex_table(void) {
00101   apitexture apitex;
00102   
00103   numtextures=0; 
00104   memset(&textable, 0, sizeof(textable));
00105 
00106   apitex.col.r=1.0;
00107   apitex.col.g=1.0; 
00108   apitex.col.b=1.0; 
00109   apitex.ambient=0.1;
00110   apitex.diffuse=0.9;
00111   apitex.specular=0.0;
00112   apitex.opacity=1.0;
00113   apitex.texturefunc=0;
00114 
00115   defaulttex.tex=rt_texture(&apitex);
00116 }
00117 
00118 static errcode add_texture(void * tex, char name[TEXNAMELEN]) {
00119   textable[numtextures].tex=tex;
00120   strcpy(textable[numtextures].name, name); 
00121 
00122   numtextures++;
00123   if (numtextures > NUMTEXS) {
00124     fprintf(stderr, "Parse: %d textures allocated, texture slots full!\n", numtextures);
00125     numtextures--; /* keep writing over last texture if we've run out.. */
00126     return PARSEALLOCERR;
00127   }
00128 
00129   return PARSENOERR;
00130 }
00131 
00132 static void * find_texture(char name[TEXNAMELEN]) {
00133   int i;
00134   
00135   for (i=0; i<numtextures; i++) {
00136     if (strcmp(name, textable[i].name) == 0) 
00137     return textable[i].tex; 
00138   }
00139   fprintf(stderr, "Undefined texture '%s', using default. \n",name);
00140   return(defaulttex.tex); 
00141 }
00142 
00143 apiflt degtorad(apiflt deg) {
00144   apiflt tmp;
00145   tmp=deg * 3.1415926 / 180.0;
00146   return tmp;
00147 }
00148 
00149 static void degvectoradvec(vector * degvec) {
00150   vector tmp;
00151 
00152   tmp.x=degtorad(degvec->x);
00153   tmp.y=degtorad(degvec->y);
00154   tmp.z=degtorad(degvec->z);
00155   *degvec=tmp;
00156 }
00157 
00158 static void InitRot3d(RotMat * rot, apiflt x, apiflt y, apiflt z) {
00159   rot->rx1=cos(y)*cos(z);
00160   rot->rx2=sin(x)*sin(y)*cos(z) - cos(x)*sin(z);
00161   rot->rx3=sin(x)*sin(z) + cos(x)*cos(z)*sin(y);
00162   
00163   rot->ry1=cos(y)*sin(z);
00164   rot->ry2=cos(x)*cos(z) + sin(x)*sin(y)*sin(z);
00165   rot->ry3=cos(x)*sin(y)*sin(z) - sin(x)*cos(z);
00166 
00167   rot->rz1=sin(y);
00168   rot->rz2=sin(x)*cos(y);
00169   rot->rz3=cos(x)*cos(y);
00170 }
00171 
00172 static void Rotate3d(RotMat * rot, vector * vec) {
00173   vector tmp;
00174   tmp.x=(vec->x*(rot->rx1) + vec->y*(rot->rx2) + vec->z*(rot->rx3));
00175   tmp.y=(vec->x*(rot->ry1) + vec->y*(rot->ry2) + vec->z*(rot->ry3));
00176   tmp.z=(vec->x*(rot->rz1) + vec->y*(rot->rz2) + vec->z*(rot->rz3));
00177   *vec=tmp; 
00178 }
00179 
00180 static void Scale3d(vector * scale, vector * vec) {
00181   vec->x=vec->x * scale->x;
00182   vec->y=vec->y * scale->y;
00183   vec->z=vec->z * scale->z;
00184 }
00185 
00186 static void Trans3d(vector * trans, vector * vec) {
00187   vec->x+=trans->x;
00188   vec->y+=trans->y;
00189   vec->z+=trans->z;
00190 }
00191 
00192 static errcode GetString(FILE * dfile, const char * string) {
00193   char data[255];
00194 
00195   fscanf(dfile,"%s",data);
00196   if (stringcmp(data, string) != 0) {
00197     fprintf(stderr, "parse: Expected %s, got %s \n",string, data);
00198     fprintf(stderr, "parse: Error while parsing object: %d \n",numobjectsparsed);
00199     return PARSEBADSYNTAX;
00200   }
00201 
00202   return PARSENOERR;
00203 }
00204 
00205 unsigned int readmodel(char * modelfile, SceneHandle scene) {
00206   FILE * dfile;
00207   errcode rc;
00208  
00209   reset_tex_table(); 
00210   dfile=NULL;
00211 
00212   dfile=fopen(modelfile,"r");
00213   if (dfile==NULL) {
00214     return PARSEBADFILE;
00215   }
00216 
00217   rc = GetScenedefs(dfile, scene); 
00218   if (rc != PARSENOERR)
00219     return rc;
00220 
00221   scenebackcol.r = 0.0; /* default background is black */
00222   scenebackcol.g = 0.0;
00223   scenebackcol.b = 0.0;
00224 
00225   numobjectsparsed=0;
00226   while ((rc = GetObject(dfile, scene)) == PARSENOERR) {
00227     numobjectsparsed++;
00228   } 
00229   fclose(dfile);
00230 
00231   if (rc == PARSEEOF)
00232     rc = PARSENOERR;
00233 
00234   rt_background(scene, scenebackcol);
00235 
00236   return rc;
00237 }
00238 
00239 
00240 static errcode GetScenedefs(FILE * dfile, SceneHandle scene) {
00241   vector Ccenter, Cview, Cup;
00242   apiflt zoom, aspectratio;
00243   int raydepth, antialiasing;
00244   char outfilename[200];
00245   int xres, yres, verbose;
00246   float a,b,c;
00247   errcode rc = PARSENOERR;
00248 
00249   rc |= GetString(dfile, "BEGIN_SCENE"); 
00250 
00251   rc |= GetString(dfile, "OUTFILE");
00252   fscanf(dfile, "%s", outfilename); 
00253 #ifdef _WIN32
00254   if (strcmp (outfilename, "/dev/null") == 0) {
00255     strcpy (outfilename, "NUL:");
00256   }
00257 #endif
00258  
00259   rc |= GetString(dfile, "RESOLUTION");
00260   fscanf(dfile, "%d %d", &xres, &yres);
00261 
00262   rc |= GetString(dfile, "VERBOSE");
00263   fscanf(dfile, "%d", &verbose);
00264 
00265   rt_scenesetup(scene, outfilename, xres, yres, verbose);
00266 
00267   rc |= GetString(dfile, "CAMERA");
00268 
00269   rc |= GetString(dfile, "ZOOM");
00270   fscanf(dfile, "%f", &a);
00271   zoom=a;
00272 
00273   rc |= GetString(dfile, "ASPECTRATIO");
00274   fscanf(dfile, "%f", &b);  
00275   aspectratio=b;
00276 
00277   rc |= GetString(dfile, "ANTIALIASING");
00278   fscanf(dfile, "%d", &antialiasing);
00279 
00280   rc |= GetString(dfile, "RAYDEPTH");
00281   fscanf(dfile, "%d", &raydepth);
00282 
00283   rc |= GetString(dfile, "CENTER");
00284   fscanf(dfile,"%f %f %f", &a, &b, &c);
00285   Ccenter.x = a;
00286   Ccenter.y = b;
00287   Ccenter.z = c;
00288 
00289   rc |= GetString(dfile, "VIEWDIR");
00290   fscanf(dfile,"%f %f %f", &a, &b, &c);
00291   Cview.x = a;
00292   Cview.y = b;
00293   Cview.z = c;
00294 
00295   rc |= GetString(dfile, "UPDIR");
00296   fscanf(dfile,"%f %f %f", &a, &b, &c);
00297   Cup.x = a;
00298   Cup.y = b;
00299   Cup.z = c;
00300 
00301   rc |= GetString(dfile, "END_CAMERA");   
00302 
00303   rt_camerasetup(scene, zoom, aspectratio, antialiasing, raydepth,
00304               Ccenter, Cview, Cup);
00305 
00306 
00307   return rc;
00308 }
00309 
00310 static errcode GetObject(FILE * dfile, SceneHandle scene) {
00311   char objtype[80];
00312  
00313   fscanf(dfile, "%s", objtype);
00314   if (!stringcmp(objtype, "END_SCENE")) {
00315     return PARSEEOF; /* end parsing */
00316   }
00317   if (!stringcmp(objtype, "TEXDEF")) {
00318     return GetTexDef(dfile);
00319   } 
00320   if (!stringcmp(objtype, "TEXALIAS")) {
00321     return GetTexAlias(dfile);
00322   }
00323   if (!stringcmp(objtype, "BACKGROUND")) {
00324     return GetBackGnd(dfile);
00325   }
00326   if (!stringcmp(objtype, "CYLINDER")) {
00327     return GetCylinder(dfile);
00328   }
00329   if (!stringcmp(objtype, "FCYLINDER")) {
00330     return GetFCylinder(dfile);
00331   }
00332   if (!stringcmp(objtype, "POLYCYLINDER")) {
00333     return GetPolyCylinder(dfile);
00334   }
00335   if (!stringcmp(objtype, "SPHERE")) {
00336     return GetSphere(dfile);
00337   }
00338   if (!stringcmp(objtype, "PLANE")) {
00339     return GetPlane(dfile);
00340   }
00341   if (!stringcmp(objtype, "RING")) {
00342     return GetRing(dfile);
00343   }
00344   if (!stringcmp(objtype, "BOX")) {
00345     return GetBox(dfile);
00346   }
00347   if (!stringcmp(objtype, "SCALARVOL")) {
00348     return GetVol(dfile);
00349   }
00350   if (!stringcmp(objtype, "TRI")) {
00351     return GetTri(dfile);
00352   }
00353   if (!stringcmp(objtype, "STRI")) {
00354     return GetSTri(dfile);
00355   }
00356   if (!stringcmp(objtype, "LIGHT")) {
00357     return GetLight(dfile);
00358   }
00359   if (!stringcmp(objtype, "SCAPE")) {
00360     return GetLandScape(dfile);
00361   }
00362   if (!stringcmp(objtype, "TPOLYFILE")) {
00363     return GetTPolyFile(dfile);
00364   }
00365 
00366   fprintf(stderr, "Found bad token: %s expected an object type\n", objtype);
00367   return PARSEBADSYNTAX;
00368 }
00369 
00370 static errcode GetVector(FILE * dfile, vector * v1) {
00371   float a, b, c;
00372   
00373   fscanf(dfile, "%f %f %f", &a, &b, &c); 
00374   v1->x=a;
00375   v1->y=b;
00376   v1->z=c;
00377 
00378   return PARSENOERR;
00379 }
00380 
00381 static errcode GetColor(FILE * dfile, color * c1) {
00382   float r, g, b;
00383   int rc; 
00384 
00385   rc = GetString(dfile, "COLOR"); 
00386   fscanf(dfile, "%f %f %f", &r, &g, &b);
00387   c1->r=r;
00388   c1->g=g;
00389   c1->b=b;
00390 
00391   return rc;
00392 }
00393 
00394 static errcode GetTexDef(FILE * dfile) {
00395   char texname[TEXNAMELEN];
00396 
00397   fscanf(dfile, "%s", texname);
00398   add_texture(GetTexBody(dfile), texname); 
00399 
00400   return PARSENOERR;
00401 }
00402 
00403 static errcode GetTexAlias(FILE * dfile) {
00404   char texname[TEXNAMELEN];
00405   char aliasname[TEXNAMELEN];
00406 
00407   fscanf(dfile, "%s", texname);
00408   fscanf(dfile, "%s", aliasname);
00409   add_texture(find_texture(aliasname), texname); 
00410 
00411   return PARSENOERR;
00412 }
00413 
00414 
00415 static errcode GetTexture(FILE * dfile, void ** tex) {
00416   char tmp[255];
00417   errcode rc = PARSENOERR;
00418 
00419   fscanf(dfile, "%s", tmp);
00420   if (!stringcmp("TEXTURE", tmp)) { 
00421     *tex = GetTexBody(dfile);
00422   }
00423   else
00424     *tex = find_texture(tmp);
00425 
00426   return rc;
00427 }
00428 
00429 void * GetTexBody(FILE * dfile) {
00430   char tmp[255];
00431   float a,b,c,d, phong, phongexp, phongtype;
00432   apitexture tex;
00433   void * voidtex; 
00434   errcode rc;
00435 
00436   rc = GetString(dfile, "AMBIENT");
00437   fscanf(dfile, "%f", &a); 
00438   tex.ambient=a;
00439 
00440   rc |= GetString(dfile, "DIFFUSE");
00441   fscanf(dfile, "%f", &b);
00442   tex.diffuse=b;
00443 
00444   rc |= GetString(dfile, "SPECULAR");
00445   fscanf(dfile, "%f", &c);
00446   tex.specular=c;
00447 
00448   rc |= GetString(dfile, "OPACITY");
00449   fscanf(dfile, "%f", &d);  
00450   tex.opacity=d;
00451 
00452   fscanf(dfile, "%s", tmp);
00453   if (!stringcmp("PHONG", tmp)) {
00454     fscanf(dfile, "%s", tmp);
00455     if (!stringcmp("METAL", tmp)) {
00456       phongtype = RT_PHONG_METAL;
00457     }
00458     else if (!stringcmp("PLASTIC", tmp)) {
00459       phongtype = RT_PHONG_PLASTIC;
00460     }
00461     else {
00462       phongtype = RT_PHONG_PLASTIC;
00463     } 
00464 
00465     fscanf(dfile, "%f", &phong);
00466     GetString(dfile, "PHONG_SIZE");
00467     fscanf(dfile, "%f", &phongexp);
00468     fscanf(dfile, "%s", tmp);
00469   }     
00470   else { 
00471     phong = 0.0;
00472     phongexp = 100.0;
00473     phongtype = RT_PHONG_PLASTIC;
00474   }
00475   
00476   fscanf(dfile, "%f %f %f", &a, &b, &c);
00477   tex.col.r = a;
00478   tex.col.g = b;
00479   tex.col.b = c;
00480  
00481   rc |= GetString(dfile, "TEXFUNC");
00482   fscanf(dfile, "%d", &tex.texturefunc);
00483   if (tex.texturefunc >= 7) {    /* if its an image map, we need a filename */
00484     fscanf(dfile, "%s", tex.imap);
00485   }
00486   if (tex.texturefunc != 0) {
00487     rc |= GetString(dfile, "CENTER");
00488     rc |= GetVector(dfile, &tex.ctr);
00489     rc |= GetString(dfile, "ROTATE");
00490     rc |= GetVector(dfile, &tex.rot);
00491     rc |= GetString(dfile, "SCALE");
00492     rc |= GetVector(dfile, &tex.scale);
00493   }
00494   if (tex.texturefunc == 9) {
00495     rc |= GetString(dfile, "UAXIS");
00496     rc |= GetVector(dfile, &tex.uaxs);
00497     rc |= GetString(dfile, "VAXIS");
00498     rc |= GetVector(dfile, &tex.vaxs);
00499   }
00500 
00501   voidtex = rt_texture(&tex);
00502   rt_tex_phong(voidtex, phong, phongexp, (int) phongtype);
00503 
00504   return voidtex;
00505 }
00506 
00507 static errcode GetLight(FILE * dfile) {
00508   apiflt rad;
00509   vector ctr;
00510   apitexture tex;
00511   float a; 
00512   errcode rc;
00513 
00514   memset(&tex, 0, sizeof(apitexture)); 
00515 
00516   rc = GetString(dfile,"CENTER"); 
00517   rc |= GetVector(dfile, &ctr); 
00518   rc |= GetString(dfile,"RAD");
00519   fscanf(dfile,"%f",&a);  /* read in radius */ 
00520   rad=a;
00521 
00522   rc |= GetColor(dfile, &tex.col);
00523   
00524   rt_light(rt_texture(&tex), ctr, rad);
00525 
00526   return rc;
00527 }
00528 
00529 static errcode GetBackGnd(FILE * dfile) {
00530   float r,g,b;
00531   
00532   fscanf(dfile, "%f %f %f", &r, &g, &b);
00533 
00534   scenebackcol.r=r;
00535   scenebackcol.g=g;
00536   scenebackcol.b=b;
00537 
00538   return PARSENOERR;
00539 }
00540 
00541 static errcode GetCylinder(FILE * dfile) {
00542   apiflt rad;
00543   vector ctr, axis;
00544   void * tex;
00545   float a;
00546   errcode rc;
00547 
00548   rc = GetString(dfile, "CENTER");
00549   rc |= GetVector(dfile, &ctr);
00550   rc |= GetString(dfile, "AXIS");
00551   rc |= GetVector(dfile, &axis);
00552   rc |= GetString(dfile, "RAD");
00553   fscanf(dfile, "%f", &a);
00554   rad=a;
00555 
00556   rc |= GetTexture(dfile, &tex);
00557   rt_cylinder(tex, ctr, axis, rad); 
00558 
00559   return rc;
00560 }
00561 
00562 static errcode GetFCylinder(FILE * dfile) {
00563   apiflt rad;
00564   vector ctr, axis;
00565   vector pnt1, pnt2;
00566   void * tex;
00567   float a;
00568   errcode rc;
00569 
00570   rc = GetString(dfile, "BASE");
00571   rc |= GetVector(dfile, &pnt1);
00572   rc |= GetString(dfile, "APEX");
00573   rc |= GetVector(dfile, &pnt2);
00574 
00575   ctr=pnt1;
00576   axis.x=pnt2.x - pnt1.x; 
00577   axis.y=pnt2.y - pnt1.y;
00578   axis.z=pnt2.z - pnt1.z;
00579 
00580   rc |= GetString(dfile, "RAD");
00581   fscanf(dfile, "%f", &a);
00582   rad=a;
00583 
00584   rc |= GetTexture(dfile, &tex);
00585   rt_fcylinder(tex, ctr, axis, rad); 
00586 
00587   return rc;
00588 }
00589  
00590 static errcode GetPolyCylinder(FILE * dfile) {
00591   apiflt rad;
00592   vector * temp;
00593   void * tex;
00594   float a;
00595   int numpts, i;
00596   errcode rc;
00597 
00598   rc = GetString(dfile, "POINTS");
00599   fscanf(dfile, "%d", &numpts);
00600 
00601   temp = (vector *) malloc(numpts * sizeof(vector));
00602 
00603   for (i=0; i<numpts; i++) {
00604     rc |= GetVector(dfile, &temp[i]);
00605   }         
00606 
00607   rc |= GetString(dfile, "RAD");
00608   fscanf(dfile, "%f", &a);
00609   rad=a;
00610 
00611   rc |= GetTexture(dfile, &tex);
00612   rt_polycylinder(tex, temp, numpts, rad); 
00613 
00614   free(temp);
00615 
00616   return rc;
00617 }
00618  
00619 
00620 static errcode GetSphere(FILE * dfile) {
00621   apiflt rad;
00622   vector ctr;
00623   void * tex;
00624   float a;
00625   errcode rc;
00626  
00627   rc = GetString(dfile,"CENTER");
00628   rc |= GetVector(dfile, &ctr); 
00629   rc |= GetString(dfile, "RAD");
00630   fscanf(dfile,"%f",&a); 
00631   rad=a;
00632 
00633   rc |= GetTexture(dfile, &tex); 
00634  
00635   rt_sphere(tex, ctr, rad);
00636 
00637   return rc;
00638 }
00639 
00640 static errcode GetPlane(FILE * dfile) {
00641   vector normal;
00642   vector ctr;
00643   void * tex;
00644   errcode rc;
00645 
00646   rc = GetString(dfile, "CENTER");
00647   rc |= GetVector(dfile, &ctr);
00648   rc |= GetString(dfile, "NORMAL");
00649   rc |= GetVector(dfile, &normal);
00650   rc |= GetTexture(dfile, &tex);
00651 
00652   rt_plane(tex, ctr, normal);
00653 
00654   return rc;
00655 }
00656 
00657 static errcode GetVol(FILE * dfile) {
00658   vector min, max;
00659   int x,y,z;  
00660   char fname[255];
00661   void * tex;
00662   errcode rc;
00663  
00664   rc = GetString(dfile, "MIN");
00665   rc |= GetVector(dfile, &min);
00666   rc |= GetString(dfile, "MAX");
00667   rc |= GetVector(dfile, &max);
00668   rc |= GetString(dfile, "DIM");
00669   fscanf(dfile, "%d %d %d ", &x, &y, &z);
00670   rc |= GetString(dfile, "FILE");
00671   fscanf(dfile, "%s", fname);  
00672   rc |= GetTexture(dfile, &tex);
00673  
00674   rt_scalarvol(tex, min, max, x, y, z, fname, NULL); 
00675 
00676   return rc;
00677 }
00678 
00679 static errcode GetBox(FILE * dfile) {
00680   vector min, max;
00681   void * tex;
00682   errcode rc;
00683 
00684   rc = GetString(dfile, "MIN");
00685   rc |= GetVector(dfile, &min);
00686   rc |= GetString(dfile, "MAX");
00687   rc |= GetVector(dfile, &max);
00688   rc |= GetTexture(dfile, &tex);
00689 
00690   rt_box(tex, min, max);
00691 
00692   return rc;
00693 }
00694 
00695 static errcode GetRing(FILE * dfile) {
00696   vector normal;
00697   vector ctr;
00698   void * tex;
00699   float a,b;
00700   errcode rc;
00701  
00702   rc = GetString(dfile, "CENTER");
00703   rc |= GetVector(dfile, &ctr);
00704   rc |= GetString(dfile, "NORMAL");
00705   rc |= GetVector(dfile, &normal);
00706   rc |= GetString(dfile, "INNER");
00707   fscanf(dfile, " %f ", &a);
00708   rc |= GetString(dfile, "OUTER");
00709   fscanf(dfile, " %f ", &b);
00710   rc |= GetTexture(dfile, &tex);
00711  
00712   rt_ring(tex, ctr, normal, a, b);
00713 
00714   return rc;
00715 }
00716 
00717 static errcode GetTri(FILE * dfile) {
00718   vector v0,v1,v2;
00719   void * tex;
00720   errcode rc;
00721 
00722   rc = GetString(dfile, "V0");
00723   rc |= GetVector(dfile, &v0);
00724 
00725   rc |= GetString(dfile, "V1");
00726   rc |= GetVector(dfile, &v1);
00727 
00728   rc |= GetString(dfile, "V2");
00729   rc |= GetVector(dfile, &v2);
00730 
00731   rc |= GetTexture(dfile, &tex);
00732 
00733   rt_tri(tex, v0, v1, v2);
00734 
00735   return rc;
00736 }
00737 
00738 static errcode GetSTri(FILE * dfile) {
00739   vector v0,v1,v2,n0,n1,n2;
00740   void * tex;
00741   errcode rc;
00742 
00743   rc = GetString(dfile, "V0");
00744   rc |= GetVector(dfile, &v0);
00745 
00746   rc |= GetString(dfile, "V1");
00747   rc |= GetVector(dfile, &v1);
00748 
00749   rc |= GetString(dfile, "V2");
00750   rc |= GetVector(dfile, &v2);
00751   
00752   rc |= GetString(dfile, "N0");
00753   rc |= GetVector(dfile, &n0);
00754 
00755   rc |= GetString(dfile, "N1");
00756   rc |= GetVector(dfile, &n1);
00757 
00758   rc |= GetString(dfile, "N2");
00759   rc |= GetVector(dfile, &n2);
00760 
00761   rc |= GetTexture(dfile, &tex);
00762   
00763   rt_stri(tex, v0, v1, v2, n0, n1, n2);
00764 
00765   return rc;
00766 }
00767 
00768 static errcode GetLandScape(FILE * dfile) {
00769   void * tex;
00770   vector ctr;
00771   apiflt wx, wy;
00772   int m, n;
00773   float a,b;
00774   errcode rc;
00775 
00776   rc = GetString(dfile, "RES");
00777   fscanf(dfile, "%d %d", &m, &n);
00778 
00779   rc |= GetString(dfile, "SCALE");
00780   fscanf(dfile, "%f %f", &a, &b);   
00781   wx=a;
00782   wy=b;
00783 
00784   rc |= GetString(dfile, "CENTER");
00785   rc |= GetVector(dfile, &ctr);
00786 
00787   rc |= GetTexture(dfile, &tex);
00788 
00789   rt_landscape(tex, m, n, ctr, wx, wy);
00790 
00791   return rc;
00792 }
00793 
00794 static errcode GetTPolyFile(FILE * dfile) {
00795   void * tex;
00796   vector ctr, rot, scale;
00797   vector v1, v2, v0;
00798   char ifname[255];
00799   FILE *ifp;
00800   int v, totalpolys;
00801   RotMat RotA;
00802   errcode rc;
00803 
00804   totalpolys=0;
00805 
00806   rc = GetString(dfile, "SCALE"); 
00807   rc |= GetVector(dfile, &scale);
00808 
00809   rc |= GetString(dfile, "ROT");
00810   rc |= GetVector(dfile, &rot);
00811 
00812   degvectoradvec(&rot); 
00813   InitRot3d(&RotA, rot.x, rot.y, rot.z);
00814 
00815   rc |= GetString(dfile, "CENTER");
00816   rc |= GetVector(dfile, &ctr);
00817 
00818   rc |= GetString(dfile, "FILE");
00819   fscanf(dfile, "%s", ifname);
00820 
00821   rc |= GetTexture(dfile, &tex);
00822 
00823   if ((ifp=fopen(ifname, "r")) == NULL) {
00824     fprintf(stderr, "Can't open data file %s for input!! Aborting...\n", ifname);
00825     return PARSEBADSUBFILE;
00826   }
00827 
00828   while (!feof(ifp)) {
00829     fscanf(ifp, "%d", &v);
00830     if (v != 3) { break; }
00831 
00832     totalpolys++;
00833     v=0; 
00834      
00835     rc |= GetVector(ifp, &v0);
00836     rc |= GetVector(ifp, &v1);
00837     rc |= GetVector(ifp, &v2);
00838 
00839     Scale3d(&scale, &v0);
00840     Scale3d(&scale, &v1);
00841     Scale3d(&scale, &v2);
00842 
00843     Rotate3d(&RotA, &v0); 
00844     Rotate3d(&RotA, &v1); 
00845     Rotate3d(&RotA, &v2); 
00846 
00847     Trans3d(&ctr, &v0);
00848     Trans3d(&ctr, &v1);
00849     Trans3d(&ctr, &v2);
00850 
00851     rt_tri(tex, v1, v0, v2);
00852   }
00853 
00854   fclose(ifp);
00855 
00856   return rc;
00857 }

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