1 /*
2      PLIB - A Suite of Portable Game Libraries
3      Copyright (C) 1998,2002  Steve Baker
4 
5      This library is free software; you can redistribute it and/or
6      modify it under the terms of the GNU Library General Public
7      License as published by the Free Software Foundation; either
8      version 2 of the License, or (at your option) any later version.
9 
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Library General Public License for more details.
14 
15      You should have received a copy of the GNU Library General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 
19      For further information visit http://plib.sourceforge.net
20 
21      $Id: ssgSaveOFF.cxx 1568 2002-09-02 06:05:49Z sjbaker $
22 */
23 /*
24  .off writer for SSG/PLIB
25  Warning: There are two formats called OFF, see comment in ssgLoadOFF
26 
27  Written by Wolfram Kuss (Wolfram.Kuss@t-online.de) in February 2001
28 
29  We only support 2D and 3D data
30 */
31 
32 #include <stdio.h>
33 #include "ssgLocal.h"
34 #include "ssgLoaderWriterStuff.h"
35 
36 
37 
ssgSaveOFF(const char * fname,ssgEntity * ent)38 int ssgSaveOFF( const char* fname, ssgEntity *ent ) {
39   FILE *fd = fopen ( fname, "w" ) ;
40 	int i;
41 
42   if ( fd == NULL ) {
43     ulSetError ( UL_WARNING, "ssgSaveOFF: Failed to open '%s' for writing",
44 		 fname );
45     return FALSE ;
46   }
47 
48   ssgVertexArray* vertices = new ssgVertexArray();
49   ssgIndexArray*  indices  = new ssgIndexArray ();
50 
51   fprintf(fd, "# Model output by ssgSaveOFF. Original graph structure was:\n");
52   ent->print(fd, "#", 0);
53 
54   sgMat4 ident;
55   sgMakeIdentMat4( ident );
56   ssgAccumVerticesAndFaces( ent, ident, vertices, indices, -1 );
57 
58 	fprintf(fd, "nOFF\n3\n"); // 3 dimensions
59 	fprintf(fd, "%d %d 0\n", vertices->getNum(), indices->getNum()/3 );
60 
61   for (i = 0; i < vertices->getNum(); i++) {
62     fprintf(fd, "%f %f %f\n", vertices->get(i)[0],
63 															vertices->get(i)[1],
64 															vertices->get(i)[2]);
65   }
66 
67   for (i = 0; i < indices->getNum(); i += 3) {
68     fprintf(fd, "3 %d %d %d\n", *indices->get(i    ) ,
69 															  *indices->get(i + 1) ,
70 															  *indices->get(i + 2) );
71   }
72 
73   fclose( fd ) ;
74 
75   delete vertices;
76   delete indices ;
77 
78   return TRUE;
79 }
80