0030058: Visualization, Select3D_SensitivePrimitiveArray - the selection is not fast...
[occt.git] / src / OpenGl / OpenGl_PrimitiveArray.hxx
1 // Created on: 2011-07-13
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-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 #ifndef OpenGl_PrimitiveArray_Header
17 #define OpenGl_PrimitiveArray_Header
18
19 #include <OpenGl_IndexBuffer.hxx>
20
21 #include <Aspect_InteriorStyle.hxx>
22 #include <Aspect_TypeOfMarker.hxx>
23 #include <Graphic3d_TypeOfPrimitiveArray.hxx>
24 #include <Graphic3d_IndexBuffer.hxx>
25 #include <Graphic3d_BoundBuffer.hxx>
26
27 #include <OpenGl_Element.hxx>
28
29 class OpenGl_GraphicDriver;
30
31 //! Class for rendering of arbitrary primitive array.
32 class OpenGl_PrimitiveArray : public OpenGl_Element
33 {
34 public:
35   //! OpenGL does not provide a constant for "none" draw mode.
36   //! So we define our own one that does not conflict with GL constants and utilizes common GL invalid value.
37   enum
38   {
39     DRAW_MODE_NONE = -1
40   };
41
42   //! Empty constructor
43   Standard_EXPORT OpenGl_PrimitiveArray (const OpenGl_GraphicDriver* theDriver);
44
45   //! Default constructor
46   Standard_EXPORT OpenGl_PrimitiveArray (const OpenGl_GraphicDriver*          theDriver,
47                                          const Graphic3d_TypeOfPrimitiveArray theType,
48                                          const Handle(Graphic3d_IndexBuffer)& theIndices,
49                                          const Handle(Graphic3d_Buffer)&      theAttribs,
50                                          const Handle(Graphic3d_BoundBuffer)& theBounds);
51
52   //! Destructor
53   Standard_EXPORT virtual ~OpenGl_PrimitiveArray();
54
55   //! Render primitives to the window
56   Standard_EXPORT virtual void Render  (const Handle(OpenGl_Workspace)& theWorkspace) const Standard_OVERRIDE;
57
58   //! Release OpenGL resources (VBOs)
59   Standard_EXPORT virtual void Release (OpenGl_Context* theContext) Standard_OVERRIDE;
60
61   //! Return true if VBOs initialization has been performed.
62   //! VBO initialization is performed during first Render() call.
63   //! Notice that this flag does not indicate VBOs validity.
64   Standard_Boolean IsInitialized() const { return myIsVboInit; }
65
66   //! Invalidate VBO content without destruction.
67   void Invalidate() const { myIsVboInit = Standard_False; }
68
69   //! @return primitive type (GL_LINES, GL_TRIANGLES and others)
70   GLint DrawMode() const { return myDrawMode; }
71
72   //! Return TRUE if primitive type generates shaded triangulation.
73   virtual Standard_Boolean IsFillDrawMode() const Standard_OVERRIDE { return myIsFillType; }
74
75   //! @return indices array
76   const Handle(Graphic3d_IndexBuffer)& Indices() const { return myIndices; }
77
78   //! @return attributes array
79   const Handle(Graphic3d_Buffer)& Attributes() const { return myAttribs; }
80
81   //! @return bounds array
82   const Handle(Graphic3d_BoundBuffer)& Bounds() const { return myBounds; }
83
84   //! Returns unique ID of primitive array. 
85   Standard_Size GetUID() const { return myUID; }
86
87   //! Initialize indices, attributes and bounds with new data.
88   Standard_EXPORT void InitBuffers (const Handle(OpenGl_Context)&        theContext,
89                                     const Graphic3d_TypeOfPrimitiveArray theType,
90                                     const Handle(Graphic3d_IndexBuffer)& theIndices,
91                                     const Handle(Graphic3d_Buffer)&      theAttribs,
92                                     const Handle(Graphic3d_BoundBuffer)& theBounds);
93
94 public:
95
96   //! Returns index VBO.
97   const Handle(OpenGl_VertexBuffer)& IndexVbo() const { return  myVboIndices; }
98
99   //! Returns attributes VBO.
100   const Handle(OpenGl_VertexBuffer)& AttributesVbo() const { return myVboAttribs; }
101
102 protected:
103
104   //! VBO initialization procedures
105   //! @param theCtx        bound GL context
106   //! @param theToKeepData when true, myAttribs will not be nullified after VBO creation
107   Standard_EXPORT Standard_Boolean buildVBO (const Handle(OpenGl_Context)& theCtx,
108                                              const Standard_Boolean        theToKeepData) const;
109
110   Standard_EXPORT void clearMemoryGL (const Handle(OpenGl_Context)& theGlCtx) const;
111
112 private:
113
114   //! Initialize normal (OpenGL-provided) VBO
115   Standard_Boolean initNormalVbo (const Handle(OpenGl_Context)& theCtx) const;
116
117   //! Main procedure to draw array
118   void drawArray (const Handle(OpenGl_Workspace)& theWorkspace,
119                   const Graphic3d_Vec4*           theFaceColors,
120                   const Standard_Boolean          theHasVertColor) const;
121
122   //! Auxiliary procedures
123   void drawEdges (const OpenGl_Vec4&              theEdgeColour,
124                   const Handle(OpenGl_Workspace)& theWorkspace) const;
125
126   void drawMarkers (const Handle(OpenGl_Workspace)& theWorkspace) const;
127
128   //! Sets OpenGL draw mode according to the input type of primitive array.
129   //! If buffer of attributes is empty, draw mode is set to NONE to avoid invalid array rendering.
130   //! @param theType type of primitive array.
131   void setDrawMode (const Graphic3d_TypeOfPrimitiveArray theType);
132
133   //! Rebuilds the array of vertex attributes so that it can be drawn without indices.
134   Standard_Boolean processIndices (const Handle(OpenGl_Context)& theContext) const;
135
136 protected:
137
138   mutable Handle(OpenGl_VertexBuffer)   myVboIndices;
139   mutable Handle(OpenGl_VertexBuffer)   myVboAttribs;
140
141   mutable Handle(Graphic3d_IndexBuffer) myIndices;
142   mutable Handle(Graphic3d_Buffer)      myAttribs;
143   mutable Handle(Graphic3d_BoundBuffer) myBounds;
144   GLshort                               myDrawMode;
145   mutable Standard_Boolean              myIsFillType;
146   mutable Standard_Boolean              myIsVboInit;
147
148   Standard_Size                         myUID; //!< Unique ID of primitive array. 
149
150 public:
151
152   DEFINE_STANDARD_ALLOC
153
154 };
155
156 #endif //OpenGl_PrimitiveArray_Header