0032465: Visualization, TKOpenGles - invalid enum on GL_RGBA16F initialization in...
[occt.git] / src / OpenGl / OpenGl_VertexBuffer.hxx
1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2013-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef _OpenGl_VertexBuffer_H__
16 #define _OpenGl_VertexBuffer_H__
17
18 #include <OpenGl_Buffer.hxx>
19 #include <Graphic3d_Buffer.hxx>
20
21 //! Vertex Buffer Object - is a general storage object for vertex attributes (position, normal, color).
22 //! Notice that you should use OpenGl_IndexBuffer specialization for array of indices.
23 class OpenGl_VertexBuffer : public OpenGl_Buffer
24 {
25 public:
26
27   //! Create uninitialized VBO.
28   Standard_EXPORT OpenGl_VertexBuffer();
29
30   //! Destroy object.
31   Standard_EXPORT virtual ~OpenGl_VertexBuffer();
32
33   //! Return buffer target GL_ARRAY_BUFFER.
34   Standard_EXPORT virtual unsigned int GetTarget() const Standard_OVERRIDE;
35
36   //! Bind this VBO to active GLSL program.
37   Standard_EXPORT void BindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
38                                          const unsigned int            theAttribLoc) const;
39
40   //! Unbind any VBO from active GLSL program.
41   Standard_EXPORT void UnbindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
42                                            const unsigned int            theAttribLoc) const;
43
44   //! Bind this VBO and enable specified attribute in OpenGl_Context::ActiveProgram() or FFP.
45   //! @param theGlCtx - handle to bound GL context;
46   //! @param theMode  - array mode (GL_VERTEX_ARRAY, GL_NORMAL_ARRAY, GL_COLOR_ARRAY, GL_INDEX_ARRAY, GL_TEXTURE_COORD_ARRAY).
47   void BindAttribute (const Handle(OpenGl_Context)&   theCtx,
48                       const Graphic3d_TypeOfAttribute theMode) const
49   {
50     if (IsValid())
51     {
52       Bind (theCtx);
53       bindAttribute (theCtx, theMode, static_cast<int> (myComponentsNb), myDataType, 0, myOffset);
54     }
55   }
56
57   //! Unbind this VBO and disable specified attribute in OpenGl_Context::ActiveProgram() or FFP.
58   //! @param theCtx handle to bound GL context
59   //! @param theMode  array mode
60   void UnbindAttribute (const Handle(OpenGl_Context)&   theCtx,
61                         const Graphic3d_TypeOfAttribute theMode) const
62   {
63     if (IsValid())
64     {
65       Unbind (theCtx);
66       unbindAttribute (theCtx, theMode);
67     }
68   }
69
70 public: //! @name advanced methods
71
72   //! Setup array pointer - either for active GLSL program OpenGl_Context::ActiveProgram()
73   //! or for FFP using bindFixed() when no program bound.
74   Standard_EXPORT static void bindAttribute (const Handle(OpenGl_Context)&   theGlCtx,
75                                              const Graphic3d_TypeOfAttribute theMode,
76                                              const Standard_Integer          theNbComp,
77                                              const unsigned int              theDataType,
78                                              const Standard_Integer          theStride,
79                                              const void*                     theOffset);
80
81   //! Disable GLSL array pointer - either for active GLSL program OpenGl_Context::ActiveProgram()
82   //! or for FFP using unbindFixed() when no program bound.
83   Standard_EXPORT static void unbindAttribute (const Handle(OpenGl_Context)&   theGlCtx,
84                                                const Graphic3d_TypeOfAttribute theMode);
85
86 private:
87 #if !defined(GL_ES_VERSION_2_0)
88   //! Setup FFP array pointer.
89   Standard_EXPORT static void bindFixed (const Handle(OpenGl_Context)&   theGlCtx,
90                                          const Graphic3d_TypeOfAttribute theMode,
91                                          const Standard_Integer          theNbComp,
92                                          const unsigned int              theDataType,
93                                          const Standard_Integer          theStride,
94                                          const void*                     theOffset);
95
96   //! Disable FFP array pointer.
97   Standard_EXPORT static void unbindFixed (const Handle(OpenGl_Context)&   theGlCtx,
98                                            const Graphic3d_TypeOfAttribute theMode);
99
100   //! Disable FFP color array pointer.
101   Standard_EXPORT static void unbindFixedColor (const Handle(OpenGl_Context)& theCtx);
102
103 #endif
104 public: //! @name methods for interleaved attributes array
105
106   //! @return true if buffer contains per-vertex color attribute
107   Standard_EXPORT virtual bool HasColorAttribute() const;
108
109   //! @return true if buffer contains per-vertex normal attribute
110   Standard_EXPORT virtual bool HasNormalAttribute() const;
111
112   //! Bind all vertex attributes to active program OpenGl_Context::ActiveProgram() or for FFP.
113   //! Default implementation does nothing.
114   Standard_EXPORT virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const;
115
116   //! Bind vertex position attribute only. Default implementation does nothing.
117   Standard_EXPORT virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const;
118
119   //! Unbind all vertex attributes. Default implementation does nothing.
120   Standard_EXPORT virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const;
121
122 public:
123
124   DEFINE_STANDARD_RTTIEXT(OpenGl_VertexBuffer, OpenGl_Buffer)
125
126 };
127
128 DEFINE_STANDARD_HANDLE(OpenGl_VertexBuffer, OpenGl_Buffer)
129
130 #endif // _OpenGl_VertexBuffer_H__