tcElevationOptimization::loadSnapShot: simplify reliability expression
[tecorrec.git] / geo / tcPixelData.cpp
blob25be0d4ee29ec83edfd8d268b4d3c308ff237823
1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * Tecorrec is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * Tecorrec is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with Tecorrec. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file tcPixelData.cpp
22 * @brief A block of pixel data.
25 #include "tcPixelData.h"
27 #include <GL/glu.h>
29 #include <inttypes.h>
32 * Abstract Pixel Data Constructors + destructor
35 /// Primary constructor.
36 tcAbstractPixelData::tcAbstractPixelData(int width, int height)
37 : m_textureId(0)
39 Q_ASSERT(width > 0);
40 Q_ASSERT(height > 0);
41 m_size[0] = width;
42 m_size[1] = height;
45 /// Destructor.
46 tcAbstractPixelData::~tcAbstractPixelData()
48 discardTexture();
52 * Accessors
55 /// Get the width of the image.
56 int tcAbstractPixelData::width() const
58 return m_size[0];
61 /// Get the height of the image.
62 int tcAbstractPixelData::height() const
64 return m_size[1];
67 /// Get (creating if necessary) a GL texture for this image.
68 GLuint tcAbstractPixelData::texture()
70 if (0 == m_textureId)
72 m_textureId = loadTexture();
74 return m_textureId;
77 /// Discard of any cached texture id for this image.
78 void tcAbstractPixelData::discardTexture()
80 glDeleteTextures(1, &m_textureId);
84 * Virtual interface for derived classes to implement.
87 /// Load the pixel data into a GL texture.
88 GLuint tcAbstractPixelData::loadTexture()
90 return 0;
94 * Pixel data texture creation
97 template <>
98 GLuint tcPixelData<GLubyte>::loadTexture()
100 GLuint textureId = 0;
101 if (0 != buffer())
103 glGenTextures(1, &textureId);
104 GLint binding = 0;
105 glGetIntegerv(GL_TEXTURE_BINDING_2D, &binding);
106 glBindTexture(GL_TEXTURE_2D, textureId);
107 if (0 == gluBuild2DMipmaps(GL_TEXTURE_2D, GL_LUMINANCE, m_size[0], m_size[1],
108 GL_LUMINANCE, GL_UNSIGNED_BYTE, buffer()))
110 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
111 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
112 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
113 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
115 else
117 glDeleteTextures(1, &textureId);
119 glBindTexture(GL_TEXTURE_2D, binding);
121 return textureId;
124 template <>
125 GLuint tcPixelData<GLfloat>::loadTexture()
127 GLuint textureId = 0;
128 if (0 != buffer())
130 glGenTextures(1, &textureId);
131 GLint binding = 0;
132 glGetIntegerv(GL_TEXTURE_BINDING_2D, &binding);
133 glBindTexture(GL_TEXTURE_2D, textureId);
134 if (0 == gluBuild2DMipmaps(GL_TEXTURE_2D, GL_LUMINANCE, m_size[0], m_size[1],
135 GL_LUMINANCE, GL_FLOAT, buffer()))
137 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
138 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
139 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
140 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
142 else
144 glDeleteTextures(1, &textureId);
146 glBindTexture(GL_TEXTURE_2D, binding);
148 return textureId;