Rigs of Rods 2023.09
Soft-body Physics Simulation
Loading...
Searching...
No Matches
TextureManager.cpp
Go to the documentation of this file.
1/*
2--------------------------------------------------------------------------------
3This source file is part of Hydrax.
4Visit ---
5
6Copyright (C) 2008 Xavier Verguín González <xavierverguin@hotmail.com>
7 <xavyiy@gmail.com>
8
9This program is free software; you can redistribute it and/or modify it under
10the terms of the GNU Lesser General Public License as published by the Free Software
11Foundation; either version 2 of the License, or (at your option) any later
12version.
13
14This program is distributed in the hope that it will be useful, but WITHOUT
15ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17
18You should have received a copy of the GNU Lesser General Public License along with
19this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21http://www.gnu.org/copyleft/lesser.txt.
22--------------------------------------------------------------------------------
23*/
24
25#include <TextureManager.h>
26
27#include <Hydrax.h>
28
29namespace Hydrax
30{
32 : mCreated(false)
33 , mHydrax(h)
34 {
35 for (int k = 0; k < 1; k++)
36 {
37 mTextures[k].reset();
38 }
39
40 mTextureNames[0] = "HydraxNormalMap";
41 }
42
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].reset();
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
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}
#define HydraxLOG(msg)
Definition Application.h:60
MaterialManager * getMaterialManager()
Get Hydrax::MaterialManager.
Definition Hydrax.h:325
Class for store variable channels of an image.
Definition Image.h:43
Size getSize() const
Get image size.
Definition Image.h:237
const float & getValue(const int &x, const int &y, const int &c) const
Get a pixel value.
Definition Image.cpp:58
Type getType() const
Get image type.
Definition Image.h:245
void reload(const MaterialType &Material)
Reload material.
Ogre::TexturePtr mTextures[1]
Our Ogre::TexturePtr array.
Hydrax * mHydrax
Hydrax main pointer.
Ogre::String mTextureNames[1]
Our Ogre::String array for store texture's names.
void create(const Size &Size)
Create height and normal map textures.
bool _createTexture(Ogre::TexturePtr &Texture, const Ogre::String &Name, const Size &Size)
Create an Ogre::Texture.
const Ogre::String & getTextureName(const TexturesID &Id) const
Get texture's name.
Ogre::TexturePtr & getTexture(const TexturesID &Id)
Get texture.
bool mCreated
Have been created already called?
TextureManager(Hydrax *h)
Constructor.
void remove()
Remove textures.
bool _updateNormalMap(Image &Image)
Update normal map.
Struct wich contains an especific width and height value.
Definition Help.h:41
int Height
Height value.
Definition Help.h:45
int Width
Width value.
Definition Help.h:43