1 // File: MeshVS_NodalColorPrsBuilder.cxx
2 // Created: Wed Nov 12 2003
3 // Author: Alexander SOLOVYOV
4 // Copyright: Open CASCADE 2003
7 // if define _POLYGONES_ ColorPrsBuilder use ArrayOfPolygons for drawing faces
9 #include <MeshVS_NodalColorPrsBuilder.ixx>
11 #include <Graphic3d_AspectFillArea3d.hxx>
12 #include <Graphic3d_AspectLine3d.hxx>
13 #include <Graphic3d_ArrayOfPolygons.hxx>
14 #include <Graphic3d_ArrayOfPolylines.hxx>
15 #include <Graphic3d_Vertex.hxx>
16 #include <Graphic3d_Group.hxx>
17 #include <Graphic3d_Array1OfVertex.hxx>
19 #include <Prs3d_ShadingAspect.hxx>
20 #include <Prs3d_Root.hxx>
21 #include <Prs3d_LineAspect.hxx>
23 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
24 #include <TColStd_ListIteratorOfListOfInteger.hxx>
25 #include <TColStd_Array1OfReal.hxx>
26 #include <TColStd_HArray1OfReal.hxx>
27 #include <TColStd_Array1OfInteger.hxx>
28 #include <TColStd_SequenceOfInteger.hxx>
29 #include <TColStd_HPackedMapOfInteger.hxx>
31 #include <MeshVS_DisplayModeFlags.hxx>
32 #include <MeshVS_DrawerAttribute.hxx>
33 #include <MeshVS_DataSource.hxx>
34 #include <MeshVS_Drawer.hxx>
35 #include <MeshVS_Mesh.hxx>
36 #include <MeshVS_MeshPrsBuilder.hxx>
37 #include <MeshVS_HArray1OfSequenceOfInteger.hxx>
38 #include <MeshVS_Buffer.hxx>
41 #include <Image_ColorImage.hxx>
42 #include <AlienImage_AlienImage.hxx>
43 #include <AlienImage_BMPAlienImage.hxx>
44 #include <Graphic3d_Texture2D.hxx>
45 #include <Graphic3d_TypeOfTextureMode.hxx>
46 #include <Standard_DefineHandle.hxx>
47 #include <PrsMgr_PresentationManager3d.hxx>
48 #include <Graphic3d_Array1OfVertexNT.hxx>
49 #include <AIS_Drawer.hxx>
50 #include <Quantity_Array1OfColor.hxx>
51 #include <Aspect_SequenceOfColor.hxx>
54 Class : MeshVS_ImageTexture2D
55 Description : Texture for nodal presentation
57 class MeshVS_ImageTexture2D : public Graphic3d_Texture2D
60 MeshVS_ImageTexture2D( Handle(Graphic3d_StructureManager) theSM,
61 const Handle(AlienImage_AlienImage)& theImg );
62 virtual ~MeshVS_ImageTexture2D();
65 DEFINE_STANDARD_RTTI(MeshVS_ImageTexture2D)
68 DEFINE_STANDARD_HANDLE(MeshVS_ImageTexture2D,Graphic3d_Texture2D)
69 IMPLEMENT_STANDARD_HANDLE(MeshVS_ImageTexture2D,Graphic3d_Texture2D)
70 IMPLEMENT_STANDARD_RTTIEXT(MeshVS_ImageTexture2D, Graphic3d_Texture2D)
72 MeshVS_ImageTexture2D::MeshVS_ImageTexture2D
73 (Handle(Graphic3d_StructureManager) theSM,
74 const Handle(AlienImage_AlienImage)& theImg)
75 : Graphic3d_Texture2D( theSM, "", Graphic3d_TOT_2D )
77 MyCInitTexture.doModulate = 1;
78 MyCInitTexture.doRepeat = 0;
79 MyCInitTexture.Mode = (int)Graphic3d_TOTM_MANUAL;
80 MyCInitTexture.doLinear = 1;
81 MyCInitTexture.sx = 1.0F;
82 MyCInitTexture.sy = 1.0F;
83 MyCInitTexture.tx = 0.0F;
84 MyCInitTexture.ty = 0.0F;
85 MyCInitTexture.angle = 0.0F;
86 MyCInitTexture.sparams[0] = 0.0F;
87 MyCInitTexture.sparams[1] = 0.0F;
88 MyCInitTexture.sparams[2] = 0.0F;
89 MyCInitTexture.sparams[3] = 0.0F;
90 MyCInitTexture.tparams[0] = 0.0F;
91 MyCInitTexture.tparams[1] = 0.0F;
92 MyCInitTexture.tparams[2] = 0.0F;
93 MyCInitTexture.tparams[3] = 0.0F;
98 MeshVS_ImageTexture2D::~MeshVS_ImageTexture2D()
102 //================================================================
103 // Function : getNearestPow2
104 // Purpose : Returns the nearest power of two greater than the
106 //================================================================
107 static inline Standard_Integer getNearestPow2( Standard_Integer theValue )
109 // Precaution against overflow
110 Standard_Integer aHalfMax = IntegerLast() >> 1, aRes = 1;
111 if ( theValue > aHalfMax ) theValue = aHalfMax;
112 while ( aRes < theValue ) aRes <<= 1;
117 Class : MeshVS_NodalColorPrsBuilder
118 Description : This class provides methods to create presentation of
119 nodes with assigned color (See hxx for more description )
122 //================================================================
123 // Function : Constructor MeshVS_NodalColorPrsBuilder
125 //================================================================
126 MeshVS_NodalColorPrsBuilder::MeshVS_NodalColorPrsBuilder ( const Handle(MeshVS_Mesh)& Parent,
127 const MeshVS_DisplayModeFlags& Flags,
128 const Handle (MeshVS_DataSource)& DS,
129 const Standard_Integer Id,
130 const MeshVS_BuilderPriority& Priority )
131 : MeshVS_PrsBuilder ( Parent, Flags, DS, Id, Priority ),
132 myUseTexture( Standard_False ),
133 myInvalidColor( Quantity_NOC_GRAY )
135 SetExcluding ( Standard_True );
138 //================================================================
141 //================================================================
142 void MeshVS_NodalColorPrsBuilder::Build ( const Handle(Prs3d_Presentation)& Prs,
143 const TColStd_PackedMapOfInteger& IDs,
144 TColStd_PackedMapOfInteger& IDsToExclude,
145 const Standard_Boolean IsElement,
146 const Standard_Integer DisplayMode) const
148 Handle (MeshVS_DataSource) aSource = GetDataSource();
149 Handle (MeshVS_Drawer) aDrawer = GetDrawer();
150 if ( aSource.IsNull() || aDrawer.IsNull() )
153 Standard_Integer aMaxFaceNodes;
154 if ( !aDrawer->GetInteger ( MeshVS_DA_MaxFaceNodes, aMaxFaceNodes ) || aMaxFaceNodes <= 0 )
157 MeshVS_Buffer aCoordsBuf (3*aMaxFaceNodes*sizeof(Standard_Real));
158 TColStd_Array1OfReal aCoords ( aCoordsBuf, 1, 3 * aMaxFaceNodes );
159 Standard_Integer NbNodes;
160 MeshVS_EntityType aType;
162 if ( !( DisplayMode & GetFlags() ) || !IsElement )
165 if ( myUseTexture && ( !myTextureCoords.Extent() || !myTextureColorMap.Length() ) ||
166 !myUseTexture && !myNodeColorMap.Extent() )
169 // subtract the hidden elements and ids to exclude (to minimise allocated memory)
170 TColStd_PackedMapOfInteger anIDs;
172 Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems();
173 if ( !aHiddenElems.IsNull() )
174 anIDs.Subtract( aHiddenElems->Map() );
175 anIDs.Subtract( IDsToExclude );
177 Standard_Boolean IsReflect = Standard_False, IsMeshSmoothShading = Standard_False;
178 aDrawer->GetBoolean( MeshVS_DA_ColorReflection, IsReflect );
179 aDrawer->GetBoolean( MeshVS_DA_SmoothShading, IsMeshSmoothShading );
181 // Following parameter are used for texture presentation only
182 int nbColors = 0; // Number of colors from color map
183 int nbTextureColors = 0; // Number of colors in texture (it will be pow of 2)
186 nbColors = myTextureColorMap.Length();
187 nbTextureColors = getNearestPow2( nbColors );
190 Standard_Integer aSize = anIDs.Extent();
192 // Calculate maximum possible number of vertices and bounds
193 Handle( MeshVS_HArray1OfSequenceOfInteger ) aTopo;
194 Standard_Integer PolygonVerticesFor3D = 0, PolygonBoundsFor3D = 0;
195 TColStd_MapIteratorOfPackedMapOfInteger it (anIDs);
196 for( ; it.More(); it.Next() )
198 Standard_Integer aKey = it.Key();
199 if ( aSource->Get3DGeom( aKey, NbNodes, aTopo ) )
200 MeshVS_MeshPrsBuilder::HowManyPrimitives
201 ( aTopo, Standard_True, Standard_False, NbNodes,
202 PolygonVerticesFor3D, PolygonBoundsFor3D );
205 // Draw faces with nodal color
206 // OCC20644 Use "plastic" material as it is "non-physic" and so it is easier
207 // to get the required colors (see TelUpdateMaterial() function in OpenGl_attri.c)
208 Graphic3d_MaterialAspect aMaterial[ 2 ];
209 aMaterial[ 0 ] = Graphic3d_MaterialAspect( Graphic3d_NOM_PLASTIC );
210 aMaterial[ 1 ] = Graphic3d_MaterialAspect( Graphic3d_NOM_PLASTIC );
212 for ( i = 0; i < 2; i++ )
216 aMaterial[ i ].SetReflectionModeOff( Graphic3d_TOR_SPECULAR );
217 aMaterial[ i ].SetReflectionModeOff( Graphic3d_TOR_AMBIENT );
218 aMaterial[ i ].SetReflectionModeOff( Graphic3d_TOR_DIFFUSE );
219 aMaterial[ i ].SetReflectionModeOff( Graphic3d_TOR_EMISSION );
222 // OCC20644 Using the material with reflection properties same as in
223 // ElementalColorPrsBuilder, to get the same colors.
224 // Additionally, ambient and diffuse coefficients are used below to scale incoming colors,
225 // to simulate TelUpdateMaterial() function from OpenGl_attri.c.
226 // This is mandatory, as these "scaled" colors are then passed directly to OpenGL
227 // as ambient and diffuse colors of the current material using glColorMaterial().
228 // In ElementalColorPrsBuilder we do not need to do scale the colors, as this
229 // is done by TelUpdateMaterial().
230 // 0.5 is used to have the colors in 3D maximally similar to those in the color scale.
231 // This is possible when the sum of all coefficient is equal to 1.
232 aMaterial[i].SetAmbient( .5 );
233 aMaterial[i].SetDiffuse( .5 );
234 aMaterial[i].SetSpecular( 0. );
235 aMaterial[i].SetEmissive( 0. );
241 // Create array of polygons for interior presentation of faces and volumes
242 Handle(Graphic3d_ArrayOfPolygons) aCPolyArr = new Graphic3d_ArrayOfPolygons
243 ( aMaxFaceNodes * aSize + PolygonVerticesFor3D, aSize + PolygonBoundsFor3D,
244 0, myUseTexture || IsReflect, !myUseTexture, Standard_False, myUseTexture );
246 // Create array of polylines for presentation of edges
247 // (used for optimization insted of SetEdgeOn method call)
248 Handle(Graphic3d_ArrayOfPolylines) aPolyL = new Graphic3d_ArrayOfPolylines
249 ( ( aMaxFaceNodes + 1 ) * aSize + PolygonVerticesFor3D, aSize + PolygonBoundsFor3D );
252 Standard_Real aMin = gp::Resolution() * gp::Resolution();
253 gp_Dir aDefNorm( 0., 0., 1. );
255 // Prepare for scaling the incoming colors
256 Standard_Real anColorRatio = aMaterial[0].Ambient();
258 for( it.Reset(); it.More(); it.Next() )
260 Standard_Integer aKey = it.Key();
261 if ( aSource->GetGeom ( aKey, Standard_True, aCoords, NbNodes, aType ) )
263 MeshVS_Buffer aNodesBuf (NbNodes*sizeof(Standard_Integer));
264 TColStd_Array1OfInteger aNodes(aNodesBuf, 1, NbNodes);
265 if ( !aSource->GetNodesByElement ( aKey, aNodes, NbNodes ) )
268 Quantity_Color aNColor;
270 Standard_Boolean isValid = Standard_True;
274 for ( i = 1; i <= NbNodes && isValid; i++ )
275 isValid = myTextureCoords.IsBound( aNodes( i ) );
279 for ( i = 1; i <= NbNodes && isValid; i++ )
280 isValid = GetColor ( aNodes( i ), aNColor );
286 // Preparing normal(s) to show reflections if requested
287 Handle(TColStd_HArray1OfReal) aNormals;
288 Standard_Boolean hasNormals =
289 ( IsReflect && aSource->GetNormalsByElement( aKey, IsMeshSmoothShading, aMaxFaceNodes, aNormals ) );
291 if ( aType == MeshVS_ET_Face )
293 aCPolyArr->AddBound ( NbNodes );
294 aPolyL->AddBound ( NbNodes + 1 );
296 for ( i = 1; i <= NbNodes; i++)
298 P = gp_Pnt( aCoords( 3 * i - 2 ), aCoords( 3 * i - 1 ), aCoords( 3 * i ) );
301 int anId = aNodes( i );
302 double aTexCoord = myTextureCoords( anId );
304 // transform texture coordinate in accordance with number of colors specified
305 // by upper level and real size of Gl texture
306 // The Gl texture has border colors interpolated with the colors from the color map,
307 // thats why we need to shrink texture coordinates around the middle point to
308 // exclude areas where the map colors are interpolated with the borders color
309 double aWrapCoord = 1.0 / (2.0 * nbTextureColors) + aTexCoord * (nbColors - 1.0) / nbTextureColors;
313 gp_Vec aNorm(aNormals->Value( 3 * i - 2 ),
314 aNormals->Value( 3 * i - 1 ),
315 aNormals->Value( 3 * i ));
316 // There are two "rows" of colors: user's invalid color at the top
317 // of texture and line of map colors at the bottom of the texture.
318 // Since the texture has borders, which are interpolated with the "rows" of colors
319 // we should specify the 0.25 offset to get the correct texture color
320 aNorm.SquareMagnitude() > aMin ?
321 aCPolyArr->AddVertex(P, gp_Dir( aNorm ),
322 gp_Pnt2d( aWrapCoord, aTexCoord >= 0 && aTexCoord <= 1 ? 0.75 : 0.25 ) ) :
323 aCPolyArr->AddVertex(P, aDefNorm,
324 gp_Pnt2d( aWrapCoord, aTexCoord >= 0 && aTexCoord <= 1 ? 0.75 : 0.25 ) );
327 aCPolyArr->AddVertex( P, aDefNorm,
328 gp_Pnt2d( aWrapCoord, aTexCoord >= 0 && aTexCoord <= 1 ? 0.75 : 0.25 ) );
332 GetColor ( aNodes( i ), aNColor );
336 // Simulating TelUpdateMaterial() from OpenGl_attri.c
337 // to get the same colors in elemental and nodal color prs builders
338 aNColor.SetValues(anColorRatio * aNColor.Red(),
339 anColorRatio * aNColor.Green(),
340 anColorRatio * aNColor.Blue(),
345 gp_Vec aNorm(aNormals->Value( 3 * i - 2 ),
346 aNormals->Value( 3 * i - 1 ),
347 aNormals->Value( 3 * i ));
348 aNorm.SquareMagnitude() > aMin ?
349 aCPolyArr->AddVertex(P, gp_Dir( aNorm ), aNColor ) :
350 aCPolyArr->AddVertex(P, aDefNorm , aNColor );
353 aCPolyArr->AddVertex(P, aDefNorm, aNColor );
356 aCPolyArr->AddVertex( P, aNColor );
358 aPolyL->AddVertex ( P );
362 aPolyL->AddVertex ( Start );
364 // if IsExcludingOn then presentation must not be built by other builders
365 if ( IsExcludingOn() )
366 IDsToExclude.Add( aKey );
368 else if ( aType == MeshVS_ET_Volume )
370 if ( !aSource->Get3DGeom( aKey, NbNodes, aTopo ) )
373 // iterate through faces of volume
374 for ( Standard_Integer k = aTopo->Lower(), last = aTopo->Upper(), normIndex = 1; k <= last; k++, normIndex++ )
376 const TColStd_SequenceOfInteger& aSeq = aTopo->Value( k );
377 Standard_Integer m = aSeq.Length(), ind;
379 // build polygon & polylines for current face
380 aCPolyArr->AddBound( m );
381 aPolyL->AddBound( m + 1 );
382 for ( Standard_Integer j = 1; j <= m; j++ )
384 ind = aSeq.Value( j );
385 P = gp_Pnt( aCoords( 3 * ind + 1 ),
386 aCoords( 3 * ind + 2 ),
387 aCoords( 3 * ind + 3 ) );
390 Standard_Integer anId = aNodes( ind + 1 );
391 Standard_Real aTexCoord = myTextureCoords( anId );
393 // transform texture coordinate in accordance with number of colors specified
394 // by upper level and real size of Gl texture
395 // The Gl texture has border colors interpolated with the colors from the color map,
396 // thats why we need to shrink texture coordinates around the middle point to
397 // exclude areas where the map colors are interpolated with the borders color
398 double aWrapCoord = 1.0 / (2.0 * nbTextureColors) + aTexCoord * (nbColors - 1.0) / nbTextureColors;
402 gp_Vec aNorm(aNormals->Value( 3 * i - 2 ),
403 aNormals->Value( 3 * i - 1 ),
404 aNormals->Value( 3 * i ));
405 // There are two "rows" of colors: user's invalid color at the top
406 // of texture and line of map colors at the bottom of the texture.
407 // Since the texture has borders, which are interpolated with the "rows" of colors
408 // we should specify the 0.25 offset to get the correct texture color
409 aNorm.SquareMagnitude() > aMin ?
410 aCPolyArr->AddVertex(P, gp_Dir( aNorm ),
411 gp_Pnt2d( aWrapCoord, aTexCoord >= 0 && aTexCoord <= 1 ? 0.75 : 0.25 ) ) :
412 aCPolyArr->AddVertex(P, aDefNorm,
413 gp_Pnt2d( aWrapCoord, aTexCoord >= 0 && aTexCoord <= 1 ? 0.75 : 0.25 ) );
416 aCPolyArr->AddVertex( P, aDefNorm,
417 gp_Pnt2d( aWrapCoord, aTexCoord >= 0 && aTexCoord <= 1 ? 0.75 : 0.25 ) );
421 GetColor( aNodes( ind + 1 ), aNColor );
424 // Simulating TelUpdateMaterial() from OpenGl_attri.c
425 // to get the same colors in elemental and nodal color prs builders
426 aNColor.SetValues(anColorRatio * aNColor.Red(),
427 anColorRatio * aNColor.Green(),
428 anColorRatio * aNColor.Blue(),
433 gp_Vec aNorm(aNormals->Value( 3 * normIndex - 2 ),
434 aNormals->Value( 3 * normIndex - 1 ),
435 aNormals->Value( 3 * normIndex ));
436 aNorm.SquareMagnitude() > aMin ?
437 aCPolyArr->AddVertex( P, gp_Dir( aNorm ), aNColor ) :
438 aCPolyArr->AddVertex( P, aDefNorm , aNColor );
441 aCPolyArr->AddVertex( P, aDefNorm, aNColor );
444 aCPolyArr->AddVertex( P, aNColor );
446 aPolyL->AddVertex ( P );
450 aPolyL->AddVertex ( Start );
453 // if IsExcludingOn then presentation must not be built by other builders
454 if ( IsExcludingOn() )
455 IDsToExclude.Add( aKey );
460 Handle(Graphic3d_AspectFillArea3d) anAsp;
462 // Aspect_InteriorStyle aStyle;
463 // Standard_Integer aStyleInt;
464 Aspect_TypeOfLine anEdgeType = Aspect_TOL_SOLID;
465 Standard_Integer anEdgeInt;
466 Standard_Real anEdgeWidth;
467 Quantity_Color anInteriorColor;
468 Quantity_Color anEdgeColor, aLineColor;
469 Standard_Boolean aShowEdges = Standard_True;
471 aDrawer->GetColor ( MeshVS_DA_InteriorColor, anInteriorColor );
472 aDrawer->GetColor ( MeshVS_DA_EdgeColor, anEdgeColor );
473 aDrawer->GetColor ( MeshVS_DA_BeamColor, aLineColor );
474 aDrawer->GetDouble ( MeshVS_DA_EdgeWidth, anEdgeWidth );
475 aDrawer->GetBoolean( MeshVS_DA_ShowEdges, aShowEdges );
477 if ( aDrawer->GetInteger ( MeshVS_DA_EdgeType, anEdgeInt ) )
478 anEdgeType = (Aspect_TypeOfLine) anEdgeInt;
482 Handle(AIS_Drawer) anAISDrawer = myParentMesh->Attributes();
483 if ( anAISDrawer.IsNull() )
486 anAISDrawer->SetShadingAspect( new Prs3d_ShadingAspect() );
487 anAsp = anAISDrawer->ShadingAspect()->Aspect();
488 if ( anAsp.IsNull() )
491 anAsp->SetFrontMaterial( aMaterial[ 0 ] );
492 anAsp->SetBackMaterial( aMaterial[ 1 ] );
495 Handle(Graphic3d_Texture2D) aTexture = CreateTexture();
496 if ( aTexture.IsNull() )
499 anAsp->SetTextureMapOn();
500 anAsp->SetTextureMap( aTexture );
501 anAsp->SetInteriorColor( Quantity_NOC_WHITE );
505 // if ( aDrawer->GetInteger ( MeshVS_DA_InteriorStyle, aStyleInt ) )
506 // aStyle = (Aspect_InteriorStyle)aStyleInt;
508 anAsp = new Graphic3d_AspectFillArea3d (
509 Aspect_IS_SOLID, Quantity_NOC_GRAY, anEdgeColor,
510 anEdgeType, anEdgeWidth, aMaterial[ 0 ], aMaterial[ 1 ] );
513 anAsp->SetDistinguishOff();
516 Handle(Graphic3d_AspectLine3d) anLAsp =
517 new Graphic3d_AspectLine3d( anEdgeColor, anEdgeType, anEdgeWidth );
519 Prs3d_Root::NewGroup ( Prs );
520 Handle(Graphic3d_Group) aGroup1 = Prs3d_Root::CurrentGroup ( Prs );
522 aGroup1->SetPrimitivesAspect( anAsp );
523 aGroup1->BeginPrimitives();
524 aGroup1->AddPrimitiveArray( aCPolyArr );
525 aGroup1->EndPrimitives();
529 Prs3d_Root::NewGroup ( Prs );
530 Handle(Graphic3d_Group) aGroup2 = Prs3d_Root::CurrentGroup ( Prs );
533 anAsp->SetTextureMapOff();
534 aGroup2->SetPrimitivesAspect( anAsp );
535 aGroup2->SetPrimitivesAspect( anLAsp );
536 aGroup2->BeginPrimitives();
537 aGroup2->AddPrimitiveArray( aPolyL );
538 aGroup2->EndPrimitives();
543 //================================================================
544 // Function : SetColors
546 //================================================================
547 void MeshVS_NodalColorPrsBuilder::SetColors (
548 const MeshVS_DataMapOfIntegerColor& theColorMap )
550 myNodeColorMap = theColorMap;
553 //================================================================
554 // Function : GetColors
556 //================================================================
557 const MeshVS_DataMapOfIntegerColor& MeshVS_NodalColorPrsBuilder::GetColors() const
559 return myNodeColorMap;
562 //================================================================
563 // Function : HasColors
565 //================================================================
566 Standard_Boolean MeshVS_NodalColorPrsBuilder::HasColors () const
568 return ( myNodeColorMap.Extent() >0 );
571 //================================================================
572 // Function : GetColor
574 //================================================================
575 Standard_Boolean MeshVS_NodalColorPrsBuilder::GetColor ( const Standard_Integer ID,
576 Quantity_Color& theColor ) const
578 Standard_Boolean aRes = myNodeColorMap.IsBound ( ID );
580 theColor = myNodeColorMap.Find ( ID );
584 //================================================================
585 // Function : SetColor
587 //================================================================
588 void MeshVS_NodalColorPrsBuilder::SetColor ( const Standard_Integer theID,
589 const Quantity_Color& theCol )
591 Standard_Boolean aRes = myNodeColorMap.IsBound ( theID );
593 myNodeColorMap.ChangeFind ( theID ) = theCol;
595 myNodeColorMap.Bind ( theID, theCol );
598 //================================================================
599 // Function : UseTexture
600 // Purpose : Specify whether texture must be used to build presentation
601 //================================================================
602 void MeshVS_NodalColorPrsBuilder::UseTexture( const Standard_Boolean theToUse )
604 myUseTexture = theToUse;
606 myNodeColorMap.Clear();
608 myTextureColorMap.Clear();
611 //================================================================
612 // Function : IsUseTexture
613 // Purpose : Verify whether texture is used to build presentation
614 //================================================================
615 Standard_Boolean MeshVS_NodalColorPrsBuilder::IsUseTexture() const
620 //================================================================
621 // Function : SetColorMap
622 // Purpose : Set colors to be used for texrture presentation.
623 // Generate texture in accordance with given parameters
624 //================================================================
625 void MeshVS_NodalColorPrsBuilder::SetColorMap( const Aspect_SequenceOfColor& theColors )
627 myTextureColorMap = theColors;
630 //================================================================
631 // Function : GetColorMap
632 // Purpose : Return colors used for texrture presentation
633 //================================================================
634 const Aspect_SequenceOfColor& MeshVS_NodalColorPrsBuilder::GetColorMap() const
636 return myTextureColorMap;
639 //================================================================
640 // Function : SetInvalidColor
641 // Purpose : Set color representing invalid texture coordinate
642 // (laying outside range [0, 1])
643 //================================================================
644 void MeshVS_NodalColorPrsBuilder::SetInvalidColor(
645 const Quantity_Color& theInvalidColor )
647 myInvalidColor = theInvalidColor;
650 //================================================================
651 // Function : GetInvalidColor
652 // Purpose : Return color representing invalid texture coordinate
653 // (laying outside range [0, 1])
654 //================================================================
655 Quantity_Color MeshVS_NodalColorPrsBuilder::GetInvalidColor() const
657 return myInvalidColor;
660 //================================================================
661 // Function : SetTextureCoords
662 // Purpose : Specify correspondence between node IDs and texture
663 // coordinates (range [0, 1])
664 //================================================================
665 void MeshVS_NodalColorPrsBuilder::SetTextureCoords (
666 const TColStd_DataMapOfIntegerReal& theMap )
668 myTextureCoords = theMap;
671 //================================================================
672 // Function : GetTextureCoords
673 // Purpose : Get correspondence between node IDs and texture
674 // coordinates (range [0, 1])
675 //================================================================
676 const TColStd_DataMapOfIntegerReal& MeshVS_NodalColorPrsBuilder::GetTextureCoords() const
678 return myTextureCoords;
681 //================================================================
682 // Function : SetTextureCoord
683 // Purpose : Specify correspondence between node ID and texture
684 // coordinate (range [0, 1])
685 //================================================================
686 void MeshVS_NodalColorPrsBuilder::SetTextureCoord( const Standard_Integer theID,
687 const Standard_Real theCoord )
689 myTextureCoords.Bind( theID, theCoord );
692 //================================================================
693 // Function : GetTextureCoord
694 // Purpose : Return correspondence between node IDs and texture
695 // coordinate (range [0, 1])
696 //================================================================
697 Standard_Real MeshVS_NodalColorPrsBuilder::GetTextureCoord( const Standard_Integer theID )
699 return myTextureCoords.IsBound( theID ) ? myTextureCoords( theID ) : -1;
702 //================================================================
703 // Function : CreateTexture
704 // Purpose : Create texture in accordance with myTextureColorMap
705 //================================================================
706 Handle(Graphic3d_Texture2D) MeshVS_NodalColorPrsBuilder::CreateTexture() const
708 Handle(Graphic3d_Texture2D) aTexture;
710 int nbColors = myTextureColorMap.Length();
714 Handle(PrsMgr_PresentationManager3d) aPrsMgr = GetPresentationManager();
715 if ( aPrsMgr.IsNull() )
718 int nbTextureColors = getNearestPow2( nbColors );
720 // Create and fill image with colors
721 Handle(Image_ColorImage) aCImage =
722 new Image_ColorImage( 0, 0, nbTextureColors, 2 );
727 for ( i = 0; i < nbColors; i++ )
729 aCImage->SetPixel( i, 0, Aspect_ColorPixel( myTextureColorMap( i + 1 ) ) );
730 aCImage->SetPixel( i, 1, Aspect_ColorPixel( myInvalidColor ) );
732 Quantity_Color aLastColor = myTextureColorMap( nbColors );
733 for ( i = nbColors; i < nbTextureColors; i++ )
735 aCImage->SetPixel( i, 0, aLastColor );
736 aCImage->SetPixel( i, 1, Aspect_ColorPixel( myInvalidColor ) );
740 // Convert image to bmp
741 Handle(AlienImage_BMPAlienImage) aTextureImage = new AlienImage_BMPAlienImage();
742 aTextureImage->FromImage( aCImage );
745 aTexture = new MeshVS_ImageTexture2D( aPrsMgr->StructureManager(), aTextureImage );