glc_selectionmaterial.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00073 void GLC_SelectionMaterial::glExecute()
00074 {
00075 if (NULL != m_pMaterial)
00076 {
00077 m_pMaterial->glExecute();
00078 }
00079 else
00080 {
00081
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 }