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>
20 IMPLEMENT_STANDARD_HANDLE (OpenGl_TextureBufferArb, OpenGl_VertexBuffer)
21 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_TextureBufferArb, OpenGl_VertexBuffer)
23 // =======================================================================
24 // function : OpenGl_TextureBufferArb
26 // =======================================================================
27 OpenGl_TextureBufferArb::OpenGl_TextureBufferArb()
28 : OpenGl_VertexBuffer(),
29 myTextureId (NO_TEXTURE),
30 myTexFormat (GL_RGBA32F)
35 // =======================================================================
36 // function : ~OpenGl_TextureBufferArb
38 // =======================================================================
39 OpenGl_TextureBufferArb::~OpenGl_TextureBufferArb()
44 // =======================================================================
45 // function : GetTarget
47 // =======================================================================
48 GLenum OpenGl_TextureBufferArb::GetTarget() const
50 return GL_TEXTURE_BUFFER_ARB; // GL_TEXTURE_BUFFER for OpenGL 3.1+
53 // =======================================================================
56 // =======================================================================
57 void OpenGl_TextureBufferArb::Release (const OpenGl_Context* theGlCtx)
59 if (myTextureId != NO_TEXTURE)
61 // application can not handle this case by exception - this is bug in code
62 Standard_ASSERT_RETURN (theGlCtx != NULL,
63 "OpenGl_TextureBufferExt destroyed without GL context! Possible GPU memory leakage...",);
65 if (theGlCtx->IsValid())
67 glDeleteTextures (1, &myTextureId);
69 myTextureId = NO_TEXTURE;
71 OpenGl_VertexBuffer::Release (theGlCtx);
74 // =======================================================================
77 // =======================================================================
78 bool OpenGl_TextureBufferArb::Create (const Handle(OpenGl_Context)& theGlCtx)
80 if (!OpenGl_VertexBuffer::Create (theGlCtx))
85 if (myTextureId == NO_TEXTURE)
87 glGenTextures (1, &myTextureId);
89 return myTextureId != NO_TEXTURE;
92 // =======================================================================
95 // =======================================================================
96 bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx,
97 const GLuint theComponentsNb,
98 const GLsizei theElemsNb,
99 const GLfloat* theData)
101 if (theComponentsNb != 1
102 && theComponentsNb != 2
103 && theComponentsNb != 4)
105 // unsupported format
108 else if (!Create (theGlCtx)
109 || !OpenGl_VertexBuffer::Init (theGlCtx, theComponentsNb, theElemsNb, theData))
114 switch (theComponentsNb)
116 case 1: myTexFormat = GL_R32F; break;
117 case 2: myTexFormat = GL_RG32F; break;
118 //case 3: myTexFormat = GL_RGB32F; break; // GL_ARB_texture_buffer_object_rgb32
119 case 4: myTexFormat = GL_RGBA32F; break;
123 BindTexture (theGlCtx);
124 theGlCtx->arbTBO->glTexBuffer (GetTarget(), myTexFormat, myBufferId);
125 UnbindTexture (theGlCtx);
130 // =======================================================================
131 // function : BindTexture
133 // =======================================================================
134 void OpenGl_TextureBufferArb::BindTexture (const Handle(OpenGl_Context)& theGlCtx,
135 const GLenum theTextureUnit) const
137 theGlCtx->core20->glActiveTexture (theTextureUnit);
138 glBindTexture (GetTarget(), myTextureId);
141 // =======================================================================
142 // function : UnbindTexture
144 // =======================================================================
145 void OpenGl_TextureBufferArb::UnbindTexture (const Handle(OpenGl_Context)& theGlCtx,
146 const GLenum theTextureUnit) const
148 theGlCtx->core20->glActiveTexture (theTextureUnit);
149 glBindTexture (GetTarget(), NO_TEXTURE);