OSGRAWSceneFileType.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038
00039 //-------------------------------
00040 //  Includes
00041 //-------------------------------
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045
00046 #include "OSGConfig.h"
00047
00048 #include <iostream>
00049 #include <fstream>
00050
00051 #include "OSGLog.h"
00052
00053 #include "OSGNode.h"
00054 #include "OSGGeometry.h"
00055 #include "OSGSimpleMaterial.h"
00056 #include "OSGTypedGeoIntegralProperty.h"
00057
00058 #include "OSGRAWSceneFileType.h"
00059
00060 OSG_USING_NAMESPACE
00061
00062 #if defined(OSG_WIN32_ICL) && !defined(OSG_CHECK_FIELDSETARG)
00063 #pragma warning (disable : 383)
00064 #endif
00065 
00066
00072 /*****************************
00073  *   Types
00074  *****************************/
00075 // Static Class Varible implementations:
00076
00077 const Char8            *RAWSceneFileType::_suffixA[] = {"raw"};
00078
00079
00080 RAWSceneFileType  RAWSceneFileType::_the(_suffixA,
00081                                          sizeof(_suffixA),
00082                                          false,
00083                                          10,
00084                                          SceneFileType::OSG_READ_SUPPORTED);
00085
00086 /*****************************
00087  *    Classvariables
00088  *****************************/
00089
00090
00091 /********************************
00092  *    Class methodes
00093  *******************************/
00094
00095
00096 /*******************************
00097 *public
00098 *******************************/
00099
00100 //----------------------------
00101 // Function name: read
00102 //----------------------------
00103 //
00104 //Parameters:
00105 //p: Scene &image, const char *fileName
00106 //GlobalVars:
00107 //g:
00108 //Returns:
00109 //r:bool
00110 // Caution
00111 //c:
00112 //Assumations:
00113 //a:
00114 //Describtions:
00115 //d: read the image from the given file
00116 //SeeAlso:
00117 //s:
00118 //
00119 //------------------------------
00120
00121 #ifdef __sgi
00122 #pragma set woff 1209
00123 #endif
00124 
00125 NodeTransitPtr RAWSceneFileType::read(      std::istream &is,
00126                                       const Char8        *,
00127                                             Resolver        ) const
00128 {
00129     NodeTransitPtr              root;
00130     GeometryUnrecPtr            geo;
00131     GeoPnt3rPropertyUnrecPtr    points;
00132     GeoVec3rPropertyUnrecPtr    normals;
00133     GeoIntegralPropertyUnrecPtr index;
00134     GeoIntegralPropertyUnrecPtr lens;
00135     GeoIntegralPropertyUnrecPtr type;
00136
00137     Vec3r vec[3];
00138
00139     UInt32 i = 0, n, triCount = 0;
00140
00141     Real32 x,y,z;
00142
00143     if(is)
00144     {
00145         root = Node    ::create();
00146         geo  = Geometry::create();
00147
00148         root->setCore( geo );
00149
00150         points = GeoPnt3rProperty::create();
00151
00152         geo->setPositions(points);
00153
00154         normals = GeoVec3rProperty::create();
00155
00156         geo->setNormals(normals);
00157
00158         triCount = i = 0;
00159
00160
00161         while(1)
00162         {
00163             is >> x >> y >> z;
00164
00165             if(is.eof())
00166             {
00167                 break;
00168             }
00169             else
00170             {
00171                 points->editFieldPtr()->push_back(Pnt3r(x, y, z));
00172
00173                 vec[i].setValues(x,y,z);
00174
00175                 std::cerr << x << " " << y << " " << z << std::endl;
00176
00177                 if(i == 2)
00178                 {
00179                     vec[0] -= vec[1];
00180                     vec[1] -= vec[2];
00181                     vec[0].crossThis(vec[1]);
00182                     vec[0].normalize();
00183
00184                     normals->editFieldPtr()->push_back(vec[0]);
00185                     normals->editFieldPtr()->push_back(vec[0]);
00186                     normals->editFieldPtr()->push_back(vec[0]);
00187
00188                     i = 0;
00189                     triCount++;
00190                 }
00191                 else
00192                 {
00193                     i++;
00194                 }
00195             }
00196         }
00197
00198         if(triCount != 0)
00199         {
00200             index = GeoUIntProperty::create();
00201
00202             geo->setIndex(index, Geometry::PositionsIndex);
00203             geo->setIndex(index, Geometry::NormalsIndex  );
00204
00205             n = triCount * 3;
00206
00207             for(i = 0; i < n; i++)
00208                 index->push_back(i);
00209
00210
00211
00212             lens = GeoUIntProperty::create();
00213
00214             geo->setLengths(lens);
00215
00216             lens->push_back(n);
00217
00218             type = GeoUInt8Property::create();
00219
00220             geo->setTypes(type);
00221
00222             type->push_back(GL_TRIANGLES);
00223         }
00224
00225         SimpleMaterialUnrecPtr mat = SimpleMaterial::create();
00226
00227         mat->setDiffuse  (Color3r(  .8f,  .8f,  .8f));
00228         mat->setSpecular (Color3r( 1.f,  1.f,  1.f ));
00229         mat->setShininess(20.f                      );
00230
00231         geo->setMaterial(mat);
00232     }
00233
00234     if(triCount)
00235     {
00236         SNOTICE << triCount << " triangle read " << std::endl;
00237     }
00238
00239     return root;
00240 }
00241
00242 #ifdef __sgi
00243 #pragma reset woff 1209
00244 #endif
00245 
00246 /******************************
00247 *protected
00248 ******************************/
00249
00250
00251 /******************************
00252 *private
00253 ******************************/
00254
00255
00256 /***************************
00257 *instance methodes
00258 ***************************/
00259
00260
00261 /***************************
00262 *public
00263 ***************************/
00264
00265
00269 RAWSceneFileType::RAWSceneFileType(const Char8  *suffixArray[],
00270                                          UInt16  suffixByteCount,
00271                                          bool    override,
00272                                          UInt32  overridePriority,
00273                                          UInt32  flags) :
00274     SceneFileType(suffixArray,
00275                   suffixByteCount,
00276                   override,
00277                   overridePriority,
00278                   flags)
00279 {
00280 }
00281
00282 RAWSceneFileType &RAWSceneFileType::the(void)
00283 {
00284     return _the;
00285 }
00286
00287 const Char8 *RAWSceneFileType::getName(void) const
00288 {
00289     return "RAW Geometry";
00290 }
00291
00292
00293 RAWSceneFileType::~RAWSceneFileType (void )
00294 {
00295     return;
00296 }
00297
00298