Tachyon (current)  Current Main Branch
nffparse.c
Go to the documentation of this file.
1 /*
2  * nffparse.c - code for parsing NFF model files
3  *
4  * (C) Copyright 1994-2022 John E. Stone
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * $Id: nffparse.c,v 1.14 2022/02/18 18:18:36 johns Exp $
8  *
9  */
10 
11 /*
12  * See the NFF.DOC file in the SPD Distribution by Eric Haines
13  * for more information on NFF.
14  */
15 
16 #include <stdio.h>
17 #include <math.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <ctype.h> /* needed for toupper(), macro.. */
21 
22 #include "tachyon.h" /* ray tracer api */
23 #include "nffparse.h" /* nff protos */
24 
25 static void * curtexture;
27 
28 /* protos */
29 int NFFGetObject(FILE * dfile, SceneHandle scene);
30 
31 
32 int NFFstringcmp(char * a, char * b) {
33  int i, s, l;
34 
35  s=strlen(a);
36  l=strlen(b);
37 
38  if (s != l)
39  return 1;
40 
41  for (i=0; i<s; i++) {
42  if (toupper(a[i]) != toupper(b[i])) {
43  return 1;
44  }
45  }
46  return 0;
47 }
48 
49 
50 void NFFGetString(FILE * dfile, char * string) {
51  char data[100];
52 
53  fscanf(dfile,"%s",data);
54 
55  if (NFFstringcmp(data, string) != 0) {
56  printf("parse: Expected %s, got %s \n",string, data);
57  printf("Rendering terminated. \n");
58  exit(1);
59  }
60 }
61 
62 void NFFGetVector(FILE * dfile, apivector * v1) {
63  float a, b, c;
64 
65  fscanf(dfile, "%f %f %f", &a, &b, &c);
66  v1->x=a;
67  v1->y=b;
68  v1->z=c;
69 }
70 
71 unsigned int ParseNFF(char *nffname, SceneHandle scene) {
72  FILE * dfile;
73  backgr.r = 0.0;
74  backgr.g = 0.0;
75  backgr.b = 0.0;
76 
77  if (nffname == NULL) {
78  return NFFBADFILE;
79  }
80  else {
81  dfile=fopen(nffname,"r");
82  if (dfile==NULL) {
83  return NFFBADFILE;
84  }
85  }
86 
87  while (NFFGetObject(dfile, scene) == NFFNOERR);
88 
89  rt_background(scene, backgr);
90 
91  return NFFNOERR;
92 }
93 
94 int NFFGetScenedefs(FILE * dfile, SceneHandle scene) {
95  apivector Ccenter, Cview, Cup;
96  apiflt zoom, aspectratio;
97  int raydepth, antialiasing;
98  int xres, yres, verbose;
99  apivector lookat;
100  float vangle, hither;
101 
102  NFFGetString(dfile, "from");
103  NFFGetVector(dfile, &Ccenter);
104 
105  NFFGetString(dfile, "at");
106  NFFGetVector(dfile, &lookat);
107  Cview.x = lookat.x - Ccenter.x;
108  Cview.y = lookat.y - Ccenter.y;
109  Cview.z = lookat.z - Ccenter.z;
110 
111  NFFGetString(dfile, "up");
112  NFFGetVector(dfile, &Cup);
113 
114  NFFGetString(dfile, "angle");
115  fscanf(dfile, "%f", &vangle);
116  zoom = 1.0; /* XXX fix me later */
117  aspectratio = 1.0;
118 
119  NFFGetString(dfile, "hither");
120  fscanf(dfile, "%f", &hither);
121 
122  NFFGetString(dfile, "resolution");
123  fscanf(dfile, "%d %d", &xres, &yres);
124 
125  antialiasing = 0;
126  raydepth = 6;
127  verbose = 0;
128 
129  rt_outputfile(scene, "outfile.tga");
130  rt_resolution(scene, xres, yres);
131  rt_verbose(scene, verbose);
132 
133  rt_camera_setup(scene, zoom, aspectratio, antialiasing, raydepth,
134  Ccenter, Cview, Cup);
135 
136  rt_background(scene, backgr);
137 
138  return NFFNOERR;
139 }
140 
141 
142 int NFFGetBackground(FILE *dfile, SceneHandle scene) {
143  float r, g, b;
144  fscanf(dfile, "%f %f %f", &r, &g, &b);
145  backgr.r = r;
146  backgr.g = g;
147  backgr.b = b;
148 
149  return NFFNOERR;
150 }
151 
152 int NFFGetLight(FILE *dfile, SceneHandle scene) {
153  apiflt rad;
154  apivector ctr;
155  apitexture tex;
156  float r, g, b;
157 
158  NFFGetVector(dfile, &ctr);
159  rad = 1.0; /* XXX hack for now */
160 
161  r = g = b = 1.0;
162 
163  fscanf(dfile, "%f %f %f", &r, &g, &b);
164  tex.col.r = r;
165  tex.col.g = g;
166  tex.col.b = b;
167 
168  rt_light(scene, rt_texture(scene, &tex), ctr, rad);
169 
170  return NFFNOERR;
171 }
172 
173 int NFFGetTexture(FILE *dfile, SceneHandle scene) {
174  apitexture tex;
175  float r, g, b, Kd, Ks, Shine, T, IOR;
176 
177  fscanf(dfile, "%f %f %f %f %f %f %f %f",
178  &r, &g, &b, &Kd, &Ks, &Shine, &T, &IOR);
179  tex.col.r = r;
180  tex.col.g = g;
181  tex.col.b = b;
182  tex.ambient = 0.1;
183  tex.diffuse = Kd;
184  tex.specular = Ks;
185  tex.opacity = (T > 0.99) ? 0.0 : (1.0 - T);
186  tex.texturefunc = 0;
187 
188  curtexture = rt_texture(scene, &tex); /* XXX memory leak city, */
189  /* we aren't keeping track of these... */
190  return NFFNOERR;
191 }
192 
193 int NFFGetCylCone(FILE *dfile, SceneHandle scene) {
194  apivector pnt1, pnt2;
195  apivector ctr, axis;
196  float baserad, apexrad;
197 
198  NFFGetVector(dfile, &pnt1);
199  fscanf(dfile, "%f", &baserad);
200 
201  NFFGetVector(dfile, &pnt2);
202  fscanf(dfile, "%f", &apexrad);
203 
204  ctr=pnt1;
205  axis.x=pnt2.x - pnt1.x;
206  axis.y=pnt2.y - pnt1.y;
207  axis.z=pnt2.z - pnt1.z;
208 
209  /* XXX should really be cone */
210  rt_fcylinder(scene, curtexture, ctr, axis, baserad);
211 
212  return NFFNOERR;
213 }
214 
215 int NFFGetSphere(FILE *dfile, SceneHandle scene) {
216  apiflt rad;
217  apivector ctr;
218  float a;
219 
220  NFFGetVector(dfile, &ctr);
221  fscanf(dfile, "%f", &a);
222  rad = a;
223 
224  rt_sphere(scene, curtexture, ctr, rad);
225 
226  return NFFNOERR;
227 }
228 
229 int NFFGetPolygon(FILE *dfile, SceneHandle scene) {
230  int numverts, i;
231  apivector v0, vold, vnew;
232 
233  fscanf(dfile, "%d", &numverts);
234  NFFGetVector(dfile, &v0);
235  NFFGetVector(dfile, &vold);
236 
237  for (i=2; i<numverts; i++) {
238  NFFGetVector(dfile, &vnew);
239  rt_tri(scene, curtexture, v0, vold, vnew);
240  vold = vnew;
241  }
242 
243  return NFFNOERR;
244 }
245 
246 int NFFGetPatch(FILE *dfile, SceneHandle scene) {
247  int numverts, i;
248  apivector v0, n0;
249  apivector vold, nold;
250  apivector vnew, nnew;
251 
252  fscanf(dfile, "%d", &numverts);
253  NFFGetVector(dfile, &v0);
254  NFFGetVector(dfile, &n0);
255  NFFGetVector(dfile, &vold);
256  NFFGetVector(dfile, &nold);
257 
258  for (i=2; i<numverts; i++) {
259  NFFGetVector(dfile, &vnew);
260  NFFGetVector(dfile, &nnew);
261  rt_stri(scene, curtexture, v0, vold, vnew, n0, nold, nnew) ;
262  vold = vnew;
263  nold = nnew;
264  }
265 
266  return NFFNOERR;
267 }
268 
269 int NFFGetObject(FILE * dfile, SceneHandle scene) {
270  char objtype[80];
271  if (fscanf(dfile, "%s", objtype) != 1)
272  return NFFEOF; /* end parsing */
273 
274  if (!NFFstringcmp(objtype, "v")) {
275  return NFFGetScenedefs(dfile, scene);
276  }
277  if (!NFFstringcmp(objtype, "b")) {
278  return NFFGetBackground(dfile, scene);
279  }
280  if (!NFFstringcmp(objtype, "l")) {
281  return NFFGetLight(dfile, scene);
282  }
283  if (!NFFstringcmp(objtype, "f")) {
284  return NFFGetTexture(dfile, scene);
285  }
286  if (!NFFstringcmp(objtype, "s")) {
287  return NFFGetSphere(dfile, scene);
288  }
289  if (!NFFstringcmp(objtype, "c")) {
290  return NFFGetCylCone(dfile, scene);
291  }
292  if (!NFFstringcmp(objtype, "p")) {
293  return NFFGetPolygon(dfile, scene);
294  }
295  if (!NFFstringcmp(objtype, "pp")) {
296  return NFFGetPatch(dfile, scene);
297  }
298 
299  return NFFEOF;
300 }
301 
302 
float g
Green color component.
Definition: tachyon.h:61
void rt_camera_setup(SceneHandle voidscene, flt zoom, flt aspectratio, int antialiasing, int raydepth, apivector camcent, apivector viewvec, apivector upvec)
Define a camera for a perspective projection, given the specified zoom factor, aspect ratio...
Definition: api.c:229
float r
Red color component.
Definition: tachyon.h:60
void * rt_texture(SceneHandle sc, apitexture *apitex)
Translate a texture definition into the internal format used by Tachyon, and returns an opaque pointe...
Definition: api.c:933
apicolor col
base object color
Definition: tachyon.h:67
int NFFGetObject(FILE *dfile, SceneHandle scene)
Definition: nffparse.c:269
void rt_resolution(SceneHandle voidscene, int hres, int vres)
Set the horizontal and vertical resolution (in pixels) for the specified scene.
Definition: api.c:372
flt diffuse
diffuse reflection
Definition: tachyon.h:70
flt opacity
how opaque the object is
Definition: tachyon.h:72
flt specular
specular reflection
Definition: tachyon.h:71
void NFFGetVector(FILE *dfile, apivector *v1)
Definition: nffparse.c:62
void rt_sphere(SceneHandle scene, void *tex, apivector ctr, flt rad)
Define a sphere with associated texture, center, and radius.
Definition: api.c:1212
int NFFGetCylCone(FILE *dfile, SceneHandle scene)
Definition: nffparse.c:193
int NFFGetScenedefs(FILE *dfile, SceneHandle scene)
Definition: nffparse.c:94
void rt_tri(SceneHandle voidscene, void *tex, apivector v0, apivector v1, apivector v2)
Define a flat-shaded triangle.
Definition: api.c:1224
flt x
X coordinate or direction component.
Definition: tachyon.h:54
flt apiflt
for backward compatibility
Definition: tachyon.h:49
flt y
Y coordinate or direction component.
Definition: tachyon.h:55
void rt_fcylinder(SceneHandle scene, void *tex, apivector ctr, apivector axis, flt rad)
Define a finite-length cylinder.
Definition: api.c:1173
void rt_outputfile(SceneHandle voidscene, const char *outname)
Set the filename for the output image for the specified scene.
Definition: api.c:350
int NFFGetLight(FILE *dfile, SceneHandle scene)
Definition: nffparse.c:152
int NFFGetPolygon(FILE *dfile, SceneHandle scene)
Definition: nffparse.c:229
int NFFGetSphere(FILE *dfile, SceneHandle scene)
Definition: nffparse.c:215
#define NFFEOF
Definition: nffparse.h:14
void * SceneHandle
Definition: tachyon.h:51
static apicolor backgr
Definition: nffparse.c:26
void rt_stri(SceneHandle voidscene, void *tex, apivector v0, apivector v1, apivector v2, apivector n0, apivector n1, apivector n2)
Define a smooth-shaded triangle using interpolated vertex normals.
Definition: api.c:1252
int texturefunc
which texture function to use
Definition: tachyon.h:66
flt ambient
ambient lighting
Definition: tachyon.h:69
void rt_background(SceneHandle voidscene, apicolor col)
Set the background color of the specified scene.
Definition: api.c:490
int NFFGetPatch(FILE *dfile, SceneHandle scene)
Definition: nffparse.c:246
float b
Blue color component.
Definition: tachyon.h:62
int NFFGetTexture(FILE *dfile, SceneHandle scene)
Definition: nffparse.c:173
#define NFFNOERR
Definition: nffparse.h:11
unsigned int ParseNFF(char *nffname, SceneHandle scene)
Definition: nffparse.c:71
void NFFGetString(FILE *dfile, char *string)
Definition: nffparse.c:50
#define NFFBADFILE
Definition: nffparse.h:12
Tachyon public API function prototypes and declarations used to drive the ray tracing engine...
int NFFstringcmp(char *a, char *b)
Definition: nffparse.c:32
void * rt_light(SceneHandle voidscene, void *tex, apivector ctr, flt rad)
Define a point light with associated texture, position, and radius.
Definition: api.c:1049
flt z
Z coordinate or direction component.
Definition: tachyon.h:56
static void * curtexture
Definition: nffparse.c:25
void rt_verbose(SceneHandle voidscene, int v)
Enables or Disables verbose messages from the Tachyon library during rendering.
Definition: api.c:414
int NFFGetBackground(FILE *dfile, SceneHandle scene)
Definition: nffparse.c:142