5206f9fef04cdede1cc2a91395ef487f0d1ed078
[occt.git] / src / MeshVS / MeshVS_ElementalColorPrsBuilder.cxx
1 // Created on: 2003-11-12
2 // Created by: Alexander SOLOVYOV
3 // Copyright (c) 2003-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <MeshVS_ElementalColorPrsBuilder.ixx>
17
18 #include <Graphic3d_AspectFillArea3d.hxx>
19 #include <Graphic3d_AspectLine3d.hxx>
20 #include <Graphic3d_ArrayOfPolygons.hxx>
21 #include <Graphic3d_ArrayOfPolylines.hxx>
22 #include <Graphic3d_ArrayOfSegments.hxx>
23 #include <Graphic3d_ArrayOfTriangles.hxx>
24 #include <Graphic3d_Group.hxx>
25
26 #include <Prs3d_ShadingAspect.hxx>
27 #include <Prs3d_Root.hxx>
28 #include <Prs3d_LineAspect.hxx>
29
30 #include <TColStd_MapOfInteger.hxx>
31 #include <TColStd_PackedMapOfInteger.hxx>
32 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
33 #include <TColStd_ListIteratorOfListOfInteger.hxx>
34 #include <TColStd_Array1OfReal.hxx>
35 #include <TColStd_SequenceOfInteger.hxx>
36 #include <TColStd_HArray1OfReal.hxx>
37 #include <TColStd_HPackedMapOfInteger.hxx>
38
39 #include <MeshVS_DisplayModeFlags.hxx>
40 #include <MeshVS_DataSource.hxx>
41 #include <MeshVS_Mesh.hxx>
42 #include <MeshVS_DataMapOfColorMapOfInteger.hxx>
43 #include <MeshVS_DataMapOfTwoColorsMapOfInteger.hxx>
44 #include <MeshVS_Drawer.hxx>
45 #include <MeshVS_DrawerAttribute.hxx>
46 #include <MeshVS_DataMapIteratorOfDataMapOfIntegerTwoColors.hxx>
47 #include <MeshVS_DataMapIteratorOfDataMapOfIntegerColor.hxx>
48 #include <MeshVS_DataMapIteratorOfDataMapOfTwoColorsMapOfInteger.hxx>
49 #include <MeshVS_DataMapIteratorOfDataMapOfColorMapOfInteger.hxx>
50 #include <MeshVS_MeshPrsBuilder.hxx>
51 #include <MeshVS_Buffer.hxx>
52 #include <MeshVS_HArray1OfSequenceOfInteger.hxx>
53
54 //================================================================
55 // Function : Constructor MeshVS_ElementalColorPrsBuilder
56 // Purpose  :
57 //================================================================
58 MeshVS_ElementalColorPrsBuilder::MeshVS_ElementalColorPrsBuilder
59   ( const Handle(MeshVS_Mesh)& Parent,
60     const MeshVS_DisplayModeFlags& Flags,
61     const Handle (MeshVS_DataSource)& DS,
62     const Standard_Integer Id,
63     const MeshVS_BuilderPriority& Priority )
64 : MeshVS_PrsBuilder ( Parent, Flags, DS, Id, Priority )
65 {
66   SetExcluding ( Standard_True );
67 }
68
69 //================================================================
70 // Function : Build
71 // Purpose  :
72 //================================================================
73 void MeshVS_ElementalColorPrsBuilder::Build ( const Handle(Prs3d_Presentation)& Prs,
74                                               const TColStd_PackedMapOfInteger& IDs,
75                                               TColStd_PackedMapOfInteger& IDsToExclude,
76                                               const Standard_Boolean IsElement,
77                                               const Standard_Integer DisplayMode) const
78 {
79   Handle (MeshVS_DataSource) aSource = GetDataSource();
80   Handle (MeshVS_Drawer)     aDrawer = GetDrawer();
81
82   if ( aSource.IsNull() || aDrawer.IsNull() )
83     return;
84
85   Standard_Integer aMaxFaceNodes;
86   if ( !aDrawer->GetInteger ( MeshVS_DA_MaxFaceNodes, aMaxFaceNodes ) && aMaxFaceNodes<=0 )
87     return;
88
89   MeshVS_DataMapOfIntegerColor* anElemColorMap = (MeshVS_DataMapOfIntegerColor*) &myElemColorMap1;
90   MeshVS_DataMapOfIntegerTwoColors* anElemTwoColorsMap = (MeshVS_DataMapOfIntegerTwoColors*)&myElemColorMap2;
91
92   MeshVS_DataMapOfColorMapOfInteger     aColorsOfElements;
93   MeshVS_DataMapOfTwoColorsMapOfInteger aTwoColorsOfElements;
94
95   MeshVS_Buffer aCoordsBuf (3*aMaxFaceNodes*sizeof(Standard_Real));
96   TColStd_Array1OfReal aCoords (aCoordsBuf, 1, 3*aMaxFaceNodes);
97   Standard_Integer NbNodes;
98   MeshVS_EntityType aType;
99
100   if ( !( DisplayMode & GetFlags() ) || !IsElement ||
101        ( myElemColorMap1.IsEmpty() && myElemColorMap2.IsEmpty() ) )
102     return;
103
104   // subtract the hidden elements and ids to exclude (to minimise allocated memory)
105   TColStd_PackedMapOfInteger anIDs;
106   anIDs.Assign( IDs );
107   Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems();
108   if ( !aHiddenElems.IsNull() )
109     anIDs.Subtract( aHiddenElems->Map() );
110   anIDs.Subtract( IDsToExclude );
111
112   // STEP 0: We looking for two colored elements, who has equal two colors and move it
113   // to map of elements with one assigned color
114   TColStd_ListOfInteger aColorOne;
115   for ( MeshVS_DataMapIteratorOfDataMapOfIntegerTwoColors anIter ( *anElemTwoColorsMap ); anIter.More(); anIter.Next () )
116   {
117     Standard_Integer aKey   = anIter.Key ();
118     MeshVS_TwoColors aValue = anIter.Value ();
119     Quantity_Color   aCol1, aCol2;
120     ExtractColors ( aValue, aCol1, aCol2 );
121     if ( aCol1 == aCol2 )
122     {
123       aColorOne.Append ( aKey );
124       anElemColorMap->Bind ( aKey, aCol1 );
125     }
126   }
127
128   for ( TColStd_ListIteratorOfListOfInteger aLIter ( aColorOne ); aLIter.More(); aLIter.Next() )
129     anElemTwoColorsMap->UnBind ( aLIter.Value() );
130
131   // The map is to resort itself by colors.
132   // STEP 1: We start sorting elements with one assigned color
133   for ( MeshVS_DataMapIteratorOfDataMapOfIntegerColor anIterM ( *anElemColorMap ); anIterM.More(); anIterM.Next () )
134   {
135     Standard_Integer aMKey = anIterM.Key ();
136     // The ID of current element
137     Standard_Boolean IsExist = Standard_False;
138     for ( MeshVS_DataMapIteratorOfDataMapOfColorMapOfInteger anIterC ( aColorsOfElements );
139           anIterC.More() && !IsExist; anIterC.Next () )
140       if ( anIterC.Key()==anIterM.Value() )
141       {
142         TColStd_MapOfInteger& aChangeValue = (TColStd_MapOfInteger&) anIterC.Value();
143         aChangeValue.Add ( aMKey );
144         IsExist = Standard_True;
145       }
146
147     if ( !IsExist )
148     {
149       TColStd_MapOfInteger aNewMap; aNewMap.Add ( aMKey );
150       aColorsOfElements.Bind ( anIterM.Value(), aNewMap );
151     }
152   }
153
154   // STEP 2: We start sorting elements with two assigned colors
155   for ( MeshVS_DataMapIteratorOfDataMapOfIntegerTwoColors anIterM2 ( *anElemTwoColorsMap ); anIterM2.More();
156         anIterM2.Next () )
157   {
158     Standard_Integer aMKey = anIterM2.Key ();
159     // The ID of current element
160     Standard_Boolean IsExist = Standard_False;
161     for ( MeshVS_DataMapIteratorOfDataMapOfTwoColorsMapOfInteger anIterC2 ( aTwoColorsOfElements );
162           anIterC2.More() && !IsExist; anIterC2.Next () )
163       if ( IsEqual ( anIterC2.Key(), anIterM2.Value() ) )
164       {
165         TColStd_MapOfInteger& aChangeValue = (TColStd_MapOfInteger&) anIterC2.Value();
166         aChangeValue.Add ( aMKey );
167         IsExist = Standard_True;
168       }
169
170     if ( !IsExist )
171     {
172       TColStd_MapOfInteger aNewMap; aNewMap.Add ( aMKey );
173       aTwoColorsOfElements.Bind ( anIterM2.Value(), aNewMap );
174     }
175   }
176
177   //Now we are ready to draw faces with equal colors
178   Aspect_TypeOfLine    anEdgeType = Aspect_TOL_SOLID;
179   Aspect_TypeOfLine    aLineType = Aspect_TOL_SOLID;
180   Standard_Integer     anEdgeInt, aLineInt;
181   Standard_Real        anEdgeWidth, aLineWidth;
182   Quantity_Color       anInteriorColor;
183   Quantity_Color       anEdgeColor, aLineColor;
184   Standard_Boolean     anEdgeOn = Standard_True, IsReflect = Standard_False,
185                        IsMeshSmoothShading = Standard_False;
186
187   aDrawer->GetColor  ( MeshVS_DA_InteriorColor, anInteriorColor );
188   aDrawer->GetColor  ( MeshVS_DA_EdgeColor, anEdgeColor );
189   aDrawer->GetColor  ( MeshVS_DA_BeamColor, aLineColor );
190   aDrawer->GetDouble ( MeshVS_DA_EdgeWidth, anEdgeWidth );
191   aDrawer->GetDouble ( MeshVS_DA_BeamWidth, aLineWidth );
192   aDrawer->GetBoolean( MeshVS_DA_ShowEdges, anEdgeOn );
193   aDrawer->GetBoolean( MeshVS_DA_ColorReflection, IsReflect );
194   aDrawer->GetBoolean( MeshVS_DA_SmoothShading, IsMeshSmoothShading );
195
196   if ( aDrawer->GetInteger ( MeshVS_DA_EdgeType, anEdgeInt) )
197     anEdgeType = (Aspect_TypeOfLine) anEdgeInt;
198
199   if ( aDrawer->GetInteger ( MeshVS_DA_BeamType, aLineInt) )
200     aLineType = (Aspect_TypeOfLine) aLineInt;
201
202   Handle( MeshVS_HArray1OfSequenceOfInteger ) aTopo;
203   Standard_Integer PolygonVerticesFor3D = 0, PolygonBoundsFor3D = 0;
204   TColStd_MapIteratorOfPackedMapOfInteger it (anIDs);
205   for( ; it.More(); it.Next() )
206   {
207     Standard_Integer aKey = it.Key();
208     if( aSource->Get3DGeom( aKey, NbNodes, aTopo ) )
209         MeshVS_MeshPrsBuilder::HowManyPrimitives
210           ( aTopo, Standard_True, Standard_False, NbNodes,
211             PolygonVerticesFor3D, PolygonBoundsFor3D );
212   }
213
214   Graphic3d_MaterialAspect aMaterial[2];
215   for (Standard_Integer i = 0; i < 2; i++)
216   {
217     // OCC20644 "plastic" is most suitable here, as it is "non-physic"
218     // so TelUpdateMaterial() from OpenGl_attri.c uses the interior
219     // color from AspectFillArea3d to calculate all material colors
220     aMaterial[i] = Graphic3d_MaterialAspect ( Graphic3d_NOM_PLASTIC );
221
222     // OCC21720 For single-colored elements turning all material components off is a good idea,
223     // as anyhow the normals are not computed and the lighting will be off,
224     // the element color will be taken from Graphic3d_AspectFillArea3d's interior color,
225     // and there is no need to spend time on updating material properties 
226     if ( !IsReflect )
227     {
228       aMaterial[i].SetReflectionModeOff(Graphic3d_TOR_AMBIENT);
229       aMaterial[i].SetReflectionModeOff(Graphic3d_TOR_DIFFUSE);
230       aMaterial[i].SetReflectionModeOff(Graphic3d_TOR_SPECULAR);
231       aMaterial[i].SetReflectionModeOff(Graphic3d_TOR_EMISSION);
232     }
233     else
234     {
235       // OCC20644 This stuff is important in order for elemental and nodal colors
236       // to produce similar visual impression and also to make colors match
237       // those in the color scale most exactly (the sum of all reflection 
238       // coefficients is equal to 1). See also MeshVS_NodalColorPrsBuilder
239       // class for more explanations.
240       aMaterial[i].SetAmbient( .5 );
241       aMaterial[i].SetDiffuse( .5 );
242       aMaterial[i].SetSpecular( 0. );
243       aMaterial[i].SetEmissive( 0. );
244     }
245   }
246
247   // Draw elements with one color
248   for ( MeshVS_DataMapIteratorOfDataMapOfColorMapOfInteger aColIter ( aColorsOfElements ); aColIter.More();
249         aColIter.Next() )
250   {
251     Standard_Integer aSize = aColIter.Value().Extent();
252     if ( aSize<=0 )
253       continue;
254
255     TColStd_PackedMapOfInteger aCustomElements;
256
257     Prs3d_Root::NewGroup ( Prs );
258     Handle ( Graphic3d_Group ) aGGroup = Prs3d_Root::CurrentGroup ( Prs );
259     Prs3d_Root::NewGroup ( Prs );
260     Handle ( Graphic3d_Group ) aLGroup = Prs3d_Root::CurrentGroup ( Prs );
261     Prs3d_Root::NewGroup ( Prs );
262     Handle ( Graphic3d_Group ) aSGroup = Prs3d_Root::CurrentGroup ( Prs );
263
264     Standard_Integer aNbFacePrimitives = 0;
265     Standard_Integer aNbVolmPrimitives = 0;
266     Standard_Integer aNbEdgePrimitives = 0;
267     Standard_Integer aNbLinkPrimitives = 0;
268
269     for (it.Reset(); it.More(); it.Next())
270     {
271       Standard_Integer aNbNodes = 0;
272
273       if (!aColIter.Value().Contains (it.Key()))
274         continue;
275
276       if (!aSource->GetGeom (it.Key(), Standard_True, aCoords, aNbNodes, aType))
277         continue;
278
279       if (aType == MeshVS_ET_Volume)
280       {
281         if (aSource->Get3DGeom (it.Key(), aNbNodes, aTopo))
282         {
283           for (Standard_Integer aFaceIdx = aTopo->Lower(); aFaceIdx <= aTopo->Upper(); ++aFaceIdx)
284           {
285             const TColStd_SequenceOfInteger& aFaceNodes = aTopo->Value (aFaceIdx);
286
287             if (anEdgeOn) // add edge segments
288             {
289               aNbEdgePrimitives += aFaceNodes.Length();
290             }
291
292             if (IsReflect) // add volumetric cell triangles
293             {
294               aNbVolmPrimitives += aFaceNodes.Length() - 2;
295             }
296           }
297         }
298       }
299       else if (aType == MeshVS_ET_Link)
300       {
301         if (anEdgeOn)
302         {
303           aNbLinkPrimitives += aNbNodes - 1; // add link segments
304         }
305       }
306       else if (aType == MeshVS_ET_Face)
307       {
308         if (anEdgeOn)
309         {
310           aNbEdgePrimitives += aNbNodes; // add edge segments
311         }
312           
313         aNbFacePrimitives += aNbNodes - 2; // add face triangles
314       }
315     }
316
317     // Here we do not use indices arrays because they are not effective for some mesh
318     // drawing modes: shrinking mode (displaces the vertices inside the polygon), 3D
319     // cell rendering (normal interpolation is not always applicable - flat shading),
320     // elemental coloring (color interpolation is impossible)
321
322     Handle (Graphic3d_ArrayOfTriangles) aFaceTriangles = new Graphic3d_ArrayOfTriangles (
323      (aNbFacePrimitives + aNbVolmPrimitives) * 3, 0, IsReflect );
324     Standard_Boolean IsPolyG = Standard_False;
325
326     Handle (Graphic3d_ArrayOfSegments) anEdgeSegments = new Graphic3d_ArrayOfSegments (aNbEdgePrimitives * 2);
327     Handle (Graphic3d_ArrayOfSegments) aLinkSegments = new Graphic3d_ArrayOfSegments (aNbLinkPrimitives * 2);
328     Standard_Boolean IsPolyL = Standard_False;
329
330     // OCC20644 NOTE: aColIter.Key() color is then scaled by TelUpdateMaterial() in OpenGl_attri.c
331     // using the material reflection coefficients. This affects the visual result.
332     Handle(Graphic3d_AspectFillArea3d) aFillAspect =
333       new Graphic3d_AspectFillArea3d ( Aspect_IS_SOLID, aColIter.Key(), anEdgeColor,
334                                        anEdgeType, anEdgeWidth, aMaterial[0], aMaterial[1] );
335
336     Handle(Graphic3d_AspectLine3d) aLinkAspect =
337       new Graphic3d_AspectLine3d ( aColIter.Key(), aLineType, aLineWidth );
338
339     Handle(Graphic3d_AspectLine3d) anEdgeAspect =
340       new Graphic3d_AspectLine3d ( anEdgeColor, anEdgeType, anEdgeWidth );
341
342     aFillAspect->SetDistinguishOff ();
343     aFillAspect->SetInteriorColor ( aColIter.Key() );
344     if (anEdgeOn)
345       aFillAspect->SetEdgeOn();
346     else
347       aFillAspect->SetEdgeOff();
348
349     for (it.Reset(); it.More(); it.Next())
350     {
351       Standard_Integer aKey = it.Key();
352       
353       if (aColIter.Value().Contains (aKey))
354       {
355         if (!aSource->GetGeom (aKey, Standard_True, aCoords, NbNodes, aType))
356           continue;
357         
358         if (aType != MeshVS_ET_Face && aType != MeshVS_ET_Link && aType != MeshVS_ET_Volume)
359         {
360           aCustomElements.Add (aKey);
361           continue;
362         }
363         
364         if (IsExcludingOn())
365           IDsToExclude.Add (aKey);
366           
367         if (aType == MeshVS_ET_Volume)
368         {
369           if (!aSource->Get3DGeom (aKey, NbNodes, aTopo))
370           {
371             continue;
372           }
373           
374           MeshVS_MeshPrsBuilder::AddVolumePrs (aTopo, aCoords,
375             NbNodes, aFaceTriangles, IsReflect, Standard_False, Standard_False, 1.0);
376
377           if (anEdgeOn)
378           {
379             MeshVS_MeshPrsBuilder::AddVolumePrs (aTopo, aCoords,
380               NbNodes, anEdgeSegments, IsReflect, Standard_False, Standard_False, 1.0);
381           }
382
383           IsPolyG = Standard_True;
384         }
385         else if (aType == MeshVS_ET_Face)
386         {
387           // Preparing normals
388           Handle(TColStd_HArray1OfReal) aNormals;
389           Standard_Boolean aHasNormals = IsReflect && aSource->GetNormalsByElement (aKey, IsMeshSmoothShading, aMaxFaceNodes, aNormals);
390
391           for (Standard_Integer aNodeIdx = 0; aNodeIdx < NbNodes - 2; ++aNodeIdx)
392           {
393             for (Standard_Integer anIdx = 0; anIdx < 3; ++anIdx)
394             {
395               if (IsReflect)
396               {
397                 aFaceTriangles->AddVertex (aCoords (3 * (anIdx == 0 ? 0 : (aNodeIdx + anIdx)) + 1),
398                                            aCoords (3 * (anIdx == 0 ? 0 : (aNodeIdx + anIdx)) + 2),
399                                            aCoords (3 * (anIdx == 0 ? 0 : (aNodeIdx + anIdx)) + 3),
400                                            aHasNormals ? aNormals->Value (3 * (anIdx == 0 ? 0 : (aNodeIdx + anIdx)) + 1) : 0.0,
401                                            aHasNormals ? aNormals->Value (3 * (anIdx == 0 ? 0 : (aNodeIdx + anIdx)) + 2) : 0.0,
402                                            aHasNormals ? aNormals->Value (3 * (anIdx == 0 ? 0 : (aNodeIdx + anIdx)) + 3) : 1.0);
403               }
404               else
405               {
406                 aFaceTriangles->AddVertex (aCoords (3 * (anIdx == 0 ? 0 : (aNodeIdx + anIdx)) + 1),
407                                            aCoords (3 * (anIdx == 0 ? 0 : (aNodeIdx + anIdx)) + 2),
408                                            aCoords (3 * (anIdx == 0 ? 0 : (aNodeIdx + anIdx)) + 3));
409               }
410             }
411           }
412
413           if (anEdgeOn)
414           {
415             for (Standard_Integer aNodeIdx = 0; aNodeIdx < NbNodes; ++aNodeIdx)
416             {
417               const Standard_Integer aNextIdx = (aNodeIdx + 1) % NbNodes;
418
419               anEdgeSegments->AddVertex (aCoords (3 * aNodeIdx + 1),
420                                          aCoords (3 * aNodeIdx + 2),
421                                          aCoords (3 * aNodeIdx + 3));
422               
423               anEdgeSegments->AddVertex (aCoords (3 * aNextIdx + 1),
424                                          aCoords (3 * aNextIdx + 2),
425                                          aCoords (3 * aNextIdx + 3));
426             }
427           }
428
429           IsPolyG = Standard_True;
430         }
431         else if (aType == MeshVS_ET_Link)
432         {
433           for (Standard_Integer aNodeIdx = 0; aNodeIdx < NbNodes - 1; ++aNodeIdx)
434           {
435             const Standard_Integer aNextIdx = aNodeIdx + 1;
436
437             aLinkSegments->AddVertex (aCoords (3 * aNodeIdx + 1),
438                                       aCoords (3 * aNodeIdx + 2),
439                                       aCoords (3 * aNodeIdx + 3));
440
441             aLinkSegments->AddVertex (aCoords (3 * aNextIdx + 1),
442                                       aCoords (3 * aNextIdx + 2),
443                                       aCoords (3 * aNextIdx + 3));
444
445             IsPolyL = Standard_True;
446           }
447         }
448       }
449     }
450
451     if (IsPolyG)
452     {
453       aFillAspect->SetEdgeOff();
454       aGGroup->SetPrimitivesAspect (aFillAspect);
455       aGGroup->AddPrimitiveArray (aFaceTriangles);
456       
457       if (anEdgeOn)
458       {
459         aFillAspect->SetEdgeOff();
460         aSGroup->AddPrimitiveArray (anEdgeSegments);
461         aSGroup->SetGroupPrimitivesAspect (anEdgeAspect);
462       }
463     }
464     if (IsPolyL)
465     {
466       aFillAspect->SetEdgeOff();
467       aLGroup->SetPrimitivesAspect (aFillAspect);
468       aLGroup->SetPrimitivesAspect (aLinkAspect);
469       aLGroup->AddPrimitiveArray (aLinkSegments);
470       if (anEdgeOn)
471         aFillAspect->SetEdgeOn();
472       else
473         aFillAspect->SetEdgeOff();
474     }
475
476     if (!aCustomElements.IsEmpty())
477       CustomBuild(Prs, aCustomElements, IDsToExclude, DisplayMode);
478   }
479
480   Graphic3d_MaterialAspect aMaterial2[2];
481   for (Standard_Integer i = 0; i < 2; i++)
482   {
483     // OCC20644 "plastic" is most suitable here, as it is "non-physic"
484     // so TelUpdateMaterial() from OpenGl_attri.c uses the interior
485     // color from AspectFillArea3d to calculate all material colors
486     aMaterial2[i] = Graphic3d_MaterialAspect ( Graphic3d_NOM_PLASTIC );
487
488     if ( !IsReflect )
489     {
490       // OCC21720 Cannot turn ALL material components off, as such a material
491       // would be ignored by TelUpdateMaterial(), but we need it in order
492       // to have different materials for front and back sides!
493       // Instead, trying to make material color "nondirectional" with 
494       // only ambient component on.
495       aMaterial2[i].SetReflectionModeOn ( Graphic3d_TOR_AMBIENT );
496       aMaterial2[i].SetReflectionModeOff( Graphic3d_TOR_DIFFUSE );
497       aMaterial2[i].SetReflectionModeOff( Graphic3d_TOR_SPECULAR );
498       aMaterial2[i].SetReflectionModeOff( Graphic3d_TOR_EMISSION );
499       aMaterial2[i].SetAmbient ( 1. );
500       aMaterial2[i].SetDiffuse ( 0. );
501       aMaterial2[i].SetSpecular( 0. );
502       aMaterial2[i].SetEmissive( 0. );
503     }
504     else
505     {
506       // OCC20644 This stuff is important in order for elemental and nodal colors
507       // to produce similar visual impression and also to make colors match
508       // those in the color scale most exactly (the sum of all reflection 
509       // coefficients is equal to 1). See also MeshVS_NodalColorPrsBuilder
510       // class for more explanations.
511       aMaterial2[i].SetAmbient( .5 );
512       aMaterial2[i].SetDiffuse( .5 );
513       aMaterial2[i].SetSpecular( 0. );
514       aMaterial2[i].SetEmissive( 0. );
515     }
516   }
517
518   // Draw faces with two color
519   for ( MeshVS_DataMapIteratorOfDataMapOfTwoColorsMapOfInteger aColIter2 ( aTwoColorsOfElements );
520         aColIter2.More(); aColIter2.Next() )
521   {
522     Prs3d_Root::NewGroup ( Prs );
523     Handle ( Graphic3d_Group ) aGroup2 = Prs3d_Root::CurrentGroup ( Prs );
524     Prs3d_Root::NewGroup ( Prs );
525     Handle ( Graphic3d_Group ) aGroup3 = Prs3d_Root::CurrentGroup ( Prs );
526
527     Standard_Integer aSize = aColIter2.Value().Extent();
528     if ( aSize<=0 )
529       continue;
530
531     Standard_Integer aNbFacePrimitives = 0;
532     Standard_Integer aNbEdgePrimitives = 0;
533     
534     for (it.Reset(); it.More(); it.Next())
535     {
536       Standard_Integer aNbNodes = 0;
537
538       if (!aColIter2.Value().Contains (it.Key()))
539         continue;
540
541       if (!aSource->GetGeom (it.Key(), Standard_True, aCoords, aNbNodes, aType))
542         continue;
543
544       if ( aType == MeshVS_ET_Face && aNbNodes > 0 )
545       {
546         if (anEdgeOn)
547         {
548           aNbEdgePrimitives += aNbNodes; // add edge segments
549         }
550
551         aNbFacePrimitives += aNbNodes - 2; // add face triangles
552       }
553     }
554
555     Handle (Graphic3d_ArrayOfTriangles) aFaceTriangles = new Graphic3d_ArrayOfTriangles
556       (aNbFacePrimitives * 3, 0, IsReflect);
557
558     Handle (Graphic3d_ArrayOfSegments) anEdgeSegments = new Graphic3d_ArrayOfSegments
559       (aNbEdgePrimitives * 2);
560
561     MeshVS_TwoColors aTC = aColIter2.Key();
562     Quantity_Color aMyIntColor, aMyBackColor;
563     ExtractColors ( aTC, aMyIntColor, aMyBackColor );
564
565     // OCC20644 NOTE: aMyIntColor color is then scaled by TelUpdateMaterial() in OpenGl_attri.c
566     // using the material reflection coefficients. This affects the visual result.
567     Handle(Graphic3d_AspectFillArea3d) anAsp =
568       new Graphic3d_AspectFillArea3d ( Aspect_IS_SOLID, aMyIntColor, anEdgeColor,
569                                          anEdgeType, anEdgeWidth, aMaterial2[0], aMaterial2[1] );
570     anAsp->SetDistinguishOn ();
571     anAsp->SetInteriorColor ( aMyIntColor );
572     anAsp->SetBackInteriorColor ( aMyBackColor );
573     /*if (anEdgeOn)
574       anAsp->SetEdgeOn();
575     else
576       anAsp->SetEdgeOff();*/
577
578     Handle(Graphic3d_AspectLine3d) anEdgeAspect =
579       new Graphic3d_AspectLine3d (anEdgeColor, anEdgeType, anEdgeWidth);
580
581     for( it.Reset(); it.More(); it.Next() )
582     {
583       Standard_Integer aKey = it.Key();
584       if( aColIter2.Value().Contains( aKey ) )
585       {
586         if ( !aSource->GetGeom ( aKey, Standard_True, aCoords, NbNodes, aType ) )
587           continue;
588
589         if( IsExcludingOn() )
590           IDsToExclude.Add( aKey );
591
592         if (aType == MeshVS_ET_Face && NbNodes > 0)
593         {
594           // Preparing normal(s) to show reflections if requested
595           Handle(TColStd_HArray1OfReal) aNormals;
596           // OCC21720 Always passing normals to OpenGL to make materials work
597           // For OpenGL: "No normals" -> "No lighting" -> "no materials taken into account"
598           Standard_Boolean aHasNormals = /*IsReflect &&*/
599             aSource->GetNormalsByElement (aKey, IsMeshSmoothShading, aMaxFaceNodes, aNormals);
600
601           for (Standard_Integer aNodeIdx = 0; aNodeIdx < NbNodes - 2; ++aNodeIdx)
602           {
603             for (Standard_Integer anIdx = 0; anIdx < 3; ++anIdx)
604             {
605               if (IsReflect)
606               {
607                 aFaceTriangles->AddVertex (aCoords (3 * (anIdx == 0 ? 0 : aNodeIdx + anIdx) + 1),
608                                            aCoords (3 * (anIdx == 0 ? 0 : aNodeIdx + anIdx) + 2),
609                                            aCoords (3 * (anIdx == 0 ? 0 : aNodeIdx + anIdx) + 3),
610                                            aHasNormals ? aNormals->Value (3 * (anIdx == 0 ? 0 : aNodeIdx + anIdx) + 1) : 0.0,
611                                            aHasNormals ? aNormals->Value (3 * (anIdx == 0 ? 0 : aNodeIdx + anIdx) + 2) : 0.0,
612                                            aHasNormals ? aNormals->Value (3 * (anIdx == 0 ? 0 : aNodeIdx + anIdx) + 3) : 1.0);
613               }
614               else
615               {
616                 aFaceTriangles->AddVertex (aCoords (3 * (anIdx == 0 ? 0 : aNodeIdx + anIdx) + 1),
617                                            aCoords (3 * (anIdx == 0 ? 0 : aNodeIdx + anIdx) + 2),
618                                            aCoords (3 * (anIdx == 0 ? 0 : aNodeIdx + anIdx) + 3));
619               }
620             }
621           }
622
623           if (anEdgeOn)
624           {
625             for (Standard_Integer aNodeIdx = 0; aNodeIdx < NbNodes; ++aNodeIdx)
626             {
627               const Standard_Integer aNextIdx = (aNodeIdx + 1) % NbNodes;
628
629               anEdgeSegments->AddVertex (aCoords (3 * aNodeIdx + 1),
630                                          aCoords (3 * aNodeIdx + 2),
631                                          aCoords (3 * aNodeIdx + 3));
632
633               anEdgeSegments->AddVertex (aCoords (3 * aNextIdx + 1),
634                                          aCoords (3 * aNextIdx + 2),
635                                          aCoords (3 * aNextIdx + 3));
636             }
637           }
638         }
639       }
640     }
641
642     aGroup2->AddPrimitiveArray (aFaceTriangles);
643     aGroup2->SetGroupPrimitivesAspect (anAsp);
644     aGroup3->AddPrimitiveArray (anEdgeSegments);
645     aGroup3->SetGroupPrimitivesAspect (anEdgeAspect);
646   }
647 }
648
649 //================================================================
650 // Function : SetColors
651 // Purpose  :
652 //================================================================
653 void MeshVS_ElementalColorPrsBuilder::SetColors1 ( const MeshVS_DataMapOfIntegerColor& theColorMap )
654 {
655   myElemColorMap1 = theColorMap;
656 }
657
658 //================================================================
659 // Function : GetColors
660 // Purpose  :
661 //================================================================
662 const MeshVS_DataMapOfIntegerColor& MeshVS_ElementalColorPrsBuilder::GetColors1 () const
663 {
664   return myElemColorMap1;
665 }
666
667 //================================================================
668 // Function : HasColors1
669 // Purpose  :
670 //================================================================
671 Standard_Boolean MeshVS_ElementalColorPrsBuilder::HasColors1 () const
672 {
673   return ( myElemColorMap1.Extent() >0 );
674 }
675
676 //================================================================
677 // Function : GetColor1
678 // Purpose  :
679 //================================================================
680 Standard_Boolean MeshVS_ElementalColorPrsBuilder::GetColor1 ( const Standard_Integer ID,
681                                                               Quantity_Color& theColor ) const
682 {
683   Standard_Boolean aRes = myElemColorMap1.IsBound ( ID );
684   if ( aRes )
685     theColor = myElemColorMap1.Find ( ID );
686
687   return aRes;
688 }
689
690 //================================================================
691 // Function : SetColor1
692 // Purpose  :
693 //================================================================
694 void MeshVS_ElementalColorPrsBuilder::SetColor1 ( const Standard_Integer theID,
695                                                   const Quantity_Color& theCol )
696 {
697   Standard_Boolean aRes = myElemColorMap1.IsBound ( theID );
698   if ( aRes )
699     myElemColorMap1.ChangeFind ( theID ) = theCol;
700   else
701     myElemColorMap1.Bind ( theID, theCol );
702 }
703
704 //================================================================
705 // Function : SetColors2
706 // Purpose  :
707 //================================================================
708 void MeshVS_ElementalColorPrsBuilder::SetColors2 ( const MeshVS_DataMapOfIntegerTwoColors& theColorMap )
709 {
710   myElemColorMap2 = theColorMap;
711 }
712
713 //================================================================
714 // Function : GetColors2
715 // Purpose  :
716 //================================================================
717 const MeshVS_DataMapOfIntegerTwoColors& MeshVS_ElementalColorPrsBuilder::GetColors2 () const
718 {
719   return myElemColorMap2;
720 }
721
722 //================================================================
723 // Function : HasColors2
724 // Purpose  :
725 //================================================================
726 Standard_Boolean MeshVS_ElementalColorPrsBuilder::HasColors2 () const
727 {
728   return (myElemColorMap2.Extent()>0);
729 }
730
731 //================================================================
732 // Function : GetColor2
733 // Purpose  :
734 //================================================================
735 Standard_Boolean MeshVS_ElementalColorPrsBuilder::GetColor2 ( const Standard_Integer ID,
736                                                               MeshVS_TwoColors& theColor ) const
737 {
738   Standard_Boolean aRes = myElemColorMap2.IsBound ( ID );
739   if ( aRes )
740     theColor = myElemColorMap2.Find ( ID );
741
742   return aRes;
743 }
744
745 //================================================================
746 // Function : GetColor2
747 // Purpose  :
748 //================================================================
749 Standard_Boolean MeshVS_ElementalColorPrsBuilder::GetColor2 ( const Standard_Integer ID,
750                                                               Quantity_Color& theColor1,
751                                                               Quantity_Color& theColor2 ) const
752 {
753   MeshVS_TwoColors aTC;
754   Standard_Boolean aRes = GetColor2 ( ID, aTC );
755   if ( aRes)
756     ExtractColors ( aTC, theColor1, theColor2 );
757   return aRes;
758 }
759
760 //================================================================
761 // Function : SetColor2
762 // Purpose  :
763 //================================================================
764 void MeshVS_ElementalColorPrsBuilder::SetColor2 ( const Standard_Integer theID,
765                                                   const Quantity_Color& theCol1,
766                                                   const Quantity_Color& theCol2 )
767 {
768   SetColor2 ( theID, BindTwoColors ( theCol1, theCol2 ) );
769 }
770
771 //================================================================
772 // Function : SetColor2
773 // Purpose  :
774 //================================================================
775 void MeshVS_ElementalColorPrsBuilder::SetColor2 ( const Standard_Integer theID,
776                                                   const MeshVS_TwoColors& theCol )
777 {
778   Standard_Boolean aRes = myElemColorMap2.IsBound ( theID );
779   if ( aRes )
780     myElemColorMap2.ChangeFind ( theID ) = theCol;
781   else
782     myElemColorMap2.Bind ( theID, theCol );
783 }