glc_selectionmaterial.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  http://glc-lib.sourceforge.net
00006 
00007  GLC-lib is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU Lesser General Public License as published by
00009  the Free Software Foundation; either version 3 of the License, or
00010  (at your option) any later version.
00011 
00012  GLC-lib is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU Lesser General Public License for more details.
00016 
00017  You should have received a copy of the GNU Lesser General Public License
00018  along with GLC-lib; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 *****************************************************************************/
00023 
00024 #include <QGLContext>
00025 
00026 #include "glc_selectionmaterial.h"
00027 #include "glc_material.h"
00028 
00029 
00030 QHash<const QGLContext*, GLC_Shader*> GLC_SelectionMaterial::m_SelectionShaderHash;
00031 GLC_uint GLC_SelectionMaterial::m_SelectionMaterialId= 0;
00032 GLC_Material* GLC_SelectionMaterial::m_pMaterial= NULL;
00033 
00034 GLC_SelectionMaterial::GLC_SelectionMaterial()
00035 {
00036 
00037 }
00038 
00039 void GLC_SelectionMaterial::useMaterial(GLC_Material* pMaterial)
00040 {
00041         if (0 == m_SelectionMaterialId)
00042         {
00043                 m_SelectionMaterialId= glc::GLC_GenUserID();
00044         }
00045         Q_ASSERT(NULL != pMaterial);
00046         if (NULL != m_pMaterial)
00047         {
00048                 m_pMaterial->delUsage(m_SelectionMaterialId);
00049                 if (m_pMaterial->isUnused())
00050                 {
00051                         delete m_pMaterial;
00052                 }
00053         }
00054                 m_pMaterial= pMaterial;
00055                 m_pMaterial->addUsage(m_SelectionMaterialId);
00056 }
00057 
00058 void GLC_SelectionMaterial::useDefautSelectionColor()
00059 {
00060         if (NULL != m_pMaterial)
00061         {
00062                 m_pMaterial->delUsage(m_SelectionMaterialId);
00063                 if (m_pMaterial->isUnused())
00064                 {
00065                         delete m_pMaterial;
00066                 }
00067                 m_pMaterial= NULL;
00068         }
00069 }
00070 
00071 
00072 // Execute OpenGL Material
00073 void GLC_SelectionMaterial::glExecute()
00074 {
00075         if (NULL != m_pMaterial)
00076         {
00077                 m_pMaterial->glExecute();
00078         }
00079         else
00080         {
00081                 // Use default selection color
00082                 static GLfloat pAmbientColor[4]= {1.0f, 0.376f, 0.223f, 1.0f};
00083 
00084                 static GLfloat pDiffuseColor[4]= {1.0f, 0.376f, 0.223f, 1.0f};
00085 
00086                 static GLfloat pSpecularColor[4]= {1.0f, 1.0f, 1.0f, 1.0f};
00087 
00088                 static GLfloat pLightEmission[4]= {0.0f, 0.0f, 0.0f, 1.0f};
00089 
00090                 static float shininess= 50.0f;
00091 
00092                 glColor4fv(pAmbientColor);
00093 
00094                 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, pAmbientColor);
00095                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pDiffuseColor);
00096                 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pSpecularColor);
00097                 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, pLightEmission);
00098                 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &shininess);
00099         }
00100 }
00101 
00102 void GLC_SelectionMaterial::initShader(const QGLContext* pContext)
00103 {
00104         Q_ASSERT(m_SelectionShaderHash.contains(pContext));
00105         m_SelectionShaderHash.value(pContext)->createAndCompileProgrammShader();
00106 }
00107 
00108 void GLC_SelectionMaterial::setShaders(QFile& vertex, QFile& fragment, const QGLContext* pContext)
00109 {
00110         if (m_SelectionShaderHash.contains(pContext))
00111         {
00112                 deleteShader(pContext);
00113         }
00114         GLC_Shader* pShader= new GLC_Shader;
00115 
00116         pShader->setVertexAndFragmentShader(vertex, fragment);
00117         m_SelectionShaderHash.insert(pContext, pShader);
00118 }
00119 
00120 
00121 void GLC_SelectionMaterial::useShader()
00122 {
00123         QGLContext* pContext= const_cast<QGLContext*>(QGLContext::currentContext());
00124         Q_ASSERT(NULL != pContext);
00125         Q_ASSERT(pContext->isValid());
00126         if(!m_SelectionShaderHash.contains(pContext))
00127         {
00128                 Q_ASSERT(pContext->isSharing());
00129                 pContext= sharingContext(pContext);
00130                 Q_ASSERT(NULL != pContext);
00131         }
00132 
00133         m_SelectionShaderHash.value(pContext)->use();
00134 }
00135 
00136 void GLC_SelectionMaterial::unUseShader()
00137 {
00138         QGLContext* pContext= const_cast<QGLContext*>(QGLContext::currentContext());
00139         Q_ASSERT(NULL != pContext);
00140         Q_ASSERT(pContext->isValid());
00141         if(!m_SelectionShaderHash.contains(pContext))
00142         {
00143                 Q_ASSERT(pContext->isSharing());
00144                 pContext= sharingContext(pContext);
00145                 Q_ASSERT(NULL != pContext);
00146         }
00147 
00148         m_SelectionShaderHash.value(pContext)->unuse();
00149 }
00150 
00152 // Private services fonction
00154 QGLContext* GLC_SelectionMaterial::sharingContext(const QGLContext* pContext)
00155 {
00156         QGLContext* pSharingContext= NULL;
00157         QHash<const QGLContext*, GLC_Shader*>::const_iterator iContext= m_SelectionShaderHash.constBegin();
00158 
00159         while ((NULL == pSharingContext) && (iContext != m_SelectionShaderHash.constEnd()))
00160         {
00161                 const QGLContext* pCurrentContext= iContext.key();
00162                 if (QGLContext::areSharing(pContext, pCurrentContext))
00163                 {
00164                         pSharingContext= const_cast<QGLContext*>(pCurrentContext);
00165                 }
00166                 ++iContext;
00167         }
00168 
00169         return pSharingContext;
00170 }
00171 
00173 void GLC_SelectionMaterial::deleteShader(const QGLContext* pContext)
00174 {
00175         Q_ASSERT(m_SelectionShaderHash.contains(pContext));
00176         GLC_Shader* pShader= m_SelectionShaderHash.value(pContext);
00177         pShader->deleteShader();
00178         delete pShader;
00179         m_SelectionShaderHash.remove(pContext);
00180 }

SourceForge.net Logo

©2005-2011 Laurent Ribon