1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <OpenGl_TextureBufferArb.hxx>
17 #include <OpenGl_Context.hxx>
18 #include <Standard_Assert.hxx>
21 // =======================================================================
22 // function : OpenGl_TextureBufferArb
24 // =======================================================================
25 OpenGl_TextureBufferArb::OpenGl_TextureBufferArb()
26 : OpenGl_VertexBuffer(),
27 myTextureId (NO_TEXTURE),
28 myTexFormat (GL_RGBA32F)
33 // =======================================================================
34 // function : ~OpenGl_TextureBufferArb
36 // =======================================================================
37 OpenGl_TextureBufferArb::~OpenGl_TextureBufferArb()
42 // =======================================================================
43 // function : GetTarget
45 // =======================================================================
46 GLenum OpenGl_TextureBufferArb::GetTarget() const
48 return GL_TEXTURE_BUFFER_ARB; // GL_TEXTURE_BUFFER for OpenGL 3.1+
51 // =======================================================================
54 // =======================================================================
55 void OpenGl_TextureBufferArb::Release (OpenGl_Context* theGlCtx)
57 if (myTextureId != NO_TEXTURE)
59 // application can not handle this case by exception - this is bug in code
60 Standard_ASSERT_RETURN (theGlCtx != NULL,
61 "OpenGl_TextureBufferExt destroyed without GL context! Possible GPU memory leakage...",);
63 if (theGlCtx->IsValid())
65 glDeleteTextures (1, &myTextureId);
67 myTextureId = NO_TEXTURE;
69 OpenGl_VertexBuffer::Release (theGlCtx);
72 // =======================================================================
75 // =======================================================================
76 bool OpenGl_TextureBufferArb::Create (const Handle(OpenGl_Context)& theGlCtx)
78 if (!OpenGl_VertexBuffer::Create (theGlCtx))
83 if (myTextureId == NO_TEXTURE)
85 glGenTextures (1, &myTextureId);
87 return myTextureId != NO_TEXTURE;
90 // =======================================================================
93 // =======================================================================
94 bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx,
95 const GLuint theComponentsNb,
96 const GLsizei theElemsNb,
97 const GLfloat* theData)
99 #if !defined(GL_ES_VERSION_2_0)
100 if (theComponentsNb < 1
101 || theComponentsNb > 4)
103 // unsupported format
106 else if (theComponentsNb == 3
107 && !theGlCtx->arbTboRGB32)
111 else if (!Create (theGlCtx)
112 || !OpenGl_VertexBuffer::Init (theGlCtx, theComponentsNb, theElemsNb, theData))
117 switch (theComponentsNb)
119 case 1: myTexFormat = GL_R32F; break;
120 case 2: myTexFormat = GL_RG32F; break;
121 case 3: myTexFormat = GL_RGB32F; break; // GL_ARB_texture_buffer_object_rgb32
122 case 4: myTexFormat = GL_RGBA32F; break;
126 BindTexture (theGlCtx);
127 theGlCtx->arbTBO->glTexBuffer (GetTarget(), myTexFormat, myBufferId);
128 UnbindTexture (theGlCtx);
136 // =======================================================================
139 // =======================================================================
140 bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx,
141 const GLuint theComponentsNb,
142 const GLsizei theElemsNb,
143 const GLuint* theData)
145 #if !defined(GL_ES_VERSION_2_0)
146 if (theComponentsNb < 1
147 || theComponentsNb > 4)
149 // unsupported format
152 else if (theComponentsNb == 3
153 && !theGlCtx->arbTboRGB32)
157 else if (!Create (theGlCtx)
158 || !OpenGl_VertexBuffer::Init (theGlCtx, theComponentsNb, theElemsNb, theData))
163 switch (theComponentsNb)
165 case 1: myTexFormat = GL_R32I; break;
166 case 2: myTexFormat = GL_RG32I; break;
167 case 3: myTexFormat = GL_RGB32I; break;
168 case 4: myTexFormat = GL_RGBA32I; break;
172 BindTexture (theGlCtx);
173 theGlCtx->arbTBO->glTexBuffer (GetTarget(), myTexFormat, myBufferId);
174 UnbindTexture (theGlCtx);
182 // =======================================================================
183 // function : BindTexture
185 // =======================================================================
186 void OpenGl_TextureBufferArb::BindTexture (const Handle(OpenGl_Context)& theGlCtx,
187 const GLenum theTextureUnit) const
189 theGlCtx->core20fwd->glActiveTexture (theTextureUnit);
190 glBindTexture (GetTarget(), myTextureId);
193 // =======================================================================
194 // function : UnbindTexture
196 // =======================================================================
197 void OpenGl_TextureBufferArb::UnbindTexture (const Handle(OpenGl_Context)& theGlCtx,
198 const GLenum theTextureUnit) const
200 theGlCtx->core20fwd->glActiveTexture (theTextureUnit);
201 glBindTexture (GetTarget(), NO_TEXTURE);