| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "igl.h" |
| 4 |
#include "imodule.h" |
| 5 |
|
| 6 |
typedef unsigned char byte; |
| 7 |
|
| 8 |
class Texture; |
| 9 |
typedef std::shared_ptr<Texture> TexturePtr; |
| 10 |
|
| 11 |
/** |
| 12 |
* \brief Interface for an object (typically an image) which can produce a bound |
| 13 |
* OpenGL texture wrapped in a Texture object. |
| 14 |
*/ |
| 15 |
class BindableTexture |
| 16 |
{ |
| 17 |
public: |
| 18 |
virtual ~BindableTexture() {} |
| 19 |
|
| 20 |
/** |
| 21 |
* Enumeration of possible roles for a texture, which might be used to |
| 22 |
* choose an appropriate OpenGL format (e.g. RGB for colour images, RG for |
| 23 |
* normal maps etc). |
| 24 |
*/ |
| 25 |
enum class Role |
| 26 |
{ |
| 27 |
/// A regular colour texture requiring RGB channels |
| 28 |
COLOUR, |
| 29 |
|
| 30 |
/// A normal map which requires only R & G channels |
| 31 |
NORMAL_MAP |
| 32 |
}; |
| 33 |
|
| 34 |
/** |
| 35 |
* \brief Bind this texture to OpenGL. |
| 36 |
* |
| 37 |
* This method invokes the necessary GL calls to bind and upload the |
| 38 |
* object's texture. It returns a Texture object representing the texture in |
| 39 |
* GL. |
| 40 |
* |
| 41 |
* \param name |
| 42 |
* Optional name to give the texture at bind time. This would usually be the |
| 43 |
* name of the image map, e.g. "textures/blah/bleh". |
| 44 |
* |
| 45 |
* \param role |
| 46 |
* Optional role to suggest an internal OpenGL format for the bound texture. |
| 47 |
* The implementing object may (but is not required to) use this information |
| 48 |
* to help choose a suitable format when binding the texture. In particular, |
| 49 |
* compressed images loaded from DDS files already have a specific format |
| 50 |
* which cannot be changed at bind time. |
| 51 |
*/ |
| 52 |
virtual TexturePtr bindTexture(const std::string& name = "", |
| 53 |
Role role = Role::COLOUR) const = 0; |
| 54 |
}; |
| 55 |
|
| 56 |
typedef std::shared_ptr<BindableTexture> BindableTexturePtr; |
| 57 |
|
| 58 |
/// Representation of a 2D image texture in an unspecified format |
| 59 |
class Image |
| 60 |
: public BindableTexture |
| 61 |
{ |
| 62 |
public: |
| 63 |
|
| 64 |
/// Return the start of the pixel data for this image |
| 65 |
virtual uint8_t* getPixels() const = 0; |
| 66 |
|
| 67 |
/// Return number of resolution levels (mipmaps) |
| 68 |
virtual std::size_t getLevels() const = 0; |
| 69 |
|
| 70 |
/// Return the width of the specified level in pixels |
| 71 |
virtual std::size_t getWidth(std::size_t level = 0) const = 0; |
| 72 |
|
| 73 |
/// Return the height of the specified level in pixels |
| 74 |
virtual std::size_t getHeight(std::size_t level = 0) const = 0; |
| 75 |
|
| 76 |
/** |
| 77 |
* \brief Test whether this image is precompressed. |
| 78 |
* |
| 79 |
* Precompressed images cannot be used as input for image manipulation |
| 80 |
* operations in shaders, due to quality loss. Note that not all DDS images |
| 81 |
* are necessarily compressed (although uncompressed DDS images are rarely |
| 82 |
* used in TDM). |
| 83 |
*/ |
| 84 |
virtual bool isPrecompressed() const { |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
/// Return the OpenGL format for this image |
| 89 |
virtual GLenum getGLFormat() const = 0; |
| 90 |
}; |
| 91 |
typedef std::shared_ptr<Image> ImagePtr; |
| 92 |
|
| 93 |
class ArchiveFile; |
| 94 |
|
| 95 |
/// Module responsible for loading images from VFS or disk filesystem |
| 96 |
class IImageLoader : |
| 97 |
public RegisterableModule |
| 98 |
{ |
| 99 |
public: |
| 100 |
|
| 101 |
/** |
| 102 |
* \brief |
| 103 |
* Load an image from a VFS path. |
| 104 |
* |
| 105 |
* Images loaded from the VFS are not specified with an extension. Various |
| 106 |
* extensions (and optional VFS directory prefixes) will be attempted in an |
| 107 |
* order specified by the .game file. For example, requesting |
| 108 |
* "textures/blah/bleh" might result in an attempt to load |
| 109 |
* "textures/blah/bleh.tga" followed by "dds/textures/blah/bleh.dds" if the |
| 110 |
* tga version cannot be found. |
| 111 |
*/ |
| 112 |
virtual ImagePtr imageFromVFS(const std::string& vfsPath) const = 0; |
| 113 |
|
| 114 |
/** |
| 115 |
* \brief |
| 116 |
* Load an image from a filesystem path. |
| 117 |
*/ |
| 118 |
virtual ImagePtr imageFromFile(const std::string& filename) const = 0; |
| 119 |
}; |
| 120 |
|
| 121 |
const char* const MODULE_IMAGELOADER("ImageLoader"); |
| 122 |
|
| 123 |
inline const IImageLoader& GlobalImageLoader() |
| 124 |
{ |
| 125 |
static module::InstanceReference<IImageLoader> _reference(MODULE_IMAGELOADER); |
| 126 |
return _reference; |
| 127 |
} |