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