Skyscraper 2.0
texture.h
Go to the documentation of this file.
1/*
2 Scalable Building Simulator - Texture Manager
3 The Skyscraper Project - Version 2.0
4 Copyright (C)2004-2024 Ryan Thoryk
5 https://www.skyscrapersim.net
6 https://sourceforge.net/projects/skyscraper/
7 Contact - ryan@skyscrapersim.net
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22*/
23
24#ifndef _SBS_TEXTURE_H
25#define _SBS_TEXTURE_H
26
27#include <OgreOverlayPrerequisites.h>
28#include <OgreColourValue.h>
29#include <OgreFont.h>
30
31namespace SBS {
32
34{
35public:
36
37 struct TextureInfo;
38
39 bool TextureOverride; //if enabled, overrides textures with ones set with SetTextureOverride()
40 bool FlipTexture; //if enabled, flips textures according to parameters set in SetTextureFlip()
41
42 TextureManager(Object *parent);
44 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);
45 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);
46 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);
47 bool LoadMaterial(const std::string &filename, const std::string &name, Real widthmult, Real heightmult, bool enable_force = false, bool force_mode = false);
48 bool UnloadTexture(const std::string &name, const std::string &group);
49 bool UnloadMaterial(const std::string &name, const std::string &group);
50 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);
51 bool RotateTexture(const std::string &name, Real angle);
52 bool RotateAnimTexture(const std::string &name, Real speed);
53 bool ScrollTexture(const std::string &name, Real x_offset, Real y_offset);
54 bool ScrollAnimTexture(const std::string &name, Real x_speed, Real y_speed);
55 bool ScaleTexture(const std::string &name, Real x_scale, Real y_scale);
56 bool TransformTexture(const std::string &name, const std::string &type, const std::string &wave_type, Real base, Real frequency, Real phase, Real amplitude);
57 Real AutoSize(Real n1, Real n2, bool iswidth, Real offset, bool enable_force, bool force_mode);
58 void SetTextureMapping(int vertindex1, Vector2 uv1, int vertindex2, Vector2 uv2, int vertindex3, Vector2 uv3);
59 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);
60 void ResetTextureMapping(bool todefaults = false);
61 void SetAutoSize(bool x, bool y);
62 void GetAutoSize(bool &x, bool &y);
63 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);
64 void SetTextureFlip(int mainneg, int mainpos, int sideneg, int sidepos, int top, int bottom);
65 void ProcessTextureFlip(Real tw, Real th);
66 bool GetTextureTiling(const std::string &texture, Real &tw, Real &th);
67 bool GetTextureForce(const std::string &texture, bool &enable_force, bool &force_mode);
68 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);
69 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);
70 void FreeTextureImages();
71 int GetTextureCount();
72 int GetMaterialCount();
73 bool GetTextureMapping(PolyArray &vertices, Vector3 &v1, Vector3 &v2, Vector3 &v3, int &direction);
74 void SetPlanarMapping(bool flat, bool FlipX, bool FlipY, bool FlipZ, bool rotate);
75 void GetPlanarMapping(bool &flat, bool &FlipX, bool &FlipY, bool &FlipZ, bool &rotate);
76 Vector2 CalculateSizing(const std::string &texture, const Vector3 &v1, const Vector3 &v2, const Vector3 &v3, int direction, Real tw, Real th);
77 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);
78 void SaveTexture(Ogre::TexturePtr texture, const std::string &filename);
79 std::string ListTextures(bool show_filename = false);
80 void IncrementTextureCount();
81 void DecrementTextureCount();
82 void IncrementMaterialCount();
83 void DecrementMaterialCount();
84 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);
85 bool UnregisterTextureInfo(std::string name, std::string material_name = "");
86 Ogre::MaterialPtr CreateMaterial(const std::string &name, const std::string &path);
87 Ogre::MaterialPtr GetMaterialByName(const std::string &name, const std::string &group = "General");
88 Ogre::TextureUnitState* BindTextureToMaterial(Ogre::MaterialPtr mMat, std::string texture_name, bool has_alpha);
89 Ogre::TextureUnitState* GetTextureUnitState(Ogre::MaterialPtr mMat);
90 std::string GetTextureName(Ogre::MaterialPtr mMat);
91 Ogre::TexturePtr GetTextureByName(const std::string &name, const std::string &group = "General");
92 std::string GetTextureMaterial(const std::string &name, bool &result, bool report = true, const std::string &polygon_name = "");
93 void CopyTexture(Ogre::TexturePtr source, Ogre::TexturePtr destination);
94 void CopyTexture(Ogre::TexturePtr source, Ogre::TexturePtr destination, const Ogre::Box &srcBox, const Ogre::Box &dstBox);
95 void FreeTextureBoxes();
96 void SetPlanarRotate(bool value);
97 bool GetPlanarRotate();
98 bool ComputeTextureMap(Matrix3 &t_matrix, Vector3 &t_vector, PolyArray &vertices, const Vector3 &p1, const Vector3 &p2, const Vector3 &p3, Real tw, Real th);
99 void EnableLighting(const std::string &material_name, bool value);
100 void EnableShadows(const std::string &material_name, bool value);
101 int GetTextureInfoCount();
102 bool GetTextureInfo(int index, TextureInfo &info);
103 bool SetTextureInfo(int index, TextureInfo &info);
104 void IncrementTextureUsage(const std::string &name);
105 void DecrementTextureUsage(const std::string &name);
106 void SetCulling(const std::string &material_name, int mode = 1);
107 Ogre::MaterialPtr SetCulling(const std::string &material_name, const std::string &name, int mode);
108 size_t GetMemoryUsage();
109 bool GetTextureImage(Ogre::TexturePtr texture);
110
111 //override textures
112 std::string mainnegtex, mainpostex, sidenegtex, sidepostex, toptex, bottomtex;
113
114 //texture flipping
115 int mainnegflip, mainposflip, sidenegflip, sideposflip, topflip, bottomflip;
116 std::vector<Real> widthscale;
117 std::vector<Real> heightscale;
118
119 //texture information structure
121 {
122 std::string name;
123 std::string material_name; //used if material is loaded instead of texture, as an alias
124 std::string filename;
127 bool enable_force; //enable forcing of tile or stretch mode?
128 bool force_mode; //false to disable autosizing, true to enable autosizing
129 int dependencies; //number of submeshes depending on this texture
130 size_t tex_size; //size of texture resource in bytes
131 size_t mat_size; //size of material resource in bytes
132 };
133
134private:
135
136 //texture mapping
137 std::vector<int> MapIndex;
138 std::vector<int> OldMapIndex;
139 std::vector<Vector2> MapUV;
140 std::vector<Vector2> OldMapUV;
141 std::vector<std::string> MapVerts1;
142 std::vector<std::string> MapVerts2;
143 std::vector<std::string> MapVerts3;
144 std::vector<std::string> OldMapVerts1;
145 std::vector<std::string> OldMapVerts2;
146 std::vector<std::string> OldMapVerts3;
147 bool AutoX, AutoY; //autosizing
148 int MapMethod; //texture mapping method - 0=planar, 1=index, 2=verts
150 bool RevX, RevY, RevZ; //extent reversals (planar texture mapping)
151 bool OldRevX, OldRevY, OldRevZ;
152 bool PlanarFlat, OldPlanarFlat;
153 bool PlanarRotate, OldPlanarRotate;
154 int DefaultMapper; //default texture mapper
155
156 void BackupMapping();
157 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);
158 Ogre::TexturePtr LoadTexture(const std::string &filename, int mipmaps, bool &has_alpha, bool use_alpha_color = false, Ogre::ColourValue alpha_color = Ogre::ColourValue::Black);
159 void UnloadMaterials();
160 bool ComputeTextureSpace(Matrix3 &m, Vector3 &v, const Vector3 &v_orig, const Vector3 &v1, Real len1, const Vector3 &v2, Real len2);
161
162 std::vector<TextureInfo> textureinfo;
163 std::vector<Ogre::TexturePtr> manual_textures;
164
165 //textures/materials count
168
170 {
171 Ogre::FontPtr font;
172 Ogre::PixelBox *box;
173 unsigned char *buffer;
174 };
175 std::vector<TexturePixelBox> textureboxes;
176
177 //function caching
178 std::string prev_material;
179};
180
181}
182
183#endif
std::vector< Real > widthscale
Definition texture.h:116
std::vector< std::string > OldMapVerts3
Definition texture.h:146
std::vector< TexturePixelBox > textureboxes
Definition texture.h:175
std::string prev_material
Definition texture.h:178
std::vector< std::string > MapVerts1
Definition texture.h:141
Ogre::MaterialPtr SetCulling(const std::string &material_name, const std::string &name, int mode)
std::vector< int > MapIndex
Definition texture.h:137
std::vector< Ogre::TexturePtr > manual_textures
Definition texture.h:163
std::vector< std::string > OldMapVerts2
Definition texture.h:145
std::string bottomtex
Definition texture.h:112
std::vector< Vector2 > OldMapUV
Definition texture.h:140
std::vector< std::string > OldMapVerts1
Definition texture.h:144
std::vector< Vector2 > MapUV
Definition texture.h:139
std::vector< std::string > MapVerts2
Definition texture.h:142
std::vector< int > OldMapIndex
Definition texture.h:138
std::vector< TextureInfo > textureinfo
Definition texture.h:162
std::vector< std::string > MapVerts3
Definition texture.h:143
std::vector< Real > heightscale
Definition texture.h:117
Ogre::Matrix3 Matrix3
Definition globals.h:64
Ogre::Vector3 Vector3
Definition globals.h:58
Ogre::Real Real
Definition globals.h:57
Ogre::Vector2 Vector2
Definition globals.h:59
#define SBSIMPEXP
Definition globals.h:53
std::vector< Vector3 > PolyArray
Definition sbs.h:118