| 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 |
#ifndef AVOGADRO_RENDERING_TEXTURE2D_H |
| 7 |
#define AVOGADRO_RENDERING_TEXTURE2D_H |
| 8 |
|
| 9 |
#include "avogadrorenderingexport.h" |
| 10 |
|
| 11 |
#include <avogadro/core/avogadrocore.h> |
| 12 |
#include <avogadro/core/types.h> |
| 13 |
#include <avogadro/core/vector.h> |
| 14 |
|
| 15 |
namespace Avogadro { |
| 16 |
namespace Rendering { |
| 17 |
|
| 18 |
/** |
| 19 |
* @class Texture2D texture2d.h <avogadro/rendering/texture2d.h> |
| 20 |
* @brief The Texture2D class manages texture resources in graphics libraries. |
| 21 |
*/ |
| 22 |
class AVOGADRORENDERING_EXPORT Texture2D |
| 23 |
{ |
| 24 |
public: |
| 25 |
/** |
| 26 |
* @brief The FilterOption enum defines options for interpolating |
| 27 |
* texels onto pixels. |
| 28 |
*/ |
| 29 |
enum FilterOption |
| 30 |
{ |
| 31 |
InvalidFilter = -1, |
| 32 |
/// Use the nearest texel for the pixel value. |
| 33 |
Nearest = 0, |
| 34 |
/// Interpolate the four neighboring texels for the pixel value. |
| 35 |
Linear |
| 36 |
}; |
| 37 |
|
| 38 |
/** |
| 39 |
* @brief The WrappingOption enum defines options for handling texture |
| 40 |
* coordinates outside of the range [0, 1]. Note that these are specified |
| 41 |
* separately for each dimension of the texture. |
| 42 |
*/ |
| 43 |
enum WrappingOption |
| 44 |
{ |
| 45 |
InvalidWrapping = -1, |
| 46 |
/// Use the texel at the nearest edge. |
| 47 |
ClampToEdge = 0, |
| 48 |
/// Repeat the texels such that texture coordinates in the range [1, 2] map |
| 49 |
/// to [1, 0]; [2, 3] map to [0, 1], and so on. |
| 50 |
MirroredRepeat, |
| 51 |
/// Repeat the texels such that integer components of the texture |
| 52 |
/// coordinates are ignored (e.g. 1.7 becomes 0.7, 2.3 becomes 0.3, etc). |
| 53 |
Repeat |
| 54 |
}; |
| 55 |
|
| 56 |
/** |
| 57 |
* @brief The InternalFormat enum defines how the texture data will be stored |
| 58 |
* by the graphics library implementation. |
| 59 |
*/ |
| 60 |
enum InternalFormat |
| 61 |
{ |
| 62 |
InvalidInternalFormat = -1, |
| 63 |
/// Each element is a single depth component. |
| 64 |
InternalDepth = 0, |
| 65 |
/// Each element contains a depth component and a stencil value. |
| 66 |
InternalDepthStencil, |
| 67 |
/// Each element contains a single color value. |
| 68 |
InternalR, |
| 69 |
/// Each element contains two color values. |
| 70 |
InternalRG, |
| 71 |
/// Each element contains three color values. |
| 72 |
InternalRGB, |
| 73 |
/// Each element contains four color values. |
| 74 |
InternalRGBA |
| 75 |
}; |
| 76 |
|
| 77 |
/** |
| 78 |
* @brief The IncomingFormat enum defines the supported formats for incoming |
| 79 |
* texture data. |
| 80 |
*/ |
| 81 |
enum IncomingFormat |
| 82 |
{ |
| 83 |
InvalidIncomingFormat = -1, |
| 84 |
/// Each element is a single red component. |
| 85 |
IncomingR = 0, |
| 86 |
/// Each element is a red then green component. |
| 87 |
IncomingRG, |
| 88 |
/// Each element is a red, green, then blue component. |
| 89 |
IncomingRGB, |
| 90 |
/// Each element is a blue, green, then red component. |
| 91 |
IncomingBGR, |
| 92 |
/// Each element is a red, green, blue, then alpha component. |
| 93 |
IncomingRGBA, |
| 94 |
/// Each element is a blue, green, red, then alpha component. |
| 95 |
IncomingBGRA, |
| 96 |
/// Each element is a single depth component. |
| 97 |
IncomingDepth, |
| 98 |
/// Each element is a depth component followed by a stencil value. |
| 99 |
IncomingDepthStencil |
| 100 |
}; |
| 101 |
|
| 102 |
Texture2D(); |
| 103 |
~Texture2D(); |
| 104 |
|
| 105 |
/** A unique integer value identifying the texture. */ |
| 106 |
Index handle() const; |
| 107 |
|
| 108 |
/** True if the texture is ready to use. */ |
| 109 |
bool ready() const { return !m_dirty; } |
| 110 |
|
| 111 |
/** Filtering options for interpolating pixel values. Default is Linear. {@ */ |
| 112 |
void setMinFilter(FilterOption opt); |
| 113 |
FilterOption minFilter() const; |
| 114 |
void setMagFilter(FilterOption opt); |
| 115 |
FilterOption magFilter() const; |
| 116 |
/** @} */ |
| 117 |
|
| 118 |
/** Wrapping options in the s-direction. Default is Repeat. @{ */ |
| 119 |
void setWrappingS(WrappingOption opt); |
| 120 |
WrappingOption wrappingS() const; |
| 121 |
/** @} */ |
| 122 |
|
| 123 |
/** Wrapping options in the t-direction. Default is Repeat. @{ */ |
| 124 |
void setWrappingT(WrappingOption opt); |
| 125 |
WrappingOption wrappingT() const; |
| 126 |
/** @} */ |
| 127 |
|
| 128 |
/** |
| 129 |
* Upload a buffer of texture data to the graphics library. |
| 130 |
* |
| 131 |
* @param buffer The texture data (see below for requirements of ContainerT). |
| 132 |
* @param dims The width and height of the texture data. |
| 133 |
* @param dataFormat The ordering of components in the buffer data. |
| 134 |
* @param internalFormat The internal storage ordering of components in the |
| 135 |
* buffer data. |
| 136 |
* |
| 137 |
* The buffer must contain homogeneous elements of the follow types: unsigned |
| 138 |
* char, char, unsigned short, short, unsigned int, int, or float. |
| 139 |
* |
| 140 |
* The first element of @a buffer is the lower-left texel. Subsequent elements |
| 141 |
* are ordered such that rows are contiguous, moving right and up. |
| 142 |
* |
| 143 |
* The ContainerT must satisfy the following requirements (short version: |
| 144 |
* use std::vector or Avogadro::Core::Array): |
| 145 |
* - ContainerT must have tightly packed values of ContainerT::value_type |
| 146 |
* - elements must be accessible by reference via ContainerT::operator[]. |
| 147 |
* - ContainerT::size() must return the number of elements in the container |
| 148 |
* as integral type ContainerT::size_type. |
| 149 |
*/ |
| 150 |
template <class ContainerT> |
| 151 |
bool upload(const ContainerT& buffer, const Vector2i& dims, |
| 152 |
IncomingFormat dataFormat, InternalFormat internalFormat); |
| 153 |
|
| 154 |
/** Bind the texture for rendering. */ |
| 155 |
bool bind() const; |
| 156 |
|
| 157 |
bool release() const; |
| 158 |
|
| 159 |
std::string error() const { return m_error; } |
| 160 |
|
| 161 |
private: |
| 162 |
bool uploadInternal(const void* buffer, const Vector2i& dims, |
| 163 |
IncomingFormat dataFormat, Avogadro::Type dataType, |
| 164 |
InternalFormat internalFormat); |
| 165 |
|
| 166 |
// Used to preserve GL state when setting/querying texture properties: |
| 167 |
Index pushTexture() const; // -1 indicates error |
| 168 |
void popTexture(Index) const; |
| 169 |
|
| 170 |
// Create a texture handle and set the defaults. |
| 171 |
bool generateTextureHandle(); |
| 172 |
|
| 173 |
class Private; |
| 174 |
Private* const d; |
| 175 |
bool m_dirty; |
| 176 |
mutable std::string m_error; |
| 177 |
}; |
| 178 |
|
| 179 |
template <class ContainerT> |
| 180 |
inline bool Texture2D::upload(const ContainerT& buffer, const Vector2i& dims, |
| 181 |
IncomingFormat incomingFormat, |
| 182 |
InternalFormat internalFormat) |
| 183 |
{ |
| 184 |
if (buffer.size() == 0 || dims[0] == 0 || dims[1] == 0) { |
| 185 |
m_error = "Refusing to upload empty array."; |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
if (buffer.size() < |
| 190 |
static_cast<typename ContainerT::size_type>(dims[0] * dims[1])) { |
| 191 |
m_error = "Buffer data is smaller than specified dimensions."; |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
using Avogadro::Type; // Not to be confused with Avogadro::Rendering::Type... |
| 196 |
Type incomingType = |
| 197 |
static_cast<Type>(TypeTraits<typename ContainerT::value_type>::EnumValue); |
| 198 |
switch (incomingType) { |
| 199 |
case Avogadro::CharType: |
| 200 |
case Avogadro::UCharType: |
| 201 |
case Avogadro::ShortType: |
| 202 |
case Avogadro::UShortType: |
| 203 |
case Avogadro::IntType: |
| 204 |
case Avogadro::UIntType: |
| 205 |
case Avogadro::FloatType: |
| 206 |
break; |
| 207 |
default: |
| 208 |
m_error = "Unsupported type for texture data: '"; |
| 209 |
m_error += TypeTraits<typename ContainerT::value_type>::name(); |
| 210 |
m_error += "'."; |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
return uploadInternal(&buffer[0], dims, incomingFormat, incomingType, |
| 215 |
internalFormat); |
| 216 |
} |
| 217 |
|
| 218 |
} // namespace Rendering |
| 219 |
} // namespace Avogadro |
| 220 |
|
| 221 |
#endif // AVOGADRO_RENDERING_TEXTURE2D_H |