0030497: [REGRESSION] Mesh - wrong Poly_Polygon3D within local selection of located...
[occt.git] / src / Graphic3d / Graphic3d_AspectFillArea3d.hxx
1 // Created on: 1991-11-04
2 // Created by: NW,JPB,CAL
3 // Copyright (c) 1991-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #ifndef _Graphic3d_AspectFillArea3d_HeaderFile
18 #define _Graphic3d_AspectFillArea3d_HeaderFile
19
20 #include <Aspect_AspectFillAreaDefinitionError.hxx>
21 #include <Aspect_PolygonOffsetMode.hxx>
22 #include <Aspect_InteriorStyle.hxx>
23 #include <Aspect_TypeOfLine.hxx>
24 #include <Graphic3d_AlphaMode.hxx>
25 #include <Graphic3d_HatchStyle.hxx>
26 #include <Graphic3d_MaterialAspect.hxx>
27 #include <Graphic3d_PolygonOffset.hxx>
28 #include <Graphic3d_ShaderProgram.hxx>
29 #include <Graphic3d_TextureMap.hxx>
30 #include <Graphic3d_TextureSet.hxx>
31 #include <Graphic3d_TypeOfShadingModel.hxx>
32 #include <Standard.hxx>
33 #include <Standard_Boolean.hxx>
34 #include <Standard_Integer.hxx>
35 #include <Standard_ShortReal.hxx>
36 #include <Standard_Real.hxx>
37 #include <Standard_Type.hxx>
38 #include <Quantity_ColorRGBA.hxx>
39
40 //! This class defines graphic attributes for opaque 3d primitives (polygons, triangles, quadrilaterals).
41 class Graphic3d_AspectFillArea3d : public Standard_Transient
42 {
43   DEFINE_STANDARD_RTTIEXT(Graphic3d_AspectFillArea3d, Standard_Transient)
44 public:
45
46   //! Creates a context table for fill area primitives defined with the following default values:
47   //!
48   //! InteriorStyle : Aspect_IS_EMPTY
49   //! InteriorColor : Quantity_NOC_CYAN1
50   //! EdgeColor     : Quantity_NOC_WHITE
51   //! EdgeLineType  : Aspect_TOL_SOLID
52   //! EdgeWidth     : 1.0
53   //! FrontMaterial : NOM_BRASS
54   //! BackMaterial  : NOM_BRASS
55   //! HatchStyle    : Aspect_HS_SOLID
56   //!
57   //! Display of back-facing filled polygons.
58   //! No distinction between external and internal faces of FillAreas.
59   //! The edges are not drawn.
60   //! Polygon offset parameters: mode = Aspect_POM_None, factor = 1., units = 0.
61   Standard_EXPORT Graphic3d_AspectFillArea3d();
62   
63   //! Creates a context table for fill area primitives defined with the specified values.
64   //! Display of back-facing filled polygons.
65   //! No distinction between external and internal faces of FillAreas.
66   //! The edges are not drawn.
67   //! Polygon offset parameters: mode = Aspect_POM_None, factor = 1., units = 0.
68   Standard_EXPORT Graphic3d_AspectFillArea3d (const Aspect_InteriorStyle theInterior,
69                                               const Quantity_Color&      theInteriorColor,
70                                               const Quantity_Color&      theEdgeColor,
71                                               const Aspect_TypeOfLine    theEdgeLineType,
72                                               const Standard_Real        theEdgeWidth,
73                                               const Graphic3d_MaterialAspect& theFrontMaterial,
74                                               const Graphic3d_MaterialAspect& theBackMaterial);
75
76   //! Return interior rendering style (Aspect_IS_EMPTY by default, which means nothing will be rendered!).
77   Aspect_InteriorStyle InteriorStyle() const { return myInteriorStyle; }
78
79   //! Modifies the interior type used for rendering
80   void SetInteriorStyle (const Aspect_InteriorStyle theStyle) { myInteriorStyle = theStyle; }
81
82   //! Returns shading model (Graphic3d_TOSM_DEFAULT by default, which means that Shading Model set as default for entire Viewer will be used)
83   Graphic3d_TypeOfShadingModel ShadingModel() const { return myShadingModel; }
84
85   //! Sets shading model
86   void SetShadingModel (const Graphic3d_TypeOfShadingModel theShadingModel) { myShadingModel = theShadingModel; }
87
88   //! Returns the way how alpha value should be treated (Graphic3d_AlphaMode_BlendAuto by default, for backward compatibility).
89   Graphic3d_AlphaMode AlphaMode() const { return myAlphaMode; }
90
91   //! Returns alpha cutoff threshold, for discarding fragments within Graphic3d_AlphaMode_Mask mode (0.5 by default).
92   //! If the alpha value is greater than or equal to this value then it is rendered as fully opaque, otherwise, it is rendered as fully transparent.
93   Standard_ShortReal AlphaCutoff() const { return myAlphaCutoff; }
94
95   //! Defines the way how alpha value should be treated.
96   void SetAlphaMode (Graphic3d_AlphaMode theMode, Standard_ShortReal theAlphaCutoff = 0.5f)
97   {
98     myAlphaMode = theMode;
99     myAlphaCutoff = theAlphaCutoff;
100   }
101
102   //! Return interior color.
103   const Quantity_Color& InteriorColor() const { return myInteriorColor.GetRGB(); }
104
105   //! Return interior color.
106   const Quantity_ColorRGBA& InteriorColorRGBA() const { return myInteriorColor; }
107
108   //! Modifies the color of the interior of the face
109   void SetInteriorColor (const Quantity_Color& theColor) { myInteriorColor.SetRGB (theColor); }
110
111   //! Modifies the color of the interior of the face
112   void SetInteriorColor (const Quantity_ColorRGBA& theColor) { myInteriorColor = theColor; }
113
114   //! Return back interior color.
115   const Quantity_Color& BackInteriorColor() const { return myBackInteriorColor.GetRGB(); }
116
117   //! Return back interior color.
118   const Quantity_ColorRGBA& BackInteriorColorRGBA() const { return myBackInteriorColor; }
119
120   //! Modifies the color of the interior of the back face
121   void SetBackInteriorColor (const Quantity_Color& theColor) { myBackInteriorColor.SetRGB (theColor); }
122
123   //! Modifies the color of the interior of the back face
124   void SetBackInteriorColor (const Quantity_ColorRGBA& theColor) { myBackInteriorColor = theColor; }
125
126   //! Returns the surface material of external faces
127   const Graphic3d_MaterialAspect& FrontMaterial() const { return myFrontMaterial; }
128
129   //! Returns the surface material of external faces
130   Graphic3d_MaterialAspect& ChangeFrontMaterial() { return myFrontMaterial; }
131
132   //! Modifies the surface material of external faces
133   void SetFrontMaterial (const Graphic3d_MaterialAspect& theMaterial) { myFrontMaterial = theMaterial; }
134
135   //! Returns the surface material of internal faces
136   const Graphic3d_MaterialAspect& BackMaterial() const { return myBackMaterial; }
137
138   //! Returns the surface material of internal faces
139   Graphic3d_MaterialAspect& ChangeBackMaterial() { return myBackMaterial; }
140
141   //! Modifies the surface material of internal faces
142   void SetBackMaterial (const Graphic3d_MaterialAspect& theMaterial) { myBackMaterial = theMaterial; }
143
144   //! Returns true if back faces should be suppressed (true by default).
145   bool ToSuppressBackFaces() const { return myToSuppressBackFaces; }
146
147   //! Assign back faces culling flag.
148   void SetSuppressBackFaces (bool theToSuppress) { myToSuppressBackFaces = theToSuppress; }
149
150   //! Returns true if back faces should be suppressed (true by default).
151   bool BackFace() const { return myToSuppressBackFaces; }
152
153   //! Allows the display of back-facing filled polygons.
154   void AllowBackFace() { myToSuppressBackFaces = false; }
155
156   //! Suppress the display of back-facing filled polygons.
157   //! A back-facing polygon is defined as a polygon whose
158   //! vertices are in a clockwise order with respect to screen coordinates.
159   void SuppressBackFace() { myToSuppressBackFaces = true; }
160
161   //! Returns true if material properties should be distinguished for back and front faces (false by default).
162   bool Distinguish() const { return myToDistinguishMaterials; }
163
164   //! Set material distinction between front and back faces.
165   void SetDistinguish (bool toDistinguish) { myToDistinguishMaterials = toDistinguish; }
166
167   //! Allows material distinction between front and back faces.
168   void SetDistinguishOn() { myToDistinguishMaterials = true; }
169
170   //! Forbids material distinction between front and back faces.
171   void SetDistinguishOff() { myToDistinguishMaterials = false; }
172
173   //! Return shader program.
174   const Handle(Graphic3d_ShaderProgram)& ShaderProgram() const { return myProgram; }
175
176   //! Sets up OpenGL/GLSL shader program.
177   void SetShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProgram) { myProgram = theProgram; }
178
179   //! Return texture array to be mapped.
180   const Handle(Graphic3d_TextureSet)& TextureSet() const { return myTextureSet; }
181
182   //! Setup texture array to be mapped.
183   void SetTextureSet (const Handle(Graphic3d_TextureSet)& theTextures) { myTextureSet = theTextures; }
184
185   //! Return texture to be mapped.
186   //Standard_DEPRECATED("Deprecated method, TextureSet() should be used instead")
187   Handle(Graphic3d_TextureMap) TextureMap() const
188   {
189     return !myTextureSet.IsNull() && !myTextureSet->IsEmpty()
190           ? myTextureSet->First()
191           : Handle(Graphic3d_TextureMap)();
192   }
193
194   //! Assign texture to be mapped.
195   //! See also SetTextureMapOn() to actually activate texture mapping.
196   //Standard_DEPRECATED("Deprecated method, SetTextureSet() should be used instead")
197   Standard_EXPORT void SetTextureMap (const Handle(Graphic3d_TextureMap)& theTexture);
198
199   //! Return true if texture mapping is enabled (false by default).
200   bool ToMapTexture() const { return myToMapTexture; }
201
202   //! Return true if texture mapping is enabled (false by default).
203   bool TextureMapState() const { return myToMapTexture; }
204
205   //! Enable or disable texture mapping (has no effect if texture is not set).
206   void SetTextureMapOn (bool theToMap) { myToMapTexture = theToMap; }
207
208   //! Enable texture mapping (has no effect if texture is not set).
209   void SetTextureMapOn() { myToMapTexture = true; }
210
211   //! Disable texture mapping.
212   void SetTextureMapOff() { myToMapTexture = false; }
213
214   //! Returns current polygon offsets settings.
215   const Graphic3d_PolygonOffset& PolygonOffset() const { return myPolygonOffset; }
216
217   //! Sets polygon offsets settings.
218   void SetPolygonOffset (const Graphic3d_PolygonOffset& theOffset) { myPolygonOffset = theOffset; }
219
220   //! Returns current polygon offsets settings.
221   void PolygonOffsets (Standard_Integer&   theMode,
222                        Standard_ShortReal& theFactor,
223                        Standard_ShortReal& theUnits) const
224   {
225     theMode   = myPolygonOffset.Mode;
226     theFactor = myPolygonOffset.Factor;
227     theUnits  = myPolygonOffset.Units;
228   }
229
230   //! Sets up OpenGL polygon offsets mechanism.
231   //! <aMode> parameter can contain various combinations of
232   //! Aspect_PolygonOffsetMode enumeration elements (Aspect_POM_None means
233   //! that polygon offsets are not changed).
234   //! If <aMode> is different from Aspect_POM_Off and Aspect_POM_None, then <aFactor> and <aUnits>
235   //! arguments are used by graphic renderer to calculate a depth offset value:
236   //!
237   //! offset = <aFactor> * m + <aUnits> * r, where
238   //! m - maximum depth slope for the polygon currently being displayed,
239   //! r - minimum window coordinates depth resolution (implementation-specific)
240   //!
241   //! Default settings for OCC 3D viewer: mode = Aspect_POM_Fill, factor = 1., units = 0.
242   //!
243   //! Negative offset values move polygons closer to the viewport,
244   //! while positive values shift polygons away.
245   //! Consult OpenGL reference for details (glPolygonOffset function description).
246   void SetPolygonOffsets (const Standard_Integer   theMode,
247                           const Standard_ShortReal theFactor = 1.0f,
248                           const Standard_ShortReal theUnits  = 0.0f)
249   {
250     myPolygonOffset.Mode   = (Aspect_PolygonOffsetMode )(theMode & Aspect_POM_Mask);
251     myPolygonOffset.Factor = theFactor;
252     myPolygonOffset.Units  = theUnits;
253   }
254
255 public:
256
257   //! Returns true if edges should be drawn (false by default).
258   bool ToDrawEdges() const { return myToDrawEdges && myEdgeType != Aspect_TOL_EMPTY; }
259
260   //! Set if edges should be drawn or not.
261   void SetDrawEdges (bool theToDraw)
262   {
263     myToDrawEdges = theToDraw;
264     if (myEdgeType == Aspect_TOL_EMPTY)
265     {
266       myEdgeType = Aspect_TOL_SOLID;
267     }
268   }
269
270   //! Returns true if edges should be drawn.
271   bool Edge() const { return ToDrawEdges(); }
272
273   //! The edges of FillAreas are drawn.
274   void SetEdgeOn() { SetDrawEdges (true); }
275
276   //! The edges of FillAreas are not drawn.
277   void SetEdgeOff() { SetDrawEdges (false); }
278
279   //! Return color of edges.
280   const Quantity_Color& EdgeColor() const { return myEdgeColor.GetRGB(); }
281
282   //! Return color of edges.
283   const Quantity_ColorRGBA& EdgeColorRGBA() const { return myEdgeColor; }
284
285   //! Modifies the color of the edge of the face
286   void SetEdgeColor (const Quantity_Color& theColor) { myEdgeColor.SetRGB (theColor); }
287
288   //! Modifies the color of the edge of the face
289   void SetEdgeColor (const Quantity_ColorRGBA& theColor) { myEdgeColor = theColor; }
290
291   //! Return edges line type.
292   Aspect_TypeOfLine EdgeLineType() const { return myEdgeType; }
293
294   //! Modifies the edge line type
295   void SetEdgeLineType (const Aspect_TypeOfLine theType) { myEdgeType = theType; }
296
297   //! Return width for edges in pixels.
298   Standard_ShortReal EdgeWidth() const { return myEdgeWidth; }
299
300   //! Modifies the edge thickness
301   //! Warning: Raises AspectFillAreaDefinitionError if the width is a negative value.
302   void SetEdgeWidth (const Standard_Real theWidth)
303   {
304     if (theWidth <= 0.0)
305     {
306       throw Aspect_AspectFillAreaDefinitionError("Bad value for EdgeLineWidth");
307     }
308     myEdgeWidth = (float )theWidth;
309   }
310
311   //! Returns TRUE if drawing element edges should discard first edge in triangle; FALSE by default.
312   //! Graphics hardware works mostly with triangles, so that wireframe presentation will draw triangle edges by default.
313   //! This flag allows rendering wireframe presentation of quad-only array split into triangles.
314   //! For this, quads should be split in specific order, so that the quad diagonal (to be NOT rendered) goes first:
315   //!     1------2
316   //!    /      /   Triangle #1: 2-0-1; Triangle #2: 0-2-3
317   //!   0------3
318   bool ToSkipFirstEdge() const { return myToSkipFirstEdge; }
319
320   //! Set skip first triangle edge flag for drawing wireframe presentation of quads array split into triangles.
321   void SetSkipFirstEdge (bool theToSkipFirstEdge) { myToSkipFirstEdge = theToSkipFirstEdge; }
322
323 public:
324
325   //! Returns the hatch type used when InteriorStyle is IS_HATCH
326   const Handle(Graphic3d_HatchStyle)& HatchStyle() const { return myHatchStyle; }
327
328   //! Modifies the hatch type used when InteriorStyle is IS_HATCH
329   void SetHatchStyle (const Handle(Graphic3d_HatchStyle)& theStyle) { myHatchStyle = theStyle; }
330
331   //! Modifies the hatch type used when InteriorStyle is IS_HATCH
332   //! @warning This method always creates a new handle for a given hatch style
333   void SetHatchStyle (const Aspect_HatchStyle theStyle)
334   {
335     if (theStyle == Aspect_HS_SOLID)
336     {
337       myHatchStyle.Nullify();
338       return;
339     }
340
341     myHatchStyle = new Graphic3d_HatchStyle (theStyle);
342   }
343
344   //! Returns the current values.
345   Standard_DEPRECATED("Deprecated method Values() should be replaced by individual property getters")
346   void Values (Aspect_InteriorStyle& theStyle,
347                Quantity_Color&       theIntColor,
348                Quantity_Color&       theEdgeColor,
349                Aspect_TypeOfLine&    theType,
350                Standard_Real&        theWidth) const
351   {
352     theStyle    = myInteriorStyle;
353     theIntColor = myInteriorColor.GetRGB();
354     theEdgeColor= myEdgeColor.GetRGB();
355     theType     = myEdgeType;
356     theWidth    = myEdgeWidth;
357   }
358
359   //! Returns the current values.
360   Standard_DEPRECATED("Deprecated method Values() should be replaced by individual property getters")
361   void Values (Aspect_InteriorStyle& theStyle,
362                Quantity_Color&       theIntColor,
363                Quantity_Color&       theBackIntColor,
364                Quantity_Color&       theEdgeColor,
365                Aspect_TypeOfLine&    theType,
366                Standard_Real&        theWidth) const
367   {
368     theStyle       = myInteriorStyle;
369     theIntColor    = myInteriorColor.GetRGB();
370     theBackIntColor= myBackInteriorColor.GetRGB();
371     theEdgeColor   = myEdgeColor.GetRGB();
372     theType        = myEdgeType;
373     theWidth       = myEdgeWidth;
374   }
375
376 protected:
377
378   Handle(Graphic3d_ShaderProgram) myProgram;
379   Handle(Graphic3d_TextureSet)    myTextureSet;
380   Graphic3d_MaterialAspect        myFrontMaterial;
381   Graphic3d_MaterialAspect        myBackMaterial;
382
383   Quantity_ColorRGBA           myInteriorColor;
384   Quantity_ColorRGBA           myBackInteriorColor;
385   Quantity_ColorRGBA           myEdgeColor;
386   Aspect_InteriorStyle         myInteriorStyle;
387   Graphic3d_TypeOfShadingModel myShadingModel;
388   Graphic3d_AlphaMode          myAlphaMode;
389   Standard_ShortReal           myAlphaCutoff;
390   Aspect_TypeOfLine            myEdgeType;
391   Standard_ShortReal           myEdgeWidth;
392   Handle(Graphic3d_HatchStyle) myHatchStyle;
393
394   Graphic3d_PolygonOffset myPolygonOffset;
395   bool                    myToSkipFirstEdge;
396   bool                    myToDistinguishMaterials;
397   bool                    myToDrawEdges;
398   bool                    myToSuppressBackFaces;
399   bool                    myToMapTexture;
400
401 };
402
403 DEFINE_STANDARD_HANDLE(Graphic3d_AspectFillArea3d, Standard_Transient)
404
405 #endif // _Graphic3d_AspectFillArea3d_HeaderFile