26#include <OgreTextureManager.h>
27#include <OgreTechnique.h>
28#include <OgreMaterialManager.h>
30#include <OgreFontManager.h>
31#include <OgreHardwarePixelBuffer.h>
32#include <OgreResourceGroupManager.h>
33#include <OgreRTShaderSystem.h>
69 for (
int i = 0; i <= 2; i++)
74 MapUV[i] = Vector2::ZERO;
94 Report(
"Loading default textures...");
97 LoadTexture(
"data/gray2-sm.jpg",
"ConnectionWall", 1, 1);
98 LoadTexture(
"data/metal1-sm.jpg",
"Connection", 1, 1);
117 Ogre::TextureManager::getSingleton().remove(
manual_textures[i]->getHandle());
123bool TextureManager::LoadTexture(
const std::string &filename,
const std::string &name,
Real widthmult,
Real heightmult,
bool enable_force,
bool force_mode,
int mipmaps,
bool use_alpha_color, Ogre::ColourValue alpha_color)
129 bool has_alpha =
false;
130 Ogre::TexturePtr mTex =
LoadTexture(filename2, mipmaps, has_alpha, use_alpha_color, alpha_color);
134 std::string texturename = mTex->getName();
147 RegisterTextureInfo(name,
"", filename, widthmult, heightmult, enable_force, force_mode, mTex->getSize(), mMat->getSize());
150 Report(
"Loaded texture '" + filename2 +
"' as '" + matname +
"', size " +
ToString((
int)mTex->getSize()));
152 Report(
"Loaded texture '" + filename2 +
"' as '" + matname +
"'");
157bool TextureManager::LoadAnimatedTexture(std::vector<std::string> filenames,
const std::string &name,
Real duration,
Real widthmult,
Real heightmult,
bool enable_force,
bool force_mode,
int mipmaps,
bool use_alpha_color, Ogre::ColourValue alpha_color)
159 std::vector<std::string> filenames2;
161 size_t num_frames = filenames.size();
164 for (
size_t i = 0; i < filenames.size(); i++)
170 bool has_alpha =
false;
173 for (
size_t i = 0; i < filenames2.size(); i++)
175 bool has_alpha2 =
false;
178 Ogre::TexturePtr mTex =
LoadTexture(filenames2[i], mipmaps, has_alpha2, use_alpha_color, alpha_color);
182 std::string texturename = mTex->getName();
184 if (has_alpha2 ==
true)
187 size += mTex->getSize();
190 Report(
"Loaded texture " + filenames2[i] +
", size " +
ToString((
int)mTex->getSize()));
203 std::string *namelist =
new std::string[num_frames];
204 for (
size_t i = 0; i < num_frames; i++)
205 namelist[i] = filenames2[i];
206 state->setAnimatedTextureName(namelist, num_frames, duration);
213 RegisterTextureInfo(name,
"",
"", widthmult, heightmult, enable_force, force_mode, size, mMat->getSize());
216 Report(
"Loaded animated texture " + matname);
221bool TextureManager::LoadAlphaBlendTexture(
const std::string &filename,
const std::string &specular_filename,
const std::string &blend_filename,
const std::string &name,
bool spherical,
Real widthmult,
Real heightmult,
bool enable_force,
bool force_mode,
int mipmaps,
bool use_alpha_color, Ogre::ColourValue alpha_color)
225 std::string specular_filename2 =
sbs->
VerifyFile(specular_filename);
226 std::string blend_filename2 =
sbs->
VerifyFile(blend_filename);
229 bool has_alpha =
false, has_alpha2 =
false;
230 Ogre::TexturePtr mTex =
LoadTexture(filename2, mipmaps, has_alpha, use_alpha_color, alpha_color);
234 std::string texturename = mTex->getName();
237 mTex =
LoadTexture(specular_filename2, mipmaps, has_alpha2, use_alpha_color, alpha_color);
240 return ReportError(
"Error loading texture" + specular_filename2);
241 std::string specular_texturename = mTex->getName();
244 mTex =
LoadTexture(blend_filename2, mipmaps, has_alpha2, use_alpha_color, alpha_color);
247 return ReportError(
"Error loading texture" + blend_filename2);
248 std::string blend_texturename = mTex->getName();
260 spec_state->setColourOperation(Ogre::LBO_ALPHA_BLEND);
264 blend_state->setColourOperationEx(Ogre::LBX_BLEND_CURRENT_ALPHA);
265 if (spherical ==
true)
266 blend_state->setEnvironmentMap(
true, Ogre::TextureUnitState::ENV_CURVED);
268 blend_state->setEnvironmentMap(
true, Ogre::TextureUnitState::ENV_PLANAR);
272 RegisterTextureInfo(name,
"", filename, widthmult, heightmult, enable_force, force_mode, mTex->getSize(), mMat->getSize());
275 Report(
"Loaded alpha blended texture '" + filename2 +
"' as '" + matname +
"'");
283 Ogre::MaterialPtr mMat;
284 std::string matname = materialname;
292 mMat->setCullingMode(Ogre::CULL_ANTICLOCKWISE);
295 RegisterTextureInfo(name, materialname,
"", widthmult, heightmult, enable_force, force_mode, 0, mMat->getSize());
298 Report(
"Loaded material " + matname);
303void TextureManager::RegisterTextureInfo(
const std::string &name,
const std::string &material_name,
const std::string &filename,
Real widthmult,
Real heightmult,
bool enable_force,
bool force_mode,
size_t tex_size,
size_t mat_size)
308 if (widthmult == 0.0)
310 if (heightmult == 0.0)
351 Ogre::TextureManager::getSingleton().remove(wrapper);
354 Report(
"Unloaded texture " + name);
366 Ogre::MaterialManager::getSingleton().remove(wrapper);
369 Report(
"Unloaded material " + name);
378 Ogre::ColourValue alpha_color = Ogre::ColourValue::Black;
380 bool use_alpha_color =
false;
386 bool has_alpha =
false;
387 Ogre::TexturePtr mTex =
LoadTexture(filename2, mipmaps, has_alpha, use_alpha_color, alpha_color);
398 width = (int)mTex->getWidth();
400 height = (int)mTex->getHeight();
402 if (x > (
int)mTex->getWidth() || y > (int)mTex->getHeight())
403 return ReportError(
"LoadTextureCropped: invalid coordinates for '" + name +
"'");
404 if (x + width > (
int)mTex->getWidth() || y + height > (int)mTex->getHeight())
405 return ReportError(
"LoadTextureCropped: invalid size for '" + name +
"'");
408 Ogre::PixelFormat format = Ogre::PF_X8R8G8B8;
409 if (has_alpha ==
true)
410 format = Ogre::PF_A8R8G8B8;
414 Ogre::TexturePtr new_texture;
417 new_texture = Ogre::TextureManager::getSingleton().createManual(texturename,
"General", Ogre::TEX_TYPE_2D, width, height, Ogre::MIP_UNLIMITED, format, Ogre::TU_DEFAULT);
421 catch (Ogre::Exception &e)
423 ReportError(
"Error creating new texture " + texturename +
"\n" + e.getDescription());
428 Ogre::Box source (x, y, x + width, y + height);
429 Ogre::Box dest (0, 0, width, height);
439 Report(
"Loaded cropped texture '" + filename2 +
"' as '" + name +
"', size " +
ToString((
int)new_texture->getSize()));
442 RegisterTextureInfo(name,
"", filename, widthmult, heightmult, enable_force, force_mode, mTex->getSize(), mMat->getSize());
456 return ReportError(
"RotateTexture: invalid texture " + texname +
"\n");
469 state->setTextureRotate(
Degree(-angle));
485 return ReportError(
"RotateAnimTexture: invalid texture " + texname +
"\n");
498 state->setRotateAnimation(speed);
514 return ReportError(
"ScrollTexture: invalid texture " + texname +
"\n");
527 state->setTextureScroll(-x_offset, y_offset);
543 return ReportError(
"ScrollAnimTexture: invalid texture " + texname +
"\n");
556 state->setScrollAnimation(x_speed, -y_speed);
572 return ReportError(
"ScaleTexture: invalid texture " + texname +
"\n");
585 state->setTextureScale(x_scale, y_scale);
606 return ReportError(
"TransformTexture: invalid texture " + texname +
"\n");
620 Ogre::TextureUnitState::TextureTransformType type;
621 if (xform_type ==
"scroll_x")
622 type = Ogre::TextureUnitState::TT_TRANSLATE_U;
623 else if (xform_type ==
"scroll_y")
624 type = Ogre::TextureUnitState::TT_TRANSLATE_V;
625 else if (xform_type ==
"rotate")
626 type = Ogre::TextureUnitState::TT_ROTATE;
627 else if (xform_type ==
"scale_x")
628 type = Ogre::TextureUnitState::TT_SCALE_U;
629 else if (xform_type ==
"scale_y")
630 type = Ogre::TextureUnitState::TT_SCALE_V;
634 Ogre::WaveformType wavetype;
636 wavetype = Ogre::WFT_SINE;
637 else if (wave ==
"triangle")
638 wavetype = Ogre::WFT_TRIANGLE;
639 else if (wave ==
"square")
640 wavetype = Ogre::WFT_SQUARE;
641 else if (wave ==
"sawtooth")
642 wavetype = Ogre::WFT_SAWTOOTH;
643 else if (wave ==
"inverse_sawtooth")
644 wavetype = Ogre::WFT_INVERSE_SAWTOOTH;
645 else if (wave ==
"pwm")
646 wavetype = Ogre::WFT_PWM;
650 state->setTransformAnimation(type, wavetype, base, frequency, phase, amplitude);
658bool TextureManager::AddTextToTexture(
const std::string &origname,
const std::string &name,
const std::string &font_filename,
Real font_size,
const std::string &text,
int x1,
int y1,
int x2,
int y2,
const std::string &h_align,
const std::string &v_align,
int ColorR,
int ColorG,
int ColorB,
bool enable_force,
bool force_mode)
668 std::string hAlign = h_align;
669 std::string vAlign = v_align;
678 std::string fontname = font_filename2 +
ToString(font_size);
679 font = Ogre::FontManager::getSingleton().getByName(fontname,
"General");
686 font = Ogre::FontManager::getSingleton().create(fontname,
"General");
688 font->setSource(font_filename2);
689 font->setTrueTypeSize(font_size);
690 font->setTrueTypeResolution(96);
692 font->addCodePointRange(Ogre::Font::CodePointRange(32, 126));
699 Report(
"Font " + font_filename2 +
" loaded as size " +
ToString(font_size) +
", with texture size " +
ToString((
int)fontTexture->getSize()));
701 catch (Ogre::Exception &e)
706 if (Ogre::TextureManager::getSingleton().getByName(fontname +
"Texture"))
707 return ReportError(
"Error loading font " + fontname +
"\n" + e.getDescription());
709 if (Ogre::FontManager::getSingleton().getByHandle(font->getHandle()))
710 return ReportError(
"Error loading font " + fontname +
"\n" + e.getDescription());
712 Ogre::TextureManager::getSingleton().remove(fontname +
"Texture");
713 Ogre::FontManager::getSingleton().remove(font->getHandle());
715 return ReportError(
"Error loading font " + fontname +
"\n" + e.getDescription());
722 return ReportError(
"AddTextToTexture: Invalid original material '" + Origname +
"'");
727 return ReportError(
"AddTextToTexture: Invalid original texture '" + texname +
"'");
729 bool has_alpha = background->hasAlpha();
732 Real widthmult, heightmult;
736 int width = (int)background->getWidth();
737 int height = (int)background->getHeight();
740 Ogre::PixelFormat format = Ogre::PF_X8R8G8B8;
741 if (has_alpha ==
true)
742 format = Ogre::PF_A8R8G8B8;
746 Ogre::TexturePtr texture;
749 texture = Ogre::TextureManager::getSingleton().createManual(texturename,
"General", Ogre::TEX_TYPE_2D, width, height, Ogre::MIP_UNLIMITED, format, Ogre::TU_STATIC|Ogre::TU_AUTOMIPMAP);
753 catch (Ogre::Exception &e)
755 ReportError(
"Error creating new texture " + texturename +
"\n" + e.getDescription());
760 width = (int)texture->getWidth();
761 height = (int)texture->getHeight();
779 if (hAlign ==
"left")
781 if (hAlign ==
"right")
786 if (vAlign ==
"bottom")
794 bool result =
WriteToTexture(Text, texture, x1, y1, x2, y2, font, Ogre::ColourValue((
float)red, (
float)green, (
float)blue, 1.0), align, valign);
805 RegisterTextureInfo(name,
"",
"", widthmult, heightmult, enable_force, force_mode, texture->getSize(), mMat->getSize());
808 Report(
"AddTextToTexture: created texture '" +
Name +
"'");
814bool TextureManager::AddTextureOverlay(
const std::string &orig_texture,
const std::string &overlay_texture,
const std::string &name,
int x,
int y,
int width,
int height,
Real widthmult,
Real heightmult,
bool enable_force,
bool force_mode)
819 std::string
Name = name;
820 std::string Origname = orig_texture;
821 std::string Overlay = overlay_texture;
827 return ReportError(
"AddTextureOverlay: Invalid original material '" + Origname +
"'");
833 return ReportError(
"AddTextureOverlay: Invalid original texture '" + texname +
"'");
835 bool has_alpha = image1->hasAlpha();
841 return ReportError(
"AddTextureOverlay: Invalid overlay material '" + Overlay +
"'");
847 return ReportError(
"AddTextureOverlay: Invalid overlay texture '" + texname +
"'");
855 width = (int)image2->getWidth();
857 height = (int)image2->getHeight();
859 if (x > (
int)image1->getWidth() || y > (int)image1->getHeight())
860 return ReportError(
"AddTextureOverlay: invalid coordinates for '" +
Name +
"'");
861 if (x + width > (
int)image1->getWidth() || y + height > (int)image1->getHeight())
862 return ReportError(
"AddTextureOverlay: invalid size for '" +
Name +
"'");
865 Ogre::PixelFormat format = Ogre::PF_X8R8G8B8;
866 if (has_alpha ==
true)
867 format = Ogre::PF_A8R8G8B8;
871 Ogre::TexturePtr new_texture;
874 new_texture = Ogre::TextureManager::getSingleton().createManual(texturename,
"General", Ogre::TEX_TYPE_2D, (Ogre::uint)image1->getWidth(), (Ogre::uint)image1->getHeight(), Ogre::MIP_UNLIMITED, format, Ogre::TU_DEFAULT);
878 catch (Ogre::Exception &e)
880 ReportError(
"Error creating new texture " + texturename +
"\n" + e.getDescription());
885 Ogre::Box source (x, y, x + width, y + height);
886 Ogre::Box source_full (0, 0, image1->getWidth(), image1->getHeight());
887 Ogre::Box overlay (0, 0, image2->getWidth(), image2->getHeight());
888 CopyTexture(image1, new_texture, source_full, source_full);
898 Report(
"AddTextureOverlay: created texture '" +
Name +
"'");
901 RegisterTextureInfo(name,
"",
"", widthmult, heightmult, enable_force, force_mode, new_texture->getSize(), mMat->getSize());
914 std::string matname = name;
941 if (polygon_name !=
"")
942 message =
"Texture '" + matname +
"' not found for polygon '" + polygon_name +
"'; using default material";
944 message =
"Texture '" + matname +
"' not found; using default material";
968 for (
int i = 0; i <= 5; i++)
978 for (
int i = 0; i <= 5; i++)
994 if (info == 1 || info == 3)
996 if (info == 2 || info == 3)
1059 if (iswidth ==
true)
1061 if ((
AutoX ==
true && enable_force ==
false) || (enable_force ==
true && force_mode ==
true))
1062 return std::abs(n1 - n2) * offset;
1068 if ((
AutoY ==
true && enable_force ==
false) || (enable_force ==
true && force_mode ==
true))
1069 return std::abs(n1 - n2) * offset;
1094 Real tw2 = tw, th2 = th;
1096 bool force_enable =
false, force_mode =
false;
1099 Real width = v1.distance(v2);
1100 Real height = v2.distance(v3);
1102 tw2 =
AutoSize(0, width,
true, tw, force_enable, force_mode);
1103 th2 =
AutoSize(0, height,
false, th, force_enable, force_mode);
1120 bool rev_x =
false, rev_y =
false, rev_z =
false;
1125 Vector3 normal = plane.normal;
1129 if (std::abs (normal.y) > std::abs (normal.x) && std::abs (normal.y) > std::abs (normal.z))
1131 else if (std::abs (normal.z) > std::abs (normal.x))
1133 else if (normal.x == 0)
1136 size_t selX = (1 << direction) & 0x3;
1137 size_t selY = (1 << selX) & 0x3;
1139 varray.reserve(vertices.size());
1140 for (
size_t i = 0; i < vertices.size(); i++)
1142 varray.emplace_back(
Vector3(vertices[i][selX], vertices[i][selY], 0));
1148 if (direction == 0 && normal.x > 0)
1151 if (direction == 1 && normal.y > 0)
1154 if (direction == 2 && normal.z < 0)
1159 if (direction == 0 && normal.x < 0)
1165 if (direction == 1 && normal.y > 0)
1174 if (direction == 2 && normal.z > 0)
1297 v1.x = -((normal.y * v1.y) + (normal.z * v1.z) + plane.d) / normal.x;
1298 v2.x = -((normal.y * v2.y) + (normal.z * v2.z) + plane.d) / normal.x;
1299 v3.x = -((normal.y * v3.y) + (normal.z * v3.z) + plane.d) / normal.x;
1306 v1.y = -((normal.x * v1.x) + (normal.z * v1.z) + plane.d) / normal.y;
1307 v2.y = -((normal.x * v2.x) + (normal.z * v2.z) + plane.d) / normal.y;
1308 v3.y = -((normal.x * v3.x) + (normal.z * v3.z) + plane.d) / normal.y;
1315 v1.z = -((normal.x * v1.x) + (normal.y * v1.y) + plane.d) / normal.z;
1316 v2.z = -((normal.x * v2.x) + (normal.y * v2.y) + plane.d) / normal.z;
1317 v3.z = -((normal.x * v3.x) + (normal.y * v3.y) + plane.d) / normal.z;
1333 Vector3 normal = plane.normal;
1337 if (std::abs (normal.y) > std::abs (normal.x) && std::abs (normal.y) > std::abs (normal.z))
1339 else if (std::abs (normal.z) > std::abs (normal.x))
1341 else if (normal.x == 0)
1348 for (
int i = 0; i < 3; i++)
1350 for (
int j = 0; j < 3; j++)
1363 size_t location =
string.find(
"x");
1364 if (location != std::string::npos)
1366 std::string number =
string.substr(location + 1);
1367 if (
ToInt(number) < (
int)vertices.size())
1374 location =
string.find(
"y");
1375 if (location != std::string::npos)
1377 std::string number =
string.substr(location + 1);
1378 if (
ToInt(number) < (
int)vertices.size())
1385 location =
string.find(
"z");
1386 if (location != std::string::npos)
1388 std::string number =
string.substr(location + 1);
1389 if (
ToInt(number) < (
int)vertices.size())
1429 Vector3 normal = plane.normal;
1433 if (std::abs (normal.y) > std::abs (normal.x) && std::abs (normal.y) > std::abs (normal.z))
1435 else if (std::abs (normal.z) > std::abs (normal.x))
1437 else if (normal.x == 0)
1447 if (todefaults ==
true)
1454 SetTextureMapping2(
"x0",
"y0",
"z0",
Vector2(0, 0),
"x1",
"y1",
"z1",
Vector2(1, 0),
"x2",
"y2",
"z2",
Vector2(1, 1));
1463 SetTextureMapping2(
OldMapVerts1[0],
OldMapVerts1[1],
OldMapVerts1[2],
OldMapUV[0],
OldMapVerts2[0],
OldMapVerts2[1],
OldMapVerts2[2],
OldMapUV[1],
OldMapVerts3[0],
OldMapVerts3[1],
OldMapVerts3[2],
OldMapUV[2]);
1517void TextureManager::SetTextureMapping2(
const std::string & x1,
const std::string & y1,
const std::string & z1,
Vector2 uv1,
const std::string & x2,
const std::string & y2,
const std::string & z2,
Vector2 uv2,
const std::string & x3,
const std::string & y3,
const std::string & z3,
Vector2 uv3)
1552 for (
int i = 0; i <= 2; i++)
1564 for (
int i = 0; i <= 2; i++)
1587void TextureManager::SetTextureOverride(
const std::string &mainneg,
const std::string &mainpos,
const std::string &sideneg,
const std::string &sidepos,
const std::string &top,
const std::string &bottom)
1605 if (show_filename ==
true)
1606 list.append(
"Name --- Filename\n\n");
1611 if (show_filename ==
true)
1695 using namespace Ogre;
1698 Ogre::ColourValue keyCol2 = keyCol;
1702 size_t pos = filename.find_last_of(
".");
1703 if (pos != String::npos && pos < (filename.length() - 1))
1704 strExt = filename.substr(pos+1);
1707 DataStreamPtr encoded = ResourceGroupManager::getSingleton().openResource(filename, resGroup);
1709 if (strExt ==
"gif")
1712 uchar enabled = 0, trans_color = 0, check = 0;
1716 for (
size_t i = 0; i < encoded->size(); i++)
1719 if (found == 3 && i < encoded->size() - 4)
1725 unsigned char transparent : 1;
1726 unsigned char userinput : 1;
1727 unsigned char disposal : 3;
1728 unsigned char reserved : 3;
1733 encoded->read(&flags, 1);
1734 enabled = flags.transparent;
1735 encoded->seek(i + 3);
1736 encoded->read(&trans_color, 1);
1741 encoded->read(&check, 1);
1744 if (check == 33 && i < encoded->size() - 4)
1751 encoded->read(&check, 1);
1758 encoded->read(&check, 1);
1768 Ogre::uint pos = (Ogre::uint(trans_color) * 3) + 13;
1770 encoded->read(&r, 1);
1771 encoded->seek(pos + 1);
1772 encoded->read(&g, 1);
1773 encoded->seek(pos + 2);
1774 encoded->read(&b, 1);
1775 keyCol2.r = float(r / 255);
1776 keyCol2.g = float(g / 255);
1777 keyCol2.b = float(b / 255);
1781 srcImg.load(encoded, strExt);
1783 unsigned int width = (
unsigned int)srcImg.getWidth(), height = (
unsigned int)srcImg.getHeight();
1785 uchar* pixelData = OGRE_ALLOC_T(uchar, PixelUtil::getMemorySize(width, height, 1, PF_A8R8G8B8), MEMCATEGORY_GENERAL);
1786 unsigned long pxDataIndex = 0, pxDataIndexStep = (
unsigned long)PixelUtil::getNumElemBytes(PF_A8R8G8B8);
1788 for(
unsigned int y = 0; y < height; y++)
1790 for(
unsigned int x = 0; x < width; x++)
1792 ColourValue pixCol = srcImg.getColourAt(x, y, 0);
1793 ColourValue diffCol = pixCol - keyCol2;
1794 pixCol.a = ((std::abs(diffCol.r) < threshold) && (std::abs(diffCol.g) < threshold) && (std::abs(diffCol.b) < threshold)) ? 0.0f : 1.0f;
1795 Ogre::PixelUtil::packColour(pixCol, PF_A8R8G8B8,
static_cast<void*
>(pixelData + pxDataIndex));
1796 pxDataIndex += pxDataIndexStep;
1800 Image chromaKeyedImg;
1801 chromaKeyedImg.loadDynamicImage(pixelData, width, height, 1, PF_A8R8G8B8,
true);
1804 Ogre::TexturePtr mTex = Ogre::TextureManager::getSingleton().loadImage(name, resGroup, chromaKeyedImg, TEX_TYPE_2D, numMipmaps);
1809bool TextureManager::WriteToTexture(
const std::string &str, Ogre::TexturePtr destTexture,
int destLeft,
int destTop,
int destRight,
int destBottom, Ogre::FontPtr font,
const Ogre::ColourValue &color,
char justify,
char vert_justify,
bool wordwrap)
1814 using namespace Ogre;
1816 if ((
int)destTexture->getHeight() < destBottom - 1)
1817 destBottom = destTexture->getHeight() - 1;
1818 if ((
int)destTexture->getWidth() < destRight - 1)
1819 destRight = destTexture->getWidth() - 1;
1823 if (!font->isLoaded())
1826 catch (Ogre::Exception &e)
1828 return ReportError(
"Error loading font " + font->getName() +
"\n" + e.getDescription());
1840 HardwarePixelBufferSharedPtr fontBuffer = fontTexture->getBuffer();
1841 HardwarePixelBufferSharedPtr destBuffer = destTexture->getBuffer();
1843 PixelBox destPb = destBuffer->lock(Box(0, 0, destTexture->getWidth() - 1, destTexture->getHeight() - 1), HardwareBuffer::HBL_NORMAL);
1862 size_t nBuffSize = fontBuffer->getSizeInBytes();
1863 textureboxes[index].buffer = (
unsigned char*)calloc(nBuffSize,
sizeof(
unsigned char));
1866 textureboxes[index].box =
new Ogre::PixelBox(fontBuffer->getWidth(), fontBuffer->getHeight(), fontBuffer->getDepth(), fontBuffer->getFormat(),
textureboxes[index].buffer);
1870 unsigned char* fontData =
static_cast<unsigned char*
>(
textureboxes[index].box->data);
1871 unsigned char* destData =
static_cast<unsigned char*
>(destPb.data);
1873 const int fontPixelSize = (int)PixelUtil::getNumElemBytes(
textureboxes[index].box->format);
1874 const int destPixelSize = (int)PixelUtil::getNumElemBytes(destPb.format);
1876 const int fontRowPitchBytes = (int)
textureboxes[index].box->rowPitch * fontPixelSize;
1877 const int destRowPitchBytes = (int)destPb.rowPitch * destPixelSize;
1879 Box *GlyphTexCoords;
1880 GlyphTexCoords =
new Box[str.size()];
1882 Font::UVRect glypheTexRect;
1886 for(
size_t i = 0; i < str.size(); i++)
1888 if ((str[i] !=
'\t') && (str[i] !=
'\n') && (str[i] !=
' '))
1890 glypheTexRect = font->getGlyphTexCoords(str[i]);
1891 GlyphTexCoords[i].left = uint32_t(glypheTexRect.left * fontTexture->getSrcWidth());
1892 GlyphTexCoords[i].top = uint32_t(glypheTexRect.top * fontTexture->getSrcHeight());
1893 GlyphTexCoords[i].right = uint32_t(glypheTexRect.right * fontTexture->getSrcWidth());
1894 GlyphTexCoords[i].bottom = uint32_t(glypheTexRect.bottom * fontTexture->getSrcHeight());
1896 if (glypheTexRect.right < glypheTexRect.left || glypheTexRect.bottom < glypheTexRect.top)
1897 return ReportError(
"Error processing font - invalid texture coordinates for font " + font->getName());
1900 size_t lastline = 0;
1901 for (
size_t j = 0; j < GlyphTexCoords[i].getHeight(); j++)
1903 for (
size_t k = 0; k < GlyphTexCoords[i].getWidth(); k++)
1905 float alpha = color.a * (fontData[(j + GlyphTexCoords[i].top) * fontRowPitchBytes + (k + GlyphTexCoords[i].left) * fontPixelSize + 1] / 255.0f);
1910 GlyphTexCoords[i].bottom = GlyphTexCoords[i].top + lastline + 1;
1912 if ((
int)GlyphTexCoords[i].getHeight() > charheight)
1913 charheight = (
int)GlyphTexCoords[i].getHeight();
1914 if ((
int)GlyphTexCoords[i].getWidth() > charwidth)
1915 charwidth = (
int)GlyphTexCoords[i].getWidth();
1922 bool carriagereturn =
true;
1923 for (
size_t strindex = 0; strindex < str.size(); strindex++)
1925 switch(str[strindex])
1928 cursorX += charwidth;
1931 cursorX += charwidth * 3;
1934 cursorY += charheight; carriagereturn =
true;
1948 int l = (int)strindex;
1952 while((l < (
int)str.size()) && (str[l] !=
'\n'))
1959 wordwidth = charwidth; ++l;
1962 wordwidth = charwidth *3; ++l;
1965 l = (int)str.size();
1970 while((l < (
int)str.size()) && (str[l] !=
' ') && (str[l] !=
'\t') && (str[l] !=
'\n'))
1972 wordwidth += (int)GlyphTexCoords[l].getWidth();
1978 wordwidth += (int)GlyphTexCoords[l].getWidth();
1982 textwidth += wordwidth;
1986 textwidth = destRight - destLeft;
1991 cursorX = destLeft + ((destRight - destLeft) / 2) - (textwidth / 2);
1996 cursorX = destRight - textwidth;
2001 cursorX = destRight;
2006 switch (vert_justify)
2009 cursorY = destTop + ((destBottom - destTop) / 2) - (charheight / 2);
2013 cursorY = destBottom - charheight + cursorY;
2018 carriagereturn =
false;
2022 if ((cursorY + charheight) >= (int)destTexture->getHeight())
2024 Report(
"Text '" + str +
"' out of bounds\n");
2030 for (
size_t i = 0; i < GlyphTexCoords[strindex].getHeight(); i++)
2032 for (
size_t j = 0; j < GlyphTexCoords[strindex].getWidth(); j++)
2034 float alpha = color.a * (fontData[(i + GlyphTexCoords[strindex].top) * fontRowPitchBytes + (j + GlyphTexCoords[strindex].left) * fontPixelSize + 1] / 255.0f);
2035 float invalpha = 1.0f - alpha;
2036 int offset = (i + cursorY) * destRowPitchBytes + (j + cursorX) * destPixelSize;
2040 PixelUtil::unpackColour(&pix, destPb.format, &destData[offset]);
2041 pix = (pix * invalpha) + (color * alpha);
2042 PixelUtil::packColour(pix, destPb.format, &destData[offset]);
2047 cursorX += (int)GlyphTexCoords[strindex].getWidth();
2054 delete[] GlyphTexCoords;
2056 destBuffer->unlock();
2065 texture->convertToImage(image,
true);
2066 image.save(filename);
2098Ogre::TexturePtr
TextureManager::LoadTexture(
const std::string &filename,
int mipmaps,
bool &has_alpha,
bool use_alpha_color, Ogre::ColourValue alpha_color)
2101 Ogre::TextureManager::getSingleton().setVerbose(
sbs->
Verbose);
2107 std::string filename2 = filename;
2110 std::string extension = filename2.substr(filename.size() - 3);
2112 if (extension ==
"gif")
2113 use_alpha_color =
true;
2117 Ogre::TexturePtr mTex;
2118 std::string texturename;
2123 if (use_alpha_color ==
false)
2131 mTex = Ogre::TextureManager::getSingleton().load(filename2, path, Ogre::TEX_TYPE_2D, mipmaps);
2140 texturename = mTex->getName();
2141 has_alpha = mTex->hasAlpha();
2147 texturename =
"kc_" + filename2;
2164 catch (Ogre::Exception &e)
2169 Ogre::TextureManager::getSingleton().remove(wrapper);
2171 ReportError(
"Error loading texture " + filename2 +
"\n" + e.getDescription());
2185 Ogre::MaterialPtr mMat;
2190 catch (Ogre::Exception& e)
2192 ReportError(
"Error creating material for texture " + name +
"\n" + e.getDescription());
2202 mMat->setCullingMode(Ogre::CULL_ANTICLOCKWISE);
2215 mat->setLightingEnabled(
false);
2218 mat->setLightingEnabled(value);
2226 Ogre::MaterialPtr ptr = 0;
2228 if (Ogre::ResourceGroupManager::getSingleton().resourceGroupExists(group) ==
false)
2237 Ogre::TextureUnitState *state = mMat->getTechnique(0)->getPass(0)->createTextureUnitState(texture_name);
2240 if (has_alpha ==
true)
2246 mMat->getTechnique(0)->getPass(0)->setAlphaRejectSettings(Ogre::CMPF_GREATER_EQUAL, 128);
2254 return mMat->getTechnique(0)->getPass(0)->getTextureUnitState(0);
2259 std::string texname =
"";
2263 texname = state->getTextureName();
2270 Ogre::TexturePtr ptr = 0;
2272 if (Ogre::ResourceGroupManager::getSingleton().resourceGroupExists(group) ==
false)
2275 ptr = Ogre::TextureManager::getSingleton().getByName(name, group);
2293 Ogre::Box srcbox (0, 0, 0, source->getWidth(), source->getHeight(), source->getDepth());
2294 Ogre::Box dstbox (0, 0, 0, destination->getWidth(), destination->getHeight(), destination->getDepth());
2308 if (srcBox.getWidth() == source->getWidth() &&
2309 srcBox.getHeight() == source->getHeight() &&
2310 dstBox.getWidth() == destination->getWidth() &&
2311 dstBox.getHeight() == destination->getHeight() &&
2312 sbs->
mRoot->getRenderSystem()->getName() !=
"Direct3D9 Rendering Subsystem" &&
2313 sbs->
mRoot->getRenderSystem()->getName() !=
"Direct3D11 Rendering Subsystem")
2315 source->copyToTexture(destination);
2319 Ogre::HardwarePixelBufferSharedPtr buffer = source->getBuffer();
2321 if (
sbs->
mRoot->getRenderSystem()->getName() ==
"Direct3D11 Rendering Subsystem")
2324 buffer->lock(srcBox, Ogre::HardwareBuffer::HBL_READ_ONLY);
2325 const Ogre::PixelBox& pb = buffer->getCurrentLock();
2326 destination->getBuffer()->blitFromMemory(pb, dstBox);
2332 destination->getBuffer()->blit(buffer, srcBox, dstBox);
2335 catch (Ogre::Exception& e)
2337 ReportError(
"Error copying texture\n" + e.getDescription());
2370 mMat->setReceiveShadows(value);
2386 mat->setCullingMode(Ogre::CullingMode::CULL_NONE);
2388 mat->setCullingMode(Ogre::CullingMode::CULL_ANTICLOCKWISE);
2390 mat->setCullingMode(Ogre::CullingMode::CULL_CLOCKWISE);
2393 ReportError(
"SetCulling: Material " + material_name +
" not found");
virtual bool ReportError(const std::string &message)
virtual void Report(const std::string &message)
void SetName(const std::string &name)
std::string GetMountPath(std::string filename, std::string &newfilename)
void CacheFilename(const std::string &filename, const std::string &result)
bool GetConfigBool(const std::string &key, bool default_value)
int GetConfigInt(const std::string &key, int default_value)
void SetLighting(Real red=1.0, Real green=1.0, Real blue=1.0)
std::string VerifyFile(const std::string &filename)
bool LoadTextureCropped(const std::string &filename, const std::string &name, int x, int y, int width, int height, Real widthmult, Real heightmult, bool enable_force=false, bool force_mode=false)
bool LoadTexture(const std::string &filename, const std::string &name, Real widthmult, Real heightmult, bool enable_force=false, bool force_mode=false, int mipmaps=-1, bool use_alpha_color=false, Ogre::ColourValue alpha_color=Ogre::ColourValue::Black)
void CopyTexture(Ogre::TexturePtr source, Ogre::TexturePtr destination)
bool ScrollTexture(const std::string &name, Real x_offset, Real y_offset)
std::vector< Real > widthscale
void EnableShadows(const std::string &material_name, bool value)
bool UnloadMaterial(const std::string &name, const std::string &group)
bool GetTextureForce(const std::string &texture, bool &enable_force, bool &force_mode)
Ogre::MaterialPtr CreateMaterial(const std::string &name, const std::string &path)
std::vector< std::string > OldMapVerts3
bool GetTextureImage(Ogre::TexturePtr texture)
void DecrementMaterialCount()
bool RotateTexture(const std::string &name, Real angle)
bool AddTextToTexture(const std::string &origname, const std::string &name, const std::string &font_filename, Real font_size, const std::string &text, int x1, int y1, int x2, int y2, const std::string &h_align, const std::string &v_align, int ColorR, int ColorG, int ColorB, bool enable_force=false, bool force_mode=false)
void SetAutoSize(bool x, bool y)
void ProcessTextureFlip(Real tw, Real th)
void SetTextureMapping(int vertindex1, Vector2 uv1, int vertindex2, Vector2 uv2, int vertindex3, Vector2 uv3)
bool ScrollAnimTexture(const std::string &name, Real x_speed, Real y_speed)
std::string ListTextures(bool show_filename=false)
std::vector< TexturePixelBox > textureboxes
TextureManager(Object *parent)
std::string prev_material
void GetAutoSize(bool &x, bool &y)
void SaveTexture(Ogre::TexturePtr texture, const std::string &filename)
Ogre::TextureUnitState * GetTextureUnitState(Ogre::MaterialPtr mMat)
std::vector< std::string > MapVerts1
bool TransformTexture(const std::string &name, const std::string &type, const std::string &wave_type, Real base, Real frequency, Real phase, Real amplitude)
std::vector< int > MapIndex
bool LoadAnimatedTexture(std::vector< std::string > filenames, const std::string &name, Real duration, Real widthmult, Real heightmult, bool enable_force=false, bool force_mode=false, int mipmaps=-1, bool use_alpha_color=false, Ogre::ColourValue alpha_color=Ogre::ColourValue::Black)
void SetPlanarRotate(bool value)
Real AutoSize(Real n1, Real n2, bool iswidth, Real offset, bool enable_force, bool force_mode)
std::vector< Ogre::TexturePtr > manual_textures
std::vector< std::string > OldMapVerts2
void SetTextureMapping2(const std::string &x1, const std::string &y1, const std::string &z1, Vector2 uv1, const std::string &x2, const std::string &y2, const std::string &z2, Vector2 uv2, const std::string &x3, const std::string &y3, const std::string &z3, Vector2 uv3)
bool GetTextureMapping(PolyArray &vertices, Vector3 &v1, Vector3 &v2, Vector3 &v3, int &direction)
int GetTextureInfoCount()
void GetPlanarMapping(bool &flat, bool &FlipX, bool &FlipY, bool &FlipZ, bool &rotate)
bool LoadMaterial(const std::string &filename, const std::string &name, Real widthmult, Real heightmult, bool enable_force=false, bool force_mode=false)
void IncrementMaterialCount()
Ogre::TexturePtr GetTextureByName(const std::string &name, const std::string &group="General")
std::vector< Vector2 > OldMapUV
void SetCulling(const std::string &material_name, int mode=1)
bool UnloadTexture(const std::string &name, const std::string &group)
bool SetTextureInfo(int index, TextureInfo &info)
bool GetTextureInfo(int index, TextureInfo &info)
void DecrementTextureCount()
bool RotateAnimTexture(const std::string &name, Real speed)
std::vector< std::string > OldMapVerts1
Vector2 CalculateSizing(const std::string &texture, const Vector3 &v1, const Vector3 &v2, const Vector3 &v3, int direction, Real tw, Real th)
void SetTextureFlip(int mainneg, int mainpos, int sideneg, int sidepos, int top, int bottom)
std::vector< Vector2 > MapUV
void ResetTextureMapping(bool todefaults=false)
bool AddTextureOverlay(const std::string &orig_texture, const std::string &overlay_texture, const std::string &name, int x, int y, int width, int height, Real widthmult, Real heightmult, bool enable_force=false, bool force_mode=false)
bool WriteToTexture(const std::string &str, Ogre::TexturePtr destTexture, int destLeft, int destTop, int destRight, int destBottom, Ogre::FontPtr font, const Ogre::ColourValue &color, char justify='l', char vert_justify='t', bool wordwrap=true)
std::vector< std::string > MapVerts2
void SetPlanarMapping(bool flat, bool FlipX, bool FlipY, bool FlipZ, bool rotate)
void SetTextureOverride(const std::string &mainneg, const std::string &mainpos, const std::string &sideneg, const std::string &sidepos, const std::string &top, const std::string &bottom)
std::string GetTextureMaterial(const std::string &name, bool &result, bool report=true, const std::string &polygon_name="")
bool LoadAlphaBlendTexture(const std::string &filename, const std::string &specular_filename, const std::string &blend_filename, const std::string &name, bool spherical, Real widthmult, Real heightmult, bool enable_force=false, bool force_mode=false, int mipmaps=-1, bool use_alpha_color=false, Ogre::ColourValue alpha_color=Ogre::ColourValue::Black)
void RegisterTextureInfo(const std::string &name, const std::string &material_name, const std::string &filename, Real widthmult, Real heightmult, bool enable_force, bool force_mode, size_t tex_size, size_t mat_size)
void DecrementTextureUsage(const std::string &name)
std::vector< int > OldMapIndex
bool ScaleTexture(const std::string &name, Real x_scale, Real y_scale)
std::vector< TextureInfo > textureinfo
Ogre::TexturePtr loadChromaKeyedTexture(const std::string &filename, const std::string &resGroup, const std::string &name, const Ogre::ColourValue &keyCol=Ogre::ColourValue::Black, int numMipmaps=-1, Real threshold=0.003)
std::vector< std::string > MapVerts3
std::vector< Real > heightscale
bool UnregisterTextureInfo(std::string name, std::string material_name="")
void IncrementTextureUsage(const std::string &name)
void IncrementTextureCount()
Ogre::TextureUnitState * BindTextureToMaterial(Ogre::MaterialPtr mMat, std::string texture_name, bool has_alpha)
Ogre::MaterialPtr GetMaterialByName(const std::string &name, const std::string &group="General")
void EnableLighting(const std::string &material_name, bool value)
bool GetTextureTiling(const std::string &texture, Real &tw, Real &th)
std::string GetTextureName(Ogre::MaterialPtr mMat)
Vector2 GetExtents(PolyArray &varray, int coord, bool flip_z=false)
Plane ComputePlane(PolyArray &vertices, bool flip_normal=true)
std::vector< Vector3 > PolyArray
void ReplaceAll(std::string &string, const std::string &original, const std::string &replacement)
int ToInt(const std::string &string)
void SetCase(std::string &string, bool uppercase)
std::string ToString(int number)
Real ToFloat(const std::string &string)
void TrimString(std::string &string)
std::string TrimStringCopy(std::string string)
std::string material_name