Spinning Topp Logo BlackTopp Studios
inc
texture.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 
41 #ifndef _graphicstexture_cpp
42 #define _graphicstexture_cpp
43 
44 #include "Graphics/texture.h"
45 
46 #include "exception.h"
47 
48 #include <Ogre.h>
49 #include <OgrePixelFormat.h>
50 
51 namespace Mezzanine
52 {
53  namespace Graphics
54  {
55  ///////////////////////////////////////////////////////////////////////////////
56  /// @class InternalTextureData
57  /// @brief This class is used to store the internal structures needed by the Texture class.
58  /// @details Specifically, this class stores a shared pointer to the Ogre Texture and only
59  /// exists because shared pointers can't be forward declared without compromising how they
60  /// work.
61  ///////////////////////////////////////
63  {
64  public:
65  /// @internal
66  /// @brief The internal representation of the Texture.
68  };//InternalTextureData
69 
70  ///////////////////////////////////////////////////////////////////////////////
71  // Texture Methods
72 
74  {
75  this->ITD = new InternalTextureData();
76  this->ITD->GraphicsTexture = InternalTexture;
77  }
78 
80  { delete this->ITD; }
81 
82  ///////////////////////////////////////////////////////////////////////////////
83  // Utility Methods
84 
86  { return this->_GetInternalTexture()->getSrcWidth(); }
87 
89  { return this->_GetInternalTexture()->getSrcHeight(); }
90 
92  { return this->_GetInternalTexture()->getSrcDepth(); }
93 
95  { return static_cast<Graphics::PixelFormat>( this->_GetInternalTexture()->getFormat() ); }
96 
98  { return this->_GetInternalTexture()->getNumMipmaps(); }
99 
101  { return this->_GetInternalTexture()->getSize(); }
102 
103  ///////////////////////////////////////////////////////////////////////////////
104  // AssetMethods
105 
106  const String& Texture::GetName() const
107  { return this->_GetInternalTexture()->getName(); }
108 
109  const String& Texture::GetGroup() const
110  { return this->_GetInternalTexture()->getGroup(); }
111 
112  ///////////////////////////////////////////////////////////////////////////////
113  // Internal Buffer Manipulation Methods
114 
115  Whole Texture::_ReadFromBuffer(UInt8* DestBuffer, const Whole BufferSize)
116  {
117  Whole TexSize = this->GetSize();
118  if( BufferSize < TexSize ) {
119  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,"Destination buffer is too small to fit all texture pixel data.");
120  }
121 
122  Ogre::HardwarePixelBufferSharedPtr PixelBuffer = this->_GetInternalTexture()->getBuffer();
123 
124  // Lock the pixel buffer and get a pixel box
125  PixelBuffer->lock(Ogre::HardwareBuffer::HBL_NORMAL);
126  const Ogre::PixelBox& Box = PixelBuffer->getCurrentLock();
127 
128  UInt8* SrcBuf = static_cast<UInt8*>(Box.data);
129 
130  Ogre::PixelUtil::bulkPixelConversion(SrcBuf,this->_GetInternalTexture()->getFormat(),DestBuffer,this->_GetInternalTexture()->getFormat(),TexSize);
131  //memcpy(DestBuffer,SrcBuf,TexSize);
132 
133  PixelBuffer->unlock();
134 
135  return TexSize;
136  }
137 
138  void Texture::_WriteToBuffer(UInt8* SrcBuffer, const Whole BufferSize, const Graphics::PixelFormat SrcFormat)
139  {
140  if( this->GetSize() != BufferSize ) {
141  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,"Texture and write buffer are different sizes. Sizes must match.");
142  }
143 
144  Ogre::HardwarePixelBufferSharedPtr PixelBuffer = this->_GetInternalTexture()->getBuffer();
145 
146  // Lock the pixel buffer and get a pixel box
147  PixelBuffer->lock(Ogre::HardwareBuffer::HBL_NORMAL);
148  const Ogre::PixelBox& Box = PixelBuffer->getCurrentLock();
149 
150  UInt8* DestBuf = static_cast<UInt8*>(Box.data);
151  Whole PixelCount = BufferSize / Ogre::PixelUtil::getNumElemBytes(static_cast<Ogre::PixelFormat>(SrcFormat));
152  Ogre::PixelUtil::bulkPixelConversion(SrcBuffer,static_cast<Ogre::PixelFormat>(SrcFormat),DestBuf,this->_GetInternalTexture()->getFormat(),PixelCount);
153 
154  // Unlock the pixel buffer
155  PixelBuffer->unlock();
156  }
157 
158  ///////////////////////////////////////////////////////////////////////////////
159  // Internal Methods
160 
162  { return this->ITD->GraphicsTexture; }
163  }//Graphics
164 }//Mezzanine
165 
166 #endif
UInt32 GetOriginalHeight() const
Gets the height of the source Texture in pixels.
Definition: texture.cpp:88
InternalTextureData * ITD
A pointer to the internal implementation of the Texture.
Definition: texture.h:68
const String & GetName() const
Gets the Name of this Texture.
Definition: texture.cpp:106
Graphics::PixelFormat GetFormat() const
Gets the pixel format of this Texture.
Definition: texture.cpp:94
Whole GetNumMipMaps() const
Gets the number of MipMaps that exist for this Texture.
Definition: texture.cpp:97
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
Ogre::TexturePtr GraphicsTexture
The internal representation of the Texture.
Definition: texture.cpp:67
Whole _ReadFromBuffer(UInt8 *DestBuffer, const Whole BufferSize)
Reads from the internal buffer and populates the provided buffer with the texture data...
Definition: texture.cpp:115
Whole GetSize() const
Gets the size of this Texture.
Definition: texture.cpp:100
uint8_t UInt8
An 8-bit unsigned integer.
Definition: datatypes.h:118
This class is used to store the internal structures needed by the Texture class.
Definition: texture.cpp:62
This implements the exception hiearchy for Mezzanine.
const String & GetGroup() const
Gets the resource group this Texture belongs to.
Definition: texture.cpp:109
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
PixelFormat
This is used to describe how bits are arraged for each pixel in an image.
~Texture()
Class Destructor.
Definition: texture.cpp:79
Thrown when parameters are checked at runtime and found invalid.
Definition: exception.h:108
UInt32 GetOriginalWidth() const
Gets the width of the source Texture in pixels.
Definition: texture.cpp:85
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
void _WriteToBuffer(UInt8 *SrcBuffer, const Whole BufferSize, const Graphics::PixelFormat SrcFormat)
Writes to the textures internal buffer. the size of this texture is smaller than the buffer being wri...
Definition: texture.cpp:138
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
Texture(Ogre::TexturePtr InternalTexture)
Class Constructor.
Definition: texture.cpp:73
Ogre::TexturePtr _GetInternalTexture() const
Gets the internal Texture pointer.
Definition: texture.cpp:161
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
UInt32 GetOriginalDepth() const
Gets the depth of the source Texture in pixels.
Definition: texture.cpp:91