405b6fdc9a15bb493d1587779c35f39cbf64dcf9
[occt.git] / src / OpenGl / OpenGl_TextureBufferArb.cxx
1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2012 OPEN CASCADE SAS
3 //
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
8 //
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 //
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
18
19 #include <OpenGl_TextureBufferArb.hxx>
20
21 #include <OpenGl_Context.hxx>
22 #include <Standard_Assert.hxx>
23
24 IMPLEMENT_STANDARD_HANDLE (OpenGl_TextureBufferArb, OpenGl_VertexBuffer)
25 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_TextureBufferArb, OpenGl_VertexBuffer)
26
27 // =======================================================================
28 // function : OpenGl_TextureBufferArb
29 // purpose  :
30 // =======================================================================
31 OpenGl_TextureBufferArb::OpenGl_TextureBufferArb()
32 : OpenGl_VertexBuffer(),
33   myTextureId (NO_TEXTURE),
34   myTexFormat (GL_RGBA32F)
35 {
36   //
37 }
38
39 // =======================================================================
40 // function : ~OpenGl_TextureBufferArb
41 // purpose  :
42 // =======================================================================
43 OpenGl_TextureBufferArb::~OpenGl_TextureBufferArb()
44 {
45   Release (NULL);
46 }
47
48 // =======================================================================
49 // function : GetTarget
50 // purpose  :
51 // =======================================================================
52 GLenum OpenGl_TextureBufferArb::GetTarget() const
53 {
54   return GL_TEXTURE_BUFFER_ARB; // GL_TEXTURE_BUFFER for OpenGL 3.1+
55 }
56
57 // =======================================================================
58 // function : Release
59 // purpose  :
60 // =======================================================================
61 void OpenGl_TextureBufferArb::Release (const OpenGl_Context* theGlCtx)
62 {
63   if (myTextureId != NO_TEXTURE)
64   {
65     // application can not handle this case by exception - this is bug in code
66     Standard_ASSERT_RETURN (theGlCtx != NULL,
67       "OpenGl_TextureBufferExt destroyed without GL context! Possible GPU memory leakage...",);
68
69     glDeleteTextures (1, &myTextureId);
70     myTextureId = NO_TEXTURE;
71   }
72   OpenGl_VertexBuffer::Release (theGlCtx);
73 }
74
75 // =======================================================================
76 // function : Create
77 // purpose  :
78 // =======================================================================
79 bool OpenGl_TextureBufferArb::Create (const Handle(OpenGl_Context)& theGlCtx)
80 {
81   if (!OpenGl_VertexBuffer::Create (theGlCtx))
82   {
83     return false;
84   }
85
86   if (myTextureId == NO_TEXTURE)
87   {
88     glGenTextures (1, &myTextureId);
89   }
90   return myTextureId != NO_TEXTURE;
91 }
92
93 // =======================================================================
94 // function : Init
95 // purpose  :
96 // =======================================================================
97 bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx,
98                                     const GLuint   theComponentsNb,
99                                     const GLsizei  theElemsNb,
100                                     const GLfloat* theData)
101 {
102   if (theComponentsNb != 1
103    && theComponentsNb != 2
104    && theComponentsNb != 4)
105   {
106     // unsupported format
107     return false;
108   }
109   else if (!Create (theGlCtx)
110         || !OpenGl_VertexBuffer::Init (theGlCtx, theComponentsNb, theElemsNb, theData))
111   {
112     return false;
113   }
114
115   switch (theComponentsNb)
116   {
117     case 1: myTexFormat = GL_R32F;    break;
118     case 2: myTexFormat = GL_RG32F;   break;
119     //case 3: myTexFormat = GL_RGB32F;  break; // GL_ARB_texture_buffer_object_rgb32
120     case 4: myTexFormat = GL_RGBA32F; break;
121   }
122
123   Bind (theGlCtx);
124   BindTexture (theGlCtx);
125   theGlCtx->arbTBO->glTexBufferARB (GetTarget(), myTexFormat, myBufferId);
126   UnbindTexture (theGlCtx);
127   Unbind (theGlCtx);
128   return true;
129 }
130
131 // =======================================================================
132 // function : BindTexture
133 // purpose  :
134 // =======================================================================
135 void OpenGl_TextureBufferArb::BindTexture (const Handle(OpenGl_Context)& theGlCtx,
136                                            const GLenum theTextureUnit) const
137 {
138   theGlCtx->core20->glActiveTexture (theTextureUnit);
139   glBindTexture (GetTarget(), myTextureId);
140 }
141
142 // =======================================================================
143 // function : UnbindTexture
144 // purpose  :
145 // =======================================================================
146 void OpenGl_TextureBufferArb::UnbindTexture (const Handle(OpenGl_Context)& theGlCtx,
147                                              const GLenum theTextureUnit) const
148 {
149   theGlCtx->core20->glActiveTexture (theTextureUnit);
150   glBindTexture (GetTarget(), NO_TEXTURE);
151 }