RigsofRods
Soft-body Physics Simulation
TextureManager.cpp
Go to the documentation of this file.
1 /*
2 --------------------------------------------------------------------------------
3 This source file is part of Hydrax.
4 Visit ---
5 
6 Copyright (C) 2008 Xavier Verguín González <xavierverguin@hotmail.com>
7  <xavyiy@gmail.com>
8 
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser General Public License as published by the Free Software
11 Foundation; either version 2 of the License, or (at your option) any later
12 version.
13 
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License along with
19 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21 http://www.gnu.org/copyleft/lesser.txt.
22 --------------------------------------------------------------------------------
23 */
24 
25 #include <TextureManager.h>
26 
27 #include <Hydrax.h>
28 
29 namespace Hydrax
30 {
32  : mCreated(false)
33  , mHydrax(h)
34  {
35  for (int k = 0; k < 1; k++)
36  {
37  mTextures[k].setNull();
38  }
39 
40  mTextureNames[0] = "HydraxNormalMap";
41  }
42 
44  {
45  remove();
46  }
47 
49  {
50  remove();
51 
52  for (int k = 0; k < 1; k++)
53  {
55  }
56 
58 
59  mCreated = true;
60  }
61 
63  {
64  if (!mCreated)
65  {
66  return;
67  }
68 
69  for (int k = 0; k < 1; k++)
70  {
71  Ogre::TextureManager::getSingleton().remove(mTextureNames[k]);
72  mTextures[k].setNull();
73  }
74 
75  mCreated = false;
76  }
77 
79  {
80  if (!mCreated)
81  {
82  HydraxLOG("Error in TextureManager::_updateNormalMap, create() does not called.");
83 
84  return false;
85  }
86 
87  if (Image.getType() != Image::TYPE_RGB)
88  {
89  HydraxLOG("Error in TextureManager::_updateNormalMap, Image type isn't correct.");
90 
91  return false;
92  }
93 
94  Ogre::TexturePtr &Texture = getTexture(TEX_NORMAL_ID);
95 
96  Size ImageSize = Image.getSize();
97 
98  if (Texture->getWidth() != ImageSize.Width ||
99  Texture->getHeight() != ImageSize.Height)
100  {
101  HydraxLOG("Error in TextureManager::update, Update size doesn't correspond to " + getTextureName(TEX_NORMAL_ID) + " texture size");
102 
103  return false;
104  }
105 
106  Ogre::HardwarePixelBufferSharedPtr pixelBuffer = Texture->getBuffer();
107 
108  pixelBuffer->lock(Ogre::HardwareBuffer::HBL_NORMAL);
109  const Ogre::PixelBox& pixelBox = pixelBuffer->getCurrentLock();
110 
111  Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
112 
113  int x, y;
114 
115  for (x = 0; x < ImageSize.Width; x++)
116  {
117  for (y = 0; y < ImageSize.Height; y++)
118  {
119  *pDest++ = Image.getValue(x,y,2); // B
120  *pDest++ = Image.getValue(x,y,1); // G
121  *pDest++ = Image.getValue(x,y,0); // R
122  *pDest++ = 255; // A
123  }
124  }
125 
126  pixelBuffer->unlock();
127 
128  return true;
129  }
130 
131  bool TextureManager::_createTexture(Ogre::TexturePtr &Texture, const Ogre::String &Name, const Size &Size)
132  {
133  try
134  {
135  Ogre::TextureManager::getSingleton().remove(Name);
136 
137  Texture = Ogre::TextureManager::getSingleton().
138  createManual(Name,
139  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
140  Ogre::TEX_TYPE_2D,
142  0,
143  Ogre::PF_BYTE_BGRA,
144  Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
145 
146  Texture->load();
147  }
148  catch (Ogre::Exception &e)
149  {
150  HydraxLOG(e.getFullDescription());
151 
152  return false;
153  }
154 
155  return true;
156  }
157 }
Hydrax::TextureManager::mHydrax
Hydrax * mHydrax
Hydrax main pointer.
Definition: TextureManager.h:141
Hydrax::TextureManager::mTextures
Ogre::TexturePtr mTextures[1]
Our Ogre::TexturePtr array.
Definition: TextureManager.h:133
y
float y
Definition: (ValueTypes) quaternion.h:6
Hydrax::TextureManager::create
void create(const Size &Size)
Create height and normal map textures.
Definition: TextureManager.cpp:48
Hydrax
Definition: CfgFileManager.cpp:28
Hydrax::TextureManager::TEX_NORMAL_ID
@ TEX_NORMAL_ID
Definition: TextureManager.h:53
Hydrax::Hydrax::getMaterialManager
MaterialManager * getMaterialManager()
Get Hydrax::MaterialManager.
Definition: Hydrax.h:325
Hydrax::Size
Struct wich contains an especific width and height value.
Definition: Help.h:40
Hydrax.h
TextureManager.h
Hydrax::TextureManager::~TextureManager
~TextureManager()
Destructor.
Definition: TextureManager.cpp:43
Hydrax::TextureManager::getTexture
Ogre::TexturePtr & getTexture(const TexturesID &Id)
Get texture.
Definition: TextureManager.h:96
Hydrax::TextureManager::TextureManager
TextureManager(Hydrax *h)
Constructor.
Definition: TextureManager.cpp:31
Hydrax::TextureManager::remove
void remove()
Remove textures.
Definition: TextureManager.cpp:62
Hydrax::TextureManager::mTextureNames
Ogre::String mTextureNames[1]
Our Ogre::String array for store texture's names.
Definition: TextureManager.h:135
Hydrax::Image::getValue
const float & getValue(const int &x, const int &y, const int &c) const
Get a pixel value.
Definition: Image.cpp:58
Hydrax::TextureManager::mCreated
bool mCreated
Have been created already called?
Definition: TextureManager.h:138
Hydrax::TextureManager::_updateNormalMap
bool _updateNormalMap(Image &Image)
Update normal map.
Definition: TextureManager.cpp:78
Hydrax::Image::getType
Type getType() const
Get image type.
Definition: Image.h:245
HydraxLOG
#define HydraxLOG(msg)
Definition: Application.h:59
Hydrax::MaterialManager::reload
void reload(const MaterialType &Material)
Reload material.
Definition: MaterialManager.cpp:3143
Hydrax::Image
Class for store variable channels of an image.
Definition: Image.h:42
Hydrax::TextureManager::_createTexture
bool _createTexture(Ogre::TexturePtr &Texture, const Ogre::String &Name, const Size &Size)
Create an Ogre::Texture.
Definition: TextureManager.cpp:131
Hydrax::Size::Width
int Width
Width value.
Definition: Help.h:43
Hydrax::MaterialManager::MAT_WATER
@ MAT_WATER
Definition: MaterialManager.h:53
Hydrax::Image::TYPE_RGB
@ TYPE_RGB
Definition: Image.h:51
Hydrax::TextureManager::getTextureName
const Ogre::String & getTextureName(const TexturesID &Id) const
Get texture's name.
Definition: TextureManager.h:105
Hydrax::Image::getSize
Size getSize() const
Get image size.
Definition: Image.h:237
Hydrax::Size::Height
int Height
Height value.
Definition: Help.h:45
x
float x
Definition: (ValueTypes) quaternion.h:5