GlTexture.cpp
Go to the documentation of this file.
1 
36 #include <iostream>
37 
38 using namespace std;
39 
40 namespace lvr2
41 {
42 
43 GlTexture::GlTexture(unsigned char* pixels, int width, int height)
44  : m_width(width), m_height(height), m_pixels(pixels)
45 {
46  m_texIndex = 0;
47  // Upload texture
48  upload();
49 }
50 
52 {
53  // Copy data
54  m_width = other.m_width;
55  m_height = other.m_height;
56  m_pixels = new unsigned char[3 * m_width * m_height];
57  m_texIndex = other.m_texIndex;
58  for(int i = 0; i < 3 * m_width * m_height; i++)
59  {
60  m_pixels[i] = other.m_pixels[i];
61  }
62 
63  // Upload texture
64  upload();
65 }
66 
68 {
69  m_texIndex = 0;
70  m_width = other.m_width;
71  m_height = other.m_height;
72  m_pixels = new unsigned char[3 * m_width * m_height];
73  std::fill(m_pixels, m_pixels + 3 * m_width * m_height, 0);
74 
75  for (size_t i = 0; i < m_width * m_height; i++)
76  {
77  size_t current_idx = i * other.m_numChannels * other.m_numBytesPerChan;
78  if (other.m_numChannels > 0)
79  {
80  m_pixels[i*3 + 0] = other.m_data[current_idx + other.m_numBytesPerChan * 0];
81  }
82  if (other.m_numChannels > 1)
83  {
84  m_pixels[i*3 + 1] = other.m_data[current_idx + other.m_numBytesPerChan * 1];
85  }
86  if (other.m_numChannels > 2)
87  {
88  m_pixels[i*3 + 2] = other.m_data[current_idx + other.m_numBytesPerChan * 2];
89  }
90  }
91 
92  // Upload texture
93  upload();
94 }
95 
97 {
98  if(m_pixels)
99  {
100  delete[] m_pixels;
101  }
102 
103  if (m_texIndex)
104  {
105  glDeleteTextures(1, &m_texIndex);
106  }
107 }
108 
110 {
111  glEnable(GL_TEXTURE_2D);
112  // Create new texure list
113  glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Set correct alignment
114  glGenTextures(1, &m_texIndex);
115 
116  /*
117  for(int i = 0; i < 3 * m_width * m_height; i++)
118  {
119  if(i % 3 == 0)
120  {
121  if((int)m_pixels[i] != 0 && (int)m_pixels[i] != 255)
122  cout << endl;
123  }
124  if((int)m_pixels[i] != 0 && (int)m_pixels[i] != 255)
125  cout << "GlTexture::upload -- " << (int)m_pixels[i] << " ";
126  }
127  */
128 
129  // Bind texture, setup parameters and upload it
130  // to video memory
131  glBindTexture(GL_TEXTURE_2D, m_texIndex); // Bind texture
132 
133  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set repeating and filtering
134  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
135  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
136  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
137  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // Do not apply lighting
138 
139  // Upload texture
140  glTexImage2D(
141  GL_TEXTURE_2D,
142  0,
143  GL_RGB,
144  m_width,
145  m_height,
146  0,
147  GL_RGB,
148  GL_UNSIGNED_BYTE,
149  m_pixels
150  );
151 }
152 
153 } // namespace lvr2
lvr2::Texture::m_data
unsigned char * m_data
The texture data.
Definition: Texture.hpp:114
lvr2::GlTexture::upload
void upload()
Does all the OpenGL stuff to make it avialable for rendering.
Definition: GlTexture.cpp:109
lvr2::GlTexture
Definition: GlTexture.hpp:56
lvr2::GlTexture::m_texIndex
GLuint m_texIndex
The texture index of the texture.
Definition: GlTexture.hpp:112
lvr2::GlTexture::m_height
int m_height
The height of the texture in pixels.
Definition: GlTexture.hpp:106
lvr2::Texture::m_numChannels
unsigned char m_numChannels
The number of color channels.
Definition: Texture.hpp:117
lvr2::GlTexture::~GlTexture
virtual ~GlTexture()
Dtor.
Definition: GlTexture.cpp:96
lvr2::GlTexture::GlTexture
GlTexture()
Empty ctor.
Definition: GlTexture.hpp:84
lvr2::Texture::m_height
unsigned short int m_height
Definition: Texture.hpp:111
lvr2::GlTexture::m_pixels
unsigned char * m_pixels
The aligned pixel data. Three bytes per pixel (r,g,b)
Definition: GlTexture.hpp:109
std
Definition: HalfEdge.hpp:124
lvr2
Definition: BaseBufferManipulators.hpp:39
lvr2::Texture
This class represents a texture.
Definition: Texture.hpp:54
lvr2::Texture::m_width
unsigned short int m_width
The dimensions of the texture.
Definition: Texture.hpp:111
GlTexture.hpp
lvr2::GlTexture::m_width
int m_width
The width of the texture in pixels.
Definition: GlTexture.hpp:103
lvr2::Texture::m_numBytesPerChan
unsigned char m_numBytesPerChan
The number of bytes per channel.
Definition: Texture.hpp:120


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:23