Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

GLUpdaterGLGeometry3DVisitor.cpp

Go to the documentation of this file.
00001 #include "GLUpdaterGLGeometry3DVisitor.h"
00002 #include "GLBox.h"
00003 #include "BoxDrawArray.h"
00004 #include "GLCone.h"
00005 #include "ConeDrawArray.h"
00006 #include "GLCylinder.h"
00007 #include "CylinderDrawArray.h"
00008 #include "GLIndexedFaceSet.h"
00009 #include "GLSphere.h"
00010 #include "SphereDrawArray.h"
00011 #include "Box.h"
00012 #include "Cone.h"
00013 #include "Cylinder.h"
00014 #include "IndexedFaceSet.h"
00015 #include "Sphere.h"
00016 #include "Normal.h"
00017 #include "Coordinate.h"
00018 #include "TextureCoordinate.h"
00019 #include "Color.h"
00020 #include "ColorRGBA.h"
00021 
00022 #include <vector>
00023 #include <list>
00024 
00025 using namespace X3DTK;
00026 using namespace std;
00027 
00028 GLUpdaterGLGeometry3DVisitor::GLUpdaterGLGeometry3DVisitor()
00029 : GLGeometry3DVisitor()
00030 {
00031   // Enter functions.
00032   defineNewEnterFunction<GLUpdaterGLGeometry3DVisitor, GLBox>(&GLUpdaterGLGeometry3DVisitor::enterGLBox);
00033   defineNewEnterFunction<GLUpdaterGLGeometry3DVisitor, GLCone>(&GLUpdaterGLGeometry3DVisitor::enterGLCone);
00034   defineNewEnterFunction<GLUpdaterGLGeometry3DVisitor, GLCylinder>(&GLUpdaterGLGeometry3DVisitor::enterGLCylinder);
00035   defineNewEnterFunction<GLUpdaterGLGeometry3DVisitor, GLIndexedFaceSet>(&GLUpdaterGLGeometry3DVisitor::enterGLIndexedFaceSet);
00036   defineNewEnterFunction<GLUpdaterGLGeometry3DVisitor, GLSphere>(&GLUpdaterGLGeometry3DVisitor::enterGLSphere);
00037 
00038 
00039   // GlobalVariables assignation.
00040   globalVariables = GVManager::getInstanceOf<GLUpdaterGlobalVariables>();
00041 }
00042 
00043 GLUpdaterGLGeometry3DVisitor::~GLUpdaterGLGeometry3DVisitor()
00044 {
00045 }
00046 
00047 void GLUpdaterGLGeometry3DVisitor::enterGLBox(GLBox *G) const
00048 {
00049   Box *B = static_cast<Box *>(G->getLink());
00050 
00051   G->getSize() = B->getSize();
00052   G->getBoxDrawArray() = BoxDrawArray::getInstance();
00053 }
00054 
00055 void GLUpdaterGLGeometry3DVisitor::enterGLCone(GLCone *G) const
00056 {
00057   Cone *C = static_cast<Cone *>(G->getLink());
00058 
00059   G->getBottomRadius() = C->getBottomRadius();
00060   G->getHeight() = C->getHeight();
00061   G->getSide() = C->getSide();
00062   G->getBottom() = C->getBottom();
00063   G->getConeDrawArray() = ConeDrawArray::getInstanceOfSection(64);
00064 }
00065 
00066 void GLUpdaterGLGeometry3DVisitor::enterGLCylinder(GLCylinder *G) const
00067 {
00068   Cylinder *C = static_cast<Cylinder *>(G->getLink());
00069   
00070   G->getBottom() = C->getBottom();
00071   G->getRadius() = C->getRadius();
00072   G->getHeight() = C->getHeight();
00073   G->getSide() = C->getSide();
00074   G->getTop() = C->getTop();
00075   G->getCylinderDrawArray() = CylinderDrawArray::getInstanceOfSection(64);
00076 }
00077 
00078 void GLUpdaterGLGeometry3DVisitor::enterGLSphere(GLSphere *G) const
00079 {
00080   Sphere *S = static_cast<Sphere *>(G->getLink());
00081 
00082   G->getRadius() = S->getRadius();
00083   G->getSphereDrawArray() = SphereDrawArray::getInstanceOfSection(64);  
00084 }
00085 
00086 void GLUpdaterGLGeometry3DVisitor::enterGLIndexedFaceSet(GLIndexedFaceSet *G) const
00087 {
00088   IndexedFaceSet *I = static_cast<IndexedFaceSet *>(G->getLink());  
00089 
00090   //for the moment if the node color has changed, all the attributes are computed
00091   //emptying the arrays
00092   G->getN3F_V3FvertexFaceArray().clear();
00093   G->getC4F_N3F_V3FvertexFaceArray().clear(); 
00094   G->getT2F_N3F_V3FvertexFaceArray().clear();
00095   G->getT2F_C4F_N3F_V3FvertexFaceArray().clear(); 
00096   G->getIndexFaceArray().clear();
00097 
00098   MFVec3f vertexArray;
00099   MFVec3f normalArray;
00100   MFInt32 coordIndex;
00101 
00102   Normal *NormalNode = static_cast<Normal *>(I->getNormal());
00103 
00104   if (NormalNode != 0)
00105     normalArray = NormalNode->getVector();
00106 
00107   //IndexedFaceSet attributes  
00108   if (I->getCoordIndex().empty())
00109     return;
00110  
00111   G->getSolid() = I->getSolid();
00112   G->getCcw() = I->getCcw();
00113 
00114   computeArrays(G, vertexArray, normalArray, coordIndex); 
00115 
00116   if ((I->getColor() != 0) && (I->getTexCoord() != 0))
00117   {
00118     G->getColor() = true;
00119     G->getTexCoord() = true;
00120     computeColor_TexCoord(G, vertexArray, normalArray, coordIndex);
00121   }
00122   if ((I->getColor() != 0) && (I->getTexCoord() == 0))
00123   {
00124     G->getColor() = true;
00125     G->getTexCoord() = false;
00126     computeColor_NoTexCoord(G, vertexArray, normalArray, coordIndex);
00127   }
00128   if ((I->getColor() == 0) && (I->getTexCoord() != 0))
00129   {
00130     G->getColor() = false;
00131     G->getTexCoord() = true;
00132     computeNoColor_TexCoord(G, vertexArray, normalArray, coordIndex);
00133   }
00134   if ((I->getColor() == 0) && (I->getTexCoord() == 0))
00135   {
00136     G->getColor() = false;
00137     G->getTexCoord() = false;
00138     computeNoColor_NoTexCoord(G, vertexArray, normalArray, coordIndex);
00139   }
00140 }
00141 
00142 void GLUpdaterGLGeometry3DVisitor::computeNoColor_NoTexCoord(GLIndexedFaceSet *G, const MFVec3f &vertexArray, const MFVec3f &normalArray, const MFInt32 &coordIndex) const
00143 {
00144   //cout << "IndexedFaceSet: no color, no texCoord" << endl;
00145 
00146   IndexedFaceSet *I = static_cast<IndexedFaceSet *>(G->getLink());
00147 
00148   Coordinate *CoordNode = static_cast<Coordinate *>(I->getCoord());
00149   if (CoordNode == 0)
00150     return;
00151 
00152   SFBool normalPerVertex = I->getNormalPerVertex();
00153   MFInt32 colorIndex = I->getColorIndex(); 
00154   MFInt32 normalIndex = I->getNormalIndex(); 
00155   
00156   //using the GL_N3F_V3F
00157   //vertex array
00158   if ((normalPerVertex) 
00159    && (normalIndex.empty()))
00160   {
00161     //cout << "vertices not duplicated" << endl;
00162     
00163     G->getVerticesDuplicated() = false;
00164     //using the GL_POLYGON 
00165     //filling the vertex array
00166     MFVec3f::const_iterator itNormal = normalArray.begin();
00167     
00168     G->getN3F_V3FvertexFaceArray().resize(vertexArray.size());
00169     vector<N3F_V3F>::iterator itElement = G->getN3F_V3FvertexFaceArray().begin();
00170       
00171     for (MFVec3f::const_iterator itCoord = vertexArray.begin(); itCoord != vertexArray.end(); ++itCoord)
00172     {
00173       (*itElement).normal = *itNormal; 
00174       (*itElement).vertex = *itCoord;
00175       ++itNormal;
00176       ++itElement;
00177     }
00178       
00179     //filling the index array
00180     //iterate the coordIndex
00181     G->getIndexFaceArray().reserve(3*coordIndex.size()/4);
00182     MFInt32::const_iterator itCoordIndex = coordIndex.begin();
00183     while (itCoordIndex != coordIndex.end())    
00184     {
00185       unsigned int first, a, b;
00186       
00187       first = *itCoordIndex;
00188       ++itCoordIndex;
00189       a = *itCoordIndex;
00190       ++itCoordIndex;
00191       
00192       while (*itCoordIndex != -1)
00193       {
00194         G->getIndexFaceArray().push_back(first);
00195         G->getIndexFaceArray().push_back(a);
00196         b = *itCoordIndex;
00197         ++itCoordIndex;
00198         G->getIndexFaceArray().push_back(b);
00199         a = b;
00200       }  
00201       ++itCoordIndex;
00202     }
00203   }
00204   else 
00205   {
00206     //cout << "vertices duplicated" << endl;
00207   
00208     G->getVerticesDuplicated() = true;
00209     //we cannot use the index array because an attribute is not constant per
00210     //instantiation of vertex
00211     //using the GL_POLYGON 
00212     //filling the arrays
00213     G->getN3F_V3FvertexFaceArray().reserve(3*coordIndex.size()/4);
00214         
00215     MFInt32::const_iterator itCoordIndex = coordIndex.begin();
00216     MFInt32::const_iterator itNormalIndex;
00217     
00218     if (normalIndex.empty())
00219       itNormalIndex = coordIndex.begin();
00220     else
00221       itNormalIndex = normalIndex.begin();  
00222     
00223     while (itCoordIndex != coordIndex.end())    
00224     {
00225       N3F_V3F first, a, b;
00226       
00227       //computing first
00228       first.normal = normalArray[*itNormalIndex];
00229       first.vertex = vertexArray[*itCoordIndex];
00230            
00231       if (normalPerVertex)
00232         ++itNormalIndex;      
00233       ++itCoordIndex;
00234       
00235       //computing a
00236       a.normal = normalArray[*itNormalIndex];
00237       a.vertex = vertexArray[*itCoordIndex];
00238            
00239       if (normalPerVertex)
00240         ++itNormalIndex;      
00241       ++itCoordIndex;
00242       
00243       while (*itCoordIndex != -1)
00244       {
00245         //pushing back first, a     
00246         G->getN3F_V3FvertexFaceArray().push_back(first);
00247         G->getN3F_V3FvertexFaceArray().push_back(a);
00248         
00249         //computing b
00250         b.normal = normalArray[*itNormalIndex];
00251         b.vertex = vertexArray[*itCoordIndex];
00252            
00253         if (normalPerVertex)
00254           ++itNormalIndex;      
00255         ++itCoordIndex;
00256         
00257         //pushing back b 
00258         G->getN3F_V3FvertexFaceArray().push_back(b);
00259         
00260         //assigning b to a
00261         a.normal = b.normal;
00262         a.vertex = b.vertex;
00263       }
00264       ++itCoordIndex;
00265       ++itNormalIndex;
00266     }
00267   }
00268 }
00269 
00270 void GLUpdaterGLGeometry3DVisitor::computeColor_NoTexCoord(GLIndexedFaceSet *G, const MFVec3f &vertexArray, const MFVec3f &normalArray, const MFInt32 &coordIndex) const
00271 {
00272   //cout << "IndexedFaceSet: color, no texCoord" << endl;
00273 
00274   IndexedFaceSet *I = static_cast<IndexedFaceSet *>(G->getLink());
00275 
00276   MFColorRGBA colorRGBA;
00277   if (I->getColor()->getTypeName() == "ColorRGBA")
00278     colorRGBA = static_cast<ColorRGBA *>(I->getColor())->getColor();
00279   else  
00280     colorRGBA = static_cast<Color *>(I->getColor())->getColor();
00281   
00282   Coordinate *CoordNode = static_cast<Coordinate *>(I->getCoord());
00283   if (CoordNode == 0)
00284     return;
00285   
00286   SFBool normalPerVertex = I->getNormalPerVertex();
00287   SFBool colorPerVertex = I->getColorPerVertex();
00288   MFInt32 colorIndex = I->getColorIndex(); 
00289   MFInt32 normalIndex = I->getNormalIndex(); 
00290   
00291   //using the GL_C4F_N3F_V3F
00292   //vertex array
00293   if ((normalPerVertex)
00294    && (colorPerVertex) 
00295    && (colorIndex.empty()) 
00296    && (normalIndex.empty()))
00297   {
00298     //cout << "vertices not duplicated" << endl;
00299 
00300     G->getVerticesDuplicated() = false;
00301     //using the GL_POLYGON 
00302     //filling the vertex array
00303     MFColorRGBA::const_iterator itColor = colorRGBA.begin();
00304     MFVec3f::const_iterator itNormal = normalArray.begin();
00305     
00306     G->getC4F_N3F_V3FvertexFaceArray().resize(vertexArray.size());
00307     vector<C4F_N3F_V3F>::iterator itElement = G->getC4F_N3F_V3FvertexFaceArray().begin();
00308         
00309     for (MFVec3f::const_iterator itCoord = vertexArray.begin(); itCoord != vertexArray.end(); ++itCoord)
00310     {
00311       (*itElement).color = *itColor;
00312       (*itElement).normal = *itNormal; 
00313       (*itElement).vertex = *itCoord;
00314       ++itColor;
00315       ++itNormal;
00316       ++itElement;
00317     }
00318     //filling the index array
00319     //iterate the coordIndex
00320     G->getIndexFaceArray().reserve(3*coordIndex.size()/4);
00321     MFInt32::const_iterator itCoordIndex = coordIndex.begin();
00322     while (itCoordIndex != coordIndex.end())    
00323     {
00324       unsigned int first, a, b;
00325       
00326       first = *itCoordIndex;
00327       ++itCoordIndex;
00328       a = *itCoordIndex;
00329       ++itCoordIndex;
00330       
00331       while (*itCoordIndex != -1)
00332       {
00333         G->getIndexFaceArray().push_back(first);
00334         G->getIndexFaceArray().push_back(a);
00335         b = *itCoordIndex;
00336         ++itCoordIndex;
00337         G->getIndexFaceArray().push_back(b);
00338         a = b;
00339       }  
00340       ++itCoordIndex;
00341     }
00342   }
00343   else //normalPerVertex or colorPertex is FALSE or ...
00344   {
00345     //cout << "vertices duplicated" << endl;
00346 
00347     G->getVerticesDuplicated() = true;
00348     //we cannot use the index array because an attribute is not constant per
00349     //instantiation of vertex
00350     //using the GL_POLYGON 
00351     //filling the vertex array
00352     //filling the arrays
00353     G->getC4F_N3F_V3FvertexFaceArray().reserve(3*coordIndex.size()/4);
00354         
00355     MFInt32::const_iterator itCoordIndex = coordIndex.begin();
00356     MFInt32::const_iterator itColorIndex;
00357     MFInt32::const_iterator itNormalIndex;
00358     
00359     if (colorIndex.empty())
00360       itColorIndex = coordIndex.begin();
00361     else
00362       itColorIndex = colorIndex.begin();  
00363     
00364     if (normalIndex.empty())
00365       itNormalIndex = coordIndex.begin();
00366     else
00367       itNormalIndex = normalIndex.begin();  
00368     
00369     while (itCoordIndex != coordIndex.end())    
00370     {
00371       C4F_N3F_V3F first, a, b;
00372       
00373       //computing first
00374       first.color = colorRGBA[*itColorIndex];
00375       first.normal = normalArray[*itNormalIndex];
00376       first.vertex = vertexArray[*itCoordIndex];
00377       
00378       if (colorPerVertex)
00379         ++itColorIndex;           
00380       if (normalPerVertex)
00381         ++itNormalIndex;      
00382       ++itCoordIndex;
00383       
00384       //computing a
00385       a.color = colorRGBA[*itColorIndex];
00386       a.normal = normalArray[*itNormalIndex];
00387       a.vertex = vertexArray[*itCoordIndex];
00388       
00389       if (colorPerVertex)
00390         ++itColorIndex;     
00391       if (normalPerVertex)
00392         ++itNormalIndex;      
00393       ++itCoordIndex;
00394       
00395       while (*itCoordIndex != -1)
00396       {
00397         //pushing back first, a     
00398         G->getC4F_N3F_V3FvertexFaceArray().push_back(first);
00399         G->getC4F_N3F_V3FvertexFaceArray().push_back(a);
00400         
00401         //computing b
00402         b.color = colorRGBA[*itColorIndex];
00403         b.normal = normalArray[*itNormalIndex];
00404         b.vertex = vertexArray[*itCoordIndex];
00405         
00406         if (colorPerVertex)
00407           ++itColorIndex;   
00408         if (normalPerVertex)
00409           ++itNormalIndex;      
00410         ++itCoordIndex;
00411         
00412         //pushing back b 
00413         G->getC4F_N3F_V3FvertexFaceArray().push_back(b);
00414         
00415         //assigning b to a
00416         a.color = b.color;
00417         a.normal = b.normal;
00418         a.vertex = b.vertex;
00419       }
00420       if (!colorPerVertex)
00421           ++itColorIndex;  
00422       ++itCoordIndex;
00423       ++itNormalIndex;
00424     }
00425   }
00426 }
00427 
00428 void GLUpdaterGLGeometry3DVisitor::computeNoColor_TexCoord(GLIndexedFaceSet *G, const MFVec3f &vertexArray, const MFVec3f &normalArray, const MFInt32 &coordIndex) const
00429 {
00430   //cout << "IndexedFaceSet: no color, texCoord" << endl;
00431 
00432   IndexedFaceSet *I = static_cast<IndexedFaceSet *>(G->getLink());
00433 
00434   Coordinate *CoordNode = static_cast<Coordinate *>(I->getCoord());
00435   if (CoordNode == 0)
00436     return;
00437   
00438   TextureCoordinate *TexCoordNode = static_cast<TextureCoordinate *>(I->getTexCoord());
00439   if (TexCoordNode == 0)
00440     return;
00441   MFVec2f texCoordArray = TexCoordNode->getPoint();
00442  
00443   SFBool normalPerVertex = I->getNormalPerVertex();
00444   MFInt32 colorIndex = I->getColorIndex(); 
00445   MFInt32 normalIndex = I->getNormalIndex(); 
00446   MFInt32 texCoordIndex = I->getTexCoordIndex(); 
00447   
00448   //using the GL_T2F_N3F_V3F
00449   //vertex array
00450   if ((normalPerVertex) 
00451    && (normalIndex.empty())
00452    && (texCoordIndex.empty()))
00453   {
00454     //cout << "vertices not duplicated" << endl;
00455   
00456     G->getVerticesDuplicated() = false;
00457     //using the GL_POLYGON 
00458     //filling the vertex array
00459     MFVec2f::const_iterator itTexCoord = texCoordArray.begin();
00460     MFVec3f::const_iterator itNormal = normalArray.begin();
00461         
00462     G->getT2F_N3F_V3FvertexFaceArray().resize(vertexArray.size());
00463     vector<T2F_N3F_V3F>::iterator itElement = G->getT2F_N3F_V3FvertexFaceArray().begin();
00464       
00465     for (MFVec3f::const_iterator itCoord = vertexArray.begin(); itCoord != vertexArray.end(); ++itCoord)
00466     {
00467       (*itElement).texCoord = *itTexCoord;
00468       (*itElement).normal = *itNormal; 
00469       (*itElement).vertex = *itCoord;
00470       ++itTexCoord;
00471       ++itNormal;
00472       ++itElement;
00473     }
00474       
00475     //filling the index array
00476     //iterate the coordIndex
00477     G->getIndexFaceArray().reserve(3*coordIndex.size()/4);
00478     MFInt32::const_iterator itCoordIndex = coordIndex.begin();
00479     while (itCoordIndex != coordIndex.end())    
00480     {
00481       unsigned int first, a, b;
00482       
00483       first = *itCoordIndex;
00484       ++itCoordIndex;
00485       a = *itCoordIndex;
00486       ++itCoordIndex;
00487       
00488       while (*itCoordIndex != -1)
00489       {
00490         G->getIndexFaceArray().push_back(first);
00491         G->getIndexFaceArray().push_back(a);
00492         b = *itCoordIndex;
00493         ++itCoordIndex;
00494         G->getIndexFaceArray().push_back(b);
00495         a = b;
00496       }  
00497       ++itCoordIndex;
00498     }
00499   }
00500   else //normalPerVertex is FALSE or ...
00501   {
00502     //cout << "vertices duplicated" << endl;
00503   
00504     G->getVerticesDuplicated() = true;
00505     //we cannot use the index array because an attribute is not constant per
00506     //instantiation of vertex
00507     //using the GL_POLYGON 
00508     //filling the arrays
00509     G->getT2F_N3F_V3FvertexFaceArray().reserve(3*coordIndex.size()/4);
00510         
00511     MFInt32::const_iterator itCoordIndex = coordIndex.begin();
00512     MFInt32::const_iterator itNormalIndex;
00513     MFInt32::const_iterator itTexCoordIndex;
00514     
00515     if (normalIndex.empty())
00516       itNormalIndex = coordIndex.begin();
00517     else
00518       itNormalIndex = normalIndex.begin();  
00519 
00520     if (texCoordIndex.empty())
00521       itTexCoordIndex = coordIndex.begin();
00522     else
00523       itTexCoordIndex = texCoordIndex.begin();  
00524 
00525     
00526     while (itCoordIndex != coordIndex.end())    
00527     {
00528       T2F_N3F_V3F first, a, b;
00529       
00530       //computing first
00531       first.normal = normalArray[*itNormalIndex];
00532       first.vertex = vertexArray[*itCoordIndex];
00533       first.texCoord = texCoordArray[*itTexCoordIndex];
00534       
00535       if (normalPerVertex)
00536         ++itNormalIndex;      
00537       ++itCoordIndex;
00538       ++itTexCoordIndex;
00539       
00540       //computing a
00541       a.normal = normalArray[*itNormalIndex];
00542       a.vertex = vertexArray[*itCoordIndex];
00543       a.texCoord = texCoordArray[*itTexCoordIndex];
00544       
00545       if (normalPerVertex)
00546         ++itNormalIndex;      
00547       ++itCoordIndex;
00548       ++itTexCoordIndex;
00549       
00550       while (*itCoordIndex != -1)
00551       {
00552         //pushing back first, a     
00553         G->getT2F_N3F_V3FvertexFaceArray().push_back(first);
00554         G->getT2F_N3F_V3FvertexFaceArray().push_back(a);
00555         
00556         //computing b
00557         b.normal = normalArray[*itNormalIndex];
00558         b.vertex = vertexArray[*itCoordIndex];
00559         b.texCoord = texCoordArray[*itTexCoordIndex];
00560               
00561         if (normalPerVertex)
00562           ++itNormalIndex;      
00563         ++itCoordIndex;
00564         ++itTexCoordIndex;
00565         
00566         //pushing back b 
00567         G->getT2F_N3F_V3FvertexFaceArray().push_back(b);
00568         
00569         //assigning b to a
00570         a.normal = b.normal;
00571         a.vertex = b.vertex;
00572         a.texCoord = b.texCoord;
00573       }
00574       ++itCoordIndex;
00575       ++itNormalIndex;
00576       ++itTexCoordIndex;
00577     }
00578   }
00579 }
00580 
00581 void GLUpdaterGLGeometry3DVisitor::computeColor_TexCoord(GLIndexedFaceSet *G, const MFVec3f &vertexArray, const MFVec3f &normalArray, const MFInt32 &coordIndex) const
00582 {
00583   //cout << "IndexedFaceSet: color, texCoord" << endl;
00584   IndexedFaceSet *I = static_cast<IndexedFaceSet *>(G->getLink());
00585 
00586   MFColorRGBA colorRGBA;
00587   if (I->getColor()->getTypeName() == "ColorRGBA")
00588     colorRGBA = static_cast<ColorRGBA *>(I->getColor())->getColor();
00589   else  
00590     colorRGBA = static_cast<Color *>(I->getColor())->getColor();
00591   
00592   Coordinate *CoordNode = static_cast<Coordinate *>(I->getCoord());
00593   if (CoordNode == 0)
00594     return;
00595   
00596   TextureCoordinate *TexCoordNode = static_cast<TextureCoordinate *>(I->getTexCoord());
00597   if (TexCoordNode == 0)
00598     return;
00599   MFVec2f texCoordArray = TexCoordNode->getPoint();
00600   
00601   SFBool normalPerVertex = I->getNormalPerVertex();
00602   SFBool colorPerVertex = I->getColorPerVertex();
00603   MFInt32 colorIndex = I->getColorIndex(); 
00604   MFInt32 normalIndex = I->getNormalIndex(); 
00605   MFInt32 texCoordIndex = I->getCoordIndex(); 
00606   
00607   //using the GL_T2F_C4F_N3F_V3F
00608   //vertex array
00609   if ((normalPerVertex)
00610    && (colorPerVertex) 
00611    && (colorIndex.empty()) 
00612    && (normalIndex.empty())
00613    && (texCoordIndex.empty()))
00614   {
00615     //cout << "vertices not duplicated" << endl;
00616 
00617     G->getVerticesDuplicated() = false;
00618     //using the GL_POLYGON 
00619     //filling the vertex array
00620     MFColorRGBA::const_iterator itColor = colorRGBA.begin();
00621     MFVec3f::const_iterator itNormal = normalArray.begin();
00622     MFVec2f::const_iterator itTexCoord = texCoordArray.begin();
00623     
00624     G->getT2F_C4F_N3F_V3FvertexFaceArray().resize(vertexArray.size());
00625     vector<T2F_C4F_N3F_V3F>::iterator itElement = G->getT2F_C4F_N3F_V3FvertexFaceArray().begin();
00626         
00627     for (MFVec3f::const_iterator itCoord = vertexArray.begin(); itCoord != vertexArray.end(); ++itCoord)
00628     {
00629       (*itElement).color = *itColor;
00630       (*itElement).normal = *itNormal; 
00631       (*itElement).vertex = *itCoord;
00632       (*itElement).texCoord = *itTexCoord;
00633       ++itColor;
00634       ++itNormal;
00635       ++itTexCoord;
00636       ++itElement;
00637     }
00638     //filling the index array
00639     //iterate the coordIndex
00640     G->getIndexFaceArray().reserve(3*coordIndex.size()/4);
00641     MFInt32::const_iterator itCoordIndex = coordIndex.begin();
00642     while (itCoordIndex != coordIndex.end())    
00643     {
00644       unsigned int first, a, b;
00645       
00646       first = *itCoordIndex;
00647       ++itCoordIndex;
00648       a = *itCoordIndex;
00649       ++itCoordIndex;
00650       
00651       while (*itCoordIndex != -1)
00652       {
00653         G->getIndexFaceArray().push_back(first);
00654         G->getIndexFaceArray().push_back(a);
00655         b = *itCoordIndex;
00656         ++itCoordIndex;
00657         G->getIndexFaceArray().push_back(b);
00658         a = b;
00659       }  
00660       ++itCoordIndex;
00661     }
00662   }
00663   else //normalPerVertex or colorPertex is FALSE or ...
00664   {
00665 
00666     //cout << "vertices duplicated" << endl;
00667 
00668     G->getVerticesDuplicated() = true;
00669     //we cannot use the index array because an attribute is not constant per
00670     //instantiation of vertex
00671     //using the GL_POLYGON 
00672     //filling the vertex array
00673     //filling the arrays
00674     G->getT2F_C4F_N3F_V3FvertexFaceArray().reserve(3*coordIndex.size()/4);
00675         
00676     MFInt32::const_iterator itCoordIndex = coordIndex.begin();
00677     MFInt32::const_iterator itColorIndex;
00678     MFInt32::const_iterator itNormalIndex;
00679     MFInt32::const_iterator itTexCoordIndex;
00680     
00681     if (colorIndex.empty())
00682       itColorIndex = coordIndex.begin();
00683     else
00684       itColorIndex = colorIndex.begin();  
00685     
00686     if (normalIndex.empty())
00687       itNormalIndex = coordIndex.begin();
00688     else
00689       itNormalIndex = normalIndex.begin();  
00690     
00691     if (texCoordIndex.empty())
00692       itTexCoordIndex = coordIndex.begin();
00693     else
00694       itTexCoordIndex = texCoordIndex.begin(); 
00695     
00696     while (itCoordIndex != coordIndex.end())    
00697     {
00698       T2F_C4F_N3F_V3F first, a, b;
00699       
00700       //computing first
00701       first.color = colorRGBA[*itColorIndex];
00702       first.normal = normalArray[*itNormalIndex];
00703       first.vertex = vertexArray[*itCoordIndex];
00704       first.texCoord = texCoordArray[*itTexCoordIndex];
00705       
00706       if (colorPerVertex)
00707         ++itColorIndex;           
00708       if (normalPerVertex)
00709         ++itNormalIndex;      
00710       ++itCoordIndex;
00711       ++itTexCoordIndex;
00712       
00713       //computing a
00714       a.color = colorRGBA[*itColorIndex];
00715       a.normal = normalArray[*itNormalIndex];
00716       a.vertex = vertexArray[*itCoordIndex];
00717       a.texCoord = texCoordArray[*itTexCoordIndex];
00718       
00719       if (colorPerVertex)
00720         ++itColorIndex;     
00721       if (normalPerVertex)
00722         ++itNormalIndex;      
00723       ++itCoordIndex;
00724       ++itTexCoordIndex;
00725       
00726       while (*itCoordIndex != -1)
00727       {
00728         //pushing back first, a     
00729         G->getT2F_C4F_N3F_V3FvertexFaceArray().push_back(first);
00730         G->getT2F_C4F_N3F_V3FvertexFaceArray().push_back(a);
00731         
00732         //computing b
00733         b.color = colorRGBA[*itColorIndex];
00734         b.normal = normalArray[*itNormalIndex];
00735         b.vertex = vertexArray[*itCoordIndex];
00736         b.texCoord = texCoordArray[*itTexCoordIndex];
00737         
00738         if (colorPerVertex)
00739           ++itColorIndex;   
00740         if (normalPerVertex)
00741           ++itNormalIndex;      
00742         ++itCoordIndex;
00743         ++itTexCoordIndex;
00744         
00745         //pushing back b 
00746         G->getT2F_C4F_N3F_V3FvertexFaceArray().push_back(b);
00747         
00748         //assigning b to a
00749         a.color = b.color;
00750         a.normal = b.normal;
00751         a.vertex = b.vertex;
00752         a.texCoord = b.texCoord;
00753       }
00754       if (!colorPerVertex)
00755         ++itColorIndex;  
00756       ++itCoordIndex;
00757       ++itNormalIndex;
00758       ++itTexCoordIndex;
00759     }
00760   }
00761 }
00762 
00763 void GLUpdaterGLGeometry3DVisitor::computeArrays(GLIndexedFaceSet *G, MFVec3f &vertexArray, MFVec3f &normalArray, MFInt32 &coordIndex) const
00764 {
00765   IndexedFaceSet *I = static_cast<IndexedFaceSet *>(G->getLink());
00766   
00767   Coordinate *CoordNode = static_cast<Coordinate *>(I->getCoord());
00768   if (CoordNode == 0)
00769     return;
00770 
00771   if (!normalArray.empty())
00772   {
00773     //no process is necessary
00774     vertexArray = CoordNode->getPoint();
00775     coordIndex = I->getCoordIndex();
00776     return;
00777   }
00778   
00779   coordIndex = I->getCoordIndex();
00780   vertexArray = CoordNode->getPoint();
00781     
00782   SFBool normalPerVertex = I->getNormalPerVertex();
00783   coordIndex = I->getCoordIndex();
00784   
00785   //computing the array containing the normals of each face
00786   MFVec3f normalFaceArray;
00787   MFInt32::const_iterator itCoordIndex = coordIndex.begin();
00788   
00789   while (itCoordIndex != coordIndex.end())
00790   {
00791     SFVec3f A = vertexArray[*itCoordIndex];
00792     ++itCoordIndex;
00793     SFVec3f B = vertexArray[*itCoordIndex];
00794     ++itCoordIndex;
00795     SFVec3f C = vertexArray[*itCoordIndex];
00796     ++itCoordIndex;
00797 
00798     //computing the normal following ccw
00799     SFVec3f AB = (B - A).normalize();
00800     SFVec3f AC = (C - A).normalize();
00801     SFVec3f N;
00802 
00803     if (G->getCcw())
00804       N = crossprod(AB, AC);
00805     else
00806       N = crossprod(AC, AB);  
00807 
00808     normalFaceArray.push_back(N);
00809     ++itCoordIndex;//the pointed value is -1
00810   }
00811 
00812   if (normalPerVertex)
00813   {
00814     //for the moment crease angle is not used
00815     normalArray = MFVec3f(vertexArray.size());
00816     
00817     //iterate the faces
00818     itCoordIndex = coordIndex.begin();
00819     MFVec3f::const_iterator itNormal = normalFaceArray.begin();
00820     
00821     while (itCoordIndex != coordIndex.end())
00822     {
00823       normalArray[*itCoordIndex] = normalArray[*itCoordIndex] + *itNormal;
00824       ++itCoordIndex;
00825       normalArray[*itCoordIndex] = normalArray[*itCoordIndex] + *itNormal;
00826       ++itCoordIndex;
00827       normalArray[*itCoordIndex] = normalArray[*itCoordIndex] + *itNormal;
00828       ++itCoordIndex;
00829       ++itCoordIndex;
00830       ++itNormal;
00831     }
00832 
00833     //then normalize
00834     for (MFVec3f::iterator itNormal = normalArray.begin(); itNormal != normalArray.end(); ++itNormal)
00835     {
00836       (*itNormal).normalize();
00837     }
00838   }
00839   else
00840   {
00841     normalArray = MFVec3f();
00842     //copying the values of normalFaceArray
00843     for (MFVec3f::const_iterator itFace = normalFaceArray.begin(); itFace != normalFaceArray.end(); ++itFace)
00844       normalArray.push_back(*itFace);
00845   }
00846 }
00847 

Generated on Wed May 14 10:03:09 2003 for X3DToolKit by doxygen1.3