NCBI C++ ToolKit
gltexture.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: gltexture.cpp 47392 2023-03-06 19:00:01Z evgeniev $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Mike DiCuccio
27  *
28  * File Description:
29  *
30  */
31 
32 
33 #include <ncbi_pch.hpp>
34 #include <gui/opengl/gltexture.hpp>
36 #include <util/image/image_io.hpp>
37 #include <gui/opengl.h>
38 
40 
41 
42 // default ctor
44  : m_TexId(0),
45  m_Target(GL_TEXTURE_2D),
46  m_WrapS(GL_CLAMP),
47  m_WrapT(GL_CLAMP),
48  m_FilterMin(GL_LINEAR),
49  m_FilterMag(GL_LINEAR),
50  m_TexEnv(GL_MODULATE)
51 {
52 }
53 
54 
55 //
56 // ctor: create an empty image
57 CGlTexture::CGlTexture(size_t w, size_t h, size_t depth)
58  : m_TexId(0),
59  m_Target(GL_TEXTURE_2D),
60  m_WrapS(GL_CLAMP),
61  m_WrapT(GL_CLAMP),
62  m_FilterMin(GL_LINEAR),
63  m_FilterMag(GL_LINEAR),
64  m_TexEnv(GL_MODULATE)
65 {
66  Init(w, h, depth);
67 }
68 
69 // ctor: create a texture with the texture id of a texture
70 // created elsewhere
71 CGlTexture::CGlTexture(GLuint tex_id, size_t w, size_t h, GLenum target)
72  : m_Width(w),
73  m_Height(h),
74  m_TexId(tex_id),
75  m_Target(target),
76  m_WrapS(GL_CLAMP),
77  m_WrapT(GL_CLAMP),
78  m_FilterMin(GL_LINEAR),
79  m_FilterMag(GL_LINEAR),
80  m_TexEnv(GL_MODULATE)
81 {
82  // only works if it's really a texture
83  if (!glIsTexture(tex_id))
84  m_TexId = 0;
85 }
86 
87 
88 //
89 // ctor: create an image around an existing image
91  : m_TexId(0),
92  m_Target(GL_TEXTURE_2D),
93  m_WrapS(GL_CLAMP),
94  m_WrapT(GL_CLAMP),
95  m_FilterMin(GL_LINEAR),
96  m_FilterMag(GL_LINEAR),
97  m_TexEnv(GL_MODULATE)
98 {
99  Swallow(image);
100 }
101 
102 
103 //
104 // get an image from a file
106  : m_ImageTag(tag),
107  m_TexId(0),
108  m_Target(GL_TEXTURE_2D),
109  m_WrapS(GL_CLAMP),
110  m_WrapT(GL_CLAMP),
111  m_FilterMin(GL_LINEAR),
112  m_FilterMag(GL_LINEAR),
113  m_TexEnv(GL_MODULATE)
114 {
116  if ( !m_Image ) {
117  string msg("CGlTexture(): cannot read image");
118  NCBI_THROW(COpenGLException, eTextureError, msg);
119  }
120 }
121 
122 
123 //
124 // initialize an empty image
125 //
126 void CGlTexture::Init(size_t w, size_t h, size_t d)
127 {
128  Clear();
129 
130  // initialize our image
131  m_Image.Reset(new CImage(w, h, d));
132 }
133 
134 //
135 // clear the current image
136 //
138 {
139  Unload();
140  m_Image.Reset();
141 }
142 
143 
144 //
145 // "swallow" an image
146 //
148 {
149  // clear the current image
150  Clear();
151 
152  // assign the wrapped image
153  m_Image.Reset(image);
154 
155  // load the texture
156  Load();
157 }
158 
159 
160 bool CGlTexture::IsValid(void) const
161 {
162  return glIsTexture(m_TexId) ? true : false;
163 }
164 
165 
166 //
167 // bind the image as a texture
168 //
170 {
171  // texture is still valid?
172  if (!IsValid()) {
173  Load();
174  }
175 
176  // Texture environment is not saved in the texture object, so we set it
177  // here
178  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, m_TexEnv);
179 
180  glEnable(m_Target);
181  glBindTexture(m_Target, m_TexId);
182 }
183 
184 //
185 // set (or reset) the opengl texture parameters that are saved in
186 // the texture object
187 //
189 {
190  if (!IsValid())
191  return;
192 
193  MakeCurrent();
194  glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
195  glTexParameteri(m_Target, GL_TEXTURE_WRAP_S, m_WrapS);
196  glTexParameteri(m_Target, GL_TEXTURE_WRAP_T, m_WrapT);
197  glTexParameteri(m_Target, GL_TEXTURE_MAG_FILTER, m_FilterMag);
198  glTexParameteri(m_Target, GL_TEXTURE_MIN_FILTER, m_FilterMin);
199 }
200 
201 
203 {
204  if (!glIsTexture(m_TexId) || m_Target != GL_TEXTURE_2D ||
205  m_Width == 0 || m_Height == 0)
206  return nullptr;
207 
208  CRef<CImage> img(new CImage(m_Width, m_Height, 3));
209 
210  MakeCurrent();
211  GLint alignment;
212  glGetIntegerv(GL_PACK_ALIGNMENT, &alignment);
213  glPixelStorei(GL_PACK_ALIGNMENT, 1);
214  glGetTexImage(m_Target, 0, GL_RGB, GL_UNSIGNED_BYTE, img->SetData());
215  glPixelStorei(GL_PACK_ALIGNMENT, alignment);
216 
217  return img.Release();
218 }
219 
220 
221 //
222 // create a representation of the texture for rendering
223 // this is either a display list or a texture object
224 //
226 {
227  //_TRACE("CGlTexture::Load()");
228  // unload the current texture
229  Unload();
230 
231  // safety first!
232  if ( !m_Image ) {
233  _TRACE(" no image to load!");
234  return;
235  }
236 
237  glGenTextures(1, &m_TexId);
238  glBindTexture(m_Target, m_TexId);
239 
240  // set texture params
241  SetParams();
242 
243  // call glTexImage2D
244  switch (m_Image->GetDepth()) {
245  case 1:
246  gluBuild2DMipmaps(m_Target, GL_ALPHA,
247  (GLint)m_Image->GetWidth(), (GLint)m_Image->GetHeight(),
248  GL_ALPHA, GL_UNSIGNED_BYTE,
249  m_Image->GetData());
250  break;
251 
252  case 3:
253  gluBuild2DMipmaps(m_Target, GL_RGB,
254  (GLint)m_Image->GetWidth(), (GLint)m_Image->GetHeight(),
255  GL_RGB, GL_UNSIGNED_BYTE,
256  m_Image->GetData());
257  break;
258 
259  case 4:
260  gluBuild2DMipmaps(m_Target, GL_RGBA,
261  (GLint)m_Image->GetWidth(), (GLint)m_Image->GetHeight(),
262  GL_RGBA, GL_UNSIGNED_BYTE,
263  m_Image->GetData());
264  break;
265 
266  default:
267  LOG_POST(Error << "CGlTexture::Load(): unhandled image depth");
268  break;
269  }
270  m_Width = m_Image->GetWidth();
272 }
273 
274 void CGlTexture::Load1DRGBA(size_t width, float* data)
275 {
276  Unload();
277 
278  m_Target = GL_TEXTURE_1D;
279  glGenTextures(1, &m_TexId);
280  glBindTexture(m_Target, m_TexId);
281  SetParams();
282  glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, static_cast<GLsizei>(width), 0, GL_RGBA, GL_FLOAT, (GLvoid*)data);
283  m_Width = width;
284  m_Height = 1;
285 }
286 
287 //
288 // unload the image as a texture
290 {
291 
292  if (glIsTexture (m_TexId))
293  glDeleteTextures (1, &m_TexId);
294 
295  m_TexId = 0;
296 
297  m_Width = 0;
298  m_Height = 0;
299 }
300 
static CImage * ReadImage(const string &file, EType type=CImageIO::eUnknown)
Definition: image_io.cpp:205
CImage –.
Definition: Image.hpp:66
size_t GetDepth(void) const
Definition: image.hpp:108
const unsigned char * GetData(void) const
Definition: image.cpp:85
size_t GetWidth(void) const
Definition: image.hpp:98
size_t GetHeight(void) const
Definition: image.hpp:99
unsigned char * SetData(void)
Definition: image.cpp:92
static unsigned char depth[2 *(256+1+29)+1]
#define true
Definition: bool.h:35
char data[12]
Definition: iconv.c:80
#define _TRACE(message)
Definition: ncbidbg.hpp:122
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
CRef< CImage > m_Image
Definition: gltexture.hpp:143
virtual void MakeCurrent()
Definition: gltexture.cpp:169
virtual CImage * GenerateImage()
Definition: gltexture.cpp:202
virtual void SetParams()
set the gl texture parameters (filtering, etc) for texture
Definition: gltexture.cpp:188
virtual bool IsValid(void) const
Definition: gltexture.cpp:160
void Load1DRGBA(size_t width, float *data)
Definition: gltexture.cpp:274
GLenum m_WrapT
Definition: gltexture.hpp:155
size_t m_Width
Definition: gltexture.hpp:136
GLint m_TexEnv
Definition: gltexture.hpp:162
GLenum m_FilterMin
Definition: gltexture.hpp:158
GLenum m_WrapS
Definition: gltexture.hpp:154
virtual void Unload()
Definition: gltexture.cpp:289
size_t m_Height
Definition: gltexture.hpp:137
virtual void Load()
Definition: gltexture.cpp:225
void Clear()
Definition: gltexture.cpp:137
GLenum m_FilterMag
Definition: gltexture.hpp:159
GLuint m_TexId
Definition: gltexture.hpp:148
GLenum m_Target
Definition: gltexture.hpp:151
void Swallow(CImage *image)
Definition: gltexture.cpp:147
void Init(size_t w, size_t h, size_t depth)
Definition: gltexture.cpp:126
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
TObjectType * Release(void)
Release a reference to the object and return a pointer to the object.
Definition: ncbiobj.hpp:846
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
const char * tag
Standard mechanism to include OpenGL headers for all platforms.
static SLJIT_INLINE sljit_ins msg(sljit_gpr r, sljit_s32 d, sljit_gpr x, sljit_gpr b)
Modified on Fri Sep 20 14:57:33 2024 by modify_doxy.py rev. 669887