GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_selectionmaterial.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21 *****************************************************************************/
23 
24 #include <QGLContext>
25 
26 #include "glc_selectionmaterial.h"
27 #include "glc_material.h"
28 
29 
30 QHash<const QGLContext*, GLC_Shader*> GLC_SelectionMaterial::m_SelectionShaderHash;
34 
36 {
37 
38 }
39 
41 {
42  if (0 == m_SelectionMaterialId)
43  {
45  }
46  Q_ASSERT(NULL != pMaterial);
47  if (NULL != m_pMaterial)
48  {
50  if (m_pMaterial->isUnused())
51  {
52  delete m_pMaterial;
53  }
54  }
55  m_pMaterial= pMaterial;
57 }
58 
60 {
61  if (NULL != m_pMaterial)
62  {
64  if (m_pMaterial->isUnused())
65  {
66  delete m_pMaterial;
67  }
68  m_pMaterial= NULL;
69  }
70 }
71 
72 // Use selection material?
73 void GLC_SelectionMaterial::setUseSelectionMaterial(bool useSelectionMaterial) {
74  m_NoSelectionMaterial = !useSelectionMaterial;
75 }
76 
77 
78 // Execute OpenGL Material
80 {
81  if (m_NoSelectionMaterial) return;
82 
83  if (NULL != m_pMaterial)
84  {
86  }
87  else
88  {
89  // Use default selection color
90  static GLfloat pAmbientColor[4]= {1.0f, 0.376f, 0.223f, 1.0f};
91 
92  static GLfloat pDiffuseColor[4]= {1.0f, 0.376f, 0.223f, 1.0f};
93 
94  static GLfloat pSpecularColor[4]= {1.0f, 1.0f, 1.0f, 1.0f};
95 
96  static GLfloat pLightEmission[4]= {0.0f, 0.0f, 0.0f, 1.0f};
97 
98  static float shininess= 50.0f;
99 
100  glColor4fv(pAmbientColor);
101 
102  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, pAmbientColor);
103  glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pDiffuseColor);
104  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pSpecularColor);
105  glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, pLightEmission);
106  glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &shininess);
107  }
108 }
109 
110 void GLC_SelectionMaterial::initShader(const QGLContext* pContext)
111 {
112  Q_ASSERT(m_SelectionShaderHash.contains(pContext));
113  m_SelectionShaderHash.value(pContext)->createAndCompileProgrammShader();
114 }
115 
116 void GLC_SelectionMaterial::setShaders(QFile& vertex, QFile& fragment, const QGLContext* pContext)
117 {
118  if (m_SelectionShaderHash.contains(pContext))
119  {
120  deleteShader(pContext);
121  }
122  GLC_Shader* pShader= new GLC_Shader;
123 
124  pShader->setVertexAndFragmentShader(vertex, fragment);
125  m_SelectionShaderHash.insert(pContext, pShader);
126 }
127 
128 
130 {
131  QGLContext* pContext= const_cast<QGLContext*>(QGLContext::currentContext());
132  Q_ASSERT(NULL != pContext);
133  Q_ASSERT(pContext->isValid());
134  if(!m_SelectionShaderHash.contains(pContext))
135  {
136  Q_ASSERT(pContext->isSharing());
137  pContext= sharingContext(pContext);
138  Q_ASSERT(NULL != pContext);
139  }
140 
141  m_SelectionShaderHash.value(pContext)->use();
142 }
143 
145 {
146  QGLContext* pContext= const_cast<QGLContext*>(QGLContext::currentContext());
147  Q_ASSERT(NULL != pContext);
148  Q_ASSERT(pContext->isValid());
149  if(!m_SelectionShaderHash.contains(pContext))
150  {
151  Q_ASSERT(pContext->isSharing());
152  pContext= sharingContext(pContext);
153  Q_ASSERT(NULL != pContext);
154  }
155 
156  m_SelectionShaderHash.value(pContext)->unuse();
157 }
158 
160 // Private services fonction
162 QGLContext* GLC_SelectionMaterial::sharingContext(const QGLContext* pContext)
163 {
164  QGLContext* pSharingContext= NULL;
165  QHash<const QGLContext*, GLC_Shader*>::const_iterator iContext= m_SelectionShaderHash.constBegin();
166 
167  while ((NULL == pSharingContext) && (iContext != m_SelectionShaderHash.constEnd()))
168  {
169  const QGLContext* pCurrentContext= iContext.key();
170  if (QGLContext::areSharing(pContext, pCurrentContext))
171  {
172  pSharingContext= const_cast<QGLContext*>(pCurrentContext);
173  }
174  ++iContext;
175  }
176 
177  return pSharingContext;
178 }
179 
181 void GLC_SelectionMaterial::deleteShader(const QGLContext* pContext)
182 {
183  Q_ASSERT(m_SelectionShaderHash.contains(pContext));
184  GLC_Shader* pShader= m_SelectionShaderHash.value(pContext);
185  pShader->deleteShader();
186  delete pShader;
187  m_SelectionShaderHash.remove(pContext);
188 }

©2005-2013 Laurent Ribon