1 // obj.cxx -- routines to handle "sorta" WaveFront .obj format files.
3 // Written by Curtis Olson, started October 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 // (Log is kept at end of this file)
38 #if defined ( __sun__ )
39 extern "C" void *memmove(void *, const void *, size_t);
40 extern "C" void *memset(void *, int, size_t);
43 #include <string> // Standard C++ library
46 #ifdef NEEDNAMESPACESTD
50 #include <Debug/fg_debug.h>
51 #include <Include/fg_constants.h>
52 #include <Include/fg_zlib.h>
53 #include <Main/options.hxx>
54 #include <Math/mat3.h>
55 #include <Math/fg_random.h>
56 #include <Math/polar3d.hxx>
57 #include <Scenery/tile.hxx>
59 #include "material.hxx"
63 static double normals[MAX_NODES][3];
66 // given three points defining a triangle, calculate the normal
67 static void calc_normal(double p1[3], double p2[3],
68 double p3[3], double normal[3])
73 v1[0] = p2[0] - p1[0]; v1[1] = p2[1] - p1[1]; v1[2] = p2[2] - p1[2];
74 v2[0] = p3[0] - p1[0]; v2[1] = p3[1] - p1[1]; v2[2] = p3[2] - p1[2];
76 MAT3cross_product(normal, v1, v2);
77 MAT3_NORMALIZE_VEC(normal,temp);
79 // fgPrintf( FG_TERRAIN, FG_DEBUG, " Normal = %.2f %.2f %.2f\n",
80 // normal[0], normal[1], normal[2]);
84 #define FG_TEX_CONSTANT 128.0
87 // Calculate texture coordinates for a given point.
88 fgPoint3d calc_tex_coords(double *node, fgPoint3d *ref) {
92 cp.x = node[0] + ref->x;
93 cp.y = node[1] + ref->y;
94 cp.z = node[2] + ref->z;
96 pp = fgCartToPolar3d(cp);
98 pp.lon = fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.lon, 25.0);
99 pp.lat = fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.lat, 25.0);
105 // Load a .obj file and build the GL fragment list
106 int fgObjLoad(char *path, fgTILE *t) {
109 char fgpath[256], line[256], material[256];
110 double approx_normal[3], normal[3], scale;
111 // double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
112 // GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
116 int in_fragment, in_faces, vncount, n1, n2, n3, n4;
117 int last1, last2, odd;
121 // First try "path.gz" (compressed format)
122 strcpy(fgpath, path);
123 strcat(fgpath, ".gz");
124 if ( (f = fgopen(fgpath, "rb")) == NULL ) {
125 // Next try "path" (uncompressed format)
126 strcpy(fgpath, path);
127 if ( (f = fgopen(fgpath, "rb")) == NULL ) {
128 // Next try "path.obj" (uncompressed format)
129 strcat(fgpath, ".gz");
130 if ( (f = fgopen(fgpath, "rb")) == NULL ) {
131 strcpy(fgpath, path);
132 fgPrintf( FG_TERRAIN, FG_ALERT,
133 "Cannot open file: %s\n", fgpath );
139 shading = current_options.get_shading();
144 t->bounding_radius = 0.0;
148 while ( fggets(f, line, 250) != NULL ) {
149 if ( line[0] == '#' ) {
151 } else if ( line[0] == '\n' ) {
152 // empty line -- ignore
153 } else if ( strncmp(line, "gbs ", 4) == 0 ) {
154 // reference point (center offset)
155 sscanf(line, "gbs %lf %lf %lf %lf\n",
156 &t->center.x, &t->center.y, &t->center.z,
157 &t->bounding_radius);
158 } else if ( strncmp(line, "bs ", 3) == 0 ) {
159 // reference point (center offset)
160 sscanf(line, "bs %lf %lf %lf %lf\n",
161 &fragment.center.x, &fragment.center.y, &fragment.center.z,
162 &fragment.bounding_radius);
163 } else if ( strncmp(line, "v ", 2) == 0 ) {
165 if ( t->ncount < MAX_NODES ) {
166 // fgPrintf( FG_TERRAIN, FG_DEBUG, "vertex = %s", line);
167 sscanf(line, "v %lf %lf %lf\n",
168 &(t->nodes[t->ncount][0]), &(t->nodes[t->ncount][1]),
169 &(t->nodes[t->ncount][2]));
174 fgPrintf( FG_TERRAIN, FG_EXIT,
175 "Read too many nodes ... dying :-(\n");
177 } else if ( strncmp(line, "vn ", 3) == 0 ) {
179 if ( vncount < MAX_NODES ) {
180 // fgPrintf( FG_TERRAIN, FG_DEBUG, "vertex normal = %s", line);
181 sscanf(line, "vn %lf %lf %lf\n",
182 &normals[vncount][0], &normals[vncount][1],
183 &normals[vncount][2]);
186 fgPrintf( FG_TERRAIN, FG_EXIT,
187 "Read too many vertex normals ... dying :-(\n");
189 } else if ( strncmp(line, "usemtl ", 7) == 0 ) {
190 // material property specification
192 // this also signals the start of a new fragment
194 // close out the previous structure and start the next
199 fragment.display_list = display_list;
201 // push this fragment onto the tile's object list
202 t->fragment_list.push_back(fragment);
207 display_list = xglGenLists(1);
208 xglNewList(display_list, GL_COMPILE);
211 // reset the existing face list
212 // printf("cleaning a fragment with %d faces\n",
213 // fragment.faces.size());
214 while ( fragment.faces.size() ) {
215 // printf("emptying face list\n");
216 fragment.faces.pop_front();
219 // scan the material line
220 sscanf(line, "usemtl %s\n", material);
222 // give the fragment a pointer back to the tile
223 fragment.tile_ptr = t;
225 // find this material in the properties list
226 map < string, fgMATERIAL, less<string> > :: iterator myfind =
227 material_mgr.material_map.find(material);
228 if ( myfind == material_mgr.material_map.end() ) {
229 fgPrintf( FG_TERRAIN, FG_ALERT,
230 "Ack! unknown usemtl name = %s in %s\n",
233 fragment.material_ptr = &((*myfind).second);
236 // initialize the fragment transformation matrix
238 for ( i = 0; i < 16; i++ ) {
239 fragment.matrix[i] = 0.0;
241 fragment.matrix[0] = fragment.matrix[5] =
242 fragment.matrix[10] = fragment.matrix[15] = 1.0;
245 // initialize fragment face counter
246 fragment.num_faces = 0;
248 } else if ( line[0] == 't' ) {
249 // start a new triangle strip
251 n1 = n2 = n3 = n4 = 0;
253 // fgPrintf( FG_TERRAIN, FG_DEBUG, " new tri strip = %s", line);
254 sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
256 fragment.add_face(n1, n2, n3);
258 // fgPrintf( FG_TERRAIN, FG_DEBUG, "(t) = ");
260 xglBegin(GL_TRIANGLE_STRIP);
266 // Shading model is "GL_SMOOTH" so use precalculated
267 // (averaged) normals
268 MAT3_SCALE_VEC(normal, normals[n1], scale);
269 xglNormal3dv(normal);
270 pp = calc_tex_coords(nodes[n1], center);
271 xglTexCoord2f(pp.lon, pp.lat);
272 // xglVertex3d(t->nodes[n1][0],t->nodes[n1][1],t->nodes[n1][2]);
273 xglVertex3dv(nodes[n1]);
275 MAT3_SCALE_VEC(normal, normals[n2], scale);
276 xglNormal3dv(normal);
277 pp = calc_tex_coords(nodes[n2], center);
278 xglTexCoord2f(pp.lon, pp.lat);
279 //xglVertex3d(t->nodes[n2][0],t->nodes[n2][1],t->nodes[n2][2]);
280 xglVertex3dv(nodes[n2]);
282 MAT3_SCALE_VEC(normal, normals[n3], scale);
283 xglNormal3dv(normal);
284 pp = calc_tex_coords(nodes[n3], center);
285 xglTexCoord2f(pp.lon, pp.lat);
286 // xglVertex3d(t->nodes[n3][0],t->nodes[n3][1],t->nodes[n3][2]);
287 xglVertex3dv(nodes[n3]);
289 // Shading model is "GL_FLAT" so calculate per face
290 // normals on the fly.
292 calc_normal(nodes[n1], nodes[n2],
293 nodes[n3], approx_normal);
295 calc_normal(nodes[n2], nodes[n1],
296 nodes[n3], approx_normal);
298 MAT3_SCALE_VEC(normal, approx_normal, scale);
299 xglNormal3dv(normal);
301 pp = calc_tex_coords(nodes[n1], center);
302 xglTexCoord2f(pp.lon, pp.lat);
303 // xglVertex3d(t->nodes[n1][0],t->nodes[n1][1],t->nodes[n1][2]);
304 xglVertex3dv(nodes[n1]);
306 pp = calc_tex_coords(nodes[n2], center);
307 xglTexCoord2f(pp.lon, pp.lat);
308 // xglVertex3d(t->nodes[n2][0],t->nodes[n2][1],t->nodes[n2][2]);
309 xglVertex3dv(nodes[n2]);
311 pp = calc_tex_coords(nodes[n3], center);
312 xglTexCoord2f(pp.lon, pp.lat);
313 // xglVertex3d(t->nodes[n3][0],t->nodes[n3][1],t->nodes[n3][2]);
314 xglVertex3dv(nodes[n3]);
322 fragment.add_face(n3, n2, n4);
325 // Shading model is "GL_SMOOTH"
326 MAT3_SCALE_VEC(normal, normals[n4], scale);
328 // Shading model is "GL_FLAT"
329 calc_normal(nodes[n3], nodes[n2], nodes[n4],
331 MAT3_SCALE_VEC(normal, approx_normal, scale);
333 xglNormal3dv(normal);
334 pp = calc_tex_coords(nodes[n4], center);
335 xglTexCoord2f(pp.lon, pp.lat);
336 // xglVertex3d(t->nodes[n4][0],t->nodes[n4][1],t->nodes[n4][2]);
337 xglVertex3dv(nodes[n4]);
343 } else if ( line[0] == 'f' ) {
347 xglBegin(GL_TRIANGLES);
351 // fgPrintf( FG_TERRAIN, FG_DEBUG, "new triangle = %s", line);*/
352 sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
354 fragment.add_face(n1, n2, n3);
356 // xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
357 xglNormal3dv(normals[n1]);
358 pp = calc_tex_coords(nodes[n1], center);
359 xglTexCoord2f(pp.lon, pp.lat);
360 // xglVertex3d(t->nodes[n1][0], t->nodes[n1][1], t->nodes[n1][2]);
361 xglVertex3dv(nodes[n1]);
363 // xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
364 xglNormal3dv(normals[n2]);
365 pp = calc_tex_coords(nodes[n2], center);
366 xglTexCoord2f(pp.lon, pp.lat);
367 // xglVertex3d(t->nodes[n2][0], t->nodes[n2][1], t->nodes[n2][2]);
368 xglVertex3dv(nodes[n2]);
370 // xglNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
371 xglNormal3dv(normals[n3]);
372 pp = calc_tex_coords(nodes[n3], center);
373 xglTexCoord2f(pp.lon, pp.lat);
374 // xglVertex3d(t->nodes[n3][0], t->nodes[n3][1], t->nodes[n3][2]);
375 xglVertex3dv(nodes[n3]);
376 } else if ( line[0] == 'q' ) {
377 // continue a triangle strip
380 // fgPrintf( FG_TERRAIN, FG_DEBUG, "continued tri strip = %s ",
382 sscanf(line, "q %d %d\n", &n1, &n2);
383 // fgPrintf( FG_TERRAIN, FG_DEBUG, "read %d %d\n", n1, n2);
386 fragment.add_face(last1, last2, n1);
388 fragment.add_face(last2, last1, n1);
392 // Shading model is "GL_SMOOTH"
393 MAT3_SCALE_VEC(normal, normals[n1], scale);
394 xglNormal3dv(normal);
396 // Shading model is "GL_FLAT"
398 calc_normal(nodes[last1], nodes[last2], nodes[n1],
401 calc_normal(nodes[last2], nodes[last1], nodes[n1],
404 MAT3_SCALE_VEC(normal, approx_normal, scale);
405 xglNormal3dv(normal);
408 pp = calc_tex_coords(nodes[n1], center);
409 xglTexCoord2f(pp.lon, pp.lat);
410 // xglVertex3d(t->nodes[n1][0], t->nodes[n1][1], t->nodes[n1][2]);
411 xglVertex3dv(nodes[n1]);
418 // fgPrintf( FG_TERRAIN, FG_DEBUG, " (cont)\n");
421 fragment.add_face(last1, last2, n2);
423 fragment.add_face(last2, last1, n2);
427 // Shading model is "GL_SMOOTH"
428 MAT3_SCALE_VEC(normal, normals[n2], scale);
429 xglNormal3dv(normal);
431 // Shading model is "GL_FLAT"
433 calc_normal(nodes[last1], nodes[last2],
434 nodes[n2], approx_normal);
436 calc_normal(nodes[last2], nodes[last1],
437 nodes[n2], approx_normal);
439 MAT3_SCALE_VEC(normal, approx_normal, scale);
440 xglNormal3dv(normal);
443 pp = calc_tex_coords(nodes[n2], center);
444 xglTexCoord2f(pp.lon, pp.lat);
445 // xglVertex3d(t->nodes[n2][0],t->nodes[n2][1],t->nodes[n2][2]);
446 xglVertex3dv(nodes[n2]);
453 fgPrintf( FG_TERRAIN, FG_WARN, "Unknown line in %s = %s\n",
459 // close out the previous structure and start the next
464 fragment.display_list = display_list;
466 // push this fragment onto the tile's object list
467 t->fragment_list.push_back(fragment);
470 // Draw normal vectors (for visually verifying normals)
473 xglColor3f(0.0, 0.0, 0.0);
474 for ( i = 0; i < t->ncount; i++ ) {
475 xglVertex3d(t->nodes[i][0],
478 xglVertex3d(t->nodes[i][0] + 500*normals[i][0],
479 t->nodes[i][1] + 500*normals[i][1],
480 t->nodes[i][2] + 500*normals[i][2]);
492 // Revision 1.1 1998/08/25 16:51:25 curt
493 // Moved from ../Scenery
495 // Revision 1.23 1998/08/20 15:16:43 curt
496 // obj.cxx: use more explicit parenthases.
497 // texload.[ch]: use const in function definitions where appropriate.
499 // Revision 1.22 1998/08/20 15:12:03 curt
500 // Used a forward declaration of classes fgTILE and fgMATERIAL to eliminate
501 // the need for "void" pointers and casts.
502 // Quick hack to count the number of scenery polygons that are being drawn.
504 // Revision 1.21 1998/08/12 21:13:04 curt
505 // material.cxx: don't load textures if they are disabled
506 // obj.cxx: optimizations from Norman Vine
507 // tile.cxx: minor tweaks
508 // tile.hxx: addition of num_faces
509 // tilemgr.cxx: minor tweaks
511 // Revision 1.20 1998/07/24 21:42:07 curt
512 // material.cxx: whups, double method declaration with no definition.
513 // obj.cxx: tweaks to avoid errors in SGI's CC.
514 // tile.cxx: optimizations by Norman Vine.
515 // tilemgr.cxx: optimizations by Norman Vine.
517 // Revision 1.19 1998/07/13 21:01:58 curt
518 // Wrote access functions for current fgOPTIONS.
520 // Revision 1.18 1998/07/12 03:18:27 curt
521 // Added ground collision detection. This involved:
522 // - saving the entire vertex list for each tile with the tile records.
523 // - saving the face list for each fragment with the fragment records.
524 // - code to intersect the current vertical line with the proper face in
525 // an efficient manner as possible.
526 // Fixed a bug where the tiles weren't being shifted to "near" (0,0,0)
528 // Revision 1.17 1998/07/08 14:47:21 curt
529 // Fix GL_MODULATE vs. GL_DECAL problem introduced by splash screen.
530 // polare3d.h renamed to polar3d.hxx
531 // fg{Cartesian,Polar}Point3d consolodated.
532 // Added some initial support for calculating local current ground elevation.
534 // Revision 1.16 1998/07/06 21:34:33 curt
535 // Added using namespace std for compilers that support this.
537 // Revision 1.15 1998/07/04 00:54:28 curt
538 // Added automatic mipmap generation.
540 // When rendering fragments, use saved model view matrix from associated tile
541 // rather than recalculating it with push() translate() pop().
543 // Revision 1.14 1998/06/17 21:36:40 curt
544 // Load and manage multiple textures defined in the Materials library.
545 // Boost max material fagments for each material property to 800.
546 // Multiple texture support when rendering.
548 // Revision 1.13 1998/06/12 00:58:05 curt
549 // Build only static libraries.
550 // Declare memmove/memset for Sloaris.
552 // Revision 1.12 1998/06/08 17:57:54 curt
553 // Working first pass at material proporty sorting.
555 // Revision 1.11 1998/06/06 01:09:31 curt
556 // I goofed on the log message in the last commit ... now fixed.
558 // Revision 1.10 1998/06/06 01:07:17 curt
559 // Increased per material fragment list size from 100 to 400.
560 // Now correctly draw viewable fragments in per material order.
562 // Revision 1.9 1998/06/05 22:39:54 curt
563 // Working on sorting by, and rendering by material properties.
565 // Revision 1.8 1998/06/05 18:19:18 curt
566 // Recognize file, file.gz, and file.obj as scenery object files.
568 // Revision 1.7 1998/05/24 02:49:09 curt
569 // Implimented fragment level view frustum culling.
571 // Revision 1.6 1998/05/23 14:09:20 curt
572 // Added tile.cxx and tile.hxx.
573 // Working on rewriting the tile management system so a tile is just a list
574 // fragments, and the fragment record contains the display list for that
577 // Revision 1.5 1998/05/20 20:53:53 curt
578 // Moved global ref point and radius (bounding sphere info, and offset) to
579 // data file rather than calculating it on the fly.
580 // Fixed polygon winding problem in scenery generation stage rather than
581 // compensating for it on the fly.
582 // Made a fgTILECACHE class.
584 // Revision 1.4 1998/05/16 13:09:57 curt
585 // Beginning to add support for view frustum culling.
586 // Added some temporary code to calculate bouding radius, until the
587 // scenery generation tools and scenery can be updated.
589 // Revision 1.3 1998/05/03 00:48:01 curt
590 // Updated texture coordinate fmod() parameter.
592 // Revision 1.2 1998/05/02 01:52:14 curt
593 // Playing around with texture coordinates.
595 // Revision 1.1 1998/04/30 12:35:28 curt
596 // Added a command line rendering option specify smooth/flat shading.
598 // Revision 1.35 1998/04/28 21:43:26 curt
599 // Wrapped zlib calls up so we can conditionally comment out zlib support.
601 // Revision 1.34 1998/04/28 01:21:42 curt
602 // Tweaked texture parameter calculations to keep the number smaller. This
603 // avoids the "swimming" problem.
604 // Type-ified fgTIME and fgVIEW.
606 // Revision 1.33 1998/04/27 15:58:15 curt
607 // Screwing around with texture coordinate generation ... still needs work.
609 // Revision 1.32 1998/04/27 03:30:13 curt
610 // Minor transformation adjustments to try to keep scenery tiles closer to
611 // (0, 0, 0) GLfloats run out of precision at the distances we need to model
612 // the earth, but we can do a bunch of pre-transformations using double math
613 // and then cast to GLfloat once everything is close in where we have less
614 // precision problems.
616 // Revision 1.31 1998/04/25 15:09:57 curt
617 // Changed "r" to "rb" in gzopen() options. This fixes bad behavior in win32.
619 // Revision 1.30 1998/04/24 14:21:08 curt
620 // Added "file.obj.gz" support.
622 // Revision 1.29 1998/04/24 00:51:07 curt
623 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
624 // Tweaked the scenery file extentions to be "file.obj" (uncompressed)
625 // or "file.obz" (compressed.)
627 // Revision 1.28 1998/04/22 13:22:44 curt
628 // C++ - ifing the code a bit.
630 // Revision 1.27 1998/04/18 04:13:17 curt
631 // Added zlib on the fly decompression support for loading scenery objects.
633 // Revision 1.26 1998/04/03 22:11:36 curt
634 // Converting to Gnu autoconf system.
636 // Revision 1.25 1998/03/14 00:30:50 curt
637 // Beginning initial terrain texturing experiments.
639 // Revision 1.24 1998/02/09 21:30:18 curt
640 // Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
642 // Revision 1.23 1998/02/09 15:07:52 curt
645 // Revision 1.22 1998/02/01 03:39:54 curt
648 // Revision 1.21 1998/01/31 00:43:25 curt
649 // Added MetroWorks patches from Carmen Volpe.
651 // Revision 1.20 1998/01/29 00:51:39 curt
652 // First pass at tile cache, dynamic tile loading and tile unloading now works.
654 // Revision 1.19 1998/01/27 03:26:42 curt
655 // Playing with new fgPrintf command.
657 // Revision 1.18 1998/01/19 19:27:16 curt
658 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
659 // This should simplify things tremendously.
661 // Revision 1.17 1998/01/13 00:23:10 curt
662 // Initial changes to support loading and management of scenery tiles. Note,
663 // there's still a fair amount of work left to be done.
665 // Revision 1.16 1997/12/30 23:09:40 curt
666 // Worked on winding problem without luck, so back to calling glFrontFace()
667 // 3 times for each scenery area.
669 // Revision 1.15 1997/12/30 20:47:51 curt
670 // Integrated new event manager with subsystem initializations.
672 // Revision 1.14 1997/12/30 01:38:46 curt
673 // Switched back to per vertex normals and smooth shading for terrain.
675 // Revision 1.13 1997/12/18 23:32:36 curt
676 // First stab at sky dome actually starting to look reasonable. :-)
678 // Revision 1.12 1997/12/17 23:13:47 curt
679 // Began working on rendering the sky.
681 // Revision 1.11 1997/12/15 23:55:01 curt
682 // Add xgl wrappers for debugging.
683 // Generate terrain normals on the fly.
685 // Revision 1.10 1997/12/12 21:41:28 curt
686 // More light/material property tweaking ... still a ways off.
688 // Revision 1.9 1997/12/12 19:52:57 curt
689 // Working on lightling and material properties.
691 // Revision 1.8 1997/12/10 01:19:51 curt
692 // Tweaks for verion 0.15 release.
694 // Revision 1.7 1997/12/08 22:51:17 curt
695 // Enhanced to handle ccw and cw tri-stripe winding. This is a temporary
696 // admission of defeat. I will eventually go back and get all the stripes
697 // wound the same way (ccw).
699 // Revision 1.6 1997/11/25 19:25:35 curt
700 // Changes to integrate Durk's moon/sun code updates + clean up.
702 // Revision 1.5 1997/11/15 18:16:39 curt
705 // Revision 1.4 1997/11/14 00:26:49 curt
706 // Transform scenery coordinates earlier in pipeline when scenery is being
707 // created, not when it is being loaded. Precalculate normals for each node
708 // as average of the normals of each containing polygon so Garoude shading is
711 // Revision 1.3 1997/10/31 04:49:12 curt
712 // Tweaking vertex orders.
714 // Revision 1.2 1997/10/30 12:38:45 curt
715 // Working on new scenery subsystem.
717 // Revision 1.1 1997/10/28 21:14:54 curt