glc_3dviewcollection.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  Version 2.0.0 Beta 1, packaged on April 2010.
00006 
00007  http://glc-lib.sourceforge.net
00008 
00009  GLC-lib is free software; you can redistribute it and/or modify
00010  it under the terms of the GNU General Public License as published by
00011  the Free Software Foundation; either version 2 of the License, or
00012  (at your option) any later version.
00013 
00014  GLC-lib is distributed in the hope that it will be useful,
00015  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  GNU General Public License for more details.
00018 
00019  You should have received a copy of the GNU General Public License
00020  along with GLC-lib; if not, write to the Free Software
00021  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023 *****************************************************************************/
00024 
00026 
00027 
00028 #include "glc_3dviewcollection.h"
00029 #include "../shading/glc_material.h"
00030 #include "../glc_openglexception.h"
00031 #include "../shading/glc_selectionmaterial.h"
00032 #include "../glc_state.h"
00033 #include "../shading/glc_shader.h"
00034 #include "../viewport/glc_viewport.h"
00035 #include "glc_spacepartitioning.h"
00036 
00037 #include <QtDebug>
00038 
00040 // Constructor/Destructor
00042 
00043 GLC_3DViewCollection::GLC_3DViewCollection()
00044 : m_3DViewInstanceHash()
00045 , m_pBoundingBox(NULL)
00046 , m_SelectedInstances()
00047 , m_ShadedPointerViewInstanceHash()
00048 , m_ShaderGroup()
00049 , m_MainInstances()
00050 , m_IsInShowSate(true)
00051 , m_UseLod(false)
00052 , m_pViewport(NULL)
00053 , m_pSpacePartitioning(NULL)
00054 , m_UseSpacePartitioning(false)
00055 {
00056 }
00057 
00058 GLC_3DViewCollection::~GLC_3DViewCollection()
00059 {
00060         // Delete all collection's elements and the collection bounding box
00061         clear();
00062 }
00064 // Set Functions
00066 
00067 // Add the specified shader to the collection
00068 bool GLC_3DViewCollection::bindShader(GLuint shaderId)
00069 {
00070         if (m_ShadedPointerViewInstanceHash.contains(shaderId))
00071         {
00072                 return false;
00073         }
00074         else
00075         {
00076                 PointerViewInstanceHash* pNodeHash= new PointerViewInstanceHash;
00077                 m_ShadedPointerViewInstanceHash.insert(shaderId, pNodeHash);
00078                 return true;
00079         }
00080 }
00081 
00082 // Unbind the specified shader from the collection
00083 bool GLC_3DViewCollection::unBindShader(GLuint shaderId)
00084 {
00085         bool result= false;
00086         if (m_ShadedPointerViewInstanceHash.contains(shaderId))
00087         {
00088                 // Find node which use the shader
00089                 QList<GLC_uint> nodeId(m_ShaderGroup.keys(shaderId));
00090 
00091                 // Move these node in the standard hash and remove them from shader group
00092                 PointerViewInstanceHash* pShaderNodeHash= m_ShadedPointerViewInstanceHash.take(shaderId);
00093                 for (int i= 0; i < nodeId.size(); ++i)
00094                 {
00095                         const GLC_uint id= nodeId[i];
00096                         GLC_3DViewInstance* pInstance= pShaderNodeHash->value(id);
00097 
00098                         if (!pInstance->isSelected())
00099                         {
00100                                 m_MainInstances.insert(id, pInstance);
00101                         }
00102                         else
00103                         {
00104                                 m_SelectedInstances.insert(id, pInstance);
00105                         }
00106                         m_ShaderGroup.remove(id);
00107                 }
00108                 pShaderNodeHash->clear();
00109                 delete pShaderNodeHash;
00110                 result= true;
00111         }
00112         Q_ASSERT(!m_ShadedPointerViewInstanceHash.contains(shaderId));
00113         return result;
00114 }
00115 
00116 // Unbind All shader
00117 bool GLC_3DViewCollection::unBindAllShader()
00118 {
00119         bool result= true;
00120         HashList::iterator iEntry= m_ShadedPointerViewInstanceHash.begin();
00121         QList<GLuint> shaderList;
00122     while (iEntry != m_ShadedPointerViewInstanceHash.constEnd())
00123     {
00124         shaderList.append(iEntry.key());
00125         ++iEntry;
00126     }
00127     const int size= shaderList.size();
00128     for (int i=0; i < size; ++i)
00129     {
00130         result= result && unBindShader(shaderList[i]);
00131     }
00132     return result;
00133 }
00134 
00135 // Add GLC_3DViewInstance in the collection
00136 bool GLC_3DViewCollection::add(const GLC_3DViewInstance& node, GLuint shaderID)
00137 {
00138         bool result= false;
00139         const GLC_uint key= node.id();
00140         if (m_3DViewInstanceHash.contains(key))
00141         {
00142                 qDebug() << "Instance already in collection";
00143                 return false;
00144         }
00145         m_3DViewInstanceHash.insert(key, node);
00146         // Create an GLC_3DViewInstance pointer of the inserted instance
00147         ViewInstancesHash::iterator iNode= m_3DViewInstanceHash.find(key);
00148         GLC_3DViewInstance* pInstance= &(iNode.value());
00149         // Chose the hash where instance is
00150         if(0 != shaderID)
00151         {
00152                 // Test if shaderId group exist
00153                 if (m_ShadedPointerViewInstanceHash.contains(shaderID))
00154                 {
00155                         m_ShaderGroup.insert(key, shaderID);
00156 
00157                         if(pInstance->isSelected())
00158                         {
00159                                 m_SelectedInstances.insert(key, pInstance);
00160                         }
00161                         else
00162                         {
00163                                 m_ShadedPointerViewInstanceHash.value(shaderID)->insert(key, pInstance);
00164                         }
00165                         result=true;
00166                 }
00167         }
00168         else if (!pInstance->isSelected())
00169         {
00170                 m_MainInstances.insert(key, pInstance);
00171                 result=true;
00172         }
00173         else
00174         {
00175                 m_SelectedInstances.insert(key, pInstance);
00176                 result=true;
00177         }
00178 
00179         if(result)
00180         {
00181                 // Bounding box validity
00182                 if (NULL != m_pBoundingBox)
00183                 {
00184                         delete m_pBoundingBox;
00185                         m_pBoundingBox= NULL;
00186                 }
00187         }
00188         return result;
00189 }
00190 // Change instance shading group
00191 void GLC_3DViewCollection::changeShadingGroup(GLC_uint instanceId, GLuint shaderId)
00192 {
00193         // Test if the specified instance exist
00194         Q_ASSERT(m_3DViewInstanceHash.contains(instanceId));
00195         // Get the instance shading group
00196         const GLuint instanceShadingGroup= shadingGroup(instanceId);
00197         // Get a pointer to the instance
00198         GLC_3DViewInstance* pInstance= NULL;
00199         if (0 == instanceShadingGroup)
00200         {
00201                 // The instance is not in a shading group
00202                 if (m_MainInstances.contains(instanceId))
00203                 {
00204                         pInstance= m_MainInstances.take(instanceId);
00205                 }
00206                 else if (m_SelectedInstances.contains(instanceId))
00207                 {
00208                         // The instance is selected don't take it
00209                         pInstance= m_SelectedInstances.value(instanceId);
00210                 }
00211                 else
00212                 {
00213                         Q_ASSERT(false);
00214                 }
00215         }
00216         else
00217         {
00218                 m_ShaderGroup.remove(instanceId);
00219                 // The instance is in a shading group
00220                 if (m_SelectedInstances.contains(instanceId))
00221                 {
00222                         // The instance is selected don't take it
00223                         pInstance= m_SelectedInstances.value(instanceId);
00224                 }
00225                 else
00226                 {
00227                         pInstance= m_ShadedPointerViewInstanceHash.value(instanceShadingGroup)->take(instanceId);
00228                 }
00229 
00230         }
00231         // Put the instance in specified shading group
00232         if (0 != shaderId)
00233         {
00234                 m_ShaderGroup.insert(instanceId, shaderId);
00235                 if (!pInstance->isSelected())
00236                 {
00237                         m_ShadedPointerViewInstanceHash.value(shaderId)->insert(instanceId, pInstance);
00238                 }
00239         }
00240         else if (!pInstance->isSelected())
00241         {
00242                 m_MainInstances.insert(instanceId, pInstance);
00243         }
00244 }
00245 
00246 // Delete geometry from the collection
00247 bool GLC_3DViewCollection::remove(GLC_uint Key)
00248 {
00249 
00250         ViewInstancesHash::iterator iNode= m_3DViewInstanceHash.find(Key);
00251 
00252         if (iNode != m_3DViewInstanceHash.end())
00253         {       // Ok, the key exist
00254 
00255                 if (selectionSize() > 0)
00256                 {
00257                         // if the geometry is selected, unselect it
00258                         unselect(Key);
00259                 }
00260 
00261                 m_MainInstances.remove(Key);
00262 
00263                 m_3DViewInstanceHash.remove(Key);               // Delete the conteneur
00264 
00265                 // Bounding box validity
00266                 if (NULL != m_pBoundingBox)
00267                 {
00268                         delete m_pBoundingBox;
00269                         m_pBoundingBox= NULL;
00270                 }
00271 
00272                 //qDebug("GLC_3DViewCollection::removeNode : Element succesfuly deleted");
00273                 return true;
00274 
00275         }
00276         else
00277         {       // KO, key doesn't exist
00278                 qDebug("GLC_Collection::removeNode : Element not deleted");
00279                 return false;
00280         }
00281 
00282 }
00283 
00284 // Clear the collection
00285 void GLC_3DViewCollection::clear(void)
00286 {
00287         // Clear Selected node Hash Table
00288         m_SelectedInstances.clear();
00289         // Clear the not transparent Hash Table
00290         m_MainInstances.clear();
00291         // Clear Other Node Hash List
00292         HashList::iterator iEntry= m_ShadedPointerViewInstanceHash.begin();
00293     while (iEntry != m_ShadedPointerViewInstanceHash.constEnd())
00294     {
00295         iEntry.value()->clear();
00296         delete iEntry.value();
00297         iEntry= m_ShadedPointerViewInstanceHash.erase(iEntry);
00298     }
00299 
00300         m_ShadedPointerViewInstanceHash.clear();
00301         m_ShaderGroup.clear();
00302 
00303         // Clear main Hash table
00304     m_3DViewInstanceHash.clear();
00305 
00306         // delete the boundingBox
00307         if (m_pBoundingBox != NULL)
00308         {
00309                 delete m_pBoundingBox;
00310                 m_pBoundingBox= NULL;
00311         }
00312 
00313         // delete the space partitioning
00314         delete m_pSpacePartitioning;
00315 }
00316 
00317 // Select a node
00318 bool GLC_3DViewCollection::select(GLC_uint key, bool primitive)
00319 {
00320         if (!m_3DViewInstanceHash.contains(key)) return false;
00321         //qDebug() << "GLC_Collection::select " << key;
00322 
00323         GLC_3DViewInstance* pSelectedNode;
00324         ViewInstancesHash::iterator iNode= m_3DViewInstanceHash.find(key);
00325         PointerViewInstanceHash::iterator iSelectedNode= m_SelectedInstances.find(key);
00326 
00327         if ((iNode != m_3DViewInstanceHash.end()) && (iSelectedNode == m_SelectedInstances.end()))
00328         {       // Ok, the key exist and the node is not selected
00329                 pSelectedNode= &(iNode.value());
00330                 m_SelectedInstances.insert(pSelectedNode->id(), pSelectedNode);
00331 
00332                 // Remove Selected Node from is previous collection
00333                 if (isInAShadingGroup(key))
00334                 {
00335                         m_ShadedPointerViewInstanceHash.value(shadingGroup(key))->remove(key);
00336                         //qDebug() << "remove from shader list";
00337                 }
00338                 else
00339                 {
00340                         m_MainInstances.remove(key);
00341                 }
00342                 pSelectedNode->select(primitive);
00343 
00344                 //qDebug("GLC_3DViewCollection::selectNode : Element succesfuly selected");
00345                 return true;
00346         }
00347         else
00348         {       // KO, key doesn't exist or node allready selected
00349                 qDebug("GLC_Collection::selectNode : Element not selected");
00350                 return false;
00351         }
00352 }
00353 
00354 // Select all instances in current show state
00355 void GLC_3DViewCollection::selectAll()
00356 {
00357         unselectAll();
00358         ViewInstancesHash::iterator iNode= m_3DViewInstanceHash.begin();
00359         while (iNode != m_3DViewInstanceHash.end())
00360         {
00361                 GLC_3DViewInstance *pCurrentInstance= &(iNode.value());
00362                 const GLC_uint instanceId= pCurrentInstance->id();
00363 
00364                 if (pCurrentInstance->isVisible() == m_IsInShowSate)
00365                 {
00366                         pCurrentInstance->select(false);
00367                         m_SelectedInstances.insert(instanceId, pCurrentInstance);
00368                         m_MainInstances.remove(instanceId);
00369                         if(isInAShadingGroup(instanceId))
00370                         {
00371                                 m_ShadedPointerViewInstanceHash.value(shadingGroup(instanceId))->remove(instanceId);
00372                         }
00373                 }
00374                 iNode++;
00375         }
00376 }
00377 
00378 // unselect a node
00379 bool GLC_3DViewCollection::unselect(GLC_uint key)
00380 {
00381         GLC_3DViewInstance* pSelectedNode;
00382 
00383         PointerViewInstanceHash::iterator iSelectedNode= m_SelectedInstances.find(key);
00384 
00385         if (iSelectedNode != m_SelectedInstances.end())
00386         {       // Ok, the key exist and the node is selected
00387                 iSelectedNode.value()->unselect();
00388 
00389                 m_SelectedInstances.remove(key);
00390 
00391                 pSelectedNode= iSelectedNode.value();
00392 
00393                 // Insert Selected Node to the right collection
00394                 if (isInAShadingGroup(key))
00395                 {
00396                         m_ShadedPointerViewInstanceHash.value(shadingGroup(key))->insert(key, pSelectedNode);
00397                 }
00398                 else
00399                 {
00400                         m_MainInstances.insert(key, pSelectedNode);
00401                 }
00402 
00403                 //qDebug("GLC_3DViewCollection::unselectNode : Node succesfuly unselected");
00404                 return true;
00405 
00406         }
00407         else
00408         {       // KO, key doesn't exist or node allready selected
00409                 //qDebug("GLC_3DViewCollection::unselectNode : Node not unselected");
00410                 return false;
00411         }
00412 }
00413 
00414 // Unselect all Node
00415 void GLC_3DViewCollection::unselectAll()
00416 {
00417         PointerViewInstanceHash::iterator iSelectedNode= m_SelectedInstances.begin();
00418 
00419     while (iSelectedNode != m_SelectedInstances.end())
00420     {
00421         GLC_3DViewInstance* pInstance= iSelectedNode.value();
00422         pInstance->unselect();
00423                 if (isInAShadingGroup(pInstance->id()))
00424                 {
00425                         m_ShadedPointerViewInstanceHash.value(shadingGroup(pInstance->id()))->insert(pInstance->id(), pInstance);
00426                 }
00427                 else
00428                 {
00429                         m_MainInstances.insert(pInstance->id(), pInstance);
00430                 }
00431 
00432         ++iSelectedNode;
00433     }
00434     // Clear selected node hash table
00435     m_SelectedInstances.clear();
00436 }
00437 
00438 // Set the polygon mode for all Instance
00439 void GLC_3DViewCollection::setPolygonModeForAll(GLenum face, GLenum mode)
00440 {
00441         ViewInstancesHash::iterator iEntry= m_3DViewInstanceHash.begin();
00442 
00443     while (iEntry != m_3DViewInstanceHash.constEnd())
00444     {
00445         // Update Instance Polygon Mode
00446         iEntry.value().setPolygonMode(face, mode);
00447         iEntry++;
00448     }
00449 
00450 }
00451 
00452 // Set Instance visibility
00453 void GLC_3DViewCollection::setVisibility(const GLC_uint key, const bool visibility)
00454 {
00455         ViewInstancesHash::iterator iNode= m_3DViewInstanceHash.find(key);
00456         if (iNode != m_3DViewInstanceHash.end())
00457         {       // Ok, the key exist
00458                 iNode.value().setVisibility(visibility);
00459                 // Bounding box validity
00460                 if (NULL != m_pBoundingBox)
00461                 {
00462                         delete m_pBoundingBox;
00463                         m_pBoundingBox= NULL;
00464                 }
00465         }
00466 }
00467 
00468 // Show all instance of the collection
00469 void GLC_3DViewCollection::showAll()
00470 {
00471         ViewInstancesHash::iterator iEntry= m_3DViewInstanceHash.begin();
00472 
00473     while (iEntry != m_3DViewInstanceHash.constEnd())
00474     {
00475         // Update Instance Polygon Mode
00476         iEntry.value().setVisibility(true);
00477         iEntry++;
00478     }
00479 
00480     // Bounding box validity
00481         if (NULL != m_pBoundingBox)
00482         {
00483                 delete m_pBoundingBox;
00484                 m_pBoundingBox= NULL;
00485         }
00486 
00487 }
00488 
00489 // Hide all instance of the collection
00490 void GLC_3DViewCollection::hideAll()
00491 {
00492         ViewInstancesHash::iterator iEntry= m_3DViewInstanceHash.begin();
00493 
00494     while (iEntry != m_3DViewInstanceHash.constEnd())
00495     {
00496         // Update Instance Polygon Mode
00497         iEntry.value().setVisibility(false);
00498         iEntry++;
00499     }
00500 
00501         // Bounding box validity
00502         if (NULL != m_pBoundingBox)
00503         {
00504                 delete m_pBoundingBox;
00505                 m_pBoundingBox= NULL;
00506         }
00507 
00508 }
00509 
00510 // Bind the space partitioning
00511 void GLC_3DViewCollection::bindSpacePartitioning(GLC_SpacePartitioning* pSpacePartitioning)
00512 {
00513         Q_ASSERT(NULL != pSpacePartitioning);
00514         Q_ASSERT(pSpacePartitioning->collectionHandle() == this);
00515 
00516         delete m_pSpacePartitioning;
00517         m_pSpacePartitioning= pSpacePartitioning;
00518 }
00519 
00520 // Unbind the space partitioning
00521 void GLC_3DViewCollection::unbindSpacePartitioning()
00522 {
00523         delete m_pSpacePartitioning;
00524         m_pSpacePartitioning= NULL;
00525         m_UseSpacePartitioning= false;
00526 
00527         ViewInstancesHash::iterator iEntry= m_3DViewInstanceHash.begin();
00528     while (iEntry != m_3DViewInstanceHash.constEnd())
00529     {
00530         // Update Instance viewable flag
00531         iEntry.value().setViewable(GLC_3DViewInstance::FullViewable);
00532         iEntry++;
00533     }
00534 
00535 }
00536 
00537 // Update the instance viewble state using frustrum culling
00538 void GLC_3DViewCollection::updateInstanceViewableState(GLC_Matrix4x4* pMatrix)
00539 {
00540         if ((NULL != m_pViewport) && m_UseSpacePartitioning && (NULL != m_pSpacePartitioning))
00541         {
00542                 if (m_pViewport->updateFrustum(pMatrix))
00543                         m_pSpacePartitioning->updateViewableInstances(m_pViewport->frustum());
00544         }
00545 }
00546 // Update the instance viewable state with the specified frustum
00547 void GLC_3DViewCollection::updateInstanceViewableState(const GLC_Frustum& frustum)
00548 {
00549         m_pSpacePartitioning->updateViewableInstances(frustum);
00550 }
00551 
00552 // Return all GLC_3DViewInstance from collection
00553 QList<GLC_3DViewInstance*> GLC_3DViewCollection::instancesHandle()
00554 {
00555         QList<GLC_3DViewInstance*> instancesList;
00556 
00557         ViewInstancesHash::iterator iEntry= m_3DViewInstanceHash.begin();
00558 
00559     while (iEntry != m_3DViewInstanceHash.constEnd())
00560     {
00561         instancesList.append(&(iEntry.value()));
00562         iEntry++;
00563     }
00564         return instancesList;
00565 }
00566 
00567 // Return all visible GLC_3DViewInstance from the collection
00568 QList<GLC_3DViewInstance*> GLC_3DViewCollection::visibleInstancesHandle()
00569 {
00570         QList<GLC_3DViewInstance*> instancesList;
00571 
00572         ViewInstancesHash::iterator iEntry= m_3DViewInstanceHash.begin();
00573 
00574     while (iEntry != m_3DViewInstanceHash.constEnd())
00575     {
00576         if (iEntry.value().isVisible())
00577         {
00578                 instancesList.append(&(iEntry.value()));
00579         }
00580         iEntry++;
00581     }
00582         return instancesList;
00583 
00584 }
00585 
00587 QList<GLC_3DViewInstance*> GLC_3DViewCollection::viewableInstancesHandle()
00588 {
00589         QList<GLC_3DViewInstance*> instancesList;
00590 
00591         ViewInstancesHash::iterator iEntry= m_3DViewInstanceHash.begin();
00592 
00593     while (iEntry != m_3DViewInstanceHash.constEnd())
00594     {
00595         if (iEntry.value().isVisible() == m_IsInShowSate)
00596         {
00597                 instancesList.append(&(iEntry.value()));
00598         }
00599         iEntry++;
00600     }
00601         return instancesList;
00602 }
00603 
00604 // Return a GLC_3DViewInstance pointer from the collection
00605 GLC_3DViewInstance* GLC_3DViewCollection::instanceHandle(GLC_uint Key)
00606 {
00607         return &(m_3DViewInstanceHash[Key]);
00608 }
00609 
00610 // return the entire collection Bounding Box
00611 GLC_BoundingBox GLC_3DViewCollection::boundingBox(void)
00612 {
00613         // If the bounding box is not valid delete it
00614         setBoundingBoxValidity();
00615 
00616         // Check if the bounding box have to be updated
00617         if ((m_pBoundingBox == NULL) && !m_3DViewInstanceHash.isEmpty())
00618         {
00619                 // There is objects in the collection and the collection or bounding box is not valid
00620                 if (m_pBoundingBox != NULL)
00621                 {
00622                         delete m_pBoundingBox;
00623                 }
00624                 m_pBoundingBox= new GLC_BoundingBox();
00625 
00626                 ViewInstancesHash::iterator iEntry= m_3DViewInstanceHash.begin();
00627             while (iEntry != m_3DViewInstanceHash.constEnd())
00628             {
00629                 if(iEntry.value().isVisible() == m_IsInShowSate)
00630                 {
00631                         // Combine Collection BoundingBox with element Bounding Box
00632                         m_pBoundingBox->combine(iEntry.value().boundingBox());
00633                 }
00634                 ++iEntry;
00635             }
00636         }
00637         else if (NULL == m_pBoundingBox)
00638         {
00639                 // The collection is empty and m_pBoundingBox == NULL
00640                 m_pBoundingBox= new GLC_BoundingBox();
00641         }
00642 
00643         return *m_pBoundingBox;
00644 }
00645 
00646 // Return the number of drawable objects
00647 int GLC_3DViewCollection::drawableObjectsSize() const
00648 {
00649         // The number of object to drw
00650         int numberOffDrawnHit= 0;
00651 
00652         // Count the number off instance to draw
00653         ViewInstancesHash::const_iterator i= m_3DViewInstanceHash.begin();
00654         while (i != m_3DViewInstanceHash.constEnd())
00655         {
00656                 //qDebug() << "transparent";
00657                 if (i.value().isVisible() == m_IsInShowSate)
00658                 {
00659                         ++numberOffDrawnHit;
00660                 }
00661                 ++i;
00662         }
00663         return numberOffDrawnHit;
00664 }
00665 
00666 // Return instances name from the specified shading group
00667 QList<QString> GLC_3DViewCollection::instanceNamesFromShadingGroup(GLuint shaderId) const
00668 {
00669         QList<QString> listOfInstanceName;
00670         QList<GLC_uint> listOfInstanceNameId= m_ShaderGroup.keys(shaderId);
00671         if (!listOfInstanceNameId.isEmpty())
00672         {
00673                 const int size= listOfInstanceNameId.size();
00674                 for (int i= 0; i < size; ++i)
00675                 {
00676                         listOfInstanceName << m_3DViewInstanceHash.value(listOfInstanceNameId[i]).name();
00677                 }
00678         }
00679         return listOfInstanceName;
00680 }
00681 
00682 // Return the number of used shading group
00683 int GLC_3DViewCollection::numberOfUsedShadingGroup() const
00684 {
00685         return m_ShaderGroup.values().toSet().size();
00686 }
00687 
00689 // OpenGL Functions
00691 
00692 void GLC_3DViewCollection::render(GLuint groupId, glc::RenderFlag renderFlag)
00693 {
00694         if (!isEmpty())
00695         {
00696                 if (renderFlag == glc::WireRenderFlag)
00697                 {
00698                 glEnable(GL_POLYGON_OFFSET_FILL);
00699                 glPolygonOffset (1.0, 1.0);
00700                 }
00701                 if (GLC_State::isInSelectionMode())
00702                 {
00703                         glDisable(GL_BLEND);
00704                         glDisable(GL_LIGHTING);
00705                         glDisable(GL_TEXTURE_2D);
00706                 }
00707                 else
00708                 {
00709                         glEnable(GL_LIGHTING);
00710                 }
00711                 glDraw(groupId, renderFlag);
00712 
00713                 if (renderFlag == glc::WireRenderFlag)
00714                 {
00715                 glDisable(GL_POLYGON_OFFSET_FILL);
00716                 }
00717 
00718         }
00719 }
00720 // Display all shader group
00721 void GLC_3DViewCollection::renderShaderGroup(glc::RenderFlag renderFlag)
00722 {
00723         if (!isEmpty())
00724         {
00725                 if (GLC_State::isInSelectionMode())
00726                 {
00727                         glDisable(GL_BLEND);
00728                         glDisable(GL_LIGHTING);
00729                         glDisable(GL_TEXTURE_2D);
00730                 }
00731 
00732                 HashList::iterator iEntry= m_ShadedPointerViewInstanceHash.begin();
00733             while (iEntry != m_ShadedPointerViewInstanceHash.constEnd())
00734             {
00735                 glDraw(iEntry.key(), renderFlag);
00736                 ++iEntry;
00737             }
00738         }
00739 }
00740 
00741 // Display the specified collection group
00742 void GLC_3DViewCollection::glDraw(GLuint groupId, glc::RenderFlag renderFlag)
00743 {
00744         // Set render Mode and OpenGL state
00745         if (!GLC_State::isInSelectionMode() && (groupId == 0))
00746         {
00747                 if (renderFlag == glc::TransparentRenderFlag)
00748                 {
00749                 glEnable(GL_BLEND);
00750                 glDepthMask(GL_FALSE);
00751                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00752 
00753                 }
00754                 else
00755                 {
00756                     glDisable(GL_BLEND);
00757                     glDepthMask(GL_TRUE);
00758                     glEnable(GL_DEPTH_TEST);
00759                 }
00760         }
00761 
00762         // Normal GLC_3DViewInstance
00763         if ((groupId == 0) && !m_MainInstances.isEmpty())
00764         {
00765             glDrawInstancesOf(&m_MainInstances, renderFlag);
00766 
00767         }
00768         // Selected GLC_3DVIewInstance
00769         else if ((groupId == 1) && !m_SelectedInstances.isEmpty())
00770         {
00771                 if (GLC_State::selectionShaderUsed()) GLC_SelectionMaterial::useShader();
00772 
00773                 glDrawInstancesOf(&m_SelectedInstances, renderFlag);
00774 
00775                 if (GLC_State::selectionShaderUsed()) GLC_SelectionMaterial::unUseShader();
00776         }
00777         // GLC_3DViewInstance with shader
00778         else if (!m_ShadedPointerViewInstanceHash.isEmpty())
00779         {
00780             if(m_ShadedPointerViewInstanceHash.contains(groupId) && !m_ShadedPointerViewInstanceHash.value(groupId)->isEmpty())
00781             {
00782                 PointerViewInstanceHash* pNodeHash= m_ShadedPointerViewInstanceHash.value(groupId);
00783 
00784                 GLC_Shader::use(groupId);
00785                 glDrawInstancesOf(pNodeHash, renderFlag);
00786                 GLC_Shader::unuse();
00787             }
00788         }
00789 
00790         // Restore OpenGL state
00791         if (renderFlag && !GLC_State::isInSelectionMode() && (groupId == 0))
00792         {
00793                 glDisable(GL_BLEND);
00794                 glDepthMask(GL_TRUE);
00795                 glEnable(GL_DEPTH_TEST);
00796         }
00797 
00798 }
00799 
00800 // Set the Bounding box validity
00801 void GLC_3DViewCollection::setBoundingBoxValidity(void)
00802 {
00803         if (NULL != m_pBoundingBox)
00804         {
00805                 if (!m_3DViewInstanceHash.isEmpty())
00806                 {
00807                         // Check instance bounding box validity
00808                         ViewInstancesHash::iterator iEntry= m_3DViewInstanceHash.begin();
00809                     while (iEntry != m_3DViewInstanceHash.constEnd())
00810                     {
00811                         if (!iEntry.value().boundingBoxValidity())
00812                         {
00813                                         delete m_pBoundingBox;
00814                                         m_pBoundingBox= NULL;
00815                                         return;
00816                         }
00817                         iEntry++;
00818                     }
00819                 }
00820                 else if (!m_pBoundingBox->isEmpty())
00821                 {
00822                         delete m_pBoundingBox;
00823                         m_pBoundingBox= NULL;
00824                 }
00825         }
00826 }

SourceForge.net Logo

©2005 Laurent Ribon