5 #include <unordered_map>
46 using std::make_unique;
49 using std::unordered_map;
50 using std::unique_ptr;
86 void GenerateConvexMeshes::removeConvexMeshes(
Prototype* prototype)
91 if (boundingVolume->isGenerated() ==
false) {
94 if (boundingVolume->getConvexMeshFile().empty() ==
false &&
95 FileSystem::getInstance()->exists(boundingVolume->getConvexMeshFile()) ==
true) {
96 FileSystem::getInstance()->removeFile(
97 FileSystem::getInstance()->getPathName(boundingVolume->getConvexMeshFile()),
98 FileSystem::getInstance()->getFileName(boundingVolume->getConvexMeshFile())
103 if (boundingVolume->getConvexMeshData().empty() ==
false) {
115 class VHACDCallback :
public VHACD::IVHACD::IUserCallback {
119 VHACDCallback(
ProgressBarScreenController* progressBarScreenController): progressBarScreenController(progressBarScreenController) {}
122 const double overallProgress,
123 const double stageProgress,
124 const double operationProgress,
125 const char*
const stage,
126 const char*
const operation)
132 class VHACDLogger :
public VHACD::IVHACD::IUserLogger {
136 void Log(
const char*
const msg)
138 Console::println(msg);
144 auto vhacd = VHACD::CreateVHACD();
147 parameters.m_maxRecursionDepth = 15;
149 if (parameters.m_resolution < 10000 || parameters.m_resolution > 64000000) {
150 throw ExceptionBase(
"Resolution must be between 10000 and 64000000");
152 if (parameters.m_minimumVolumePercentErrorAllowed < 0.0f || parameters.m_minimumVolumePercentErrorAllowed > 100.0f) {
153 throw ExceptionBase(
"Concavity must be between 0.0 and 100.0");
155 if (parameters.m_maxNumVerticesPerCH < 4 || parameters.m_maxNumVerticesPerCH > 1024) {
156 throw ExceptionBase(
"Max number of vertices per convex hull must be between 4 and 1024");
158 if (parameters.m_maxConvexHulls < 1 || parameters.m_maxConvexHulls > 64) {
159 throw ExceptionBase(
"Max number of convex hulls must be between 1 and 64");
162 VHACDLogger vhacdLogger;
163 parameters.m_logger = &vhacdLogger;
170 vector<float> meshPoints;
171 vector<int> meshTriangles;
174 auto meshModel = unique_ptr<Model>(
182 vector<Triangle> meshFaceTriangles;
184 for (
const auto& triangle: meshFaceTriangles) {
185 meshTriangles.push_back(meshPoints.size() / 3 + 0);
186 meshTriangles.push_back(meshPoints.size() / 3 + 1);
187 meshTriangles.push_back(meshPoints.size() / 3 + 2);
188 for (
auto i = 0; i < triangle.getVertices().size(); i++) {
189 meshPoints.push_back(triangle.getVertices()[i].getX());
190 meshPoints.push_back(triangle.getVertices()[i].getY());
191 meshPoints.push_back(triangle.getVertices()[i].getZ());
200 (
unsigned int)meshPoints.size() / 3,
201 (
const unsigned int *)&meshTriangles[0],
202 (
unsigned int)meshTriangles.size() / 3,
205 if (vhacdResult ==
true) {
206 auto convexHulls = vhacd->GetNConvexHulls();
207 VHACD::IVHACD::ConvexHull convexHull;
208 for (
auto i = 0; i < convexHulls; i++) {
209 vhacd->GetConvexHull(i, convexHull);
210 auto convexHullModel = unique_ptr<Model>(
212 fileName +
".cm." + to_string(i) +
".tm",
214 convexHull.m_triangles
217 convexMeshTMsData.push_back(vector<uint8_t>());
218 TMWriter::write(convexHullModel.get(), convexMeshTMsData[convexMeshTMsData.size() - 1]);
230 Console::println(
"Could not create convex hulls: " +
string(exception.what()));
231 convexMeshTMsData.clear();
241 auto meshModel = unique_ptr<Model>(
250 for (
auto i = 0; i < meshObjectModel.
getNodeCount(); i++) {
251 vector<Triangle> nodeTriangles;
253 auto convexHullModel = unique_ptr<Model>(
255 fileName +
".cm." + to_string(i) +
".tm",
259 convexMeshTMsData.push_back(vector<uint8_t>());
260 TMWriter::write(convexHullModel.get(), convexMeshTMsData[convexMeshTMsData.size() - 1]);
272 Console::println(
"Could not create convex hulls: " +
string(exception.what()));
273 convexMeshTMsData.clear();
282 auto model = make_unique<Model>(
id,
id, UpVector::Y_UP, RotationOrder::XYZ,
nullptr);
284 auto material = make_unique<Material>(
"primitive");
285 material->setSpecularMaterialProperties(make_unique<SpecularMaterialProperties>().release());
286 material->getSpecularMaterialProperties()->setAmbientColor(
Color4(0.5f, 0.5f, 0.5f, 1.0f));
287 material->getSpecularMaterialProperties()->setDiffuseColor(
Color4(1.0f, 0.5f, 0.5f, 0.5f));
288 material->getSpecularMaterialProperties()->setSpecularColor(
Color4(0.0f, 0.0f, 0.0f, 1.0f));
290 auto node = make_unique<Node>(model.get(),
nullptr,
"node",
"node");
291 vector<Vector3> vertices;
292 vector<Vector3> normals;
294 int normalIndex = -1;
295 for (
const auto& vertex: points) {
296 vertices.emplace_back(
297 static_cast<float>(vertex.mX),
298 static_cast<float>(vertex.mY),
299 static_cast<float>(vertex.mZ)
302 for (
const auto& triangle: triangles) {
303 normalIndex = normals.size();
305 array<Vector3, 3> faceVertices = {
306 vertices[triangle.mI0],
307 vertices[triangle.mI1],
308 vertices[triangle.mI2]
310 for (
const auto& normal: ModelTools::computeNormals(faceVertices)) {
311 normals.push_back(normal);
324 FacesEntity nodeFacesEntity(node.get(),
"faces entity");
327 vector<FacesEntity> nodeFacesEntities;
328 nodeFacesEntities.push_back(nodeFacesEntity);
329 node->setVertices(vertices);
330 node->setNormals(normals);
331 node->setFacesEntities(nodeFacesEntities);
332 model->getNodes()[
"node"] = node.get();
333 model->getSubNodes()[
"node"] = node.get();
336 model->getMaterials()[material->getId()] = material.get();
339 ModelTools::prepareForIndexedRendering(model.get());
341 return model.release();
345 auto model = make_unique<Model>(
id,
id, UpVector::Y_UP, RotationOrder::XYZ,
nullptr);
347 auto material = make_unique<Material>(
"primitive");
348 material->setSpecularMaterialProperties(make_unique<SpecularMaterialProperties>().release());
349 material->getSpecularMaterialProperties()->setAmbientColor(
Color4(0.5f, 0.5f, 0.5f, 1.0f));
350 material->getSpecularMaterialProperties()->setDiffuseColor(
Color4(1.0f, 0.5f, 0.5f, 0.5f));
351 material->getSpecularMaterialProperties()->setSpecularColor(
Color4(0.0f, 0.0f, 0.0f, 1.0f));
353 auto node = make_unique<Node>(model.get(),
nullptr,
"node",
"node");
354 vector<Vector3> vertices;
355 vector<Vector3> normals;
358 for (
const auto& triangle: triangles) {
359 for (
const auto& vertex: triangle.getVertices()) {
360 vertices.push_back(vertex);
363 array<Vector3, 3> faceVertices = {
364 triangle.getVertices()[0],
365 triangle.getVertices()[1],
366 triangle.getVertices()[2],
368 for (
const auto& normal: ModelTools::computeNormals(faceVertices)) {
369 normals.push_back(normal);
383 FacesEntity nodeFacesEntity(node.get(),
"faces entity");
386 vector<FacesEntity> nodeFacesEntities;
387 nodeFacesEntities.push_back(nodeFacesEntity);
388 node->setVertices(vertices);
389 node->setNormals(normals);
390 node->setFacesEntities(nodeFacesEntities);
392 model->getNodes()[
"node"] = node.get();
393 model->getSubNodes()[
"node"] = node.get();
396 model->getMaterials()[material->getId()] = material.get();
399 ModelTools::prepareForIndexedRendering(model.get());
401 return model.release();
Color 4 definition class.
Represents a model face, consisting of vertex, normal, tangent and bitangent vectors,...
Node faces entity A node can have multiple entities containing faces and a applied material.
void setMaterial(Material *material)
Set up the entity's material.
void setFaces(const vector< Face > &faces)
Set up entity's faces.
Representation of a 3D model.
Represents rotation orders of a model.
Represents specular material properties.
Triangle entity, this is not directly connectable with physics engine.
Prototype bounding volume definition.
PrototypeBoundingVolume * getBoundingVolume(int idx)
Get bounding volume at given index.
int getBoundingVolumeCount()
void removeBoundingVolume(int idx)
Remove bounding volume at given index.
void getTriangles(vector< Triangle > &triangles, int nodeIdx=-1)
Retrieves list of triangles of all or given nodes.
GUI node controller base class.
GUI parent node base class thats supporting child nodes.
GUI screen node that represents a screen that can be rendered via GUI system.
File system singleton class.
Standard file system implementation.
Mutable utf8 aware string class.
std::exception Exception
Exception base class.