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 glDeleteTextures (1, &myTextureId);
70 myTextureId = NO_TEXTURE;
72 OpenGl_VertexBuffer::Release (theGlCtx);
75 // =======================================================================
78 // =======================================================================
79 bool OpenGl_TextureBufferArb::Create (const Handle(OpenGl_Context)& theGlCtx)
81 if (!OpenGl_VertexBuffer::Create (theGlCtx))
86 if (myTextureId == NO_TEXTURE)
88 glGenTextures (1, &myTextureId);
90 return myTextureId != NO_TEXTURE;
93 // =======================================================================
96 // =======================================================================
97 bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx,
98 const GLuint theComponentsNb,
99 const GLsizei theElemsNb,
100 const GLfloat* theData)
102 if (theComponentsNb != 1
103 && theComponentsNb != 2
104 && theComponentsNb != 4)
106 // unsupported format
109 else if (!Create (theGlCtx)
110 || !OpenGl_VertexBuffer::Init (theGlCtx, theComponentsNb, theElemsNb, theData))
115 switch (theComponentsNb)
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;
124 BindTexture (theGlCtx);
125 theGlCtx->arbTBO->glTexBufferARB (GetTarget(), myTexFormat, myBufferId);
126 UnbindTexture (theGlCtx);
131 // =======================================================================
132 // function : BindTexture
134 // =======================================================================
135 void OpenGl_TextureBufferArb::BindTexture (const Handle(OpenGl_Context)& theGlCtx,
136 const GLenum theTextureUnit) const
138 theGlCtx->core20->glActiveTexture (theTextureUnit);
139 glBindTexture (GetTarget(), myTextureId);
142 // =======================================================================
143 // function : UnbindTexture
145 // =======================================================================
146 void OpenGl_TextureBufferArb::UnbindTexture (const Handle(OpenGl_Context)& theGlCtx,
147 const GLenum theTextureUnit) const
149 theGlCtx->core20->glActiveTexture (theTextureUnit);
150 glBindTexture (GetTarget(), NO_TEXTURE);