00001 #pragma warning ( disable : 4786 )
00002
00003 #include <assert.h>
00004 #include <map>
00005 #include <stdio.h>
00006 #include <string.h>
00007 #include "utility.h"
00008 #include "VrmlAppearance.h"
00009 #include "VrmlBboxCenter.h"
00010 #include "VrmlBboxSize.h"
00011 #include "VrmlChildren.h"
00012 #include "VrmlCoordinate3.h"
00013 #include "VrmlGroup.h"
00014 #include "VrmlIndexedFaceSet.h"
00015 #include "VrmlInline.h"
00016 #include "VrmlMaterial.h"
00017 #include "VrmlNode.h"
00018 #include "VrmlOneWordNode.h"
00019 #include "VrmlRotation.h"
00020 #include "VrmlScale.h"
00021 #include "VrmlSeparator.h"
00022 #include "VrmlShape.h"
00023 #include "VrmlTransform.h"
00024 #include "VrmlTranslation.h"
00025 #include "VrmlWorldInfo.h"
00026
00027
00028 std::map< std::string, VrmlNode* > VrmlNode::g_DataBase ;
00029
00030
00031
00032
00033
00034
00035 static void CleanVrmlNodes()
00036 {
00037
00038 std::map< std::string, VrmlNode* >::iterator it;
00039 for( it = VrmlNode::g_DataBase.begin(); it != VrmlNode::g_DataBase.end(); it++ )
00040 {
00041 VrmlNode* deleteMe = ( *it ).second;
00042 }
00043
00044 VrmlNode::g_DataBase.clear();
00045 }
00046
00047
00048
00049
00050
00051
00052 VrmlNode::VrmlNode()
00053 :
00054 m_RefCount( 0 )
00055 {
00056 static bool firstTime = true;
00057 if( firstTime )
00058 {
00059 int result = ::atexit( CleanVrmlNodes );
00060 assert( result == 0 );
00061 firstTime = false;
00062 }
00063
00064 #ifdef _DEBUG
00065 vrmlType[ 0 ] = 0x00;
00066 strcpy( vrmlType, "==NO NAME==" );
00067 #endif
00068 }
00069
00070
00071
00072
00073
00074
00075 VrmlNode::~VrmlNode()
00076 {
00077
00078 assert( m_RefCount == 0 );
00079 }
00080
00081
00082
00083
00084
00085
00086 void VrmlNode::AddRef()
00087 {
00088 m_RefCount++;
00089 }
00090
00091
00092
00093
00094
00095
00096 void VrmlNode::CleanupGlobals()
00097 {
00098
00099 std::map< std::string, VrmlNode* >::iterator it = g_DataBase.begin();
00100
00101 for( ; it != g_DataBase.end(); it++ )
00102 {
00103 VrmlNode* node = ( *it ).second;
00104 node->Release();
00105 }
00106 g_DataBase.clear();
00107 }
00108
00109
00110
00111
00112
00113
00114 Keyword VrmlNode::GetKeyword( char*& buffer )
00115 {
00116 char keyword[ 80 ] = "";
00117 VrmlNode::GetKeywordString( buffer, keyword );
00118
00119 if( strcmp( keyword, VrmlKeywordAmbientColor ) == 0 )
00120 {
00121 return VRML_AmbientColor;
00122 }
00123 if( strcmp( keyword, VrmlKeywordAmbientIntensity ) == 0 )
00124 {
00125 return VRML_AmbientIntensity;
00126 }
00127 if( strcmp( keyword, VrmlKeywordAppearanceLower ) == 0 )
00128 {
00129 return VRML_AppearanceLower;
00130 }
00131 if( strcmp( keyword, VrmlKeywordAppearanceUpper ) == 0 )
00132 {
00133 return VRML_AppearanceUpper;
00134 }
00135 if( strcmp( keyword, VrmlKeywordBboxCenter ) == 0 )
00136 {
00137 return VRML_BboxCenter;
00138 }
00139 if( strcmp( keyword, VrmlKeywordBboxSize ) == 0 )
00140 {
00141 return VRML_BboxSize;
00142 }
00143 if( strcmp( keyword, VrmlKeywordCcw ) == 0 )
00144 {
00145 return VRML_Ccw;
00146 }
00147 if( strcmp( keyword, VrmlKeywordChildren ) == 0 )
00148 {
00149 return VRML_Children;
00150 }
00151 if( strcmp( keyword, VrmlKeywordColor ) == 0 )
00152 {
00153 return VRML_Color;
00154 }
00155 if( strcmp( keyword, VrmlKeywordColorPerVertex ) == 0 )
00156 {
00157 return VRML_ColorPerVertex;
00158 }
00159 if( strcmp( keyword, VrmlKeywordCollision ) == 0 )
00160 {
00161 return VRML_Collision;
00162 }
00163 if( strcmp( keyword, VrmlKeywordConvex ) == 0 )
00164 {
00165 return VRML_Convex;
00166 }
00167 if( strcmp( keyword, "Coordinate" ) == 0 )
00168 {
00169 return VRML_Coordinate3;
00170 }
00171 if( strcmp( keyword, VrmlKeywordCoord ) == 0 )
00172 {
00173 return VRML_Coord;
00174 }
00175 if( strcmp( keyword, VrmlKeywordCoordIndex ) == 0 )
00176 {
00177 return VRML_CoordIndex;
00178 }
00179 if( strcmp( keyword, "Coordinate3" ) == 0 )
00180 {
00181 return VRML_Coordinate3;
00182 }
00183 if( strcmp( keyword, VrmlKeywordCreaseAngle ) == 0 )
00184 {
00185 return VRML_CreaseAngle;
00186 }
00187 if( strcmp( keyword, VrmlKeywordCylinder ) == 0 )
00188 {
00189 return VRML_Cylinder;
00190 }
00191 if( strcmp( keyword, VrmlKeywordDefine ) == 0 )
00192 {
00193 return VRML_Define;
00194 }
00195 if( strcmp( keyword, VrmlKeywordDiffuseColor ) == 0 )
00196 {
00197 return VRML_DiffuseColor;
00198 }
00199 if( strcmp( keyword, VrmlKeywordDirectionalLight ) == 0 )
00200 {
00201 return VRML_DirectionalLight;
00202 }
00203 if( strcmp( keyword, "DrawStyle" ) == 0 )
00204 {
00205 return VRML_DrawStyle;
00206 }
00207 if( strcmp( keyword, VrmlKeywordEmissiveColor ) == 0 )
00208 {
00209 return VRML_EmissiveColor;
00210 }
00211 if( strcmp( keyword, VrmlKeywordGeometry ) == 0 )
00212 {
00213 return VRML_Geometry;
00214 }
00215 if( strcmp( keyword, VrmlKeywordGroup ) == 0 )
00216 {
00217 return VRML_Group;
00218 }
00219 if( strcmp( keyword, VrmlKeywordInline ) == 0 )
00220 {
00221 return VRML_Inline;
00222 }
00223 if( strcmp( keyword, "IndexedFaceSet" ) == 0 )
00224 {
00225 return VRML_IndexedFaceSet;
00226 }
00227 if( strcmp( keyword, "IndexedLineSet" ) == 0 )
00228 {
00229 return VRML_IndexedLineSet;
00230 }
00231 if( strcmp( keyword, VrmlKeywordInfo ) == 0 )
00232 {
00233 return VRML_Info;
00234 }
00235 if( strcmp( keyword, VrmlKeywordMaterialBinding ) == 0 )
00236 {
00237 return VRML_MaterialBinding;
00238 }
00239 if( strcmp( keyword, VrmlKeywordMaterialLower ) == 0 )
00240 {
00241 return VRML_MaterialLower;
00242 }
00243 if( strcmp( keyword, VrmlKeywordMaterialUpper ) == 0 )
00244 {
00245 return VRML_MaterialUpper;
00246 }
00247 if( strcmp( keyword, VrmlKeywordMaterialIndex ) == 0 )
00248 {
00249 return VRML_MaterialIndex;
00250 }
00251 if( strcmp( keyword, VrmlKeywordMatrixTransform ) == 0 )
00252 {
00253 return VRML_MatrixTransform;
00254 }
00255 if( strcmp( keyword, VrmlKeywordNavigationInfo ) == 0 )
00256 {
00257 return VRML_NavigationInfo;
00258 }
00259 if( strcmp( keyword, VrmlKeywordNormal ) == 0 )
00260 {
00261 return VRML_Normal;
00262 }
00263 if( strcmp( keyword, VrmlKeywordNormalBinding ) == 0 )
00264 {
00265 return VRML_NormalBinding;
00266 }
00267 if( strcmp( keyword, VrmlKeywordNormalIndex ) == 0 )
00268 {
00269 return VRML_NormalIndex;
00270 }
00271 if( strcmp( keyword, VrmlKeywordNormalPerVertex ) == 0 )
00272 {
00273 return VRML_NormalPerVertex;
00274 }
00275 if( strcmp( keyword, "PointSet" ) == 0 )
00276 {
00277 return VRML_PointSet;
00278 }
00279 if( strcmp( keyword, VrmlKeywordRenderCulling ) == 0 )
00280 {
00281 return VRML_RenderCulling;
00282 }
00283 if( strcmp( keyword, VrmlKeywordRotation ) == 0 )
00284 {
00285 return VRML_Rotation;
00286 }
00287 if( strcmp( keyword, VrmlKeywordRotationUpper ) == 0 )
00288 {
00289 return VRML_Rotation;
00290 }
00291 if( strcmp( keyword, VrmlKeywordScale ) == 0 )
00292 {
00293 return VRML_Scale;
00294 }
00295 if( strcmp( keyword, VrmlKeywordScaleFactor ) == 0 )
00296 {
00297 return VRML_Scale;
00298 }
00299 if( strcmp( keyword, VrmlKeywordScaleUpper ) == 0 )
00300 {
00301 return VRML_Scale;
00302 }
00303 if( strcmp( keyword, VrmlKeywordScaleOrientation ) == 0 )
00304 {
00305 return VRML_ScaleOrientation;
00306 }
00307 if( strcmp( keyword, VrmlKeywordSeparator ) == 0 )
00308 {
00309 return VRML_Separator;
00310 }
00311 if( strcmp( keyword, VrmlKeywordShape ) == 0 )
00312 {
00313 return VRML_Shape;
00314 }
00315 if( strcmp( keyword, "ShapeHints" ) == 0 )
00316 {
00317 return VRML_ShapeHints;
00318 }
00319 if( strcmp( keyword, VrmlKeywordShininess ) == 0 )
00320 {
00321 return VRML_Shininess;
00322 }
00323 if( strcmp( keyword, VrmlKeywordSpecularColor ) == 0 )
00324 {
00325 return VRML_SpecularColor;
00326 }
00327 if( strcmp( keyword, VrmlKeywordSphere ) == 0 )
00328 {
00329 return VRML_Sphere;
00330 }
00331 if( strcmp( keyword, VrmlKeywordSolid ) == 0 )
00332 {
00333 return VRML_Solid;
00334 }
00335 if( strcmp( keyword, VrmlKeywordTexCoord ) == 0 )
00336 {
00337 return VRML_TexCoord;
00338 }
00339 if( strcmp( keyword, VrmlKeywordTexCoordIndex ) == 0 )
00340 {
00341 return VRML_TexCoordIndex;
00342 }
00343 if( strcmp( keyword, VrmlKeywordTextureCoordinate ) == 0 )
00344 {
00345 return VRML_TextureCoordinate;
00346 }
00347 if( strcmp( keyword, VrmlKeywordTexture ) == 0 )
00348 {
00349 return VRML_Texture;
00350 }
00351 if( strcmp( keyword, VrmlKeywordTextureCoordIndex ) == 0 )
00352 {
00353 return VRML_TextureCoordIndex;
00354 }
00355 if( strcmp( keyword, VrmlKeywordTimeSensor ) == 0 )
00356 {
00357 return VRML_TimeSensor;
00358 }
00359 if( strcmp( keyword, VrmlKeywordTransform ) == 0 )
00360 {
00361 return VRML_Transform;
00362 }
00363 if( strcmp( keyword, VrmlKeywordTranslation ) == 0 )
00364 {
00365 return VRML_Translation;
00366 }
00367 if( strcmp( keyword, VrmlKeywordTranslationUpper ) == 0 )
00368 {
00369 return VRML_Translation;
00370 }
00371 if( strcmp( keyword, VrmlKeywordTransparency ) == 0 )
00372 {
00373 return VRML_Transparency;
00374 }
00375 if( strcmp( keyword, VrmlKeywordUse ) == 0 )
00376 {
00377 return VRML_Use;
00378 }
00379 if( strcmp( keyword, VrmlKeywordUrl ) == 0 )
00380 {
00381 return VRML_Url;
00382 }
00383 if( strcmp( keyword, VrmlKeywordVector ) == 0 )
00384 {
00385 return VRML_Vector;
00386 }
00387 if( strcmp( keyword, VrmlKeywordViewpoint ) == 0 )
00388 {
00389 return VRML_Viewpoint;
00390 }
00391 if( strcmp( keyword, VrmlKeywordWorldInfo ) == 0 )
00392 {
00393 return VRML_WorldInfo;
00394 }
00395 printf( "VrmlNode::GetKeyword - Discovered invalid keyword : %s\n", keyword );
00396 IJG_Assert( false );
00397 return VRML_Invalid;
00398 }
00399
00400
00401
00402
00403
00404
00405 void VrmlNode::GetKeywordString( char*& buffer, char* keyword )
00406 {
00407 EatWhite( buffer );
00408
00409
00410 int position = strcspn( buffer, " \t\n\r{" );
00411
00412 strncpy( keyword, buffer, position );
00413 buffer += position;
00414 }
00415
00416
00417
00418
00419
00420
00421 void VrmlNode::Parse( char*& buffer )
00422 {
00423
00424 int depth = 0;
00425
00426
00427
00428
00429 assert( *buffer = '{' );
00430 buffer++;
00431 depth++;
00432 while( depth != 0 )
00433 {
00434 buffer = strpbrk( buffer, "{}" );
00435 if( *buffer == '{' )
00436 {
00437 depth++;
00438 }
00439 if( *buffer == '}' )
00440 {
00441 depth--;
00442 }
00443 buffer++;
00444 }
00445 }
00446
00447
00448
00449
00450
00451
00452 VrmlNode* VrmlNode::ParseDefNode( char*& buffer )
00453 {
00454
00455 char keywordString[ 80 ] = "";
00456 VrmlNode::GetKeywordString( buffer, keywordString );
00457
00458
00459 Keyword keyword = VrmlNode::GetKeyword( buffer );
00460 EatWhite( buffer );
00461 buffer++;
00462 VrmlNode* node = VrmlNode::ParseNode( keyword, buffer );
00463 node->AddRef();
00464
00465 g_DataBase.insert( std::map< std::string, VrmlNode* >::value_type( keywordString, node ) );
00466
00467 return node;
00468 }
00469
00470
00471
00472
00473
00474
00475 VrmlNode* VrmlNode::ParseNode( Keyword keyword, char*& buffer )
00476 {
00477 VrmlNode* node = NULL;
00478 switch( keyword )
00479 {
00480 case VRML_AppearanceUpper:
00481 {
00482 node = new VrmlAppearance;
00483 node->SetType( VrmlKeywordAppearanceUpper );
00484 break;
00485 }
00486 case VRML_BboxCenter:
00487 {
00488 node = new VrmlBboxCenter;
00489 node->SetType( VrmlKeywordBboxCenter );
00490 break;
00491 }
00492 case VRML_BboxSize:
00493 {
00494 node = new VrmlBboxSize;
00495 node->SetType( VrmlKeywordBboxSize );
00496 break;
00497 }
00498 case VRML_Children:
00499 {
00500 node = new VrmlChildren;
00501 node->SetType( VrmlKeywordChildren );
00502 break;
00503 }
00504 case VRML_Coordinate3:
00505 {
00506 node = new VrmlCoordinate3;
00507 node->SetType( "Coordinate3" );
00508 break;
00509 }
00510 case VRML_Cylinder:
00511 {
00512 node = new VrmlNode;
00513 node->SetType( VrmlKeywordCylinder );
00514 break;
00515 }
00516 case VRML_Define:
00517 {
00518 node = VrmlNode::ParseDefNode( buffer );
00519 return node;
00520 break;
00521 }
00522 case VRML_DrawStyle:
00523 {
00524 node = new VrmlNode;
00525 node->SetType( VrmlKeywordDrawStyle );
00526 break;
00527 }
00528 case VRML_DirectionalLight:
00529 {
00530 node = new VrmlNode;
00531 node->SetType( VrmlKeywordDirectionalLight );
00532 break;
00533 }
00534 case VRML_Group:
00535 {
00536 node = new VrmlGroup;
00537 node->SetType( "Group" );
00538 break;
00539 }
00540 case VRML_Info:
00541 {
00542 node = new VrmlNode;
00543 node->SetType( VrmlKeywordInfo );
00544 break;
00545 }
00546 case VRML_IndexedFaceSet:
00547 {
00548 node = new VrmlIndexedFaceSet;
00549 node->SetType( "IndexedFaceSet" );
00550 break;
00551 }
00552 case VRML_IndexedLineSet:
00553 {
00554 node = new VrmlNode;
00555 node->SetType( VrmlKeywordIndexedLineSet );
00556 break;
00557 }
00558 case VRML_Inline:
00559 {
00560 node = new VrmlInline;
00561 node->SetType( VrmlKeywordInline );
00562 break;
00563 }
00564 case VRML_MaterialUpper:
00565 {
00566 node = new VrmlMaterial;
00567 node->SetType( VrmlKeywordMaterialUpper );
00568 break;
00569 }
00570 case VRML_MaterialBinding:
00571 {
00572 node = new VrmlNode;
00573 node->SetType( VrmlKeywordMaterialBinding );
00574 break;
00575 }
00576 case VRML_MatrixTransform:
00577 {
00578 node = new VrmlNode;
00579 node->SetType( VrmlKeywordMatrixTransform );
00580 break;
00581 }
00582 case VRML_NavigationInfo:
00583 {
00584 node = new VrmlNode;
00585 node->SetType( VrmlKeywordNavigationInfo );
00586 break;
00587 }
00588 case VRML_NormalBinding:
00589 {
00590 node = new VrmlNode;
00591 node->SetType( VrmlKeywordNormalBinding );
00592 break;
00593 }
00594 case VRML_PointSet:
00595 {
00596 node = new VrmlNode;
00597 node->SetType( VrmlKeywordPointSet );
00598 break;
00599 }
00600 case VRML_RenderCulling:
00601 {
00602 node = new VrmlOneWordNode;
00603 node->SetType( ::VrmlKeywordRenderCulling );
00604 break;
00605 }
00606 case VRML_Rotation:
00607 {
00608 node = new VrmlRotation;
00609 node->SetType( VrmlKeywordRotation );
00610 break;
00611 }
00612 case VRML_Scale:
00613 {
00614 node = new VrmlScale;
00615 node->SetType( VrmlKeywordScale );
00616 break;
00617 }
00618 case VRML_Separator:
00619 {
00620 node = new VrmlSeparator;
00621 node->SetType( "Separator" );
00622 break;
00623 }
00624 case VRML_Shape:
00625 {
00626 node = new VrmlShape;
00627 node->SetType( VrmlKeywordShape );
00628 break;
00629 }
00630 case VRML_ShapeHints:
00631 {
00632 node = new VrmlNode;
00633 node->SetType( "ShapeHints" );
00634 break;
00635 }
00636 case VRML_Sphere:
00637 {
00638 node = new VrmlNode;
00639 node->SetType( VrmlKeywordSphere );
00640 break;
00641 }
00642 case VRML_TextureCoordinate:
00643 {
00644 node = new VrmlNode;
00645 node->SetType( VrmlKeywordTextureCoordinate );
00646 break;
00647 }
00648 case VRML_TimeSensor:
00649 {
00650 node = new VrmlNode;
00651 node->SetType( VrmlKeywordTimeSensor );
00652 break;
00653 }
00654 case VRML_Transform:
00655 {
00656 node = new VrmlTransform;
00657 node->SetType( VrmlKeywordTransform );
00658 break;
00659 }
00660 case VRML_Translation:
00661 {
00662 node = new VrmlTranslation;
00663 node->SetType( VrmlKeywordTranslation );
00664 break;
00665 }
00666 case VRML_Use:
00667 {
00668 node = VrmlNode::ParseUseNode( buffer );
00669 return node;
00670 break;
00671 }
00672 case VRML_Viewpoint:
00673 {
00674
00675 node = new VrmlNode;
00676 node->SetType( VrmlKeywordViewpoint );
00677 break;
00678 }
00679 case VRML_WorldInfo:
00680 {
00681
00682 node = new VrmlWorldInfo;
00683 node->SetType( "WorldInfo" );
00684 break;
00685 }
00686 case VRML_Invalid:
00687 {
00688 printf( "VrmlNode::ParseNode - parsed invalid node\n" );
00689 assert( false );
00690 break;
00691 }
00692 default:
00693 {
00694 assert( false );
00695 node = new VrmlNode;
00696 node->SetType( "MISC" );
00697 break;
00698 }
00699 }
00700 if (node == NULL)
00701 return NULL;
00702 node->Parse( buffer );
00703 return node;
00704 }
00705
00706
00707
00708
00709
00710
00711 VrmlNode* VrmlNode::ParseUseNode( char*& buffer )
00712 {
00713
00714 char keywordString[ 80 ] = "";
00715 VrmlNode::GetKeywordString( buffer, keywordString );
00716
00717
00718 std::map< std::string, VrmlNode* >::iterator it = g_DataBase.find( keywordString );
00719
00720 if( it != g_DataBase.end() )
00721 {
00722 return ( *it ).second;
00723 }
00724 assert( false );
00725 return NULL;
00726 }
00727
00728
00729
00730
00731
00732
00733 void VrmlNode::Release()
00734 {
00735 assert( m_RefCount > 0 );
00736 m_RefCount--;
00737 if( m_RefCount == 0 )
00738 {
00739 delete this;
00740 }
00741 }
00742
00743
00744
00745
00746
00747
00748 void VrmlNode::Render( VrmlStack& stack ) const
00749 {
00750 #ifdef _DEBUG
00751 printf( "WARNING: do not know how to render node %s\n", this->vrmlType );
00752 #endif
00753 }
00754
00755
00756
00757
00758
00759
00760 void VrmlNode::RenderOpenGl( VrmlStack& stack ) const
00761 {
00762 this->Render( stack );
00763 }
00764
00765
00766
00767
00768
00769
00770
00771 IGS_Object* VrmlNode::RenderToIGS( VrmlStack& stack ) const
00772 {
00773 this->Render( stack );
00774 return NULL;
00775 }
00776
00777
00778
00779
00780
00781
00782 void VrmlNode::SetType( const char* string )
00783 {
00784 #ifdef _DEBUG
00785 strcpy( this->vrmlType, string );
00786 #endif
00787 }