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: ssgLoadTRI.cxx 1568 2002-09-02 06:05:49Z sjbaker $
22 */
23
24 //
25 // TRI ( AC3D triangle file ) import for SSG/PLIB
26 // Written by Dave McClurg (dpm@efn.org) in March-2000
27 //
28
29 #include "ssgLocal.h"
30
31 #define MAX_TRI 100000
32
33 struct triData
34 {
35 sgVec3 v[3] ;
36 int color ;
37 } ;
38
39 /******************************************************************************/
40
ssgLoadTRI(const char * fname,const ssgLoaderOptions * options)41 ssgEntity *ssgLoadTRI ( const char *fname, const ssgLoaderOptions* options )
42
43 /******************************************************************************/
44
45 /*
46 Purpose:
47
48 reads an AC3D triangle file.
49
50 Example:
51
52 Each line contains 9 floating point values and a 1 hex value for color.
53 the 9 floating point values represent 3 vertices of a triangle
54 the color format is 0xRRGGBB (eg 0xffffff is white)
55
56 0.0 0.0 0.0 1.0 0.0 0.0 1.0 1.0 0.0 0xffffff
57
58 */
59 {
60 ssgSetCurrentOptions ( (ssgLoaderOptions*)options ) ;
61 const ssgLoaderOptions* current_options = ssgGetCurrentOptions () ;
62
63 //open the file
64 char filename [ 1024 ] ;
65 current_options -> makeModelPath ( filename, fname ) ;
66
67 FILE *loader_fd = fopen ( filename, "ra" ) ;
68
69 if ( loader_fd == NULL )
70 {
71 ulSetError ( UL_WARNING, "ssgLoadTRI: Failed to open '%s' for reading", filename ) ;
72 return NULL ;
73 }
74
75 //read the data
76 triData* tri = new triData [ MAX_TRI ] ;
77 int num_tri = 0 ;
78
79 char buffer [ 1024 ] ;
80
81 while ( fgets ( buffer, 1024, loader_fd ) != NULL )
82 {
83 float coord [9] ;
84 int color ;
85
86 if ( sscanf ( buffer, "%e %e %e %e %e %e %e %e %e %d",
87 &coord[0], &coord[1], &coord[2],
88 &coord[3], &coord[4], &coord[5],
89 &coord[6], &coord[7], &coord[8],
90 &color ) != 10 )
91 {
92 ulSetError ( UL_WARNING, "ssgLoadTRI: Can't parse triangle: %s", buffer ) ;
93 }
94 else if ( num_tri < MAX_TRI )
95 {
96 float* cp = coord;
97 for ( int i=0; i<3; i++ )
98 for ( int j=0; j<3; j++ )
99 tri[ num_tri ].v[ i ][ j ] = *cp++;
100
101 tri[ num_tri ].color = color ;
102 num_tri ++ ;
103 }
104 else
105 {
106 break;
107 }
108 }
109
110 fclose ( loader_fd ) ;
111
112 ssgBranch* current_branch = NULL ;
113
114 if ( num_tri )
115 {
116 ssgVertexArray* vlist = new ssgVertexArray ( num_tri * 3 ) ;
117 for ( int i=0; i<num_tri; i++ )
118 for ( int j=0; j<3; j++ )
119 vlist -> add ( tri[ i ].v[ j ] ) ;
120 ssgVtxTable *vtab = new ssgVtxTable ( GL_TRIANGLES, vlist, 0, 0, 0 );
121 current_branch = new ssgTransform () ;
122 current_branch -> addKid ( vtab ) ;
123 }
124
125 delete[] tri ;
126
127 return current_branch ;
128 }
129