Structural deformable models
glImage.h
Go to the documentation of this file.
1 #ifndef _GLIMAGE_H_
2 #define _GLIMAGE_H_
3 
4 #include <GL/gl.h>
5 #include <vector>
6 #include "mathutil.h"
7 #include "Image.h"
8 
9 class GLImage {
10 public:
11 GLImage() : m_Initialized(false) {}
12 GLImage(const std::vector< Image<byte> >& image) : m_Initialized(false)
13  {
14  setImage(image);
15  }
16 
17 GLImage(const Image<byte>& image) : m_Initialized(false)
18  {
19  setImage(image);
20  }
21 
23  {
24  unsetImage();
25  }
26 
27  void setImage(const Image<byte>& image)
28  {
29  unsetImage();
30  m_sx = image.getSizeX();
31  m_sy = image.getSizeY();
32  dword p2x = power2(m_sx);
33  dword p2y = power2(m_sy);
34  m_tx = float(m_sx)/p2x;
35  m_ty = float(m_sy)/p2y;
36  Image<byte> p2i(p2x, p2y);
37  p2i.insert(image);
38  glGenTextures(1, &m_gltexture);// Create One Texture
39 // Create Linear Filtered Texture
40  glBindTexture(GL_TEXTURE_2D, m_gltexture);
41  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
42  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
43  glTexImage2D(GL_TEXTURE_2D, 0, 3, p2x, p2y, 0,
44  GL_LUMINANCE, GL_UNSIGNED_BYTE, p2i.getData());
45  m_Initialized = true;
46  }
47 
48  void setImage(const std::vector< Image<byte> >& image)
49  {
50  unsetImage();
51  if(image.empty()) return;
52  Image<byte> combimg;
53  combimg.interleave(image, 3);
54  m_sx = combimg.getSizeX()/3; //image[0].getSizeX();
55  m_sy = combimg.getSizeY(); //image[0].getSizeY();
56  dword p2x = power2(m_sx);
57  dword p2y = power2(m_sy);
58  m_tx = float(m_sx)/float(p2x);
59  m_ty = float(m_sy)/float(p2y);
60  Image<byte> p2i(p2x*3, p2y);
61  p2i.insert(combimg);
62  glGenTextures(1, &m_gltexture);// Create One Texture
63 // Create Linear Filtered Texture
64  glBindTexture(GL_TEXTURE_2D, m_gltexture);
65  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
66  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
67  glTexImage2D(GL_TEXTURE_2D, 0, 3, p2x, p2y, 0, GL_RGB,
68  GL_UNSIGNED_BYTE, p2i.getData());
69  m_Initialized = true;
70  }
71 
72  void unsetImage()
73  {
74  if(!m_Initialized) return;
75  glDeleteTextures(1, &m_gltexture);
76  m_Initialized = false;
77  }
78 
79  bool bindTexture() const
80  {
81  if(m_Initialized) glBindTexture(GL_TEXTURE_2D, m_gltexture);
82  return m_Initialized;
83  }
84 
85  void draw(float x, float y, float sx=1.f, float sy=1.f) const
86  {
87  if(!bindTexture()) return;
88  float bx = x + sx*m_sx;
89  float by = y + sy*m_sy;
90  glBegin(GL_QUADS);// Begin Drawing The Textured Quad
91  glTexCoord2f(0.0f, 0.0f); glVertex2f( x, y);
92  glTexCoord2f(m_tx, 0.0f); glVertex2f( bx, y);
93  glTexCoord2f(m_tx, m_ty); glVertex2f( bx, by);
94  glTexCoord2f(0.0f, m_ty); glVertex2f( x, by);
95  glEnd();
96  }
97 
98  operator bool() const { return m_Initialized; }
99  float getSizeX() const { return m_sx; }
100  float getSizeY() const { return m_sy; }
101 
102 protected:
103  GLuint m_gltexture;
104  int m_sx, m_sy;
105  float m_tx, m_ty;
107 };
108 
109 #endif
void setImage(const std::vector< Image< byte > > &image)
Definition: glImage.h:48
Definition: glImage.h:9
~GLImage()
Definition: glImage.h:22
Image< T > & interleave(const std::vector< Image< T > > &img, dword ncomp=0)
Definition: Image.h:150
void setImage(const Image< byte > &image)
Definition: glImage.h:27
void draw(float x, float y, float sx=1.f, float sy=1.f) const
Definition: glImage.h:85
int getSizeX() const
Definition: Image.h:42
void unsetImage()
Definition: glImage.h:72
GLImage()
Definition: glImage.h:11
GLImage(const Image< byte > &image)
Definition: glImage.h:17
bool bindTexture() const
Definition: glImage.h:79
float m_tx
Definition: glImage.h:105
int m_sy
Definition: glImage.h:104
float getSizeX() const
Definition: glImage.h:99
GLImage(const std::vector< Image< byte > > &image)
Definition: glImage.h:12
int m_sx
Definition: glImage.h:104
unsigned long dword
Definition: simpletypes.h:6
dword power2(dword x)
Definition: mathutil.h:117
bool m_Initialized
Definition: glImage.h:106
void insert(const Image< T > &ii, const int x=0, const int y=0)
Definition: Image.h:568
const T * getData() const
Definition: Image.h:39
float m_ty
Definition: glImage.h:105
GLuint m_gltexture
Definition: glImage.h:103
float getSizeY() const
Definition: glImage.h:100
int getSizeY() const
Definition: Image.h:43