| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "texture2d.h" |
| 7 |
|
| 8 |
#include "avogadrogl.h" |
| 9 |
|
| 10 |
namespace Avogadro::Rendering { |
| 11 |
|
| 12 |
namespace { |
| 13 |
GLint convertFilterOptionToGL(Texture2D::FilterOption opt) |
| 14 |
{ |
| 15 |
switch (opt) { |
| 16 |
case Texture2D::Nearest: |
| 17 |
return GL_NEAREST; |
| 18 |
case Texture2D::Linear: |
| 19 |
return GL_LINEAR; |
| 20 |
default: |
| 21 |
return -1; |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
Texture2D::FilterOption convertFilterOptionFromGL(GLint opt) |
| 26 |
{ |
| 27 |
switch (opt) { |
| 28 |
case GL_NEAREST: |
| 29 |
return Texture2D::Nearest; |
| 30 |
case GL_LINEAR: |
| 31 |
return Texture2D::Linear; |
| 32 |
default: |
| 33 |
return Texture2D::InvalidFilter; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
GLint convertWrappingOptionToGL(Texture2D::WrappingOption opt) |
| 38 |
{ |
| 39 |
switch (opt) { |
| 40 |
case Texture2D::ClampToEdge: |
| 41 |
return GL_CLAMP_TO_EDGE; |
| 42 |
case Texture2D::MirroredRepeat: |
| 43 |
return GL_MIRRORED_REPEAT; |
| 44 |
case Texture2D::Repeat: |
| 45 |
return GL_REPEAT; |
| 46 |
default: |
| 47 |
return -1; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
Texture2D::WrappingOption convertWrappingOptionFromGL(GLint opt) |
| 52 |
{ |
| 53 |
switch (opt) { |
| 54 |
case GL_CLAMP_TO_EDGE: |
| 55 |
return Texture2D::ClampToEdge; |
| 56 |
case GL_MIRRORED_REPEAT: |
| 57 |
return Texture2D::MirroredRepeat; |
| 58 |
case GL_REPEAT: |
| 59 |
return Texture2D::Repeat; |
| 60 |
default: |
| 61 |
return Texture2D::InvalidWrapping; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
GLint convertInternalFormatToGL(Texture2D::InternalFormat fmt) |
| 66 |
{ |
| 67 |
switch (fmt) { |
| 68 |
case Texture2D::InternalDepth: |
| 69 |
return GL_DEPTH_COMPONENT; |
| 70 |
case Texture2D::InternalDepthStencil: |
| 71 |
return GL_DEPTH_STENCIL; |
| 72 |
case Texture2D::InternalR: |
| 73 |
return GL_RED; |
| 74 |
case Texture2D::InternalRG: |
| 75 |
return GL_RG; |
| 76 |
case Texture2D::InternalRGB: |
| 77 |
return GL_RGB; |
| 78 |
case Texture2D::InternalRGBA: |
| 79 |
return GL_RGBA; |
| 80 |
default: |
| 81 |
return -1; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
GLint convertIncomingFormatToGL(Texture2D::IncomingFormat fmt) |
| 86 |
{ |
| 87 |
switch (fmt) { |
| 88 |
case Texture2D::IncomingR: |
| 89 |
return GL_RED; |
| 90 |
case Texture2D::IncomingRG: |
| 91 |
return GL_RG; |
| 92 |
case Texture2D::IncomingRGB: |
| 93 |
return GL_RGB; |
| 94 |
case Texture2D::IncomingBGR: |
| 95 |
return GL_BGR; |
| 96 |
case Texture2D::IncomingRGBA: |
| 97 |
return GL_RGBA; |
| 98 |
case Texture2D::IncomingBGRA: |
| 99 |
return GL_BGRA; |
| 100 |
case Texture2D::IncomingDepth: |
| 101 |
return GL_DEPTH_COMPONENT; |
| 102 |
case Texture2D::IncomingDepthStencil: |
| 103 |
return GL_DEPTH_STENCIL; |
| 104 |
default: |
| 105 |
return -1; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
GLenum convertTypeToGL(Type type) |
| 110 |
{ |
| 111 |
switch (type) { |
| 112 |
case CharType: |
| 113 |
return GL_BYTE; |
| 114 |
case UCharType: |
| 115 |
return GL_UNSIGNED_BYTE; |
| 116 |
case ShortType: |
| 117 |
return GL_SHORT; |
| 118 |
case UShortType: |
| 119 |
return GL_UNSIGNED_SHORT; |
| 120 |
case IntType: |
| 121 |
return GL_INT; |
| 122 |
case UIntType: |
| 123 |
return GL_UNSIGNED_INT; |
| 124 |
case FloatType: |
| 125 |
return GL_FLOAT; |
| 126 |
case DoubleType: |
| 127 |
return GL_DOUBLE; |
| 128 |
default: |
| 129 |
return 0; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
} // namespace |
| 134 |
|
| 135 |
class Texture2D::Private |
| 136 |
{ |
| 137 |
public: |
| 138 |
Private() : textureId(0) {} |
| 139 |
~Private() |
| 140 |
{ |
| 141 |
if (textureId > 0) |
| 142 |
glDeleteTextures(1, &textureId); |
| 143 |
} |
| 144 |
|
| 145 |
mutable GLuint textureId; |
| 146 |
}; |
| 147 |
|
| 148 |
Texture2D::Texture2D() : d(new Private) {} |
| 149 |
|
| 150 |
Texture2D::~Texture2D() |
| 151 |
{ |
| 152 |
delete d; |
| 153 |
} |
| 154 |
|
| 155 |
Index Texture2D::handle() const |
| 156 |
{ |
| 157 |
return static_cast<Index>(d->textureId); |
| 158 |
} |
| 159 |
|
| 160 |
void Texture2D::setMinFilter(Texture2D::FilterOption opt) |
| 161 |
{ |
| 162 |
Index old = pushTexture(); |
| 163 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 164 |
convertFilterOptionToGL(opt)); |
| 165 |
popTexture(old); |
| 166 |
} |
| 167 |
|
| 168 |
Texture2D::FilterOption Texture2D::minFilter() const |
| 169 |
{ |
| 170 |
Index old = pushTexture(); |
| 171 |
GLint result; |
| 172 |
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, &result); |
| 173 |
popTexture(old); |
| 174 |
|
| 175 |
return convertFilterOptionFromGL(result); |
| 176 |
} |
| 177 |
|
| 178 |
void Texture2D::setMagFilter(Texture2D::FilterOption opt) |
| 179 |
{ |
| 180 |
Index old = pushTexture(); |
| 181 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, |
| 182 |
convertFilterOptionToGL(opt)); |
| 183 |
popTexture(old); |
| 184 |
} |
| 185 |
|
| 186 |
Texture2D::FilterOption Texture2D::magFilter() const |
| 187 |
{ |
| 188 |
Index old = pushTexture(); |
| 189 |
GLint result; |
| 190 |
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, &result); |
| 191 |
popTexture(old); |
| 192 |
|
| 193 |
return convertFilterOptionFromGL(result); |
| 194 |
} |
| 195 |
|
| 196 |
void Texture2D::setWrappingS(Texture2D::WrappingOption opt) |
| 197 |
{ |
| 198 |
Index old = pushTexture(); |
| 199 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, |
| 200 |
convertWrappingOptionToGL(opt)); |
| 201 |
popTexture(old); |
| 202 |
} |
| 203 |
|
| 204 |
Texture2D::WrappingOption Texture2D::wrappingS() const |
| 205 |
{ |
| 206 |
Index old = pushTexture(); |
| 207 |
GLint result; |
| 208 |
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, &result); |
| 209 |
popTexture(old); |
| 210 |
|
| 211 |
return convertWrappingOptionFromGL(result); |
| 212 |
} |
| 213 |
|
| 214 |
void Texture2D::setWrappingT(Texture2D::WrappingOption opt) |
| 215 |
{ |
| 216 |
Index old = pushTexture(); |
| 217 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, |
| 218 |
convertWrappingOptionToGL(opt)); |
| 219 |
popTexture(old); |
| 220 |
} |
| 221 |
|
| 222 |
Texture2D::WrappingOption Texture2D::wrappingT() const |
| 223 |
{ |
| 224 |
Index old = pushTexture(); |
| 225 |
GLint result; |
| 226 |
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, &result); |
| 227 |
popTexture(old); |
| 228 |
|
| 229 |
return convertWrappingOptionFromGL(result); |
| 230 |
} |
| 231 |
|
| 232 |
bool Texture2D::bind() const |
| 233 |
{ |
| 234 |
return pushTexture() != MaxIndex; |
| 235 |
} |
| 236 |
|
| 237 |
bool Texture2D::release() const |
| 238 |
{ |
| 239 |
popTexture(0); |
| 240 |
return true; |
| 241 |
} |
| 242 |
|
| 243 |
bool Texture2D::uploadInternal(const void* buffer, const Vector2i& dims, |
| 244 |
Texture2D::IncomingFormat dataFormat, |
| 245 |
Avogadro::Type dataType, |
| 246 |
Texture2D::InternalFormat internalFormat) |
| 247 |
{ |
| 248 |
// The dataType has already been validated. |
| 249 |
Index old = pushTexture(); |
| 250 |
glTexImage2D(GL_TEXTURE_2D, 0, convertInternalFormatToGL(internalFormat), |
| 251 |
dims[0], dims[1], 0, convertIncomingFormatToGL(dataFormat), |
| 252 |
convertTypeToGL(dataType), |
| 253 |
static_cast<GLvoid*>(const_cast<void*>(buffer))); |
| 254 |
popTexture(old); |
| 255 |
return true; |
| 256 |
} |
| 257 |
|
| 258 |
Index Texture2D::pushTexture() const |
| 259 |
{ |
| 260 |
GLint currentHandle; |
| 261 |
glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤tHandle); |
| 262 |
|
| 263 |
if (d->textureId == 0) { |
| 264 |
if (!const_cast<Texture2D*>(this)->generateTextureHandle()) |
| 265 |
return MaxIndex; |
| 266 |
} |
| 267 |
|
| 268 |
glBindTexture(GL_TEXTURE_2D, d->textureId); |
| 269 |
|
| 270 |
return static_cast<Index>(currentHandle); |
| 271 |
} |
| 272 |
|
| 273 |
void Texture2D::popTexture(Index id) const |
| 274 |
{ |
| 275 |
glBindTexture(GL_TEXTURE_2D, static_cast<GLuint>(id)); |
| 276 |
} |
| 277 |
|
| 278 |
bool Texture2D::generateTextureHandle() |
| 279 |
{ |
| 280 |
if (d->textureId > 0) { |
| 281 |
m_error = "Refusing to overwrite existing texture handle."; |
| 282 |
return false; |
| 283 |
} |
| 284 |
|
| 285 |
glGenTextures(1, &d->textureId); |
| 286 |
|
| 287 |
if (d->textureId == 0) { |
| 288 |
m_error = "Error generating texture handle."; |
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
// Set up defaults to match the documentation: |
| 293 |
setMinFilter(Linear); |
| 294 |
setMagFilter(Linear); |
| 295 |
setWrappingS(Repeat); |
| 296 |
setWrappingT(Repeat); |
| 297 |
|
| 298 |
return true; |
| 299 |
} |
| 300 |
|
| 301 |
} // namespace Avogadro::Rendering |