| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- /****************************************************************************
- Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
- http://www.cocos2d-x.org
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- #pragma once
- #include "../Macro.h"
- #include "../Types.h"
- #include "GraphicsHandle.h"
- #include "RenderTarget.h"
- #include "base/CCData.h"
- #include <vector>
- RENDERER_BEGIN
- class DeviceGraphics;
- /**
- * @addtogroup gfx
- * @{
- */
- /**
- * The base texture class
- */
- class Texture : public RenderTarget
- {
- public:
- /**
- * @enum Filter
- * Texture filter modes
- */
- enum class Filter : int8_t
- {
- NONE = -1,
- NEAREST = 0,
- LINEAR = 1
- };
- /**
- * @enum Filter
- * Texture wrap modes
- */
- enum class WrapMode : uint16_t
- {
- REPEAT = GL_REPEAT,
- CLAMP = GL_CLAMP_TO_EDGE,
- MIRROR = GL_MIRRORED_REPEAT
- };
- /**
- * @enum Format
- * Texture formats
- */
- enum class Format : uint8_t
- {
- BEGIN = 0,
- // compress formats
- RGB_DXT1 = 0,
- RGBA_DXT1 = 1,
- RGBA_DXT3 = 2,
- RGBA_DXT5 = 3,
- RGB_ETC1 = 4,
- RGB_PVRTC_2BPPV1 = 5,
- RGBA_PVRTC_2BPPV1 = 6,
- RGB_PVRTC_4BPPV1 = 7,
- RGBA_PVRTC_4BPPV1 = 8,
- //
- // normal formats
- A8 = 9,
- L8 = 10,
- L8_A8 = 11,
- R5_G6_B5 = 12,
- R5_G5_B5_A1 = 13,
- R4_G4_B4_A4 = 14,
- RGB8 = 15, // each channel has 8 bits
- RGBA8 = 16, // each channel has 8 bits
- RGB16F = 17, // each channel has 16 bits
- RGBA16F = 18, // each channel has 16 bits
- RGB32F = 19, // each channel has 32 bits
- RGBA32F = 20, // each channel has 32 bits
- R32F = 21,
- _111110F = 22,
- SRGB = 23,
- SRGBA = 24,
- //
- // depth formats
- D16 = 25,
- END = 25
- //
- };
-
- /**
- * @struct Image
- * Raw image data
- */
- struct Image
- {
- uint8_t* data = nullptr;
- size_t length = 0;
- };
- /**
- * @struct Options
- * Texture setting options including format, width, height, wrap mode, filter etc.
- */
- struct Options
- {
- /**
- * Image mipmaps
- */
- std::vector<Image> images;
- /**
- * The maximum anisotropy for the texture
- */
- int32_t anisotropy = 1;
- /**
- * The internal format specifying the color components in the texture
- */
- GLenum glInternalFormat = GL_RGBA;
- /**
- * The pixel format of the texel data
- */
- GLenum glFormat = GL_RGB;
- /**
- * The data type of the texel data
- */
- GLenum glType = GL_UNSIGNED_BYTE;
- /**
- * The width of the texture
- */
- uint16_t width = 4;
- /**
- * The height of the texture
- */
- uint16_t height = 4;
- uint8_t bpp = 0;
-
- /**
- * The wrapping function for texture coordinate s
- */
- WrapMode wrapS = WrapMode::CLAMP;
- /**
- * The wrapping function for texture coordinate t
- */
- WrapMode wrapT = WrapMode::CLAMP;
- /**
- * The texture minification filter
- */
- Filter minFilter = Filter::LINEAR;
- /**
- * The texture magnification filter
- */
- Filter magFilter = Filter::LINEAR;
- /**
- * The texture filter for mipmaps
- */
- Filter mipFilter = Filter::LINEAR;
-
- /**
- * Specifies whether the texture have mipmaps
- */
- bool hasMipmap = false;
- /**
- * Specifies whether the texture if flipped vertically
- */
- bool flipY = false;
- /**
- * Specifies whether the texture have alpha premultiplied
- */
- bool premultiplyAlpha = false;
- /**
- * Specifies whether the texture is compressed
- */
- bool compressed = false;
- };
- /**
- * @struct ImageOption
- * The informations of Image which indicates the mipmap level, width, height, filpY and premultiplyAlpha
- */
- struct ImageOption
- {
- Image image;
- int32_t level = 0;
- uint16_t width = 4;
- uint16_t height = 4;
- bool flipY = false;
- bool premultiplyAlpha = false;
- };
- /**
- * @struct SubImageOption
- * The informations of sub image which indicates the area to update, mipmap level, filpY and premultiplyAlpha
- */
- struct SubImageOption
- {
- SubImageOption(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t level, bool flipY, bool premultiplyAlpha)
- : x(x)
- , y(y)
- , width(width)
- , height(height)
- , level(level)
- , flipY(flipY)
- , premultiplyAlpha(premultiplyAlpha)
- {}
-
- SubImageOption() {}
- uint32_t imageDataLength = 0;
- uint16_t x = 0;
- uint16_t y = 0;
- uint16_t width = 0;
- uint16_t height = 0;
- uint8_t* imageData = nullptr;
- uint8_t level = 0;
- bool flipY = false;
- bool premultiplyAlpha = false;
- };
- /**
- * Gets the target gl location
- */
- inline GLuint getTarget() const { return _target; }
- /**
- * Gets the width of texture
- */
- inline uint16_t getWidth() const { return _width; }
- /**
- * Gets the height of texture
- */
- inline uint16_t getHeight() const { return _height; }
-
- inline void setAlphaAtlas(bool value) { _useAlphaAtlas = value; }
- inline bool isAlphaAtlas() const { return _useAlphaAtlas; }
-
- protected:
-
- static GLenum glFilter(Filter filter, Filter mipFilter = Filter::NONE);
-
- static bool isPow2(int32_t v) {
- return !(v & (v - 1)) && (!!v);
- }
- Texture();
- virtual ~Texture();
- bool init(DeviceGraphics* device);
- DeviceGraphics* _device;
- GLint _anisotropy;
- GLuint _target;
-
- WrapMode _wrapS;
- WrapMode _wrapT;
- uint16_t _width;
- uint16_t _height;
- uint8_t _bpp = 0;
- Filter _minFilter;
- Filter _magFilter;
- Filter _mipFilter;
- GLenum _glInternalFormat;
- GLenum _glFormat;
- GLenum _glType;
- bool _hasMipmap;
- bool _compressed;
-
- bool _useAlphaAtlas = false;
- };
- // end of gfx group
- /// @}
- RENDERER_END
|