00001
00011 #ifndef VRMLPARSER_H
00012 #define VRMLPARSER_H
00013
00014 #include "IS3DCommon.H"
00015 #include "ModelObj.H"
00016 #include "ProgressBar.H"
00017 #include "Event.H"
00018 #include "FsaMgr.H"
00019
00020 namespace IS3D {
00021
00022
00023
00024 typedef ReferenceCountedPointer< class VRMLNode > VRMLNodeRef;
00025 typedef ReferenceCountedPointer< class VRMLSeparatorNode > VRMLSeparatorNodeRef;
00026 typedef ReferenceCountedPointer< class VRMLMaterialNode > VRMLMaterialNodeRef;
00027 typedef ReferenceCountedPointer< class VRMLCoordinatesNode > VRMLCoordinatesNodeRef;
00028 typedef ReferenceCountedPointer< class VRMLNormalsNode > VRMLNormalsNodeRef;
00029 typedef ReferenceCountedPointer< class VRMLIndexedFaceNode > VRMLIndexedFaceNodeRef;
00030 typedef ReferenceCountedPointer< class VRMLGroupNode > VRMLGroupNodeRef;
00031 typedef ReferenceCountedPointer< class VRMLSwitchNode > VRMLSwitchNodeRef;
00032 typedef ReferenceCountedPointer< class VRMLPerspCameraNode > VRMLPerspCameraNodeRef;
00033
00034
00035
00036 class VRMLNode : public ReferenceCountedObject {
00037 public:
00038 enum { SEP_NODE, MAT_NODE, COORD3_NODE, NORMALS_NODE,
00039 INDEXED_FACE_NODE, GROUP_NODE, SWITCH_NODE,
00040 PCAM_NODE,
00041 NO_MORE_NODES_IN_SCOPE
00042 };
00043 virtual int getNodeType() = 0;
00044 };
00045
00046 class VRMLPerspCameraNode : public VRMLNode {
00047 public:
00048 virtual int getNodeType() { return PCAM_NODE; }
00049 Vector3 _position;
00050 Vector3 _lookVec;
00051 double _rotAboutLookVec;
00052 };
00053
00054 class VRMLSeparatorNode : public VRMLNode {
00055 public:
00056 virtual int getNodeType() { return SEP_NODE; }
00057 Array< VRMLGroupNodeRef > _groups;
00058 Array< VRMLSwitchNodeRef > _switches;
00059 Array< VRMLMaterialNodeRef > _materials;
00060 Array< VRMLCoordinatesNodeRef > _coordinates;
00061 Array< VRMLNormalsNodeRef > _normals;
00062 Array< VRMLSeparatorNodeRef > _children;
00063 Array< VRMLIndexedFaceNodeRef > _faces;
00064 VRMLPerspCameraNodeRef _perspCamera;
00065 };
00066
00067 class VRMLMaterialNode : public VRMLNode {
00068 public:
00069 virtual int getNodeType() { return MAT_NODE; }
00070
00071
00072
00073 Array< Vector3 > _ambient;
00074 Array< Vector3 > _specular;
00075 Array< Vector3 > _diffuse;
00076 Array< Vector3 > _emissive;
00077 Array< double > _shininess, _transparency;
00078 };
00079
00080 class VRMLCoordinatesNode : public VRMLNode {
00081 public:
00082 virtual int getNodeType() { return COORD3_NODE; }
00083 Array< Vector3 > _points;
00084 };
00085
00086 class VRMLNormalsNode : public VRMLNode {
00087 public:
00088 virtual int getNodeType() { return NORMALS_NODE; }
00089 Array< Vector3 > _normal_vectors;
00090 };
00091
00092 class VRMLIndexedFaceNode : public VRMLNode {
00093 public:
00094 virtual int getNodeType() { return INDEXED_FACE_NODE; }
00095 Array< Array<int> > _faces_vertices;
00096 };
00097
00098 class VRMLGroupNode : public VRMLNode {
00099 public:
00100 virtual int getNodeType() { return GROUP_NODE; }
00101 Array< VRMLSwitchNodeRef > _switches;
00102 };
00103
00104 class VRMLSwitchNode : public VRMLNode {
00105 public:
00106 virtual int getNodeType() { return SWITCH_NODE; }
00107 Array< VRMLSeparatorNodeRef > _separators;
00108 };
00109
00110
00111
00112
00113 typedef ReferenceCountedPointer<class VRMLParser> VRMLParserRef;
00114
00121 class VRMLParser : public ReferenceCountedObject
00122 {
00123 public:
00124
00125 VRMLParser(const std::string &filename,
00126 bool getCameraFromFile = true,
00127 bool addModelsToDrawMgr = true,
00128 bool loadInSecondThread = true);
00129
00130 virtual ~VRMLParser();
00131
00132 void parseAndCreateModels();
00133 void addAllModelsToDrawMgr();
00134
00135 int getNumModels();
00136 ModelObjRef getModel(int index=0);
00137
00138 float getPercentageDoneLoading();
00139 void checkForThreadComplete(EventRef e);
00140
00141 private:
00142
00143 Array<ModelObjRef> _models;
00144
00145
00146 void recursiveTraverseVRML(VRMLSeparatorNodeRef root);
00147
00148
00149 int peekNextNodeType ( TextInput& vrmlTextFile );
00150 VRMLPerspCameraNodeRef parsePerspCameraNode ( TextInput& vrmlTextFile );
00151 VRMLSeparatorNodeRef parseSeparatorNode ( TextInput& vrmlTextFile, bool isRootSep=false );
00152 VRMLMaterialNodeRef parseMaterialNode ( TextInput& vrmlTextFile );
00153 VRMLCoordinatesNodeRef parseCoordinate3Node( TextInput& vrmlTextFile );
00154 VRMLNormalsNodeRef parseNormalsNode ( TextInput& vrmlTextFile );
00155 VRMLIndexedFaceNodeRef parseIndexedFaceNode( TextInput& vrmlTextFile );
00156 VRMLGroupNodeRef parseGroupNode ( TextInput& vrmlTextFile );
00157 VRMLSwitchNodeRef parseSwitchNode ( TextInput& vrmlTextFile );
00158
00159 Array< Array<int> > reshapeDelimitedIntArray(const Array<int>& srcArray, int delimiter);
00160 void readThreeTuples( TextInput& vrmlTextFile, Array< Vector3 >& values );
00161 void readScalars ( TextInput& vrmlTextFile, Array< double >& values );
00162 void readInts ( TextInput& vrmlTextFile, Array< int >& values );
00163 Vector3 readThreeTuple ( TextInput& vrmlTextFile );
00164 double readScalar ( TextInput& vrmlTextFile );
00165 void eatString( TextInput& vrmlTextFile, const std::string expectedToken );
00166 void eatBeginCurly ( TextInput& vrmlTextFile );
00167 void eatCloseCurly ( TextInput& vrmlTextFile );
00168 void eatBeginBrace ( TextInput& vrmlTextFile );
00169 void eatCloseBrace ( TextInput& vrmlTextFile );
00170 void eatUntilCloseCurly( TextInput& vrmlTextFile );
00171
00172 private:
00173
00174
00175 SDL_mutex* _threadMutex;
00176 bool _parseAndCreateModelsDone;
00177 float _percDoneLoading;
00178
00179 std::string _filename;
00180 bool _useCamera;
00181 bool _addModels;
00182 int _numLinesInFile;
00183 ProgressBarRef _progressBar;
00184 FsaRef _fsa;
00185
00186 public:
00187
00188
00189 static VRMLParserRef _parserLoadingInThread;
00190 };
00191
00192
00193
00194 }
00195
00196 #endif
00197