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         const QGLContext* pContext= QGLContext::currentContext();
00124         Q_ASSERT(NULL != pContext);
00125         Q_ASSERT(m_SelectionShaderHash.contains(pContext));
00126         m_SelectionShaderHash.value(pContext)->use();
00127 }
00128 
00129 void GLC_SelectionMaterial::unUseShader()
00130 {
00131         const QGLContext* pContext= QGLContext::currentContext();
00132         Q_ASSERT(NULL != pContext);
00133         Q_ASSERT(m_SelectionShaderHash.contains(pContext));
00134 
00135         m_SelectionShaderHash.value(pContext)->unuse();
00136 }
00137 
00139 void GLC_SelectionMaterial::deleteShader(const QGLContext* pContext)
00140 {
00141         Q_ASSERT(m_SelectionShaderHash.contains(pContext));
00142         GLC_Shader* pShader= m_SelectionShaderHash.value(pContext);
00143         pShader->deleteShader();
00144         delete pShader;
00145         m_SelectionShaderHash.remove(pContext);
00146 }

SourceForge.net Logo

©2005-2011 Laurent Ribon