0024739: TKOpenGl - port ray-tracing from OpenCL to GLSL for better integration and...
[occt.git] / src / OpenGl / OpenGl_VertexBuffer.hxx
CommitLineData
5e27df78 1// Created by: Kirill GAVRILOV
d5f74e42 2// Copyright (c) 2013-2014 OPEN CASCADE SAS
5e27df78 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
5e27df78 5//
d5f74e42 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
973c2be1 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.
5e27df78 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
5e27df78 14
15#ifndef _OpenGl_VertexBuffer_H__
16#define _OpenGl_VertexBuffer_H__
17
18#include <OpenGl_GlCore20.hxx>
19#include <OpenGl_Resource.hxx>
20
21class Handle(OpenGl_Context);
22class OpenGl_Context;
23
24//! Vertex Buffer Object - is a general storage object for vertex attributes (position, normal, color).
25//! Notice that you should use OpenGl_IndexBuffer specialization for array of indices.
26class OpenGl_VertexBuffer : public OpenGl_Resource
27{
28
29public:
30
31 //! Helpful constants
32 static const GLuint NO_BUFFER = 0;
33
34public:
35
36 //! Create uninitialized VBO.
37 Standard_EXPORT OpenGl_VertexBuffer();
38
39 //! Destroy object.
40 Standard_EXPORT virtual ~OpenGl_VertexBuffer();
41
42 Standard_EXPORT virtual GLenum GetTarget() const;
43
44 //! @return true if current object was initialized
45 inline bool IsValid() const
46 {
47 return myBufferId != NO_BUFFER;
48 }
49
50 //! @return the number of components per generic vertex attribute.
51 inline GLuint GetComponentsNb() const
52 {
53 return myComponentsNb;
54 }
55
56 //! @return number of vertex attributes / number of vertices.
57 inline GLsizei GetElemsNb() const
58 {
59 return myElemsNb;
60 }
61
62 //! @return data type of each component in the array.
63 inline GLenum GetDataType() const
64 {
65 return myDataType;
66 }
67
68 //! Creates VBO name (id) if not yet generated.
69 //! Data should be initialized by another method.
70 Standard_EXPORT bool Create (const Handle(OpenGl_Context)& theGlCtx);
71
72 //! Destroy object - will release GPU memory if any.
73 Standard_EXPORT virtual void Release (const OpenGl_Context* theGlCtx);
74
75 //! Bind this VBO.
76 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theGlCtx) const;
77
78 //! Unbind this VBO.
79 Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theGlCtx) const;
80
81 //! Notice that VBO will be unbound after this call.
82 //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
83 //! @param theElemsNb - elements count;
84 //! @param theData - pointer to GLfloat data (vertices/normals etc.).
85 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx,
86 const GLuint theComponentsNb,
87 const GLsizei theElemsNb,
88 const GLfloat* theData);
89
90 //! Notice that VBO will be unbound after this call.
91 //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
92 //! @param theElemsNb - elements count;
93 //! @param theData - pointer to GLuint data (indices etc.).
94 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx,
95 const GLuint theComponentsNb,
96 const GLsizei theElemsNb,
97 const GLuint* theData);
98
99 //! Notice that VBO will be unbound after this call.
100 //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
101 //! @param theElemsNb - elements count;
102 //! @param theData - pointer to GLubyte data (indices/colors etc.).
103 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx,
104 const GLuint theComponentsNb,
105 const GLsizei theElemsNb,
106 const GLubyte* theData);
107
108 //! Notice that VBO will be unbound after this call.
109 //! Function replaces portion of data within this VBO using glBufferSubData().
110 //! The VBO should be initialized before call.
111 //! @param theElemFrom - element id from which replace buffer data (>=0);
99d99a6d 112 //! @param theElemsNb - elements count (theElemFrom + theElemsNb <= GetElemsNb());
5e27df78 113 //! @param theData - pointer to GLfloat data.
114 Standard_EXPORT bool SubData (const Handle(OpenGl_Context)& theGlCtx,
115 const GLsizei theElemFrom,
116 const GLsizei theElemsNb,
117 const GLfloat* theData);
118
99d99a6d 119 //! Notice that VBO will be unbound after this call.
120 //! Function replaces portion of data within this VBO using glBufferSubData().
121 //! The VBO should be initialized before call.
122 //! @param theElemFrom element id from which replace buffer data (>=0);
123 //! @param theElemsNb elements count (theElemFrom + theElemsNb <= GetElemsNb());
124 //! @param theData pointer to GLuint data.
125 Standard_EXPORT bool SubData (const Handle(OpenGl_Context)& theGlCtx,
126 const GLsizei theElemFrom,
127 const GLsizei theElemsNb,
128 const GLuint* theData);
129
130 //! Notice that VBO will be unbound after this call.
131 //! Function replaces portion of data within this VBO using glBufferSubData().
132 //! The VBO should be initialized before call.
133 //! @param theElemFrom element id from which replace buffer data (>=0);
134 //! @param theElemsNb elements count (theElemFrom + theElemsNb <= GetElemsNb());
135 //! @param theData pointer to GLubyte data.
136 Standard_EXPORT bool SubData (const Handle(OpenGl_Context)& theGlCtx,
137 const GLsizei theElemFrom,
138 const GLsizei theElemsNb,
139 const GLubyte* theData);
140
5e27df78 141 //! Bind this VBO to active GLSL program.
142 Standard_EXPORT void BindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
143 const GLuint theAttribLoc) const;
144
145 //! Unbind any VBO from active GLSL program.
146 Standard_EXPORT void UnbindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
147 const GLuint theAttribLoc) const;
148
149 //! Bind this VBO as fixed pipeline attribute.
150 //! @param theGlCtx - handle to bound GL context;
151 //! @param theMode - array mode (GL_VERTEX_ARRAY, GL_NORMAL_ARRAY, GL_COLOR_ARRAY, GL_INDEX_ARRAY, GL_TEXTURE_COORD_ARRAY).
152 Standard_EXPORT void BindFixed (const Handle(OpenGl_Context)& theGlCtx,
153 const GLenum theMode) const;
154
155 //! Unbind this VBO as fixed pipeline attribute.
156 //! @param theGlCtx - handle to bound GL context;
157 //! @param theMode - array mode.
158 Standard_EXPORT void UnbindFixed (const Handle(OpenGl_Context)& theGlCtx,
159 const GLenum theMode) const;
160
161protected:
162
163 GLuint myBufferId; //!< VBO name (index)
164 GLuint myComponentsNb; //!< Number of components per generic vertex attribute, must be 1, 2, 3, or 4
165 GLsizei myElemsNb; //!< Number of vertex attributes / number of vertices
166 GLenum myDataType; //!< Data type (GL_FLOAT, GL_UNSIGNED_INT, GL_UNSIGNED_BYTE etc.)
167
168public:
169
170 DEFINE_STANDARD_RTTI(OpenGl_VertexBuffer) // Type definition
171
172};
173
174DEFINE_STANDARD_HANDLE(OpenGl_VertexBuffer, OpenGl_Resource)
175
176#endif // _OpenGl_VertexBuffer_H__