1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
42 #include "OSGConfig.h"
54 #include "OSGGeometry.h"
55 #include "OSGTypedGeoVectorProperty.h"
56 #include "OSGTypedGeoIntegralProperty.h"
57 #include "OSGGeoFunctions.h"
58 #include "OSGSimpleMaterial.h"
59 #include "OSGSceneFileHandler.h"
61 #include "OSGOFFSceneFileType.h"
66 /*! \class OSG::OFFSceneFileType
67 \ingroup GrpSystemFileIO
71 #if defined(OSG_WIN32_ICL) && !defined(OSG_CHECK_FIELDSETARG)
72 #pragma warning (disable : 383)
75 /*****************************
77 *****************************/
78 // Static Class Varible implementations:
79 const Char8
*OFFSceneFileType:: _suffixA
[] = { "off" };
81 OFFSceneFileType
OFFSceneFileType:: _the(_suffixA
,
85 SceneFileType::OSG_READ_SUPPORTED
);
87 /*****************************
89 *****************************/
91 /********************************
93 *******************************/
95 /*******************************
97 *******************************/
99 //----------------------------
100 // Function name: read
101 //----------------------------
104 //p: Scene &image, const char *fileName
114 //d: read the image from the given file
118 //------------------------------
119 NodeTransitPtr
OFFSceneFileType::read( std::istream
&is
,
123 typedef std::vector
<int> Face
;
125 std::vector
<Face
> faceVec
;
129 NodeTransitPtr
root(NULL
);
130 GeometryUnrecPtr geo
= NULL
;
137 GeoPnt3fPropertyUnrecPtr points
= NULL
;
138 GeoUInt32PropertyUnrecPtr index
= NULL
;
139 GeoUInt32PropertyUnrecPtr lens
= NULL
;
140 GeoUInt8PropertyUnrecPtr type
= NULL
;
141 GeoVec3fPropertyUnrecPtr norms
= NULL
;
142 GeoColor4fPropertyUnrecPtr colors
= NULL
;
143 GeoVec2fPropertyUnrecPtr texcoords
= NULL
;
144 SimpleMaterialUnrecPtr mat
= NULL
;
147 Int32 i
, j
, k
, n
, vN
, fN
, pType
;
148 Int32 triCount
= 0, vertexCount
, faceCount
, edgeCount
;
150 bool hasNormals
= false;
151 bool hasColors
= false;
152 bool hasTexCoords
= false;
153 bool has4DimPoints
= false;
160 is
>> head
>> vertexCount
>> faceCount
>> edgeCount
;
162 FDEBUG(("OFF Head/vertexCount/faceCount: %s/%d/%d\n", head
,
163 vertexCount
, faceCount
));
165 if(!vertexCount
|| !faceCount
)
170 if(strstr(head
, "ST"))
175 if(strstr(head
, "C"))
180 if(strstr(head
, "N"))
185 if(strstr(head
, "4"))
187 has4DimPoints
= true;
190 if(strstr(head
, "nOFF"))
198 FWARNING(("OFFSceneFileType::read: nOFF with ndim != 3 "
199 "not supported.\n"));
207 FWARNING(("OFFSceneFileType::read: 4D points not supported.\n"));
212 //-------------------------------------------------------------------
213 // create the OSG objects
214 root
= Node ::create();
215 geo
= Geometry ::create();
216 points
= GeoPnt3fProperty ::create();
217 index
= GeoUInt32Property::create();
218 lens
= GeoUInt32Property::create();
219 type
= GeoUInt8Property ::create();
220 mat
= SimpleMaterial ::create();
223 norms
= GeoVec3fProperty::create();
225 colors
= GeoColor4fProperty::create();
227 texcoords
= GeoVec2fProperty::create();
229 mat
->setDiffuse(Color3f(0.42f
, 0.42f
, 0.52f
));
230 mat
->setSpecular(Color3f(1, 1, 1));
231 mat
->setShininess(20);
235 geo
->setProperty(points
, Geometry::PositionsIndex
);
236 geo
->setIndex (index
, Geometry::PositionsIndex
);
238 geo
->setLengths(lens
);
239 geo
->setTypes (type
);
241 geo
->setMaterial(mat
);
245 geo
->setProperty(norms
, Geometry::NormalsIndex
);
246 geo
->setIndex (index
, Geometry::NormalsIndex
);
250 geo
->setProperty(colors
, Geometry::ColorsIndex
);
251 geo
->setIndex (index
, Geometry::ColorsIndex
);
255 geo
->setProperty(texcoords
, Geometry::TexCoordsIndex
);
256 geo
->setIndex (index
, Geometry::TexCoordsIndex
);
259 //-------------------------------------------------------------------
260 // read/set the points
262 for(i
= 0; (!is
.eof()) && (i
< vertexCount
); i
++)
264 SceneFileHandler::the()->updateReadProgress();
268 point
.setValues(Real32(x
), Real32(y
), Real32(z
));
269 points
->push_back(point
);
274 norm
.setValues(Real32(x
), Real32(y
), Real32(z
));
275 norms
->push_back(norm
);
280 is
>> x
>> y
>> z
>> a
;
281 color
.setValuesRGBA(Real32(x
),
285 colors
->editField().push_back(color
);
291 texcoord
.setValues(Real32(x
), Real32(y
));
292 texcoords
->push_back(texcoord
);
296 //-------------------------------------------------------------------
298 // TODO; should we 'reserve' some index mem (3,4,..) ?
299 faceVec
.resize(faceCount
);
302 for(i
= 0; (!is
.eof()) && (i
< faceCount
); i
++)
308 for(j
= 0; (!is
.eof()) && (j
< n
); j
++)
311 if((k
>= 0) && (k
< vertexCount
))
312 faceVec
[i
].push_back(k
);
315 FFATAL(("Invalid vertex index %d in face %d\n", k
, i
));
318 is
.ignore(1000, '\n');
322 FFATAL(("Invalid face vec num %d\n", n
));
326 //-------------------------------------------------------------------
328 for(i
= 3; i
<= 5; i
++)
332 for(j
= 0; j
< faceCount
; j
++)
334 fN
= UInt32(faceVec
[j
].size());
341 vN
= UInt32(faceVec
[j
].size());
344 for(k
= 0; k
< vN
; ++k
)
346 index
->editFieldPtr()->push_back(faceVec
[j
][k
]);
352 type
->push_back(GL_POLYGON
);
362 pType
= GL_TRIANGLES
;
375 type
->push_back(pType
);
380 FNOTICE(("Number of triangle read: %d\n", triCount
));
382 createSharedIndex(geo
);
385 calcVertexNormals(geo
);
387 SceneFileHandler::the()->updateReadProgress(100);
394 /******************************
396 ******************************/
398 /******************************
400 ******************************/
402 /***************************
404 ***************************/
406 /***************************
408 ***************************/
410 /**constructors & destructors**/
412 //----------------------------
413 // Function name: OFFSceneFileType
414 //----------------------------
417 //p: const char *suffixArray[], UInit16 suffixByteCount
427 //d: Default Constructor
431 //------------------------------
432 OFFSceneFileType::OFFSceneFileType(const Char8
*suffixArray
[],
433 UInt16 suffixByteCount
,
435 UInt32 overridePriority
,
437 SceneFileType(suffixArray
,
445 //----------------------------
446 // Function name: OFFSceneFileType
447 //----------------------------
450 //p: const OFFSceneFileType &obj
460 //d: Copy Constructor
464 //------------------------------
466 OFFSceneFileType::OFFSceneFileType(const OFFSceneFileType
&obj
) :
471 //----------------------------
472 // Function name: ~OFFSceneFileType
473 //----------------------------
490 //------------------------------
492 OFFSceneFileType
&OFFSceneFileType::the(void)
497 OFFSceneFileType::~OFFSceneFileType(void)
502 const Char8
*OFFSceneFileType::getName(void) const
504 return "Geomview Geometry";