TDME2  1.9.200
importtmodel-main.cpp
Go to the documentation of this file.
1 #include <memory>
2 #include <string>
3 
4 #include <tdme/tdme.h>
14 #include <tdme/engine/Engine.h>
15 #include <tdme/engine/Version.h>
19 #include <tdme/utilities/Console.h>
23 
24 using std::make_unique;
25 using std::string;
26 using std::unique_ptr;
27 
46 
47 namespace tdme {
48 namespace tools {
49 namespace cli {
50 
51 /**
52  * Import tmodel application
53  * @author andreas.drewke
54  */
56  : public virtual Application
57 {
58 private:
60  string modelFileName;
63 
64 public:
65  // forbid class copy
67 
68  /**
69  * Public constructor
70  */
76  }
77 
78  /**
79  * Public denstructor
80  */
82  }
83 
84  /**
85  * Main
86  * @param argc argument count
87  * @param argv argument values
88  * @return exit code
89  */
90  inline static int main(int argc, char** argv) {
91  auto useBC7TextureCompression = true;
92  if (string(argv[1]) == "--no-texture-compression") useBC7TextureCompression = false;
93  string tModelFileName = argv[1 + (useBC7TextureCompression == false?1:0)];
94  string modelFileName = argv[2 + (useBC7TextureCompression == false?1:0)];
95  string bvsModelFileName = argc >= 4 + (useBC7TextureCompression == false?1:0)?argv[3 + (useBC7TextureCompression == false?1:0)]:"";
97  return importTModelApplication->run(argc, argv, "Import TModel Application", nullptr, Application::WINDOW_HINT_INVISIBLE);
98  }
99 
100  // overridden methods
101  void display() override {
102  try {
103  unique_ptr<Prototype> prototype;
104  // load model
105  Console::println("Loading model: " + modelFileName);
106  auto model = unique_ptr<Model>(
107  ModelReader::read(
108  FileSystem::getInstance()->getPathName(modelFileName),
109  FileSystem::getInstance()->getFileName(modelFileName),
111  )
112  );
113  // load tmm
114  if (FileSystem::getInstance()->exists(tModelFileName) == false) {
115  Console::println("Creating tmodel: " + tModelFileName);
116  auto pathName = FileSystem::getInstance()->getPathName(tModelFileName);
117  auto fileName = FileSystem::getInstance()->getFileName(tModelFileName);
118  auto fileNameWithoutExtension = StringTools::substring(fileName, 0, fileName.rfind('.'));
119  prototype = make_unique<Prototype>(
120  -1,
121  Prototype_Type::MODEL,
122  fileNameWithoutExtension,
123  fileNameWithoutExtension,
124  pathName + "/" + fileName,
125  FileSystem::getInstance()->getPathName(modelFileName) + "/" + FileSystem::getInstance()->getFileName(modelFileName),
126  string(),
127  model.release()
128  );
129  } else {
130  Console::println("Loading tmodel: " + tModelFileName);
131  prototype = unique_ptr<Prototype>(
132  PrototypeReader::read(
133  FileSystem::getInstance()->getPathName(tModelFileName),
134  FileSystem::getInstance()->getFileName(tModelFileName),
135  nullptr,
137  )
138  );
139  prototype->setModel(model.release());
140  }
141  // remove old bv mesh model files
142  GenerateConvexMeshes::removeConvexMeshes(prototype.get());
143  // load new convex meshes bv model
144  if (bvsModelFileName.empty() == false) {
145  Console::println("Loading convex mesh bounding volumes model: " + bvsModelFileName);
146  vector<vector<uint8_t>> convexMeshTMsData;
147  if (GenerateConvexMeshes::generateConvexMeshes(
148  prototype.get(),
149  GenerateConvexMeshes::MODE_IMPORT,
150  nullptr,
151  FileSystem::getInstance()->getPathName(bvsModelFileName),
152  FileSystem::getInstance()->getFileName(bvsModelFileName),
153  convexMeshTMsData) == true) {
154  for (const auto& convexMeshTMData: convexMeshTMsData) {
155  //
156  try {
157  auto prototypeBoundingVolume = make_unique<PrototypeBoundingVolume>(prototype.get());
158  prototypeBoundingVolume->setupConvexMesh(convexMeshTMData);
159  prototype->addBoundingVolume(prototypeBoundingVolume.release());
160  } catch (Exception& exception) {
161  Console::println(string("An error occurred: ") + exception.what());
162  }
163  }
164  //
165  }
166  }
167  Console::println("Saving tmodel: " + tModelFileName);
168  PrototypeWriter::write(
169  FileSystem::getInstance()->getPathName(tModelFileName),
170  FileSystem::getInstance()->getFileName(tModelFileName),
171  prototype.get(),
173  );
174  } catch (Exception& exception) {
175  Console::println("An error occurred: " + string(exception.what()));
176  }
177  Application::exit(0);
178  }
179 
180  void dispose() override {
181  Engine::getInstance()->dispose();
182  }
183 
184  void initialize() override {
185  Engine::getInstance()->initialize();
186  }
187 
188  void reshape(int32_t width, int32_t height) override {
189  Engine::getInstance()->reshape(width, height);
190  }
191 };
192 
193 };
194 };
195 };
196 
197 int main(int argc, char** argv)
198 {
199  Console::println(string("importtmodel ") + Version::getVersion());
200  Console::println(Version::getCopyright());
201  Console::println();
202  if (argc < 3) {
203  Console::println("Usage: importtmodel [--no-texture-compression] model.tmodel modelfile.ext [bvs-model.ext]");
204  Application::exit(1);
205  }
207 }
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
Representation of a 3D model.
Definition: Model.h:35
Prototype definition.
Definition: Prototype.h:55
File system singleton class.
Definition: FileSystem.h:17
static int main(int argc, char **argv)
Main.
ImportTModelApplication(const string &tModelFileName, const string &modelFileName, const string &bvsModelFileName, bool useBC7TextureCompression)
Public constructor.
void reshape(int32_t width, int32_t height) override
Console class.
Definition: Console.h:29
Model tools functions class.
Definition: ModelTools.h:42
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