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

GLUpdaterGLRenderingVisitor.cpp

Go to the documentation of this file.
00001 #include "GLUpdaterGLRenderingVisitor.h"
00002 #include "GLIndexedLineSet.h"
00003 #include "GLPointSet.h"
00004 #include "GLTriangleFanSet.h"
00005 #include "GLTriangleSet.h"
00006 #include "GLTriangleStripSet.h"
00007 
00008 #include "RenderingNodes.h"
00009 
00010 #include <vector>
00011 
00012 using namespace X3DTK;
00013 using namespace std;
00014 
00015 GLUpdaterGLRenderingVisitor::GLUpdaterGLRenderingVisitor()
00016 : GLRenderingVisitor()
00017 {
00018   // Enter functions.
00019   defineNewEnterFunction<GLUpdaterGLRenderingVisitor, GLIndexedLineSet>(&GLUpdaterGLRenderingVisitor::enterGLIndexedLineSet);
00020   defineNewEnterFunction<GLUpdaterGLRenderingVisitor, GLPointSet>(&GLUpdaterGLRenderingVisitor::enterGLPointSet);
00021   defineNewEnterFunction<GLUpdaterGLRenderingVisitor, GLTriangleFanSet>(&GLUpdaterGLRenderingVisitor::enterGLTriangleFanSet);
00022   defineNewEnterFunction<GLUpdaterGLRenderingVisitor, GLTriangleSet>(&GLUpdaterGLRenderingVisitor::enterGLTriangleSet);
00023   defineNewEnterFunction<GLUpdaterGLRenderingVisitor, GLTriangleStripSet>(&GLUpdaterGLRenderingVisitor::enterGLTriangleStripSet);
00024 
00025 
00026   // GlobalVariables assignation.
00027   globalVariables = GVManager::getInstanceOf<GLUpdaterGlobalVariables>();
00028 }
00029 
00030 GLUpdaterGLRenderingVisitor::~GLUpdaterGLRenderingVisitor()
00031 {
00032 }
00033 
00034 void GLUpdaterGLRenderingVisitor::enterGLIndexedLineSet(GLIndexedLineSet *G) const
00035 {
00036   IndexedLineSet *I = static_cast<IndexedLineSet *>(G->getLink());
00037   
00038   //emptying the arrays
00039   G->getPointArray() = MFVec3f();
00040   for (vector<vector<C4UB_V3F> *>::const_iterator itLineArray = G->getLineArrayArray().begin(); itLineArray != G->getLineArrayArray().end(); ++itLineArray)
00041     delete (*itLineArray);
00042   G->getLineArrayArray() = vector<vector<C4UB_V3F> *>();  
00043   
00044   for (vector<MFInt32 *>::const_iterator itLineIndex = G->getLineIndexArray().begin(); itLineIndex != G->getLineIndexArray().end(); ++itLineIndex)
00045     delete (*itLineIndex);
00046   G->getLineIndexArray() = vector<MFInt32 *>();
00047 
00048   Coordinate *CoordNode = static_cast<Coordinate *>(I->getCoord());
00049   if (CoordNode == 0)
00050     return;
00051   G->getPointArray() = CoordNode->getPoint();
00052 
00053   MFInt32 coordIndex = I->getCoordIndex();
00054   G->getLineWidth() = I->getLineWidth();
00055   
00056   if (I->getColor() != 0)
00057   {
00058     G->getColor() = true;
00059     MFColorRGBA colorRGBA = static_cast<ColorRGBA *>(I->getColor())->getColor();
00060     G->getColorPerVertex() = I->getColorPerVertex();
00061     MFInt32 colorIndex = I->getColorIndex();
00062   
00063     if (G->getColorPerVertex())
00064     {
00065       //we use line_strip but the points are duplicated because of the fact 
00066       //that a same point can be of two colors
00067       //there is one array per continuous line
00068       //filling the vertex arrays
00069       MFInt32::const_iterator itCoordIndex = coordIndex.begin();
00070          
00071       if (!colorIndex.empty())
00072       {
00073         MFInt32::const_iterator itColorIndex = colorIndex.begin();  
00074           
00075         while (itCoordIndex != coordIndex.end())
00076         {
00077           vector<C4UB_V3F> *lineArray = new vector<C4UB_V3F>();
00078           while ((itCoordIndex != coordIndex.end()) && (*itCoordIndex != -1))
00079           {
00080             C4UB_V3F element;
00081             SFColorRGBA currentColor = colorRGBA[*itColorIndex];
00082             element.r = (unsigned char)floorf(255.0f*currentColor.r);
00083             element.g = (unsigned char)floorf(255.0f*currentColor.g);
00084             element.b = (unsigned char)floorf(255.0f*currentColor.b);
00085             element.a = (unsigned char)floorf(255.0f*currentColor.a);
00086             element.vertex = G->getPointArray()[*itCoordIndex];
00087             lineArray->push_back(element);
00088             ++itCoordIndex;
00089             ++itColorIndex;
00090           }
00091           G->getLineArrayArray().push_back(lineArray);
00092           ++itCoordIndex;
00093         }
00094       }
00095       else
00096       {
00097         while (itCoordIndex != coordIndex.end())
00098         {
00099           vector<C4UB_V3F> *lineArray = new vector<C4UB_V3F>();
00100           while ((itCoordIndex != coordIndex.end()) && (*itCoordIndex != -1))
00101           {
00102             C4UB_V3F element;
00103             SFColorRGBA currentColor = colorRGBA[*itCoordIndex];
00104             element.r = (unsigned char)floorf(255.0f*currentColor.r);
00105             element.g = (unsigned char)floorf(255.0f*currentColor.g);
00106             element.b = (unsigned char)floorf(255.0f*currentColor.b);
00107             element.a = (unsigned char)floorf(255.0f*currentColor.a);
00108             element.vertex = G->getPointArray()[*itCoordIndex];
00109         
00110             lineArray->push_back(element);
00111             ++itCoordIndex;
00112           }
00113           G->getLineArrayArray().push_back(lineArray);
00114           ++itCoordIndex;
00115         }
00116       }
00117     }
00118     else
00119     {
00120       //the colors are applies to the segments, so we define a large array where each 
00121       //point is duplicated and use independant lines
00122       //reading the content of the coordIndex
00123       G->getColor() = false;
00124       MFInt32::const_iterator itCoordIndex = coordIndex.begin();
00125     
00126       if (!colorIndex.empty())
00127       {
00128         MFInt32::const_iterator itColorIndex = colorIndex.begin(); 
00129         
00130         while (itCoordIndex != coordIndex.end())
00131         {
00132           unsigned int precCoordIndex = *itCoordIndex;
00133           ++itCoordIndex;
00134           if (itCoordIndex == coordIndex.end())
00135             break;
00136           
00137           if (*itCoordIndex != -1)
00138           {
00139             C4UB_V3F element;
00140             SFColorRGBA currentColor = colorRGBA[*itColorIndex];
00141             element.r = (unsigned char)floorf(255.0f*currentColor.r);
00142             element.g = (unsigned char)floorf(255.0f*currentColor.g);
00143             element.b = (unsigned char)floorf(255.0f*currentColor.b);
00144             element.a = (unsigned char)floorf(255.0f*currentColor.a);
00145             element.vertex = G->getPointArray()[precCoordIndex];
00146             ++itColorIndex;
00147             //we push back the two elements keeping the color
00148             G->getPointLineArray().push_back(element);
00149             
00150             element.vertex = G->getPointArray()[*itCoordIndex];
00151             G->getPointLineArray().push_back(element);
00152           }   
00153           else
00154           {
00155             ++itCoordIndex;
00156           }  
00157         }
00158       }
00159       else
00160       {
00161         while (itCoordIndex != coordIndex.end())
00162         {
00163           unsigned int precCoordIndex = *itCoordIndex;
00164           ++itCoordIndex;
00165           if (itCoordIndex == coordIndex.end())
00166             break;
00167           
00168           if (*itCoordIndex != -1)
00169           {
00170             C4UB_V3F element;
00171             SFColorRGBA currentColor = colorRGBA[*itCoordIndex];
00172             element.r = (unsigned char)floorf(255.0f*currentColor.r);
00173             element.g = (unsigned char)floorf(255.0f*currentColor.g);
00174             element.b = (unsigned char)floorf(255.0f*currentColor.b);
00175             element.a = (unsigned char)floorf(255.0f*currentColor.a);
00176             element.vertex = G->getPointArray()[precCoordIndex];
00177           
00178             //we push back the two elements keeping the color
00179             G->getPointLineArray().push_back(element);
00180             
00181             element.vertex = G->getPointArray()[*itCoordIndex];
00182             G->getPointLineArray().push_back(element);
00183           }   
00184           else
00185           {
00186             ++itCoordIndex;
00187           }  
00188         }
00189       }
00190     }
00191   }
00192   else
00193   {
00194     //the material properties are applied
00195     //defining the set of indices
00196     //reading the content of the coordIndex
00197     MFInt32::const_iterator itCoordIndex = coordIndex.begin();
00198     while (itCoordIndex != coordIndex.end())
00199     {
00200       MFInt32 *lineIndex = new MFInt32();
00201       while ((itCoordIndex != coordIndex.end()) && (*itCoordIndex != -1))
00202       {
00203         lineIndex->push_back(*itCoordIndex);
00204         ++itCoordIndex;
00205       }
00206       G->getLineIndexArray().push_back(lineIndex);
00207       ++itCoordIndex;
00208     }
00209   }
00210 }
00211 
00212 void GLUpdaterGLRenderingVisitor::enterGLPointSet(GLPointSet *G) const
00213 {
00214   PointSet *P = static_cast<PointSet *>(G->getLink());
00215 
00216   // emptying the arrays
00217   G->getPointArray() = MFVec3f();
00218   G->getPointSetArray() = vector<C4UB_V3F>();
00219 
00220   Coordinate *CoordNode = static_cast<Coordinate *>(P->getCoord());  
00221   if (CoordNode == 0)
00222     return;
00223 
00224   G->getPointArray() = CoordNode->getPoint();
00225 
00226   if (P->getColor() != 0)
00227   {
00228     MFColorRGBA colorRGBA = static_cast<ColorRGBA *>(P->getColor())->getColor();
00229       
00230     MFColorRGBA::const_iterator itColor = colorRGBA.begin();
00231     
00232     for (MFVec3f::const_iterator itPoint = G->getPointArray().begin(); itPoint != G->getPointArray().end(); ++itPoint)
00233     {
00234       C4UB_V3F element;
00235       SFColorRGBA currentColor = *itColor;
00236       element.r = (unsigned char)floorf(255.0f*currentColor.r);
00237       element.g = (unsigned char)floorf(255.0f*currentColor.g);
00238       element.b = (unsigned char)floorf(255.0f*currentColor.b);
00239       element.a = (unsigned char)floorf(255.0f*currentColor.a);
00240       element.vertex = *itPoint;
00241       G->getPointSetArray().push_back(element);
00242       ++itColor;
00243     }
00244   }
00245 }
00246 
00247 void GLUpdaterGLRenderingVisitor::enterGLTriangleFanSet(GLTriangleFanSet *G) const
00248 {
00249   TriangleFanSet *T = static_cast<TriangleFanSet *>(G->getLink());
00250   
00251   //emptying the arrays
00252   for (vector<vector<N3F_V3F> *>::const_iterator itFanArray = G->getN3F_V3FvertexFanArrayArray().begin(); itFanArray != G->getN3F_V3FvertexFanArrayArray().end(); ++itFanArray)
00253     delete (*itFanArray);
00254   G->getN3F_V3FvertexFanArrayArray() = vector<vector<N3F_V3F> *>();
00255 
00256   for (vector<vector<C4F_N3F_V3F> *>::const_iterator itFanArray = G->getC4F_N3F_V3FvertexFanArrayArray().begin(); itFanArray != G->getC4F_N3F_V3FvertexFanArrayArray().end(); ++itFanArray)
00257     delete (*itFanArray);
00258   G->getC4F_N3F_V3FvertexFanArrayArray() = vector<vector<C4F_N3F_V3F> *>(); 
00259 
00260   Normal *NormalNode = static_cast<Normal *>(T->getNormal());
00261   MFVec3f normalArray;
00262   if (NormalNode != 0)
00263     normalArray = NormalNode->getVector();
00264 
00265   G->getSolid() = T->getSolid();
00266   G->getCcw() = T->getCcw();
00267 
00268   //computing normals if necessary
00269   if (normalArray.empty())
00270     computeNormalArray(G, normalArray);
00271     
00272   if ((T->getColor() != 0) && (T->getTexCoord() != 0))
00273   {
00274     G->getColor() = true;
00275     G->getTexCoord() = true;
00276     computeColor_TexCoord(G, normalArray);
00277   }  
00278   
00279   if ((T->getColor() != 0) && (T->getTexCoord() == 0))
00280   {
00281     G->getColor() = true;
00282     G->getTexCoord() = false;
00283     computeColor_NoTexCoord(G, normalArray);
00284   }  
00285   
00286   if ((T->getColor() == 0) && (T->getTexCoord() != 0))
00287   {
00288     G->getColor() = false;
00289     G->getTexCoord() = true;
00290     computeNoColor_TexCoord(G, normalArray);
00291   }  
00292 
00293   if ((T->getColor() == 0) && (T->getTexCoord() == 0))
00294   {
00295     G->getColor() = false;
00296     G->getTexCoord() = false;
00297     computeNoColor_NoTexCoord(G, normalArray);
00298   }
00299 }
00300 
00301 void GLUpdaterGLRenderingVisitor::enterGLTriangleSet(GLTriangleSet *G) const
00302 {
00303   TriangleSet *T = static_cast<TriangleSet *>(G->getLink());
00304 
00305   //emptying arrays
00306   G->getN3F_V3FvertexTriangleArray() = vector<N3F_V3F>();
00307   G->getC4F_N3F_V3FvertexTriangleArray() = vector<C4F_N3F_V3F>();
00308 
00309   Normal *NormalNode = static_cast<Normal *>(T->getNormal());
00310   MFVec3f normalArray;
00311   if (NormalNode != 0)
00312     normalArray = NormalNode->getVector();
00313 
00314   G->getSolid() = T->getSolid();
00315   G->getCcw() = T->getCcw();
00316 
00317   //computing normals if necessary
00318   if (normalArray.empty())
00319     computeNormalArray(G, normalArray);
00320 
00321   if ((T->getColor() != 0) && (T->getTexCoord() != 0))
00322   {
00323     G->getColor() = true;
00324     G->getTexCoord() = true;
00325     computeColor_TexCoord(G, normalArray);
00326   }  
00327   
00328   if ((T->getColor() != 0) && (T->getTexCoord() == 0))
00329   {
00330     G->getColor() = true;
00331     G->getTexCoord() = false;
00332     computeColor_NoTexCoord(G, normalArray);
00333   }  
00334   
00335   if ((T->getColor() == 0) && (T->getTexCoord() != 0))
00336   {
00337     G->getColor() = false;
00338     G->getTexCoord() = true;
00339     computeNoColor_TexCoord(G, normalArray);
00340   }  
00341 
00342   if ((T->getColor() == 0) && (T->getTexCoord() == 0))
00343   {
00344     G->getColor() = false;
00345     G->getTexCoord() = false;
00346     computeNoColor_NoTexCoord(G, normalArray);
00347   }
00348 }
00349 
00350 void GLUpdaterGLRenderingVisitor::enterGLTriangleStripSet(GLTriangleStripSet *G) const
00351 {
00352   TriangleStripSet *T = static_cast<TriangleStripSet *>(G->getLink());
00353 
00354   //emptying the arrays
00355   for (vector<vector<N3F_V3F> *>::const_iterator itStripArray = G->getN3F_V3FvertexStripArrayArray().begin(); itStripArray != G->getN3F_V3FvertexStripArrayArray().end(); ++itStripArray)
00356     delete (*itStripArray);
00357   G->getN3F_V3FvertexStripArrayArray() = vector<vector<N3F_V3F> *>();
00358  
00359   for (vector<vector<C4F_N3F_V3F> *>::const_iterator itStripArray = G->getC4F_N3F_V3FvertexStripArrayArray().begin(); itStripArray != G->getC4F_N3F_V3FvertexStripArrayArray().end(); ++itStripArray)
00360   delete (*itStripArray);
00361   G->getC4F_N3F_V3FvertexStripArrayArray() = vector<vector<C4F_N3F_V3F> *>(); 
00362 
00363   Normal *NormalNode = static_cast<Normal *>(T->getNormal());
00364   MFVec3f normalArray;
00365   if (NormalNode != 0)
00366     normalArray = NormalNode->getVector();
00367 
00368   G->getSolid() = T->getSolid();
00369   G->getCcw() = T->getCcw();
00370 
00371   //computing normals if necessary
00372   if (normalArray.empty())
00373     computeNormalArray(G, normalArray);
00374 
00375   if ((T->getColor() != 0) && (T->getTexCoord() != 0))
00376   {
00377     G->getColor() = true;
00378     G->getTexCoord() = true;
00379     computeColor_TexCoord(G, normalArray);
00380   }  
00381   
00382   if ((T->getColor() != 0) && (T->getTexCoord() == 0))
00383   {
00384     G->getColor() = true;
00385     G->getTexCoord() = false;
00386     computeColor_NoTexCoord(G, normalArray);
00387   }  
00388   
00389   if ((T->getColor() == 0) && (T->getTexCoord() != 0))
00390   {
00391     G->getColor() = false;
00392     G->getTexCoord() = true;
00393     computeNoColor_TexCoord(G, normalArray);
00394   }  
00395 
00396   if ((T->getColor() == 0) && (T->getTexCoord() == 0))
00397   {
00398     G->getColor() = false;
00399     G->getTexCoord() = false;
00400     computeNoColor_NoTexCoord(G, normalArray);
00401   } 
00402 }
00403 
00404 void GLUpdaterGLRenderingVisitor::computeNoColor_NoTexCoord(GLTriangleFanSet *G, const MFVec3f &normalArray) const
00405 {
00406   TriangleFanSet *T = static_cast<TriangleFanSet *>(G->getLink());
00407   
00408   Coordinate *CoordNode = static_cast<Coordinate *>(T->getCoord());
00409   if (CoordNode == 0)
00410     return;
00411   MFVec3f vertexArray = CoordNode->getPoint();
00412 
00413   SFBool normalPerVertex = T->getNormalPerVertex();
00414   MFInt32 fanCount = T->getFanCount();
00415     
00416   if (normalPerVertex)
00417   {
00418     G->getVerticesDuplicated() = false;
00419     //using the GL_TRIANGLE_FAN
00420     MFVec3f::const_iterator itCoord = vertexArray.begin();
00421     MFVec3f::const_iterator itNormal = normalArray.begin();
00422        
00423     //iterate the fans
00424     for (MFInt32::const_iterator itFan = fanCount.begin(); itFan != fanCount.end(); ++ itFan)
00425     {
00426       //filling a new array
00427       vector<N3F_V3F> *vertexFanArray = new vector<N3F_V3F>();
00428       for (int i = 0; i < *itFan; ++i)
00429       {
00430         N3F_V3F element;  
00431         element.normal = *itNormal;
00432         element.vertex = *itCoord;
00433         vertexFanArray->push_back(element);
00434             
00435         ++itCoord;
00436         ++itNormal;
00437       }
00438       G->getN3F_V3FvertexFanArrayArray().push_back(vertexFanArray);
00439     }
00440   }
00441   else //normalPerVertex is FALSE
00442   {
00443     G->getVerticesDuplicated() = true;
00444     //using the GL_TRIANGLES
00445     MFVec3f::const_iterator itCoord = vertexArray.begin();
00446     MFVec3f::const_iterator itNormal = normalArray.begin();
00447         
00448     //iterate the fans
00449     for (MFInt32::const_iterator itFan = fanCount.begin(); itFan != fanCount.end(); ++ itFan)
00450     {
00451       //filling a new array
00452       vector<N3F_V3F> *vertexFanArray = new vector<N3F_V3F>();
00453       N3F_V3F center;
00454       center.normal = *itNormal;
00455       center.vertex = *itCoord;
00456       ++itCoord;
00457         
00458       N3F_V3F element;  
00459       element.normal = *itNormal;
00460       element.vertex = *itCoord;
00461 
00462       for (int i = 2; i < *itFan; ++i)
00463       {
00464         //push center
00465         center.normal = *itNormal;
00466         vertexFanArray->push_back(center);
00467  
00468         //push Pi
00469         element.normal = *itNormal;
00470         vertexFanArray->push_back(element);
00471          
00472         ++itCoord;
00473          
00474         //push Pi+1  
00475         element.normal = *itNormal;
00476         element.vertex = *itCoord;
00477         vertexFanArray->push_back(element);
00478            
00479         ++itNormal;  
00480       }
00481       G->getN3F_V3FvertexFanArrayArray().push_back(vertexFanArray);
00482     }
00483   } 
00484 }
00485 
00486 void GLUpdaterGLRenderingVisitor::computeColor_NoTexCoord(GLTriangleFanSet *G, const MFVec3f &normalArray) const
00487 {
00488   TriangleFanSet *T = static_cast<TriangleFanSet *>(G->getLink());
00489   
00490   MFColorRGBA colorRGBA = static_cast<ColorRGBA *>(T->getColor())->getColor();
00491   
00492   Coordinate *CoordNode = static_cast<Coordinate *>(T->getCoord());
00493   if (CoordNode == 0)
00494     return;
00495   MFVec3f vertexArray = CoordNode->getPoint();
00496 
00497   SFBool normalPerVertex = T->getNormalPerVertex();
00498   SFBool colorPerVertex = T->getColorPerVertex();
00499   MFInt32 fanCount = T->getFanCount();
00500     
00501   if ((normalPerVertex) && (colorPerVertex))
00502   {
00503     G->getVerticesDuplicated() = false;
00504     //using the GL_TRIANGLE_FAN
00505     MFColorRGBA::const_iterator itColor = colorRGBA.begin();
00506     MFVec3f::const_iterator itCoord = vertexArray.begin();
00507     MFVec3f::const_iterator itNormal = normalArray.begin();
00508         
00509     //iterate the fans
00510     for (MFInt32::const_iterator itFan = fanCount.begin(); itFan != fanCount.end(); ++ itFan)
00511     {
00512       //filling a new array
00513       vector<C4F_N3F_V3F> *vertexFanArray = new vector<C4F_N3F_V3F>();
00514       for (int i = 0; i < *itFan; ++i)
00515       {
00516         C4F_N3F_V3F element;  
00517         element.color = *itColor;
00518         element.normal = *itNormal;
00519         element.vertex = *itCoord;
00520         vertexFanArray->push_back(element);
00521           
00522         ++itColor;
00523         ++itCoord;
00524         ++itNormal;
00525       }
00526       G->getC4F_N3F_V3FvertexFanArrayArray().push_back(vertexFanArray);
00527     }
00528   }
00529   else //normalPerVertex or colorPertex is FALSE
00530   {
00531     G->getVerticesDuplicated() = true;
00532     //using the GL_TRIANGLES
00533     MFColorRGBA::const_iterator itColor = colorRGBA.begin();
00534     MFVec3f::const_iterator itCoord = vertexArray.begin();
00535     MFVec3f::const_iterator itNormal = normalArray.begin();
00536         
00537     //iterate the fans
00538     for (MFInt32::const_iterator itFan = fanCount.begin(); itFan != fanCount.end(); ++ itFan)
00539     {
00540       //filling a new array
00541       vector<C4F_N3F_V3F> *vertexFanArray = new vector<C4F_N3F_V3F>();
00542       C4F_N3F_V3F center;
00543       center.color = *itColor;
00544       center.normal = *itNormal;
00545       center.vertex = *itCoord;
00546  
00547       ++itCoord;
00548       if (colorPerVertex)
00549         ++itColor;
00550       if (normalPerVertex)
00551         ++itNormal;  
00552       C4F_N3F_V3F element;  
00553       element.color = *itColor;
00554       element.normal = *itNormal;
00555       element.vertex = *itCoord;
00556 
00557       for (int i = 2; i < *itFan; ++i)
00558       {
00559         //push center
00560         if (!colorPerVertex)
00561           center.color = *itColor;
00562         if (!normalPerVertex)
00563           center.normal = *itNormal;
00564         vertexFanArray->push_back(center);
00565  
00566         //push Pi
00567         if (!colorPerVertex)
00568           element.color = *itColor;
00569         if (!normalPerVertex)  
00570           element.normal = *itNormal;
00571         vertexFanArray->push_back(element);
00572            
00573         ++itCoord;
00574         if (colorPerVertex)
00575           ++itColor;
00576         if (normalPerVertex)
00577           ++itNormal;  
00578             
00579         //push Pi+1  
00580         element.color = *itColor;
00581         element.normal = *itNormal;
00582         element.vertex = *itCoord;
00583         vertexFanArray->push_back(element);
00584           
00585         if (!colorPerVertex)
00586           ++itColor;
00587         if (!normalPerVertex)
00588           ++itNormal;  
00589       }
00590       G->getC4F_N3F_V3FvertexFanArrayArray().push_back(vertexFanArray);
00591     }
00592   } 
00593 }
00594 
00595 void GLUpdaterGLRenderingVisitor::computeNoColor_TexCoord(GLTriangleFanSet *G, const MFVec3f &normalArray) const
00596 {
00597 }
00598 
00599 void GLUpdaterGLRenderingVisitor::computeColor_TexCoord(GLTriangleFanSet *G, const MFVec3f &normalArray) const
00600 {
00601 }
00602 
00603 void GLUpdaterGLRenderingVisitor::computeNormalArray(GLTriangleFanSet *G, MFVec3f &normalArray) const
00604 {
00605   TriangleFanSet *T = static_cast<TriangleFanSet *>(G->getLink());
00606 
00607   Coordinate *CoordNode = static_cast<Coordinate *>(T->getCoord());
00608   if (CoordNode == 0)
00609     return;
00610   MFVec3f vertexArray = CoordNode->getPoint();
00611 
00612   SFBool normalPerVertex = T->getNormalPerVertex();
00613   MFInt32 fanCount = T->getFanCount();
00614   
00615   //computing the array containing the normals of each face
00616   MFVec3f normalFaceArray;
00617   
00618   MFVec3f::const_iterator itCoord = vertexArray.begin();
00619   for (MFInt32::const_iterator itFan = fanCount.begin(); itFan != fanCount.end(); ++itFan)
00620   {
00621     SFVec3f Center = *itCoord;
00622     ++itCoord;
00623     SFVec3f A = *itCoord;
00624     ++itCoord;
00625 
00626     for (SFInt32 i = 2; i < *itFan; ++i) 
00627     {
00628       SFVec3f B = *itCoord;
00629       ++itCoord;
00630     
00631       //computing the normal following ccw
00632       SFVec3f AB = B - Center;
00633       SFVec3f AC = Center - A;
00634       SFVec3f N;
00635       if (G->getCcw())
00636         N = crossprod(AB, AC);
00637       else
00638         N = crossprod(AC, AB);  
00639       normalFaceArray.push_back(N);
00640     }
00641   }
00642   
00643   if (normalPerVertex)
00644   {
00645     normalArray = MFVec3f(vertexArray.size());
00646     
00647     //iterate the fans
00648     MFVec3f::iterator itNormal = normalArray.begin();
00649     MFVec3f::const_iterator itFace = normalFaceArray.begin();
00650     for (MFInt32::const_iterator itFan = fanCount.begin(); itFan != fanCount.end(); ++itFan)
00651     {
00652       SFVec3f Center = *itNormal; //memorizing the center of the fan
00653       ++itNormal;//positioning on the second point
00654       SFVec3f N0 = *itFace;
00655       Center = Center + N0;
00656       ++itFace;
00657       *itNormal = N0;//the first point has the normal of the first face
00658       ++itNormal;
00659       for (SFInt32 i = 2; i < *itFan; ++i) 
00660       {
00661         SFVec3f N1 = *itFace;
00662         Center = Center + N1;
00663         SFVec3f N = N0 + N1;
00664         N.normalize();
00665         *itNormal = N;
00666         N0 = N1;
00667         ++itNormal;
00668       }
00669       Center.normalize();//normalizing the center of the fan
00670     }
00671   }
00672   else
00673   {
00674     //copying the values of normalFaceArray
00675     for (MFVec3f::const_iterator itFace = normalFaceArray.begin(); itFace != normalFaceArray.end(); ++itFace)
00676       normalArray.push_back(*itFace);
00677   }
00678 }
00679 
00680 void GLUpdaterGLRenderingVisitor::computeNoColor_NoTexCoord(GLTriangleSet *G, const MFVec3f &normalArray) const
00681 {
00682   TriangleSet *T = static_cast<TriangleSet *>(G->getLink());
00683 
00684   Coordinate *CoordNode = static_cast<Coordinate *>(T->getCoord());
00685   if (CoordNode == 0)
00686     return;
00687   MFVec3f vertexArray = CoordNode->getPoint();
00688 
00689   SFBool normalPerVertex = T->getNormalPerVertex();
00690   if (normalPerVertex)
00691   {
00692     MFVec3f::const_iterator itNormal = normalArray.begin();
00693         
00694     //iterate the coords
00695     for (MFVec3f::const_iterator itCoord = vertexArray.begin(); itCoord != vertexArray.end(); ++ itCoord)
00696     {
00697       N3F_V3F element;  
00698       element.normal = *itNormal;
00699       element.vertex = *itCoord;
00700       G->getN3F_V3FvertexTriangleArray().push_back(element);
00701             
00702       ++itNormal;
00703     }
00704   }
00705   else //normalPerVertex is FALSE
00706   {
00707     MFVec3f::const_iterator itNormal = normalArray.begin();
00708         
00709     //iterate the coords
00710     unsigned int vertexCount = 0;
00711     for (MFVec3f::const_iterator itCoord = vertexArray.begin(); itCoord != vertexArray.end(); ++ itCoord)
00712     {
00713       N3F_V3F element;  
00714       element.normal = *itNormal;
00715       element.vertex = *itCoord;
00716       G->getN3F_V3FvertexTriangleArray().push_back(element);
00717         
00718       if ((vertexCount + 1)%3 == 0)      
00719         ++itNormal;
00720     }
00721   }
00722 }
00723 
00724 void GLUpdaterGLRenderingVisitor::computeColor_NoTexCoord(GLTriangleSet *G, const MFVec3f &normalArray) const
00725 {
00726   TriangleSet *T = static_cast<TriangleSet *>(G->getLink());
00727 
00728   MFColorRGBA colorRGBA = static_cast<ColorRGBA *>(T->getColor())->getColor();
00729   
00730   Coordinate *CoordNode = static_cast<Coordinate *>(T->getCoord());
00731   if (CoordNode == 0)
00732     return;
00733   MFVec3f vertexArray = CoordNode->getPoint();
00734 
00735   SFBool normalPerVertex = T->getNormalPerVertex();
00736   SFBool colorPerVertex = T->getColorPerVertex();
00737       
00738   if ((normalPerVertex) && (colorPerVertex))
00739   {
00740     MFColorRGBA::const_iterator itColor = colorRGBA.begin();
00741     MFVec3f::const_iterator itNormal = normalArray.begin();
00742         
00743     //iterate the coords
00744     for (MFVec3f::const_iterator itCoord = vertexArray.begin(); itCoord != vertexArray.end(); ++itCoord)
00745     {
00746       C4F_N3F_V3F element;  
00747       element.color = *itColor;
00748       element.normal = *itNormal;
00749       element.vertex = *itCoord;
00750       G->getC4F_N3F_V3FvertexTriangleArray().push_back(element);
00751             
00752       ++itColor;
00753       ++itNormal;
00754     }
00755   }
00756   else //normalPerVertex or colorPertex is FALSE
00757   {
00758     MFColorRGBA::const_iterator itColor = colorRGBA.begin();
00759     MFVec3f::const_iterator itNormal = normalArray.begin();
00760         
00761     //iterate the coords
00762     unsigned int vertexCount = 0;
00763     for (MFVec3f::const_iterator itCoord = vertexArray.begin(); itCoord != vertexArray.end(); ++ itCoord)
00764     {
00765       C4F_N3F_V3F element;  
00766       element.color = *itColor;
00767       element.normal = *itNormal;
00768       element.vertex = *itCoord;
00769       G->getC4F_N3F_V3FvertexTriangleArray().push_back(element);
00770         
00771       if ((colorPerVertex) || ((vertexCount)%3 == 2))    
00772         ++itColor;
00773       if ((normalPerVertex) || ((vertexCount)%3 == 2))      
00774         ++itNormal;
00775           
00776       vertexCount++;  
00777     }
00778   }  
00779 }
00780 
00781 void GLUpdaterGLRenderingVisitor::computeNoColor_TexCoord(GLTriangleSet *G, const MFVec3f &normalArray) const
00782 {
00783 }
00784 
00785 void GLUpdaterGLRenderingVisitor::computeColor_TexCoord(GLTriangleSet *G, const MFVec3f &normalArray) const
00786 {
00787 }
00788 
00789 void GLUpdaterGLRenderingVisitor::computeNormalArray(GLTriangleSet *G, MFVec3f &normalArray) const
00790 {
00791   TriangleSet *T = static_cast<TriangleSet *>(G->getLink());
00792 
00793   Coordinate *CoordNode = static_cast<Coordinate *>(T->getCoord());
00794   if (CoordNode == 0)
00795     return;
00796   MFVec3f vertexArray = CoordNode->getPoint();
00797 
00798   SFBool normalPerVertex = T->getNormalPerVertex();
00799     
00800   //computing the array containing the normals of each face
00801   MFVec3f normalFaceArray;
00802   
00803   MFVec3f::const_iterator itCoord = vertexArray.begin();
00804   while (itCoord != vertexArray.end())
00805   {
00806     SFVec3f A = *itCoord;
00807     ++itCoord;
00808     SFVec3f B = *itCoord;
00809     ++itCoord;
00810     SFVec3f C = *itCoord;
00811     ++itCoord;
00812     
00813     SFVec3f AB = B - A;
00814     SFVec3f AC = C - A;
00815     SFVec3f N;
00816     if (G->getCcw())
00817       N = crossprod(AB, AC);
00818     else
00819       N = crossprod(AC, AB);  
00820     normalFaceArray.push_back(N);
00821   }
00822   
00823   if (normalPerVertex)
00824   {
00825     normalArray = MFVec3f(vertexArray.size());
00826      
00827     MFVec3f::iterator itNormal = normalArray.begin();
00828     for (MFVec3f::const_iterator itFace = normalFaceArray.begin(); itFace != normalFaceArray.end(); ++itFace)
00829     {
00830       *itNormal = *itFace;
00831       ++itNormal;
00832       *itNormal = *itFace;
00833       ++itNormal;
00834       *itNormal = *itFace;
00835       ++itNormal;
00836     }
00837   }
00838   else
00839   {
00840     //copying the values of normalFaceArray
00841     for (MFVec3f::const_iterator itFace = normalFaceArray.begin(); itFace != normalFaceArray.end(); ++itFace)
00842       normalArray.push_back(*itFace);
00843   }
00844 }
00845 
00846 void GLUpdaterGLRenderingVisitor::computeNoColor_NoTexCoord(GLTriangleStripSet *G, const MFVec3f &normalArray) const
00847 {
00848   TriangleStripSet *T = static_cast<TriangleStripSet *>(G->getLink());
00849 
00850   Coordinate *CoordNode = static_cast<Coordinate *>(T->getCoord());
00851   if (CoordNode == 0)
00852     return;
00853   MFVec3f vertexArray = CoordNode->getPoint();
00854 
00855   SFBool normalPerVertex = T->getNormalPerVertex();
00856   MFInt32 stripCount = T->getStripCount();
00857     
00858   if (normalPerVertex)
00859   {
00860     G->getVerticesDuplicated() = false;
00861     //using the GL_N3F_V3F
00862     if (normalPerVertex)
00863     {
00864       //using the GL_TRIANGLE_STRIP
00865       MFVec3f::const_iterator itCoord = vertexArray.begin();
00866       MFVec3f::const_iterator itNormal = normalArray.begin();
00867         
00868       //iterate the strips
00869       for (MFInt32::const_iterator itStrip = stripCount.begin(); itStrip != stripCount.end(); ++ itStrip)
00870       {
00871         //filling a new array
00872         vector<N3F_V3F> *vertexStripArray = new vector<N3F_V3F>();
00873         for (int i = 0; i < *itStrip; ++i)
00874         {
00875           N3F_V3F element;  
00876           element.normal = *itNormal;
00877           element.vertex = *itCoord;
00878           vertexStripArray->push_back(element);
00879             
00880           ++itCoord;
00881           ++itNormal;
00882         }
00883         G->getN3F_V3FvertexStripArrayArray().push_back(vertexStripArray);
00884       }
00885     } 
00886   }
00887   else //normalPerVertex is FALSE
00888   {
00889     G->getVerticesDuplicated() = true;
00890     //using the GL_TRIANGLES
00891     MFVec3f::const_iterator itCoord = vertexArray.begin();
00892     MFVec3f::const_iterator itNormal = normalArray.begin();
00893         
00894     //iterate the strips
00895     for (MFInt32::const_iterator itStrip = stripCount.begin(); itStrip != stripCount.end(); ++ itStrip)
00896     {
00897       //filling a new array
00898       vector<N3F_V3F> *vertexStripArray = new vector<N3F_V3F>();
00899       N3F_V3F center;
00900       center.normal = *itNormal;
00901       center.vertex = *itCoord;
00902       ++itCoord;
00903         
00904       N3F_V3F element;  
00905       element.normal = *itNormal;
00906       element.vertex = *itCoord;
00907 
00908       for (int i = 2; i < *itStrip; ++i)
00909       {
00910         //push center
00911         center.normal = *itNormal;
00912         vertexStripArray->push_back(center);
00913  
00914         //push Pi
00915         element.normal = *itNormal;
00916         vertexStripArray->push_back(element);
00917         ++itCoord;
00918          
00919         //push Pi+1  
00920         element.normal = *itNormal;
00921         element.vertex = *itCoord;
00922         vertexStripArray->push_back(element);
00923         ++itNormal;  
00924       }
00925       G->getN3F_V3FvertexStripArrayArray().push_back(vertexStripArray);
00926     }
00927   } 
00928 }
00929 
00930 void GLUpdaterGLRenderingVisitor::computeColor_NoTexCoord(GLTriangleStripSet *G, const MFVec3f &normalArray) const
00931 {
00932   TriangleStripSet *T = static_cast<TriangleStripSet *>(G->getLink());
00933 
00934   MFColorRGBA colorRGBA = static_cast<ColorRGBA *>(T->getColor())->getColor();
00935   
00936   Coordinate *CoordNode = static_cast<Coordinate *>(T->getCoord());
00937   if (CoordNode == 0)
00938     return;
00939   MFVec3f vertexArray = CoordNode->getPoint();
00940 
00941   SFBool normalPerVertex = T->getNormalPerVertex();
00942   SFBool colorPerVertex = T->getColorPerVertex();
00943   MFInt32 stripCount = T->getStripCount();
00944     
00945   if ((normalPerVertex) && (colorPerVertex))
00946   {
00947     G->getVerticesDuplicated() = false;
00948     //using the GL_TRIANGLE_STRIP
00949     MFColorRGBA::const_iterator itColor = colorRGBA.begin();
00950     MFVec3f::const_iterator itCoord = vertexArray.begin();
00951     MFVec3f::const_iterator itNormal = normalArray.begin();
00952         
00953     //iterate the strips
00954     for (MFInt32::const_iterator itStrip = stripCount.begin(); itStrip != stripCount.end(); ++ itStrip)
00955     {
00956       //filling a new array
00957       vector<C4F_N3F_V3F> *vertexStripArray = new vector<C4F_N3F_V3F>();
00958       for (SFInt32 i = 0; i < *itStrip; ++i)
00959       {
00960         C4F_N3F_V3F element;  
00961         element.color = *itColor;
00962         element.normal = *itNormal;
00963         element.vertex = *itCoord;
00964         vertexStripArray->push_back(element);
00965             
00966         ++itColor;
00967         ++itCoord;
00968         ++itNormal;
00969       }
00970       G->getC4F_N3F_V3FvertexStripArrayArray().push_back(vertexStripArray);
00971     }
00972   }
00973   else //normalPerVertex or colorPertex is FALSE
00974   {
00975     G->getVerticesDuplicated() = true;
00976     //using the GL_TRIANGLES
00977     MFColorRGBA::const_iterator itColor = colorRGBA.begin();
00978     MFVec3f::const_iterator itCoord = vertexArray.begin();
00979     MFVec3f::const_iterator itNormal = normalArray.begin();
00980         
00981     //iterate the strips
00982     bool orientation = true; //var used to change the order of the vertices in the
00983     //case of culling
00984     for (MFInt32::const_iterator itStrip = stripCount.begin(); itStrip != stripCount.end(); ++ itStrip)
00985     {
00986       //filling a new array
00987       vector<C4F_N3F_V3F> *vertexStripArray = new vector<C4F_N3F_V3F>();
00988       C4F_N3F_V3F P0;
00989       P0.color = *itColor;
00990       P0.normal = *itNormal;
00991       P0.vertex = *itCoord;
00992  
00993       ++itCoord;
00994       if (colorPerVertex)
00995         ++itColor;
00996       if (normalPerVertex)
00997         ++itNormal;  
00998 
00999       C4F_N3F_V3F P1, P2;  
01000       P1.color = *itColor;
01001       P1.normal = *itNormal;
01002       P1.vertex = *itCoord;
01003 
01004       for (SFInt32 i = 2; i < *itStrip; ++i)
01005       {
01006         //push center
01007         if (!colorPerVertex)
01008           P0.color = *itColor;
01009         if (!normalPerVertex)
01010           P0.normal = *itNormal;
01011         vertexStripArray->push_back(P0);
01012  
01013         //push Pi
01014         if (!colorPerVertex)
01015           P1.color = *itColor;
01016         if (!normalPerVertex)  
01017           P1.normal = *itNormal;
01018           
01019           
01020         ++itCoord;
01021         if (colorPerVertex)
01022           ++itColor;
01023         if (normalPerVertex)
01024           ++itNormal;  
01025             
01026         //push Pi+1
01027         P2.color = *itColor;
01028         P2.normal = *itNormal;
01029         P2.vertex = *itCoord;
01030          
01031         if (orientation)
01032         {
01033           vertexStripArray->push_back(P1);
01034           vertexStripArray->push_back(P2);
01035         }
01036         else
01037         {
01038           vertexStripArray->push_back(P2);
01039           vertexStripArray->push_back(P1);
01040         }  
01041         orientation = !orientation;
01042             
01043         if (!colorPerVertex)
01044           ++itColor;
01045         if (!normalPerVertex)
01046           ++itNormal;  
01047          
01048         //P0 becomes P1, P1 P2  
01049         P0 = P1;  
01050         P1 = P2;
01051       }
01052       G->getC4F_N3F_V3FvertexStripArrayArray().push_back(vertexStripArray);
01053     }
01054   }  
01055 }
01056 
01057 void GLUpdaterGLRenderingVisitor::computeNoColor_TexCoord(GLTriangleStripSet *G, const MFVec3f &normalArray) const
01058 {
01059 }
01060 
01061 void GLUpdaterGLRenderingVisitor::computeColor_TexCoord(GLTriangleStripSet *G, const MFVec3f &normalArray) const
01062 {
01063 }
01064 
01065 void GLUpdaterGLRenderingVisitor::computeNormalArray(GLTriangleStripSet *G, MFVec3f &normalArray) const
01066 {
01067   TriangleStripSet *T = static_cast<TriangleStripSet *>(G->getLink());
01068 
01069   Coordinate *CoordNode = static_cast<Coordinate *>(T->getCoord());
01070   if (CoordNode == 0)
01071     return;
01072   MFVec3f vertexArray = CoordNode->getPoint();
01073 
01074   SFBool normalPerVertex = T->getNormalPerVertex();
01075   MFInt32 stripCount = T->getStripCount();
01076   
01077   //computing the array containing the normals of each face
01078   MFVec3f normalFaceArray;
01079   bool orientation = true;
01080   MFVec3f::const_iterator itCoord = vertexArray.begin();
01081   for (MFInt32::const_iterator itStrip = stripCount.begin(); itStrip != stripCount.end(); ++itStrip)
01082   {
01083     SFVec3f A = *itCoord;
01084     ++itCoord;
01085     SFVec3f B = *itCoord;
01086     ++itCoord;
01087 
01088     for (SFInt32 i = 2; i < *itStrip; ++i) 
01089     {
01090       SFVec3f C = *itCoord;
01091       ++itCoord;
01092     
01093       //computing the normal following ccw
01094       SFVec3f AB = B - A;
01095       SFVec3f AC = C - A;
01096       SFVec3f N;
01097       if (G->getCcw())
01098       {
01099         if (orientation)
01100           N = crossprod(AB, AC);
01101         else
01102           N = crossprod(AC, AB);    
01103       }   
01104       else
01105       {
01106         if (orientation)
01107           N = crossprod(AC, AB);              
01108         else
01109           N = crossprod(AB, AC);
01110       }
01111       normalFaceArray.push_back(N);
01112       A = B;
01113       B = C;
01114     }
01115   }
01116   
01117   if (normalPerVertex)
01118   {
01119     normalArray = MFVec3f(vertexArray.size());
01120     
01121     //iterate the fans
01122     MFVec3f::iterator itNormal = normalArray.begin();
01123     MFVec3f::const_iterator itFace = normalFaceArray.begin();
01124     for (MFInt32::const_iterator itStrip = stripCount.begin(); itStrip != stripCount.end(); ++itStrip)
01125     {
01126       SFVec3f N0 = *itFace;
01127       ++itFace;
01128       *itNormal = N0; 
01129       ++itNormal;
01130       for (SFInt32 i = 2; i < *itStrip; ++i) 
01131       {
01132         SFVec3f N1 = *itFace;
01133         *itNormal = N0 + N1;
01134         itNormal->normalize();
01135         N0 = N1;
01136         ++itNormal;
01137         ++itFace;
01138       }
01139     }
01140   }
01141   else
01142   {
01143     //copying the values of normalFaceArray
01144     for (MFVec3f::const_iterator itFace = normalFaceArray.begin(); itFace != normalFaceArray.end(); ++itFace)
01145       normalArray.push_back(*itFace);
01146   }
01147 }
01148 

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