00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <stdlib.h>
00042 #include <stdio.h>
00043
00044 #include "OSGConfig.h"
00045
00046 #include <OSGGL.h>
00047
00048 #include <iostream>
00049 #include <fstream>
00050
00051 #include <vector>
00052
00053 #include <OSGLog.h>
00054
00055 #include <OSGNode.h>
00056 #include <OSGGeometry.h>
00057 #include <OSGGeoProperty.h>
00058 #include <OSGGeoFunctions.h>
00059 #include <OSGSimpleMaterial.h>
00060 #include <OSGSceneFileHandler.h>
00061
00062 #include "OSGOFFSceneFileType.h"
00063
00064 OSG_USING_NAMESPACE
00065
00066
00072 #if defined(OSG_WIN32_ICL) && !defined(OSG_CHECK_FIELDSETARG)
00073 #pragma warning (disable : 383)
00074 #endif
00075
00076
00077
00078
00079
00080 const Char8 *OFFSceneFileType:: _suffixA[] = { "off" };
00081
00082 OFFSceneFileType OFFSceneFileType:: _the(_suffixA,
00083 sizeof(_suffixA),
00084 false,
00085 10,
00086 SceneFileType::OSG_READ_SUPPORTED);
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 NodePtr OFFSceneFileType::read(std::istream &is, const Char8 *) const
00121 {
00122 typedef std::vector<int> Face;
00123
00124 std::vector<Face> faceVec;
00125
00126 char head[256];
00127 NodePtr root;
00128 GeometryPtr geo;
00129 Pnt3f point;
00130 Vec3f norm;
00131 Color4f color;
00132 Vec2f texcoord;
00133 GeoPositions3fPtr points;
00134 GeoIndicesUI32Ptr index;
00135 GeoPLengthsPtr lens;
00136 GeoPTypesPtr type;
00137 GeoNormals3fPtr norms;
00138 GeoColors4fPtr colors;
00139 GeoTexCoords2fPtr texcoords;
00140 SimpleMaterialPtr mat;
00141 Int32 i, j, k, n, vN, fN, pType;
00142 Int32 triCount = 0, vertexCount, faceCount, edgeCount;
00143 Real32 x, y, z, a;
00144 bool hasNormals = false, hasColors = false, hasTexCoords = false,
00145 has4DimPoints = false;
00146
00147 if(!is)
00148 {
00149 return NullFC;
00150 }
00151
00152 is >> head >> vertexCount >> faceCount >> edgeCount;
00153
00154 FDEBUG(("OFF Head/vertexCount/faceCount: %s/%d/%d\n", head,
00155 vertexCount, faceCount));
00156
00157 if(!vertexCount || !faceCount)
00158 {
00159 return NullFC;
00160 }
00161
00162 if(strstr(head, "ST"))
00163 {
00164 hasTexCoords = true;
00165 }
00166
00167 if(strstr(head, "C"))
00168 {
00169 hasColors = true;
00170 }
00171
00172 if(strstr(head, "N"))
00173 {
00174 hasNormals = true;
00175 }
00176
00177 if(strstr(head, "4"))
00178 {
00179 has4DimPoints = true;
00180 }
00181
00182 if(strstr(head, "nOFF"))
00183 {
00184 int ndim;
00185
00186 is >> ndim;
00187
00188 if(ndim != 3)
00189 {
00190 FWARNING(("OFFSceneFileType::read: nOFF with ndim != 3 not supported.\n"));
00191 return NullFC;
00192 }
00193 }
00194
00195 if(has4DimPoints)
00196 {
00197 FWARNING(("OFFSceneFileType::read: 4D points not supported.\n"));
00198 return NullFC;
00199 }
00200
00201
00202
00203
00204 root = Node::create();
00205 geo = Geometry::create();
00206 points = GeoPositions3f::create();
00207 index = GeoIndicesUI32::create();
00208 lens = GeoPLengthsUI32::create();
00209 type = GeoPTypesUI8::create();
00210 mat = SimpleMaterial::create();
00211 if (hasNormals)
00212 norms = GeoNormals3f::create();
00213 if (hasColors)
00214 colors = GeoColors4f::create();
00215 if (hasTexCoords)
00216 texcoords = GeoTexCoords2f::create();
00217
00218 beginEditCP(mat);
00219 {
00220 mat->setDiffuse(Color3f(0.42, 0.42, 0.52));
00221 mat->setSpecular(Color3f(1, 1, 1));
00222 mat->setShininess(20);
00223 }
00224 endEditCP(mat);
00225
00226 beginEditCP(root, Node::CoreFieldMask);
00227 {
00228 root->setCore(geo);
00229 }
00230 endEditCP(root, Node::CoreFieldMask);
00231
00232 beginEditCP(geo);
00233 {
00234 geo->setPositions(points);
00235 geo->setIndices(index);
00236 geo->setLengths(lens);
00237 geo->setTypes(type);
00238 geo->setMaterial(mat);
00239 if (hasNormals)
00240 geo->setNormals(norms);
00241 if (hasColors)
00242 geo->setColors(colors);
00243 if (hasTexCoords)
00244 geo->setTexCoords(texcoords);
00245
00246 }
00247 endEditCP(geo);
00248
00249
00250
00251 beginEditCP(points);
00252 if(hasNormals)
00253 beginEditCP(norms);
00254 if(hasColors)
00255 beginEditCP(colors);
00256 if(hasTexCoords)
00257 beginEditCP(texcoords);
00258
00259 {
00260 for(i = 0; (!is.eof()) && (i < vertexCount); i++)
00261 {
00262 SceneFileHandler::the().updateReadProgress();
00263 is >> x >> y >> z;
00264 point.setValues(Real32(x), Real32(y), Real32(z));
00265 points->push_back(point);
00266
00267 if(hasNormals)
00268 {
00269 is >> x >> y >> z;
00270 norm.setValues(Real32(x), Real32(y), Real32(z));
00271 norms->push_back(norm);
00272 }
00273
00274 if(hasColors)
00275 {
00276 is >> x >> y >> z >> a;
00277 color.setValuesRGBA(Real32(x),
00278 Real32(y),
00279 Real32(z),
00280 Real32(a));
00281 colors->editField().push_back(color);
00282 }
00283
00284 if(hasTexCoords)
00285 {
00286 is >> x >> y;
00287 texcoord.setValues(Real32(x), Real32(y));
00288 texcoords->push_back(texcoord);
00289 }
00290 }
00291 }
00292 endEditCP(points);
00293 if(hasNormals)
00294 endEditCP(norms);
00295 if(hasColors)
00296 endEditCP(colors);
00297 if(hasTexCoords)
00298 endEditCP(texcoords);
00299
00300
00301
00302
00303 faceVec.resize(faceCount);
00304 triCount = 0;
00305 for(i = 0; (!is.eof()) && (i < faceCount); i++)
00306 {
00307 is >> n;
00308 if(n >= 0)
00309 {
00310 triCount += n - 2;
00311 for(j = 0; (!is.eof()) && (j < n); j++)
00312 {
00313 is >> k;
00314 if((k >= 0) && (k < vertexCount))
00315 faceVec[i].push_back(k);
00316 else
00317 {
00318 FFATAL(("Invalid vertex index %d in face %d\n", k, i
00319 ));
00320 }
00321 }
00322 is.ignore(1000, '\n');
00323 }
00324 else
00325 {
00326 FFATAL(("Invalid face vec num %d\n", n));
00327 }
00328 }
00329
00330
00331
00332 for(i = 3; i <= 5; i++)
00333 {
00334 n = 0;
00335 for(j = 0; j < faceCount; j++)
00336 {
00337 fN = faceVec[j].size();
00338 if(fN >= 5)
00339 fN = 5;
00340 if(fN == i)
00341 {
00342 n += vN = faceVec[j].size();
00343 for(k = vN - 1; k >= 0; k--)
00344 {
00345 index->editFieldPtr()->push_back(faceVec[j][k]);
00346 }
00347
00348 if(i == 5)
00349 {
00350 beginEditCP(lens);
00351 {
00352 lens->push_back(n);
00353 }
00354 endEditCP(lens);
00355
00356 beginEditCP(type, FieldBits::AllFields);
00357 {
00358 type->push_back(GL_POLYGON);
00359 }
00360 endEditCP(type, FieldBits::AllFields);
00361 }
00362 }
00363 }
00364
00365 if(n)
00366 {
00367 switch(i)
00368 {
00369 case 3:
00370 pType = GL_TRIANGLES;
00371 break;
00372 case 4:
00373 pType = GL_QUADS;
00374 break;
00375 default:
00376 pType = 0;
00377 break;
00378 }
00379
00380 if(pType)
00381 {
00382 beginEditCP(lens);
00383 {
00384 lens->push_back(n);
00385 }
00386 endEditCP(lens);
00387
00388 beginEditCP(type, FieldBits::AllFields);
00389 {
00390 type->push_back(pType);
00391 }
00392 endEditCP(type, FieldBits::AllFields);
00393 }
00394 }
00395 }
00396
00397 FNOTICE(("Number of triangle read: %d\n", triCount));
00398
00399 createSharedIndex(geo);
00400 if(!hasNormals)
00401 calcVertexNormals(geo);
00402
00403 SceneFileHandler::the().updateReadProgress(100);
00404 return root;
00405 }
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445 OFFSceneFileType::OFFSceneFileType(const Char8 *suffixArray[],
00446 UInt16 suffixByteCount, bool override,
00447 UInt32 overridePriority,
00448 UInt32 flags) :
00449 SceneFileType(suffixArray, suffixByteCount,
00450 override, overridePriority, flags)
00451 {
00452 }
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474 OFFSceneFileType::OFFSceneFileType(const OFFSceneFileType &obj) :
00475 SceneFileType(obj)
00476 {
00477 return;
00478 }
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500 OFFSceneFileType &OFFSceneFileType::the(void)
00501 {
00502 return _the;
00503 }
00504
00505
00506 OFFSceneFileType::~OFFSceneFileType(void)
00507 {
00508 return;
00509 }
00510
00511
00512 const Char8 *OFFSceneFileType::getName(void) const
00513 {
00514 return "Geomview Geometry";
00515 }
00516
00517
00518
00519
00520
00521 #ifdef __sgi
00522 #pragma set woff 1174
00523 #endif
00524
00525 #ifdef OSG_LINUX_ICC
00526 #pragma warning( disable : 177 )
00527 #endif
00528
00529 namespace
00530 {
00531 static Char8 cvsid_cpp[] = "@(#)$Id: $";
00532 static Char8 cvsid_hpp[] = OSGOFFSCENEFILETYPE_HEADER_CVSID;
00533 }