1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2012 OPEN CASCADE SAS
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.
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.
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.
19 #include <OpenGl_TextureBufferArb.hxx>
21 #include <OpenGl_Context.hxx>
22 #include <Standard_Assert.hxx>
24 IMPLEMENT_STANDARD_HANDLE (OpenGl_TextureBufferArb, OpenGl_VertexBuffer)
25 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_TextureBufferArb, OpenGl_VertexBuffer)
27 // =======================================================================
28 // function : OpenGl_TextureBufferArb
30 // =======================================================================
31 OpenGl_TextureBufferArb::OpenGl_TextureBufferArb()
32 : OpenGl_VertexBuffer(),
33 myTextureId (NO_TEXTURE),
34 myTexFormat (GL_RGBA32F)
39 // =======================================================================
40 // function : ~OpenGl_TextureBufferArb
42 // =======================================================================
43 OpenGl_TextureBufferArb::~OpenGl_TextureBufferArb()
48 // =======================================================================
49 // function : GetTarget
51 // =======================================================================
52 GLenum OpenGl_TextureBufferArb::GetTarget() const
54 return GL_TEXTURE_BUFFER_ARB; // GL_TEXTURE_BUFFER for OpenGL 3.1+
57 // =======================================================================
60 // =======================================================================
61 void OpenGl_TextureBufferArb::Release (const OpenGl_Context* theGlCtx)
63 if (myTextureId != NO_TEXTURE)
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...",);
69 if (theGlCtx->IsValid())
71 glDeleteTextures (1, &myTextureId);
73 myTextureId = NO_TEXTURE;
75 OpenGl_VertexBuffer::Release (theGlCtx);
78 // =======================================================================
81 // =======================================================================
82 bool OpenGl_TextureBufferArb::Create (const Handle(OpenGl_Context)& theGlCtx)
84 if (!OpenGl_VertexBuffer::Create (theGlCtx))
89 if (myTextureId == NO_TEXTURE)
91 glGenTextures (1, &myTextureId);
93 return myTextureId != NO_TEXTURE;
96 // =======================================================================
99 // =======================================================================
100 bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx,
101 const GLuint theComponentsNb,
102 const GLsizei theElemsNb,
103 const GLfloat* theData)
105 if (theComponentsNb != 1
106 && theComponentsNb != 2
107 && theComponentsNb != 4)
109 // unsupported format
112 else if (!Create (theGlCtx)
113 || !OpenGl_VertexBuffer::Init (theGlCtx, theComponentsNb, theElemsNb, theData))
118 switch (theComponentsNb)
120 case 1: myTexFormat = GL_R32F; break;
121 case 2: myTexFormat = GL_RG32F; break;
122 //case 3: myTexFormat = GL_RGB32F; break; // GL_ARB_texture_buffer_object_rgb32
123 case 4: myTexFormat = GL_RGBA32F; break;
127 BindTexture (theGlCtx);
128 theGlCtx->arbTBO->glTexBufferARB (GetTarget(), myTexFormat, myBufferId);
129 UnbindTexture (theGlCtx);
134 // =======================================================================
135 // function : BindTexture
137 // =======================================================================
138 void OpenGl_TextureBufferArb::BindTexture (const Handle(OpenGl_Context)& theGlCtx,
139 const GLenum theTextureUnit) const
141 theGlCtx->core20->glActiveTexture (theTextureUnit);
142 glBindTexture (GetTarget(), myTextureId);
145 // =======================================================================
146 // function : UnbindTexture
148 // =======================================================================
149 void OpenGl_TextureBufferArb::UnbindTexture (const Handle(OpenGl_Context)& theGlCtx,
150 const GLenum theTextureUnit) const
152 theGlCtx->core20->glActiveTexture (theTextureUnit);
153 glBindTexture (GetTarget(), NO_TEXTURE);