0024228: TKOpenGL - destroy GL context at view close
[occt.git] / src / OpenGl / OpenGl_TextureBufferArb.cxx
CommitLineData
5e27df78 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
24IMPLEMENT_STANDARD_HANDLE (OpenGl_TextureBufferArb, OpenGl_VertexBuffer)
25IMPLEMENT_STANDARD_RTTIEXT(OpenGl_TextureBufferArb, OpenGl_VertexBuffer)
26
27// =======================================================================
28// function : OpenGl_TextureBufferArb
29// purpose :
30// =======================================================================
31OpenGl_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// =======================================================================
43OpenGl_TextureBufferArb::~OpenGl_TextureBufferArb()
44{
45 Release (NULL);
46}
47
48// =======================================================================
49// function : GetTarget
50// purpose :
51// =======================================================================
52GLenum 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// =======================================================================
61void 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
fd4a6963 69 if (theGlCtx->IsValid())
70 {
71 glDeleteTextures (1, &myTextureId);
72 }
5e27df78 73 myTextureId = NO_TEXTURE;
74 }
75 OpenGl_VertexBuffer::Release (theGlCtx);
76}
77
78// =======================================================================
79// function : Create
80// purpose :
81// =======================================================================
82bool OpenGl_TextureBufferArb::Create (const Handle(OpenGl_Context)& theGlCtx)
83{
84 if (!OpenGl_VertexBuffer::Create (theGlCtx))
85 {
86 return false;
87 }
88
89 if (myTextureId == NO_TEXTURE)
90 {
91 glGenTextures (1, &myTextureId);
92 }
93 return myTextureId != NO_TEXTURE;
94}
95
96// =======================================================================
97// function : Init
98// purpose :
99// =======================================================================
100bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx,
101 const GLuint theComponentsNb,
102 const GLsizei theElemsNb,
103 const GLfloat* theData)
104{
105 if (theComponentsNb != 1
106 && theComponentsNb != 2
107 && theComponentsNb != 4)
108 {
109 // unsupported format
110 return false;
111 }
112 else if (!Create (theGlCtx)
113 || !OpenGl_VertexBuffer::Init (theGlCtx, theComponentsNb, theElemsNb, theData))
114 {
115 return false;
116 }
117
118 switch (theComponentsNb)
119 {
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;
124 }
125
126 Bind (theGlCtx);
127 BindTexture (theGlCtx);
128 theGlCtx->arbTBO->glTexBufferARB (GetTarget(), myTexFormat, myBufferId);
129 UnbindTexture (theGlCtx);
130 Unbind (theGlCtx);
131 return true;
132}
133
134// =======================================================================
135// function : BindTexture
136// purpose :
137// =======================================================================
138void OpenGl_TextureBufferArb::BindTexture (const Handle(OpenGl_Context)& theGlCtx,
139 const GLenum theTextureUnit) const
140{
141 theGlCtx->core20->glActiveTexture (theTextureUnit);
142 glBindTexture (GetTarget(), myTextureId);
143}
144
145// =======================================================================
146// function : UnbindTexture
147// purpose :
148// =======================================================================
149void OpenGl_TextureBufferArb::UnbindTexture (const Handle(OpenGl_Context)& theGlCtx,
150 const GLenum theTextureUnit) const
151{
152 theGlCtx->core20->glActiveTexture (theTextureUnit);
153 glBindTexture (GetTarget(), NO_TEXTURE);
154}