12#include <OgrePrerequisites.h>
13#include <OgrePixelFormat.h>
14#include <OgreTexture.h>
15#include <OgreTextureManager.h>
16#include <OgreHardwarePixelBuffer.h>
17#include <OgreRenderSystem.h>
18#include <OgreString.h>
19#include <OgreStringConverter.h>
33 TexturePtr map = TextureManager::getSingleton().
load(fileName, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, 0);
36 return load(map, channel);
41 const String key = texture->getName() + StringConverter::toString((
int)channel);
43 std::map<String, DensityMap*>::iterator i;
66 delete[]
static_cast<uint8*
>(
pixels->data);
75 assert(map.isNull() ==
false);
79 selfKey = map->getName() + StringConverter::toString((
int)channel);
84 HardwarePixelBufferSharedPtr buff = map->getBuffer();
87 pixels =
new PixelBox(Box(0, 0, buff->getWidth(), buff->getHeight()), PF_BYTE_L);
92 buff->blitToMemory(*
pixels);
96 PixelBox tmpPixels(Box(0, 0, buff->getWidth(), buff->getHeight()), PF_R8G8B8A8);
97 tmpPixels.data =
new uint8[tmpPixels.getConsecutiveSize()];
98 buff->blitToMemory(tmpPixels);
101 size_t channelOffset;
107 default: OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
"Invalid channel",
"GrassLayer::setDensityMap()");
break;
111 uint8 *inputPtr = (uint8*)tmpPixels.data + channelOffset;
112 uint8 *outputPtr = (uint8*)
pixels->data;
113 uint8 *outputEndPtr = outputPtr +
pixels->getConsecutiveSize();
114 while (outputPtr != outputEndPtr){
115 *outputPtr++ = *inputPtr;
120 delete[]
static_cast<uint8*
>(tmpPixels.data);
132 if(x < mapBounds.left || x >= mapBounds.right || z < mapBounds.top || z >= mapBounds.bottom)
135 size_t mapWidth =
pixels->getWidth(), mapHeight =
pixels->getHeight();
145 size_t xindex = size_t(mapWidth * (
x - mapBounds.left) / mapBounds.width());
146 size_t zindex = size_t(mapHeight * (
z - mapBounds.top) / mapBounds.height());
148 uint8 *data =
reinterpret_cast<uint8*
>(
pixels->data);
149 return data[mapWidth * zindex + xindex] * 0.00392157f;
159 if(x < mapBounds.left || x >= mapBounds.right || z < mapBounds.top || z >= mapBounds.bottom)
162 uint32 mapWidth = (uint32)
pixels->getWidth();
163 uint32 mapHeight = (uint32)
pixels->getHeight();
173 Ogre::Real xIndexFloat = (mapWidth * (
x - mapBounds.left) / mapBounds.width()) - 0.5f;
174 Ogre::Real zIndexFloat = (mapHeight * (
z - mapBounds.top) / mapBounds.height()) - 0.5f;
176 uint32 xIndex = (uint32)xIndexFloat;
177 uint32 zIndex = (uint32)zIndexFloat;
178 if (xIndex < 0 || zIndex < 0 || xIndex >= mapWidth-1 || zIndex >= mapHeight-1)
181 Ogre::Real xRatio = xIndexFloat - xIndex;
182 Ogre::Real xRatioInv = 1.f - xRatio;
183 Ogre::Real zRatio = zIndexFloat - zIndex;
184 Ogre::Real zRatioInv = 1.f - zRatio;
186 uint8 *data = (uint8*)
pixels->data;
188 Ogre::Real val11 = data[mapWidth * zIndex + xIndex] * 0.0039215686274509803921568627451f;
189 Ogre::Real val21 = data[mapWidth * zIndex + xIndex + 1] * 0.0039215686274509803921568627451f;
190 Ogre::Real val12 = data[mapWidth * ++zIndex + xIndex] * 0.0039215686274509803921568627451f;
191 Ogre::Real val22 = data[mapWidth * zIndex + xIndex + 1] * 0.0039215686274509803921568627451f;
193 Ogre::Real val1 = xRatioInv * val11 + xRatio * val21;
194 Ogre::Real val2 = xRatioInv * val12 + xRatio * val22;
196 return zRatioInv * val1 + zRatio * val2;
208 TexturePtr map = TextureManager::getSingleton().
load(fileName, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
211 return load(map, channel);
216 const String key = texture->getName() + StringConverter::toString((
int)channel);
218 std::map<String, ColorMap*>::iterator i;
225 m =
new ColorMap(texture, channel);
241 delete[]
static_cast<uint8*
>(
pixels->data);
250 assert(map.isNull() ==
false);
254 selfKey = map->getName() + StringConverter::toString((
int)channel);
259 HardwarePixelBufferSharedPtr buff = map->getBuffer();
262 VertexElementType format = Root::getSingleton().getRenderSystem()->getColourVertexElementType();
264 case VET_COLOUR_ARGB:
266 pixels =
new PixelBox(Box(0, 0, buff->getWidth(), buff->getHeight()), PF_A8R8G8B8);
268 case VET_COLOUR_ABGR:
270 pixels =
new PixelBox(Box(0, 0, buff->getWidth(), buff->getHeight()), PF_A8B8G8R8);
272 if (buff->getFormat() == PF_L8)
276 OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
"Unknown RenderSystem color format",
"GrassLayer::setColorMap()");
284 buff->blitToMemory(*
pixels);
288 PixelBox tmpPixels(Box(0, 0, buff->getWidth(), buff->getHeight()), PF_R8G8B8A8);
289 tmpPixels.data =
new uint8[tmpPixels.getConsecutiveSize()];
290 buff->blitToMemory(tmpPixels);
293 size_t channelOffset;
299 default: OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
"Invalid channel",
"ColorMap::ColorMap()");
break;
303 uint8 *inputPtr = (uint8*)tmpPixels.data + channelOffset;
304 uint8 *outputPtr = (uint8*)
pixels->data;
305 uint8 *outputEndPtr = outputPtr +
pixels->getConsecutiveSize();
306 while (outputPtr != outputEndPtr){
307 *outputPtr++ = *inputPtr;
308 *outputPtr++ = *inputPtr;
309 *outputPtr++ = *inputPtr;
315 delete[]
static_cast<uint8*
>(tmpPixels.data);
325 if(x < mapBounds.left || x >=
mapBounds.right || z < mapBounds.top || z >=
mapBounds.bottom)
330 uint32 mapWidth = (uint32)
pixels->getWidth();
331 uint32 mapHeight = (uint32)
pixels->getHeight();
336 uint32 *data = (uint32*)
pixels->data;
337 return data[mapWidth * zindex + xindex];
342 uint8 a1 = (color1 & 0xFF);
343 uint8 b1 = (color1 >> 8 & 0xFF);
344 uint8 c1 = (color1 >> 16 & 0xFF);
345 uint8 d1 = (color1 >> 24 & 0xFF);
347 uint8 a2 = (color2 & 0xFF);
348 uint8 b2 = (color2 >> 8 & 0xFF);
349 uint8 c2 = (color2 >> 16 & 0xFF);
350 uint8 d2 = (color2 >> 24 & 0xFF);
352 uint8 a = uint8(ratioInv * a1 + ratio * a2);
353 uint8 b = uint8(ratioInv * b1 + ratio * b2);
354 uint8 c = uint8(ratioInv * c1 + ratio * c2);
355 uint8 d = uint8(ratioInv * d1 + ratio * d2);
357 return a | (b << 8) | (c << 16) | (d << 24);
365 if(x < mapBounds.left || x >=
mapBounds.right || z < mapBounds.top || z >=
mapBounds.bottom)
370 uint32 mapWidth = (uint32)
pixels->getWidth();
371 uint32 mapHeight = (uint32)
pixels->getHeight();
375 uint32 xIndex = (uint32)xIndexFloat;
376 uint32 zIndex = (uint32)zIndexFloat;
377 if (xIndex < 0 || zIndex < 0 || xIndex > mapWidth-1 || zIndex > mapHeight-1)
380 Ogre::Real xRatio = xIndexFloat - xIndex;
381 Ogre::Real xRatioInv = 1.f - xRatio;
382 Ogre::Real zRatio = zIndexFloat - zIndex;
383 Ogre::Real zRatioInv = 1.f - zRatio;
385 uint32 *data = (uint32*)
pixels->data;
387 uint32 val11 = data[mapWidth * zIndex + xIndex];
388 uint32 val21 = data[mapWidth * zIndex + xIndex + 1];
389 uint32 val12 = data[mapWidth * ++zIndex + xIndex];
390 uint32 val22 = data[mapWidth * zIndex + xIndex + 1];
A 2D greyscale image that is assigned to a certain region of your world to represent color levels.
ColorMap(Ogre::TexturePtr map, MapChannel channel)
static ColorMap * load(const Ogre::String &fileName, MapChannel channel=CHANNEL_COLOR)
Ogre::uint32 _getColorAt_Bilinear(Ogre::Real x, Ogre::Real z, const Ogre::TRect< Ogre::Real > &mapBounds)
Ogre::uint32 _getColorAt(Ogre::Real x, Ogre::Real z, const Ogre::TRect< Ogre::Real > &mapBounds)
Ogre::TRect< Ogre::Real > mapBounds
static std::map< Ogre::String, ColorMap * > selfList
Ogre::uint32 _interpolateColor(Ogre::uint32 color1, Ogre::uint32 color2, Ogre::Real ratio, Ogre::Real ratioInv)
Ogre::Real _getDensityAt_Bilinear(Ogre::Real x, Ogre::Real z, const Ogre::TRect< Ogre::Real > &mapBounds)
static std::map< Ogre::String, DensityMap * > selfList
DensityMap(Ogre::TexturePtr texture, MapChannel channel)
static DensityMap * load(const Ogre::String &fileName, MapChannel channel=CHANNEL_COLOR)
Ogre::Real _getDensityAt_Unfiltered(Ogre::Real x, Ogre::Real z, const Ogre::TRect< Ogre::Real > &mapBounds)
@ MAPFILTER_BILINEAR
Use bilinear filtering - slower, but will appear less blocky.
MapChannel
Specifies which color channel(s) to extract from an image.
@ CHANNEL_GREEN
Use only the image's green color channel.
@ CHANNEL_BLUE
Use only the image's blue color channel.
@ CHANNEL_ALPHA
Use only the image's alpha channel.
@ CHANNEL_RED
Use only the image's red color channel.
@ CHANNEL_COLOR
Use the image's full RGB color information.