GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_renderproperties.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 "glc_renderproperties.h"
25 
26 // Default constructor
28 : m_Uid(glc::GLC_GenUserID())
29 , m_IsSelected(false)
30 , m_PolyFace(GL_FRONT_AND_BACK)
31 , m_PolyMode(GL_FILL)
32 , m_RenderMode(glc::NormalRenderMode)
33 , m_SavedRenderMode(m_RenderMode)
34 , m_pOverwriteMaterial(NULL)
35 , m_OverwriteOpacity(-1.0f)
36 , m_pBodySelectedPrimitvesId(NULL)
37 , m_pOverwritePrimitiveMaterialMaps(NULL)
38 , m_RenderingFlag(glc::ShadingFlag)
39 , m_CurrentBody(0)
40 , m_MaterialsUsage()
41 {
42 
43 }
44 
45 // Copy constructor
47 : m_Uid(glc::GLC_GenUserID())
48 , m_IsSelected(renderProperties.m_IsSelected)
49 , m_PolyFace(renderProperties.m_PolyFace)
50 , m_PolyMode(renderProperties.m_PolyMode)
51 , m_RenderMode(renderProperties.m_RenderMode)
52 , m_SavedRenderMode(renderProperties.m_SavedRenderMode)
53 , m_pOverwriteMaterial(renderProperties.m_pOverwriteMaterial)
54 , m_OverwriteOpacity(renderProperties.m_OverwriteOpacity)
55 , m_pBodySelectedPrimitvesId(NULL)
56 , m_pOverwritePrimitiveMaterialMaps(NULL)
57 , m_RenderingFlag(renderProperties.m_RenderingFlag)
58 , m_CurrentBody(renderProperties.m_CurrentBody)
59 , m_MaterialsUsage(renderProperties.m_MaterialsUsage)
60 {
61  // Update overwrite material usage
62  if (NULL != m_pOverwriteMaterial)
63  {
65  }
66 
67  // Copy the Hash of set of id of selected primitives
68  if (NULL != renderProperties.m_pBodySelectedPrimitvesId)
69  {
70  m_pBodySelectedPrimitvesId= new QHash<int, QSet<GLC_uint>* >();
71  QHash<int, QSet<GLC_uint>* >::const_iterator iSet= renderProperties.m_pBodySelectedPrimitvesId->constBegin();
72  while (renderProperties.m_pBodySelectedPrimitvesId->constEnd() != iSet)
73  {
74  // Copy the current body set of id of selected primitive
75  m_pBodySelectedPrimitvesId->insert(iSet.key(), new QSet<GLC_uint>(*(iSet.value())));
76  ++iSet;
77  }
78  }
79 
80  // Copy of the overwrite primitive materials maps
81  if (NULL != renderProperties.m_pOverwritePrimitiveMaterialMaps)
82  {
83  // Copy the hash table of overwrite materials
84  m_pOverwritePrimitiveMaterialMaps= new QHash<int, QHash<GLC_uint, GLC_Material*>* >;
85  QHash<int, QHash<GLC_uint, GLC_Material*>* >::const_iterator iMatMaps= renderProperties.m_pOverwritePrimitiveMaterialMaps->constBegin();
86  while (renderProperties.m_pOverwritePrimitiveMaterialMaps->constEnd() != iMatMaps)
87  {
88  QHash<GLC_uint, GLC_Material*>* pBodyMatMap= new QHash<GLC_uint, GLC_Material*>(*(iMatMaps.value()));
89  m_pOverwritePrimitiveMaterialMaps->insert(iMatMaps.key(), pBodyMatMap);
90  ++iMatMaps;
91  }
92  }
93 
94  // Update material usage
95  QHash<GLC_Material*, int>::iterator iMatUsage= m_MaterialsUsage.begin();
96  while (m_MaterialsUsage.constEnd() != iMatUsage)
97  {
98  iMatUsage.key()->addUsage(m_Uid);
99  ++iMatUsage;
100  }
101 }
102 
103 // Assignement operator
105 {
106  if (this != &renderProperties)
107  {
108  clear();
109  m_IsSelected= renderProperties.m_IsSelected;
110  m_PolyFace= renderProperties.m_PolyFace;
111  m_PolyMode= renderProperties.m_PolyMode;
112  m_RenderMode= renderProperties.m_RenderMode;
113  m_SavedRenderMode= renderProperties.m_SavedRenderMode;
114  m_pOverwriteMaterial= renderProperties.m_pOverwriteMaterial;
115  m_OverwriteOpacity= renderProperties.m_OverwriteOpacity;
118  m_RenderingFlag= renderProperties.m_RenderingFlag;
119  m_CurrentBody= renderProperties.m_CurrentBody;
120 
121  // Update overwrite material usage
122  if (NULL != m_pOverwriteMaterial)
123  {
125  }
126 
127  // Copy the Hash of set of id of selected primitives
128  if (NULL != renderProperties.m_pBodySelectedPrimitvesId)
129  {
130  m_pBodySelectedPrimitvesId= new QHash<int, QSet<GLC_uint>* >();
131  QHash<int, QSet<GLC_uint>* >::const_iterator iSet= renderProperties.m_pBodySelectedPrimitvesId->constBegin();
132  while (renderProperties.m_pBodySelectedPrimitvesId->constEnd() != iSet)
133  {
134  // Copy the current body set of id of selected primitive
135  m_pBodySelectedPrimitvesId->insert(iSet.key(), new QSet<GLC_uint>(*(iSet.value())));
136  ++iSet;
137  }
138  }
139 
140  // Update primitive overwrite material usage
141  if (NULL != renderProperties.m_pOverwritePrimitiveMaterialMaps)
142  {
143  // Copy the hash table of overwrite materials
144  m_pOverwritePrimitiveMaterialMaps= new QHash<int, QHash<GLC_uint, GLC_Material*>* >;
145  QHash<int, QHash<GLC_uint, GLC_Material*>* >::const_iterator iMatMaps= renderProperties.m_pOverwritePrimitiveMaterialMaps->constBegin();
146  while (renderProperties.m_pOverwritePrimitiveMaterialMaps->constEnd() != iMatMaps)
147  {
148  QHash<GLC_uint, GLC_Material*>* pBodyMatMap= new QHash<GLC_uint, GLC_Material*>(*(iMatMaps.value()));
149  m_pOverwritePrimitiveMaterialMaps->insert(iMatMaps.key(), pBodyMatMap);
150 
151  QHash<GLC_uint, GLC_Material*>::iterator iMatMap= pBodyMatMap->begin();
152  while (pBodyMatMap->constEnd() != iMatMap)
153  {
154  iMatMap.value()->addUsage(m_Uid);
155  ++iMatMap;
156  }
157 
158  ++iMatMaps;
159  }
160  }
161  }
162 
163  return *this;
164 }
165 
166 // Destructor
168 {
169  clear();
170 }
171 
172 // Return true if rendering properties needs to render with transparency
174 {
175  bool renderWithTransparency= false;
177  {
178  Q_ASSERT(NULL != m_pOverwriteMaterial);
179  renderWithTransparency= m_pOverwriteMaterial->isTransparent();
180  }
182  {
183  Q_ASSERT(-1.0f != m_OverwriteOpacity);
184  renderWithTransparency= (m_OverwriteOpacity < 1.0f);
185  }
188  {
189  Q_ASSERT(NULL != m_pOverwritePrimitiveMaterialMaps);
190  Q_ASSERT(!m_pOverwritePrimitiveMaterialMaps->isEmpty());
191 
192  QList<QHash<GLC_uint, GLC_Material* >* > hashList= m_pOverwritePrimitiveMaterialMaps->values();
193  QSet<GLC_Material*> materialSet;
194  const int size= hashList.size();
195  for (int i= 0; i < size; ++i)
196  {
197  materialSet.unite(QSet<GLC_Material*>::fromList(hashList.at(i)->values()));
198  }
199 
200  QSet<GLC_Material*>::const_iterator iMat= materialSet.constBegin();
201  while ((materialSet.constEnd() != iMat) && !renderWithTransparency)
202  {
203  renderWithTransparency= (*iMat)->isTransparent();
204  ++iMat;
205  }
206  }
207 
208  return renderWithTransparency;
209 }
210 
212 {
213  bool isDefault= (NULL == m_pOverwriteMaterial);
214  isDefault= isDefault && (m_OverwriteOpacity == -1.0f);
215  return isDefault;
216 }
217 
218 // Clear the content of the render properties and update materials usage
220 {
221  if (NULL != m_pOverwriteMaterial)
222  {
223  // Delete the material if it is unused
226  m_pOverwriteMaterial= NULL;
227  }
228 
230 
232 }
233 
234 // Set the overwrite material
236 {
237  Q_ASSERT(NULL != pMaterial);
238  if (NULL != m_pOverwriteMaterial)
239  {
242  }
243  m_pOverwriteMaterial= pMaterial;
244 
246 }
247 
248 // Return true if the specified primitive id of the specified body index is selected
250 {
251  bool result= false;
253  {
254  result= m_pBodySelectedPrimitvesId->value(index)->contains(id);
255  }
256  return result;
257 }
258 
259 // Set the list of selected primitives id
260 void GLC_RenderProperties::addSetOfSelectedPrimitivesId(const QSet<GLC_uint>& set, int body)
261 {
262  if (NULL == m_pBodySelectedPrimitvesId)
263  {
264  m_pBodySelectedPrimitvesId= new QHash<int, QSet<GLC_uint>* >();
265  m_pBodySelectedPrimitvesId->insert(body, new QSet<GLC_uint>(set));
266  }
267  else if (!m_pBodySelectedPrimitvesId->contains(body))
268  {
269  m_pBodySelectedPrimitvesId->insert(body, new QSet<GLC_uint>(set));
270  }
271  else
272  {
273  m_pBodySelectedPrimitvesId->value(body)->unite(set);
274  }
275 }
276 
277 // Add a selected primitive
279 {
280  if (NULL == m_pBodySelectedPrimitvesId)
281  {
282  m_pBodySelectedPrimitvesId= new QHash<int, QSet<GLC_uint>* >();
283  m_pBodySelectedPrimitvesId->insert(body, new QSet<GLC_uint>());
284 
285  }
286  else if (!m_pBodySelectedPrimitvesId->contains(body))
287  {
288  m_pBodySelectedPrimitvesId->insert(body, new QSet<GLC_uint>());
289  }
290  m_pBodySelectedPrimitvesId->value(body)->insert(id);
291 }
292 
293 // Clear selectedPrimitive Set
295 {
296  if (NULL != m_pBodySelectedPrimitvesId)
297  {
298  QHash<int, QSet<GLC_uint>* >::const_iterator iSet= m_pBodySelectedPrimitvesId->constBegin();
299  while (m_pBodySelectedPrimitvesId->constEnd() != iSet)
300  {
301  delete iSet.value();
302  ++iSet;
303  }
304  }
307 }
308 
309 // Add an overwrite primitive material
311 {
312  Q_ASSERT(NULL != pMaterial);
314  {
315  if (m_pOverwritePrimitiveMaterialMaps->contains(bodyIndex))
316  {
317  QHash<GLC_uint, GLC_Material*>* pHash= m_pOverwritePrimitiveMaterialMaps->value(bodyIndex);
318  if (pHash->contains(id))
319  {
320  if (pHash->value(id) != pMaterial)
321  {
322  GLC_Material* pOldMaterial= pHash->value(id);
323  unUseMaterial(pOldMaterial);
324  pHash->remove(id);
325 
326  pHash->insert(id, pMaterial);
327  useMaterial(pMaterial);
328  }
329  // Else, noting to do
330  }
331  else
332  {
333  pHash->insert(id, pMaterial);
334  useMaterial(pMaterial);
335  }
336  }
337  else
338  {
339  QHash<GLC_uint, GLC_Material*>* pHash= new QHash<GLC_uint, GLC_Material*>();
340  pHash->insert(id, pMaterial);
341  useMaterial(pMaterial);
342  m_pOverwritePrimitiveMaterialMaps->insert(bodyIndex, pHash);
343  }
344  }
345  else
346  {
347  m_pOverwritePrimitiveMaterialMaps= new QHash<int, QHash<GLC_uint, GLC_Material*>* >();
348  QHash<GLC_uint, GLC_Material*>* pHash= new QHash<GLC_uint, GLC_Material*>();
349  pHash->insert(id, pMaterial);
350  m_pOverwritePrimitiveMaterialMaps->insert(bodyIndex, pHash);
351  useMaterial(pMaterial);
352  }
353 }
354 
355 // Clear overwrite primitive materials
357 {
359  {
360  Q_ASSERT(!m_MaterialsUsage.isEmpty());
361  QHash<int, QHash<GLC_uint, GLC_Material* >* >::iterator iHash= m_pOverwritePrimitiveMaterialMaps->begin();
362  while (m_pOverwritePrimitiveMaterialMaps->constEnd() != iHash)
363  {
364  delete iHash.value();
365  ++iHash;
366  }
367 
368  QHash<GLC_Material*, int>::iterator iMat= m_MaterialsUsage.begin();
369  while (m_MaterialsUsage.constEnd() != iMat)
370  {
371  GLC_Material* pMat= iMat.key();
372  pMat->delUsage(m_Uid);
373  if (pMat->isUnused()) delete pMat;
374  ++iMat;
375  }
376  m_MaterialsUsage.clear();
377  }
378  else
379  {
380  Q_ASSERT(m_MaterialsUsage.isEmpty());
381  }
382 
385 
387  {
389  }
390 }
391 

©2005-2013 Laurent Ribon