TDME2  1.9.200
converttotm-main.cpp
Go to the documentation of this file.
1 #include <memory>
2 #include <string>
3 
4 #include <tdme/tdme.h>
13 #include <tdme/engine/Engine.h>
14 #include <tdme/engine/Texture.h>
15 #include <tdme/engine/Version.h>
18 #include <tdme/utilities/Console.h>
21 
22 using std::string;
23 using std::unique_ptr;
24 
41 
42 namespace tdme {
43 namespace tools {
44 namespace cli {
45 
46 /**
47  * Convert to tm application
48  * @author andreas.drewke
49  */
51  : public virtual Application
52 {
53 private:
54  vector<string> modelFileNames;
55  bool useBC7TextureCompression { true };
56 
57 public:
58  // forbid class copy
60 
61  /**
62  * Public constructor
63  * @param modelFileNames model file names
64  */
68  }
69 
70  /**
71  * Public denstructor
72  */
74  }
75 
76  /**
77  * Main
78  * @param argc argument count
79  * @param argv argument values
80  * @return exit code
81  */
82  inline static int main(int argc, char** argv) {
83  auto useBC7TextureCompression = true;
84  vector<string> modelFileNames;
85  for (auto i = 1; i < argc; i++) {
86  string argumentValue = argv[i];
87  if (argumentValue == "--no-texture-compression") {
89  continue;
90  }
91  modelFileNames.push_back(argumentValue);
92  }
93  auto convertToTMApplication = new ConvertToTMApplication(useBC7TextureCompression, modelFileNames);
94  return convertToTMApplication->run(argc, argv, "Convert to tm Application", nullptr, Application::WINDOW_HINT_INVISIBLE);
95  }
96 
97  // overridden methods
98  void display() override {
99  Console::println("Exporting models: Using BC7 texture compression: " + string(useBC7TextureCompression == true?"true":"false"));
100  try {
101  //
102  auto scaleTo = 1024.0f;
103  //
104  for (const auto& inputFileName: modelFileNames) {
105  auto outputFileName = StringTools::substring(inputFileName, 0, inputFileName.rfind('.')) + ".tm";
106  try {
107  Console::println("Loading model: " + inputFileName);
108  auto model = unique_ptr<Model>(
109  ModelReader::read(
110  FileSystem::getInstance()->getPathName(inputFileName),
111  FileSystem::getInstance()->getFileName(inputFileName),
113  )
114  );
115  for (const auto& [materialId, material]: model->getMaterials()) {
116  auto specularMaterialProperties = material->getSpecularMaterialProperties();
117  // specular material
118  if (specularMaterialProperties != nullptr) {
119  // diffuse texture
120  {
121  auto texture = specularMaterialProperties->getDiffuseTexture();
122  if (texture != nullptr) {
123  auto widthScale = scaleTo / texture->getTextureWidth();
124  auto heightScale = scaleTo / texture->getTextureHeight();
125  auto scale = Math::min(widthScale, heightScale);
126  if (scale < 1.0f) {
127  auto scaledTexture = TextureReader::scale(texture, texture->getTextureWidth() * scale, texture->getTextureHeight() * scale, string());
128  specularMaterialProperties->setDiffuseTexture(scaledTexture);
129  scaledTexture->releaseReference();
130  }
131  }
132  }
133  // spec texture
134  {
135  auto texture = specularMaterialProperties->getSpecularTexture();
136  if (texture != nullptr) {
137  auto widthScale = scaleTo / texture->getTextureWidth();
138  auto heightScale = scaleTo / texture->getTextureHeight();
139  auto scale = Math::min(widthScale, heightScale);
140  if (scale < 1.0f) {
141  auto scaledTexture = TextureReader::scale(texture, texture->getTextureWidth() * scale, texture->getTextureHeight() * scale, string());
142  specularMaterialProperties->setSpecularTexture(scaledTexture);
143  scaledTexture->releaseReference();
144  }
145  }
146  }
147  // normal texture
148  {
149  auto texture = specularMaterialProperties->getNormalTexture();
150  if (texture != nullptr) {
151  auto widthScale = scaleTo / texture->getTextureWidth();
152  auto heightScale = scaleTo / texture->getTextureHeight();
153  auto scale = Math::min(widthScale, heightScale);
154  if (scale < 1.0f) {
155  auto scaledTexture = TextureReader::scale(texture, texture->getTextureWidth() * scale, texture->getTextureHeight() * scale, string());
156  specularMaterialProperties->setNormalTexture(scaledTexture);
157  scaledTexture->releaseReference();
158  }
159  }
160  }
161  }
162  //
163  auto pbrMaterialProperties = material->getPBRMaterialProperties();
164  // pbr material
165  if (pbrMaterialProperties != nullptr) {
166  // base texture
167  {
168  auto texture = pbrMaterialProperties->getBaseColorTexture();
169  if (texture != nullptr) {
170  auto widthScale = scaleTo / texture->getTextureWidth();
171  auto heightScale = scaleTo / texture->getTextureHeight();
172  auto scale = Math::min(widthScale, heightScale);
173  if (scale < 1.0f) {
174  auto scaledTexture = TextureReader::scale(texture, texture->getTextureWidth() * scale, texture->getTextureHeight() * scale, string());
175  pbrMaterialProperties->setBaseColorTexture(scaledTexture);
176  scaledTexture->releaseReference();
177  }
178  }
179  }
180  // metallic roughness texture
181  {
182  auto texture = pbrMaterialProperties->getMetallicRoughnessTexture();
183  if (texture != nullptr) {
184  auto widthScale = scaleTo / texture->getTextureWidth();
185  auto heightScale = scaleTo / texture->getTextureHeight();
186  auto scale = Math::min(widthScale, heightScale);
187  if (scale < 1.0f) {
188  auto scaledTexture = TextureReader::scale(texture, texture->getTextureWidth() * scale, texture->getTextureHeight() * scale, string());
189  pbrMaterialProperties->setMetallicRoughnessTexture(scaledTexture);
190  scaledTexture->releaseReference();
191  }
192  }
193  }
194  // normal texture
195  {
196  auto texture = pbrMaterialProperties->getNormalTexture();
197  if (texture != nullptr) {
198  auto widthScale = scaleTo / texture->getTextureWidth();
199  auto heightScale = scaleTo / texture->getTextureHeight();
200  auto scale = Math::min(widthScale, heightScale);
201  if (scale < 1.0f) {
202  auto scaledTexture = TextureReader::scale(texture, texture->getTextureWidth() * scale, texture->getTextureHeight() * scale, string());
203  pbrMaterialProperties->setNormalTexture(scaledTexture);
204  scaledTexture->releaseReference();
205  }
206  }
207  }
208  }
209  }
210  //
211  Console::println("Exporting model: " + outputFileName);
212  TMWriter::write(
213  model.get(),
214  FileSystem::getInstance()->getPathName(outputFileName),
215  FileSystem::getInstance()->getFileName(outputFileName),
217  );
218  } catch (Exception& exception) {
219  Console::println("An error occurred: " + string(exception.what()));
220  }
221  }
222  } catch (Exception& exception) {
223  Console::println("An error occurred: " + string(exception.what()));
224  }
225  Application::exit(0);
226  }
227 
228  void dispose() override {
229  Engine::getInstance()->dispose();
230  }
231 
232  void initialize() override {
233  Engine::getInstance()->initialize();
234  }
235 
236  void reshape(int32_t width, int32_t height) override {
237  Engine::getInstance()->reshape(width, height);
238  }
239 };
240 
241 };
242 };
243 };
244 
245 int main(int argc, char** argv)
246 {
247  Console::println(string("converttotm ") + Version::getVersion());
248  Console::println(Version::getCopyright());
249  Console::println();
250  if (argc < 2) {
251  Console::println("Usage: converttotm [--no-texture-compression] inputfile1 [inputfileN]");
252  Application::exit(Application::EXITCODE_FAILURE);
253  }
255 }
Application base class, please make sure to allocate application on heap to have correct application ...
Definition: Application.h:41
Engine main class.
Definition: Engine.h:131
Texture entity.
Definition: Texture.h:24
Represents a material.
Definition: Material.h:23
Representation of a 3D model.
Definition: Model.h:35
Represents specular material properties.
Represents specular material properties.
File system singleton class.
Definition: FileSystem.h:17
ConvertToTMApplication(bool useBC7TextureCompression, const vector< string > &modelFileNames)
Public constructor.
static int main(int argc, char **argv)
Main.
void reshape(int32_t width, int32_t height) override
Console class.
Definition: Console.h:29
String tools class.
Definition: StringTools.h:22
int main(int argc, char **argv)
std::exception Exception
Exception base class.
Definition: Exception.h:18
Definition: fwd-tdme.h:4
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6