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: ssgLoadStrip.cxx 1568 2002-09-02 06:05:49Z sjbaker $
22 */
23 
24 #include "ssgLocal.h"
25 
26 static ssgLoaderOptions* current_options;
27 
ssgLoadStrip(const char * fname,const ssgLoaderOptions * options)28 ssgEntity* ssgLoadStrip( const char* fname, const ssgLoaderOptions* options ) {
29   ssgSetCurrentOptions ( (ssgLoaderOptions*)options ) ;
30   current_options = ssgGetCurrentOptions () ;
31 
32   char filename [ 1024 ] ;
33   current_options -> makeModelPath ( filename, fname ) ;
34 
35   FILE* model_file = fopen(filename, "r");
36 
37   if (model_file == NULL) {
38     ulSetError(UL_WARNING, "ssgLoadStrip: Couldn't open file '%s'.", filename);
39     return NULL;
40   }
41 
42   ssgSimpleState* state = new ssgSimpleState();
43   state->setOpaque();
44   state->disable(GL_BLEND);
45   state->disable(GL_ALPHA_TEST);
46   state->disable(GL_TEXTURE_2D);
47   state->enable(GL_COLOR_MATERIAL);
48   state->enable(GL_LIGHTING);
49   state->setShadeModel(GL_SMOOTH);
50   state->setMaterial(GL_AMBIENT , 0.7f, 0.7f, 0.0f, 1.0f);
51   state->setMaterial(GL_DIFFUSE , 0.7f, 0.7f, 0.0f, 1.0f);
52   state->setMaterial(GL_SPECULAR, 1.0f, 1.0f, 1.0f, 1.0f);
53   state->setMaterial(GL_EMISSION, 0.0f, 0.0f, 0.0f, 1.0f);
54   state->setShininess(50);
55 
56   int i;
57   int num_vertices = ulEndianReadLittle32(model_file);
58   ssgBranch* model = new ssgBranch();
59   ssgVertexArray* vertices = new ssgVertexArray();
60   ssgNormalArray* normals  = new ssgNormalArray();
61 
62   for (i = 0; i < num_vertices; i++) {
63     sgVec3 new_vec;
64     int j;
65 
66     for (j = 0; j < 3; j++) {
67       new_vec[j] = ulEndianReadLittleFloat(model_file);
68     }
69     vertices->add(new_vec);
70 
71     for (j = 0; j < 3; j++) {
72       new_vec[j] = ulEndianReadLittleFloat(model_file);
73     }
74     normals->add(new_vec);
75   }
76 
77   int num_strips = ulEndianReadLittle32(model_file);
78   for (i = 0; i < num_strips; i++) {
79     int num_indices = ulEndianReadLittle32(model_file);
80 
81     ssgIndexArray* strip_indices = new ssgIndexArray(num_indices);
82 
83     for (int j = 0; j < num_indices; j++) {
84       strip_indices->add( ulEndianReadLittle16(model_file) );
85     }
86 
87     ssgVtxArray* varr = new ssgVtxArray( GL_TRIANGLE_STRIP,
88 					 vertices,
89 					 normals,
90 					 NULL,
91 					 NULL,
92 					 strip_indices );
93     varr->setState( state );
94     varr->setCullFace( TRUE );
95 
96     ssgLeaf* leaf = current_options->createLeaf(varr, NULL);
97     model->addKid(leaf);
98   }
99 
100   int num_indices = ulEndianReadLittle32(model_file);
101   ssgIndexArray* indices = new ssgIndexArray(num_indices);
102   for (i = 0; i < num_indices; i++) {
103     indices->add( ulEndianReadLittle16(model_file) );
104   }
105 
106   ssgVtxArray* varr = new ssgVtxArray( GL_TRIANGLES,
107 				       vertices,
108 				       normals,
109 				       NULL,
110 				       NULL,
111 				       indices );
112   varr->setState( state );
113   varr->setCullFace( TRUE );
114 
115   ssgLeaf* leaf = current_options->createLeaf(varr, NULL);
116   model->addKid(leaf);
117 
118   return model;
119 }
120