Ignore:
Timestamp:
06/05/06 23:14:04 (18 years ago)
Author:
mattausch
Message:

added mesh instance support
improved support for occlusion queries + other extensions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp

    r971 r1001  
    2929#include "Triangle3.h" 
    3030#include "ViewCellsManager.h" 
     31#include "ResourceManager.h" 
    3132 
    3233namespace GtpVisibilityPreprocessor { 
     
    6061#define ROTATE_SCENE 0 
    6162 
    62  
    63 static void RotateMesh(Mesh *mesh) 
     63static int sUniqueMeshIdx = 0; 
     64 
     65// hack: rotate mesh by n degrees 
     66static void RotateMesh(Mesh *mesh, const float angle) 
    6467{ 
    6568        VertexContainer::iterator it, it_end = mesh->mVertices.end(); 
    6669 
    67         const float angle = 30.0f * PI / 180.0f; 
    68         const Matrix4x4 rot = RotationYMatrix(angle); 
     70        const float angleRad = angle * PI / 180.0f; 
     71        const Matrix4x4 rot = RotationYMatrix(angleRad); 
    6972 
    7073        for (it = mesh->mVertices.begin(); it != it_end; ++ it) 
     
    8386  , mSpaceCount(0) 
    8487  , mLoadPolygonsAsMeshes(loadPolygonsAsMeshes) 
    85 { 
    86   mCurrentNode = root; 
    87   // this matrix should never be removed from stack 
    88   //mTransformations.push(IdentityMatrix()); 
     88  , mCurrentMesh(NULL) 
     89{ 
     90        mCurrentNode = root; 
     91 
     92        // this matrix should never be removed from stack 
     93        //mTransformations.push(IdentityMatrix()); 
    8994} 
    9095 
     
    99104//  StdInParseHandlers: Implementation of the SAX DocumentHandler interface 
    100105// --------------------------------------------------------------------------- 
     106 
    101107void X3dParseHandlers::endElement(const XMLCh* const name) 
    102108{ 
    103   StrX lname(name); 
    104   string element(lname.LocalForm()); 
    105    
    106   // only create new mesh instance if define mechanism was not used 
    107   if (element == "Shape") 
    108     EndShape(); 
    109  
    110   if (element == "Transform") 
    111           EndTransform(); 
    112 } 
    113  
    114  
    115  
    116  
    117 void X3dParseHandlers::ApplyTransformation(Mesh *mesh,  
    118                                                                                    const Matrix4x4 &m) const 
    119 { 
    120         VertexContainer::iterator it, it_end = mesh->mVertices.end(); 
    121  
    122         for (it = mesh->mVertices.begin(); it != it_end; ++ it) 
    123         { 
    124                 (*it) = m * (*it);         
    125         } 
     109        StrX lname(name); 
     110        string element(lname.LocalForm()); 
     111 
     112        // only create new mesh instance if define mechanism was not used 
     113        if (element == "Shape") 
     114                EndShape(); 
     115 
     116        if (element == "Transform") 
     117                EndTransform(); 
    126118} 
    127119 
     
    134126                trafos.pop(); 
    135127 
    136                 ApplyTransformation(mesh, m); 
    137         } 
    138 } 
     128                mesh->ApplyTransformation(m); 
     129        } 
     130} 
     131 
     132 
     133void X3dParseHandlers::ApplyTransformations(TrafoStack trafos,  
     134                                                                                        TransformedMeshInstance *mi) const 
     135{ 
     136        while (!trafos.empty()) 
     137        { 
     138                const Matrix4x4 m = trafos.top(); 
     139                trafos.pop(); 
     140 
     141                mi->ApplyWorldTransform(m); 
     142        } 
     143} 
     144 
    139145 
    140146void X3dParseHandlers::StartTransform(AttributeList&  attributes) 
     
    143149 
    144150        const int len = attributes.getLength(); 
     151 
    145152    Matrix4x4 *rotm = NULL; 
    146153        Matrix4x4 *scalem = NULL; 
     
    208215void X3dParseHandlers::EndShape() 
    209216{ 
    210         // this shape is a definition => don't create mesh instance 
    211         if (mIsMeshDefinition)  
    212 { 
    213                 mMeshDefinitions[mCurrentMeshName.c_str()] = mCurrentMesh; 
    214                 //cout << "new definition: " << mCurrentMeshName << endl; 
     217         //-- if shape is merely a definition => don't create particular mesh instance 
     218         if (!mCurrentMesh || mIsMeshDefinition)  
     219         {  
     220                 return; 
     221         } 
     222 
     223         //-- each polygon is one single mesh 
     224         if (mLoadPolygonsAsMeshes) 
     225         { 
     226                 FaceContainer::const_iterator fit, fit_end = mCurrentMesh->mFaces.end(); 
     227 
     228                 cout << "m"; 
    215229                 
    216                 return; 
    217         } 
    218  
    219         // each polygon is one single mesh 
    220         if (mLoadPolygonsAsMeshes) 
    221         { 
    222                 //if (mCurrentMesh->mFaces.empty())     cout << "error!" << endl; 
    223  
    224                 FaceContainer::const_iterator fit, fit_end = mCurrentMesh->mFaces.end(); 
    225  
    226                 cout << "m"; 
    227                 //cout << "m: " << mCurrentMesh->mFaces.size() << endl; 
    228                 for (fit = mCurrentMesh->mFaces.begin(); fit != fit_end; ++ fit) 
    229                 { 
    230                         cout << "f"; 
    231  
    232                         Face *face = *fit; 
    233                         // only one face per mesh 
    234                         Mesh *mesh = new Mesh(); 
    235                         VertexIndexContainer vc; 
    236                                          
    237                         VertexIndexContainer::const_iterator vit, vit_end = face->mVertexIndices.end(); 
     230                 for (fit = mCurrentMesh->mFaces.begin(); fit != fit_end; ++ fit) 
     231                 { 
     232                         cout << "f"; 
     233 
     234                         Face *face = *fit; 
     235                 
     236                         // we create a new mesh for each face from the current mesh 
     237                         Mesh *mesh = MeshManager::GetSingleton()->CreateResource(); 
    238238                         
    239                         int i = 0; 
    240                         for (vit = face->mVertexIndices.begin(); vit != vit_end; ++ vit, ++ i) 
     239                         VertexIndexContainer::const_iterator vit, vit_end = face->mVertexIndices.end(); 
     240                 
     241                         int i = 0; 
     242                         VertexIndexContainer vcIndices; 
     243 
     244                         for (vit = face->mVertexIndices.begin(); vit != vit_end; ++ vit, ++ i) 
     245                         { 
     246                                 cout << "i"; 
     247                                 const int index = (*vit); 
     248 
     249                                 // add vertices 
     250                                 mesh->mVertices.push_back(mCurrentMesh->mVertices[index]); 
     251 
     252                                 // indices don't make much sense if mesh = face, but 
     253                                 // we need them anyway ... 
     254                                 vcIndices.push_back(i); 
     255                         } 
     256 
     257                         mesh->mFaces.push_back(new Face(vcIndices)); 
     258 
     259                         // NOTE: should rather be written into trafo of mesh instance 
     260                         ApplyTransformations(mTransformations, mesh); 
     261 
     262                         mesh->Preprocess(); 
     263                                 
     264                         // make an instance of this mesh 
     265                         MeshInstance *mi = new MeshInstance(mesh); 
     266                         mCurrentNode->mGeometry.push_back(mi); 
     267                 } 
     268 
     269                 // this mesh is not needed, unless it is used as a definition 
     270                 if (!mUsingMeshDefinition)  
     271                         MeshManager::GetSingleton()->DestroyEntry(mCurrentMesh->GetId()); 
     272        } 
     273        else // default usage: create a mesh instance from the current mesh 
     274        {  
     275                MeshInstance *mi; 
     276 
     277                if (0 && !mUsingMeshDefinition)  
     278                { 
     279                        // make an instance of this mesh 
     280            mi = new MeshInstance(mCurrentMesh); 
     281 
     282                        // this mesh is used only once => write transformations directly into it 
     283                        ApplyTransformations(mTransformations, mCurrentMesh); 
     284                } 
     285                else 
     286                { 
     287                         // make an instance of this mesh 
     288                         TransformedMeshInstance *tmi = new TransformedMeshInstance(mCurrentMesh); 
     289 
     290                         // apply transformation on the instance of the mesh  
     291                         ApplyTransformations(mTransformations, tmi); 
     292 
     293                         mi = tmi; 
     294                } 
     295                 
     296                if (mCurrentMaterial) 
     297                { 
     298                        // HACK: add the material to the mesh directly if no material yet 
     299                        if (mCurrentMesh->mMaterial) 
    241300                        { 
    242                                 cout << "i"; 
    243                                 int index = (*vit); 
    244                                 // add vertices 
    245                                 mesh->mVertices.push_back(mCurrentMesh->mVertices[index]); 
    246                                 vc.push_back(i); 
     301                                mCurrentMesh->mMaterial = mCurrentMaterial; 
    247302                        } 
    248  
    249                         mesh->mFaces.push_back(new Face(vc)); 
    250  
    251                         // NOTE: should rather be written into trafo of mesh instance 
    252                         ApplyTransformations(mTransformations, mesh); 
    253  
    254                         mesh->Preprocess(); 
    255                                  
    256                         // make an instance of this mesh 
    257                         MeshInstance *mi = new MeshInstance(mesh); 
    258                         mCurrentNode->mGeometry.push_back(mi); 
    259                 } 
    260  
    261                 // LEAK!! TODO: delete if not defd 
    262                 if (!mUsingMeshDefinition) 
    263                         delete mCurrentMesh; 
    264  
    265                 //mCurrentVertices.clear(); 
    266                 //mCurrentVertexIndices.clear(); 
    267         } 
    268         else 
    269         {  
    270                 if (!mCurrentMesh->mFaces.empty())  
    271                 { 
    272                         // should rather be written into the transformation 
    273                         // of a mesh instance 
    274                         ApplyTransformations(mTransformations, mCurrentMesh); 
    275  
    276                         mCurrentMesh->Preprocess(); 
    277                         // make an instance of this mesh 
    278                         MeshInstance *mi = new MeshInstance(mCurrentMesh); 
    279  
    280                         mCurrentNode->mGeometry.push_back(mi); 
    281                         // set the object id to a unique value 
    282                         //mi->SetId(mCurrentObjectId ++); 
    283                 }  
    284                 else  
    285                 { 
    286                         // empty mesh => discard 
    287                         cout<<"X"; 
    288  
    289                         delete mCurrentMesh; 
    290                 } 
    291  
     303                        else // add material to the instance 
     304                        { 
     305                                mi->SetMaterial(mCurrentMaterial); 
     306                        } 
     307                } 
     308 
     309                // create local mesh kd tree 
     310                mCurrentMesh->Preprocess(); 
     311                // add to scene graph 
     312                mCurrentNode->mGeometry.push_back(mi); 
     313         
     314                // reset current mesh 
    292315                mCurrentMesh = NULL; 
    293316        } 
     
    297320void X3dParseHandlers::StartIndexedFaceSet(AttributeList&  attributes) 
    298321{ 
    299   int len = attributes.getLength(); 
    300  
    301   int i; 
    302   VertexIndexContainer vertices; 
     322        //-- indexedfaceset corresponds  to Mesh in our implementation 
     323        const int len = attributes.getLength(); 
     324 
     325        VertexIndexContainer vertices; 
    303326   
    304327        mIsMeshDefinition = false; 
    305328        mUsingMeshDefinition = false; 
    306         for (i = 0; i < len; ++ i)  
     329 
     330        for (int i = 0; i < len; ++ i)  
    307331        { 
    308                 string attrName(StrX(attributes.getName(i)).LocalForm()); 
    309            
    310                 // this is a definition of a mesh 
    311                 if (attrName == "DEF") 
    312                 { 
     332                const string attrName(StrX(attributes.getName(i)).LocalForm()); 
     333         
     334                //-- we use an already defined mesh 
     335                if (attrName == "USE") 
     336                { 
     337                        StrX attrValue(attributes.getValue(i)); 
     338                        const char *meshName = attrValue.LocalForm(); 
     339 
     340                        mUsingMeshDefinition = true; 
     341 
     342            // retrieve mesh from mesh container 
     343                        const int meshIdx = mMeshDefinitions[meshName]; 
     344 
     345                        mCurrentMesh =  
     346                                MeshManager::GetSingleton()->FindEntry(meshIdx); 
     347 
     348                        //Debug << "retrieving mesh definition: " << mCurrentMeshName << endl; 
     349                        cout << "u"; 
     350                } 
     351                else if (attrName == "DEF") //-- a definition of a mesh 
     352                { 
     353                        const StrX attrValue(attributes.getValue(i)); 
     354                        const char *meshName = attrValue.LocalForm(); 
     355 
     356                        // this is only a definition, don't create actual  instance 
    313357                        mIsMeshDefinition = true; 
     358                         
     359                        //-- create new mesh definition 
     360                        mCurrentMesh = MeshManager::GetSingleton()->CreateResource(); 
     361                         
     362                        // store the mesh defination in a lookup table 
     363                        mMeshDefinitions[meshName] = mCurrentMesh->GetId(); 
     364                        cout << "d"; 
     365                } 
     366                 
     367                //-- read coordinate indices for current mesh 
     368                else if (attrName == "coordIndex")  
     369                { 
    314370                        StrX attrValue(attributes.getValue(i)); 
    315371                        const char *ptr = attrValue.LocalForm(); 
    316                         mCurrentMeshName = ptr; 
    317  
    318                         cout << "d"; 
    319                 } 
    320                  
    321                 // we use an already defined mesh 
    322                 if (attrName == "USE") 
    323                 { 
    324                         StrX attrValue(attributes.getValue(i)); 
    325  
    326                         // discard new mesh and assign defined mesh 
    327                         DEL_PTR(mCurrentMesh); 
    328                         const char *ptr = attrValue.LocalForm(); 
    329  
    330                         mCurrentMesh = mMeshDefinitions[ptr]; 
    331                         mUsingMeshDefinition = true; 
    332                         cout << "u"; 
    333                 } 
    334                  
    335                 if (attrName == "coordIndex")  
    336                 { 
    337                         StrX attrValue(attributes.getValue(i)); 
     372 
     373                        //-- immediate use: create a new mesh using a generic name 
     374                        if (!mCurrentMesh) 
     375                        { 
     376                                mCurrentMesh = MeshManager::GetSingleton()->CreateResource(); 
     377                        } 
     378 
    338379                        // handle coordIndex 
    339380                        vertices.clear(); 
    340                         const char *ptr = attrValue.LocalForm(); 
     381                         
    341382                   
    342383                        char *endptr; 
     
    372413 
    373414void 
    374 X3dParseHandlers::StartMaterial( 
    375                                 AttributeList&  attributes) 
    376 { 
    377   int len = attributes.getLength(); 
    378   int i; 
    379   if (!mCurrentMesh->mMaterial) 
    380     mCurrentMesh->mMaterial = new Material; 
    381   for (i=0; i < len; i++) { 
    382     string attrName(StrX(attributes.getName(i)).LocalForm()); 
    383     StrX attrValue(attributes.getValue(i)); 
    384     const char *ptr = attrValue.LocalForm(); 
    385     if (attrName == "diffuseColor") { 
    386       float r, g, b; 
    387       if (sscanf(ptr, "%f %f %f", &r, &g, &b) == 3) 
    388         mCurrentMesh->mMaterial->mDiffuseColor = RgbColor(r, g, b); 
    389     } 
    390   } 
    391 } 
    392  
    393 void 
    394 X3dParseHandlers::StartCoordinate( 
    395                                   AttributeList&  attributes) 
    396 { 
    397         int len = attributes.getLength(); 
     415X3dParseHandlers::StartMaterial(AttributeList&  attributes) 
     416{ 
     417        const int len = attributes.getLength(); 
     418                 
     419        mCurrentMaterial = MaterialManager::GetSingleton()->CreateResource(); 
     420   
     421        for (int i = 0; i < len; ++ i)  
     422        { 
     423                const string attrName(StrX(attributes.getName(i)).LocalForm()); 
     424                 
     425                const StrX attrValue(attributes.getValue(i)); 
     426                const char *ptr = attrValue.LocalForm(); 
     427 
     428 
     429                //-- we use an already defined material 
     430                if (attrName == "USE") 
     431                { 
     432                        //mUsingMaterialDefinition = true; 
     433                        string matName(ptr); 
     434 
     435            // retrieve mesh from mesh container 
     436                        const int matIdx = mMaterialDefinitions[matName]; 
     437 
     438                        mCurrentMaterial =  
     439                                MaterialManager::GetSingleton()->FindEntry(matIdx); 
     440 
     441                        //Debug << "retrieving mesh definition: " << mCurrentMeshName << endl; 
     442                        cout << "u"; 
     443                } 
     444                else if (attrName == "DEF") //-- a definition of a material 
     445                { 
     446                        //mIsMaterialDefinition = true; 
     447                        string matName(ptr); 
     448                 
     449                        //-- create new material definition 
     450                        mCurrentMaterial = MaterialManager::GetSingleton()->CreateResource(); 
     451                        // store the mesh defination in a lookup table 
     452                        mMaterialDefinitions[matName] = mCurrentMaterial->GetId(); 
     453                        cout << "d"; 
     454                } 
     455                // TODO: support not only diffuse material 
     456                else if (attrName == "diffuseColor")  
     457                { 
     458                        float r, g, b; 
     459       
     460                        if (sscanf(ptr, "%f %f %f", &r, &g, &b) == 3) 
     461                                mCurrentMaterial->mDiffuseColor = RgbColor(r, g, b); 
     462                } 
     463        } 
     464} 
     465 
     466 
     467void 
     468X3dParseHandlers::StartCoordinate(AttributeList&  attributes) 
     469{ 
     470        const int len = attributes.getLength(); 
    398471         
    399472        int i; 
     
    402475        for (i=0; i < len; i++)  
    403476        { 
    404                 string attrName(StrX(attributes.getName(i)).LocalForm()); 
     477                const string attrName(StrX(attributes.getName(i)).LocalForm()); 
    405478           
    406479                if (attrName == "point")  
     
    441514                                vertices.push_back(v); 
    442515                        } 
    443                          
     516         
     517                        // substitute vertices into current mesh 
    444518                        mCurrentMesh->mVertices = vertices; 
    445519                } 
     
    456530   
    457531  if (element == "IndexedFaceSet") { 
    458     // create a new mesh node in the scene graph 
    459     StartIndexedFaceSet(attributes); 
     532          // create a new mesh node in the scene graph 
     533          StartIndexedFaceSet(attributes); 
    460534  } 
    461535  
    462536  if (element == "Shape") { 
    463     cout << "+"; 
    464         mCurrentMesh = new Mesh; 
     537          cout << "+";  
     538 
     539          // reset current shape values 
     540          mCurrentMesh = NULL; 
     541          mCurrentMaterial = NULL; 
     542          mCurrentVertexIndices.clear(); 
     543          //mCurrentVertices.clear(); 
    465544  } 
    466545   
    467   if (element == "Coordinate")  
    468   { 
    469     if (mCurrentMesh) 
    470       StartCoordinate(attributes); 
     546  if (element == "Coordinate") { 
     547          StartCoordinate(attributes); 
    471548  } 
    472    
    473   if (element == "Material") { 
    474     StartMaterial(attributes); 
     549 
     550  // todo 
     551  if (0 && element == "Material") { 
     552          StartMaterial(attributes); 
    475553  } 
    476554   
     
    479557  } 
    480558 
    481   mElementCount++; 
     559  ++ mElementCount; 
    482560  mAttrCount += attributes.getLength(); 
    483561} 
     
    485563void 
    486564X3dParseHandlers::characters(const XMLCh* const chars, 
    487                             const unsigned int length) 
     565                                                        const unsigned int length) 
    488566{ 
    489567  mCharacterCount += length; 
     
    492570void 
    493571X3dParseHandlers::ignorableWhitespace(const XMLCh* const chars, 
    494                                       const unsigned int length) 
     572                                                                          const unsigned int length) 
    495573{ 
    496574  mSpaceCount += length; 
     
    507585 
    508586 
    509  
    510587// --------------------------------------------------------------------------- 
    511588//  StdInParseHandlers: Overrides of the SAX ErrorHandler interface 
    512589// --------------------------------------------------------------------------- 
     590 
     591 
    513592void 
    514593X3dParseHandlers::error(const SAXParseException& e) 
     
    600679        const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis(); 
    601680        parser->parse(filename.c_str()); 
     681         
    602682        const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis(); 
    603683        duration = endMillis - startMillis; 
     
    674754void X3dViewCellsParseHandlers::endElement(const XMLCh* const name) 
    675755{ 
    676   StrX lname(name); 
    677   string element(lname.LocalForm()); 
    678   
    679   if (element == "Shape") 
    680     EndShape(); 
     756        StrX lname(name); 
     757 
     758        string element(lname.LocalForm()); 
     759 
     760        if (element == "Shape") 
     761                EndShape(); 
    681762} 
    682763 
     
    694775        int i; 
    695776         
    696         // clear previous vertex indices 
    697         mCurrentVertexIndices.clear(); 
    698  
    699  
    700777        for (i=0; i < len; i++)  
    701778        { 
Note: See TracChangeset for help on using the changeset viewer.