commit x64 compilation from lulu cause the other branch dont seems to compile properly at home
This commit is contained in:
201
include/modules/tools/geometry_merge.cpp
Normal file
201
include/modules/tools/geometry_merge.cpp
Normal file
@ -0,0 +1,201 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#include "tools/geometry_merge.h"
|
||||
#include "core/geometry.h"
|
||||
#include "math/matrix4.h"
|
||||
#include "log/log.h"
|
||||
|
||||
using namespace GS::Core;
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Geometry *GS::Core::MergeGeometry(Geometry *geo_a, Geometry *geo_b, const Matrix4 *mtx_a, const Matrix4 *mtx_b)
|
||||
{
|
||||
if (!geo_a || !geo_b)
|
||||
__ERR__(__LOG_E__ << "Cannot merge NULL geometry.\n", NULL)
|
||||
|
||||
if (!mtx_a)
|
||||
mtx_a = &Matrix4::IdentityMatrix();
|
||||
if (!mtx_b)
|
||||
mtx_b = &Matrix4::IdentityMatrix();
|
||||
|
||||
// Allocate a new geometry.
|
||||
Geometry *geo_o = new Geometry;
|
||||
if (!geo_o)
|
||||
__ERR__(__LOG_E__ << "Failed to allocate merged geometry.\n", NULL)
|
||||
|
||||
geo_o->name = geo_a->name; // Nothing fancy here, the string length would blow up on large merge.
|
||||
|
||||
// Update data for all geometries.
|
||||
if (geo_a->pol_normal || geo_b->pol_normal)
|
||||
{
|
||||
geo_a->ComputePolygonNormal();
|
||||
geo_b->ComputePolygonNormal();
|
||||
}
|
||||
if (geo_a->vtx_normal || geo_b->vtx_normal)
|
||||
{
|
||||
geo_a->ComputeVertexNormal();
|
||||
geo_b->ComputeVertexNormal();
|
||||
}
|
||||
|
||||
// Append vertex.
|
||||
if (geo_o->vtx.Allocate(geo_a->vtx.GetCount() + geo_b->vtx.GetCount()))
|
||||
{
|
||||
Vector4 *pvtx = &geo_o->vtx[0];
|
||||
for (uint n = 0; n < geo_a->vtx.GetCount(); ++n)
|
||||
*pvtx++ = geo_a->vtx[n] * *mtx_a;
|
||||
for (uint n = 0; n < geo_b->vtx.GetCount(); ++n)
|
||||
*pvtx++ = geo_b->vtx[n] * *mtx_b;
|
||||
}
|
||||
|
||||
// Append polygon.
|
||||
if (geo_o->binding.Allocate(geo_a->binding.GetCount() + geo_b->binding.GetCount()))
|
||||
{
|
||||
uint *pbind = &geo_o->binding[0];
|
||||
for (uint n = 0; n < geo_a->binding.GetCount(); ++n)
|
||||
*pbind++ = geo_a->binding[n];
|
||||
for (uint n = 0; n < geo_b->binding.GetCount(); ++n)
|
||||
*pbind++ = geo_b->binding[n] + geo_a->vtx.GetCount();
|
||||
}
|
||||
|
||||
if (geo_o->pol.Allocate(geo_a->pol.GetCount() + geo_b->pol.GetCount()))
|
||||
{
|
||||
uint total_binding = 0;
|
||||
|
||||
Polygon *ppol = &geo_o->pol[0];
|
||||
for (uint n = 0; n < geo_a->pol.GetCount(); ++n)
|
||||
{
|
||||
ppol->vtx_count = geo_a->pol[n].vtx_count;
|
||||
ppol->material = geo_a->pol[n].material;
|
||||
ppol->binding = &geo_o->binding[total_binding];
|
||||
total_binding += ppol->vtx_count;
|
||||
ppol++;
|
||||
}
|
||||
for (uint n = 0; n < geo_b->pol.GetCount(); ++n)
|
||||
{
|
||||
ppol->vtx_count = geo_b->pol[n].vtx_count;
|
||||
ppol->material = (ushort)(geo_b->pol[n].material + geo_a->material_table.GetCount());
|
||||
ppol->binding = &geo_o->binding[total_binding];
|
||||
total_binding += ppol->vtx_count;
|
||||
ppol++;
|
||||
}
|
||||
}
|
||||
|
||||
// Append polygon normal.
|
||||
static Vector4 default_normal(0, 0, 1);
|
||||
|
||||
if (geo_a->pol_normal || geo_b->pol_normal)
|
||||
if (geo_o->pol_normal.Allocate(geo_o->pol.GetCount()))
|
||||
{
|
||||
Vector4 *ppnrm = &geo_o->pol_normal[0];
|
||||
for (uint n = 0; n < geo_a->pol.GetCount(); ++n)
|
||||
{
|
||||
if (geo_a->pol_normal)
|
||||
{
|
||||
mtx_a->ApplyRotation(ppnrm, &geo_a->pol_normal[n]);
|
||||
*ppnrm++ = ppnrm->Normalized();
|
||||
}
|
||||
else
|
||||
*ppnrm++ = default_normal;
|
||||
}
|
||||
for (uint n = 0; n < geo_b->pol.GetCount(); ++n)
|
||||
{
|
||||
if (geo_b->pol_normal)
|
||||
{
|
||||
mtx_b->ApplyRotation(ppnrm, &geo_b->pol_normal[n]);
|
||||
*ppnrm++ = ppnrm->Normalized();
|
||||
}
|
||||
else
|
||||
*ppnrm++ = default_normal;
|
||||
}
|
||||
}
|
||||
|
||||
// Append vertex normal.
|
||||
if (geo_a->vtx_normal || geo_b->vtx_normal)
|
||||
if (geo_o->vtx_normal.Allocate(geo_o->binding.GetCount()))
|
||||
{
|
||||
Vector4 *pvnrm = &geo_o->vtx_normal[0];
|
||||
for (uint n = 0; n < geo_a->binding.GetCount(); ++n)
|
||||
{
|
||||
if (geo_a->vtx_normal)
|
||||
{
|
||||
mtx_a->ApplyRotation(pvnrm, &geo_a->vtx_normal[n]);
|
||||
*pvnrm++ = pvnrm->Normalized();
|
||||
}
|
||||
else
|
||||
*pvnrm++ = Vector4(0, 0, 1);
|
||||
}
|
||||
for (uint n = 0; n < geo_b->binding.GetCount(); ++n)
|
||||
{
|
||||
if (geo_b->vtx_normal)
|
||||
{
|
||||
mtx_b->ApplyRotation(pvnrm, &geo_b->vtx_normal[n]);
|
||||
*pvnrm++ = pvnrm->Normalized();
|
||||
}
|
||||
else
|
||||
*pvnrm++ = Vector4(0, 0, 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Append RGB.
|
||||
if (geo_a->rgb || geo_b->rgb)
|
||||
if (geo_o->rgb.Allocate(geo_o->binding.GetCount()))
|
||||
{
|
||||
Color *prgb = &geo_o->rgb[0];
|
||||
for (uint n = 0; n < geo_a->binding.GetCount(); ++n)
|
||||
*prgb++ = geo_a->rgb ? geo_a->rgb[n] : Color::White;
|
||||
for (uint n = 0; n < geo_b->binding.GetCount(); ++n)
|
||||
*prgb++ = geo_b->rgb ? geo_b->rgb[n] : Color::White;
|
||||
}
|
||||
|
||||
// Append all UV channels.
|
||||
static Vector2 default_uv(0.5, 0.5);
|
||||
|
||||
for (uint n = 0; n < __UV_PER_GEOMETRY__; ++n)
|
||||
if (geo_a->uv[n] || geo_b->uv[n])
|
||||
if (geo_o->uv[n].Allocate(geo_o->binding.GetCount()))
|
||||
{
|
||||
Vector2 *puv = &geo_o->uv[n][0];
|
||||
|
||||
if (geo_a->uv[n])
|
||||
for (uint v = 0; v < geo_a->binding.GetCount(); ++v)
|
||||
*puv++ = geo_a->uv[n][v];
|
||||
else
|
||||
for (uint v = 0; v < geo_a->binding.GetCount(); ++v)
|
||||
*puv++ = default_uv;
|
||||
|
||||
if (geo_b->uv[n])
|
||||
for (uint v = 0; v < geo_b->binding.GetCount(); ++v)
|
||||
*puv++ = geo_b->uv[n][v];
|
||||
else
|
||||
for (uint v = 0; v < geo_b->binding.GetCount(); ++v)
|
||||
*puv++ = default_uv;
|
||||
}
|
||||
|
||||
// Append all materials.
|
||||
geo_o->material_table.Allocate(geo_a->material_table.GetCount() + geo_b->material_table.GetCount());
|
||||
Geometry::MaterialSlot *pslot = geo_o->material_table.c_ptr();
|
||||
|
||||
for (uint n = 0; n < geo_a->material_table.GetCount(); ++n)
|
||||
{
|
||||
pslot->name = geo_a->material_table[n].name;
|
||||
pslot->use_cache = geo_a->material_table[n].use_cache;
|
||||
++pslot;
|
||||
}
|
||||
for (uint n = 0; n < geo_b->material_table.GetCount(); ++n)
|
||||
{
|
||||
pslot->name = geo_b->material_table[n].name;
|
||||
pslot->use_cache = geo_b->material_table[n].use_cache;
|
||||
++pslot;
|
||||
}
|
||||
|
||||
// Merge materials.
|
||||
geo_o->MergeDuplicateMaterials();
|
||||
|
||||
return geo_o;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
235
include/modules/tools/resource_explorer.cpp
Normal file
235
include/modules/tools/resource_explorer.cpp
Normal file
@ -0,0 +1,235 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#include "tools/resource_explorer.h"
|
||||
#include "metafile/nml.h"
|
||||
#include "platform.h"
|
||||
#include "filesystem/filesystem.h"
|
||||
|
||||
using namespace GS::Resource;
|
||||
using namespace GS::NML;
|
||||
using GS::String;
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
DependencyRule scene3d_dependency_rules[] =
|
||||
{
|
||||
// Instances
|
||||
{ "Items/*Instance/Template", "Scene" },
|
||||
|
||||
// Terrains
|
||||
{ "Items/*MTerrain/Terrain/Blendmap", "Texture" },
|
||||
{ "Items/*MTerrain/Terrain/Blendmap", "Shader" },
|
||||
{ "Items/*MTerrain/Terrain/MaterialName", "Material" },
|
||||
{ "Items/*MTerrain/Terrain/Heightmap/Data", "Data" },
|
||||
{ "Items/*MTerrain/Terrain/Layers/*Layer/Diffuse", "Texture" },
|
||||
{ "Items/*MTerrain/Terrain/Layers/*Layer/Normal", "Texture" },
|
||||
{ "Items/*MTerrain/Terrain/Layers/*Layer/Specular", "Texture" },
|
||||
{ "Items/*MTerrain/Terrain/Layers/*Layer/Self", "Texture" },
|
||||
|
||||
// Lights
|
||||
{ "Items/*MLight/Light/ProjectionMap", "Texture" },
|
||||
|
||||
// Objects
|
||||
{ "Items/*MObject/Object/Geometry", "Geometry" },
|
||||
|
||||
// All items
|
||||
{ "Items/*/MItem/ScriptedObject/*ScriptUnit/Script", "Script" }, // legacy
|
||||
{ "Items/*/MItem/ScriptedObject/*ScriptUnit/ScriptPath", "Script" },
|
||||
{ "Items/*/MItem/PhysicItem/Shapes/*PhysicShape/Mesh", "Geometry" },
|
||||
|
||||
// Scene
|
||||
{ "ScriptedObject/*ScriptUnit/Script", "Script" }, // legacy
|
||||
{ "ScriptedObject/*ScriptUnit/ScriptPath", "Script" },
|
||||
{ 0, 0 }
|
||||
};
|
||||
DependencyRule geometry_dependency_rules[] =
|
||||
{
|
||||
{ "Materials/*MaterialRef", "Material" },
|
||||
{ "Materials/*MaterialRefEx/Name", "Material" },
|
||||
|
||||
{ "LodProxy", "Geometry" },
|
||||
{ "ShadowProxy", "Geometry" },
|
||||
|
||||
{ 0, 0 }
|
||||
};
|
||||
DependencyRule material_dependency_rules[] =
|
||||
{
|
||||
{ "*TextureStage/Texture", "Texture" },
|
||||
{ "Shader", "Shader" },
|
||||
{ 0, 0 }
|
||||
};
|
||||
DependencyRule shader_tree_dependency_rules[] =
|
||||
{
|
||||
{ "Map/*Entry/Param/Texture", "Texture" },
|
||||
{ 0, 0 }
|
||||
};
|
||||
DependencyRule shader_dependency_rules[] =
|
||||
{
|
||||
{ "Input/*Uniform/Texture", "Texture" },
|
||||
{ 0, 0 }
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
ExplorerRule resource_rules[] =
|
||||
{
|
||||
{ "Scene3d", "Scene", scene3d_dependency_rules },
|
||||
{ "Geometry", "Geometry", geometry_dependency_rules },
|
||||
{ "Material", "Material", material_dependency_rules },
|
||||
{ "Shader Tree", "ShaderMap", shader_tree_dependency_rules },
|
||||
{ "Shader", "Shader", shader_dependency_rules },
|
||||
{ 0, 0 }
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
const char *Explorer::DependencyTag::GetName() const
|
||||
{ return tag->GetString(); }
|
||||
void Explorer::DependencyTag::SetName(const char *n)
|
||||
{ tag->SetString(n); }
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool resource_filter(const Explorer::Resource *r, const char *name) { return r->name == name; }
|
||||
bool dependency_filter(const Explorer::Dependency *d, const char *name) { return !String::strccmp(d->GetName(), name); }
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
const Explorer::Dependency *Explorer::Resource::FindDependency(const char *n) const
|
||||
{
|
||||
return ListFindEx(dependencies, dependency_filter, n);
|
||||
}
|
||||
bool Explorer::Resource::HasMissingDependencies() const
|
||||
{
|
||||
ListForeachPtr(Dependency *, dep, dependencies)
|
||||
if (dep->missing)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void Explorer::ExploreResourceRuleTags(const GS::StringList &tags, uint pos, Tag *tag, Resource *r, int depth)
|
||||
{
|
||||
if (pos == tags.GetCount()) // end of path
|
||||
{
|
||||
if (tag->GetString())
|
||||
{
|
||||
String name = tag->GetString();
|
||||
|
||||
// if (!r->FindDependency(name))
|
||||
{
|
||||
DependencyTag *dependency = new DependencyTag(tag);
|
||||
r->dependencies.Add(dependency);
|
||||
|
||||
if (name.StartsWith("@sys/")) // engine generated resource
|
||||
dependency->missing = false;
|
||||
|
||||
else if (name.StartsWith("@core/"))
|
||||
dependency->missing = !Platform::Get().io->Exists(name);
|
||||
|
||||
// Assert dependency is found.
|
||||
else if (!name.IsAbsolutePath())
|
||||
{
|
||||
// Check in project path.
|
||||
if (!project_path.IsEmpty())
|
||||
{
|
||||
String _name = String::Format("%s/%s", project_path.c_str(), name.c_str()).CleanFilePath();
|
||||
if ((dependency->missing = !Platform::Get().io->Exists(_name)) == false)
|
||||
name = _name;
|
||||
}
|
||||
|
||||
// Check in core path if still missing.
|
||||
if (dependency->missing)
|
||||
if (!core_path.IsEmpty())
|
||||
{
|
||||
String _name = String::Format("%s/%s", core_path.c_str(), name.c_str()).CleanFilePath();
|
||||
if ((dependency->missing = !Platform::Get().io->Exists(_name)) == false)
|
||||
name = _name;
|
||||
}
|
||||
}
|
||||
ExploreResource(name, project_path, core_path, depth - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const String &t = tags[pos];
|
||||
|
||||
if (t.StartsWith("*")) // iterate current tag
|
||||
{
|
||||
String match = t.Mid(1);
|
||||
NMLTagForeach(c, *tag)
|
||||
if (match.IsEmpty() || (c->name == match))
|
||||
ExploreResourceRuleTags(tags, pos + 1, c, r, depth);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Tag *c = tag->GetTag(t)) // no tag means rule not met
|
||||
ExploreResourceRuleTags(tags, pos + 1, c, r, depth);
|
||||
}
|
||||
}
|
||||
}
|
||||
bool Explorer::ExploreResourceRule(ExplorerRule &rule, Resource *r, int depth)
|
||||
{
|
||||
Tag *tag = r->file->GetTag(rule.root);
|
||||
if (!tag)
|
||||
return false;
|
||||
|
||||
r->type = rule.type;
|
||||
|
||||
for (int n = 0; rule.rules[n].type; ++n)
|
||||
{
|
||||
// Split rule tags.
|
||||
StringList tags;
|
||||
String(rule.rules[n].path).Split("/", tags);
|
||||
|
||||
ExploreResourceRuleTags(tags, 0, tag, r, depth);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Explorer::Resource *Explorer::ExploreResource(const char *name, const char *_project_path, const char *_core_path, int depth)
|
||||
{
|
||||
if (depth <= 0)
|
||||
return NULL;
|
||||
|
||||
project_path = _project_path;
|
||||
core_path = _core_path;
|
||||
|
||||
// Metafile.
|
||||
if (!Parser::IsMetafile(name))
|
||||
return NULL;
|
||||
|
||||
// Check if that resource has already been loaded.
|
||||
if (Resource *_r = ListFindEx(resources, resource_filter, name))
|
||||
return _r;
|
||||
|
||||
// Load file.
|
||||
AutoPtr <Resource> r(new Resource(name, "Null"));
|
||||
r->file = Parser::Load(name);
|
||||
if (r->file.IsNull())
|
||||
return NULL;
|
||||
|
||||
// Explore resource.
|
||||
for (int n = 0; resource_rules[n].type; ++n)
|
||||
if (ExploreResourceRule(resource_rules[n], r, depth))
|
||||
{
|
||||
resources.Add(r);
|
||||
return r.Detach();
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
const Explorer::Resource *Explorer::FindResource(const char *name) const
|
||||
{
|
||||
return ListFindEx(resources, resource_filter, name);
|
||||
}
|
||||
void Explorer::Clear()
|
||||
{
|
||||
resources.Clear();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
186
include/modules/tools/scene_merge_object_list.cpp
Normal file
186
include/modules/tools/scene_merge_object_list.cpp
Normal file
@ -0,0 +1,186 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#include "tools/scene_merge_object_list.h"
|
||||
#include "tools/geometry_merge.h"
|
||||
#include "scene3d/scene.h"
|
||||
#include "scene3d/mobject.h"
|
||||
#include "physic/physic_world.h"
|
||||
#include "core/graphic_resource_factory.h"
|
||||
|
||||
using namespace GS;
|
||||
using namespace GS::Core;
|
||||
using namespace GS::S3D;
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
struct ItemTransform
|
||||
{
|
||||
sMItem i;
|
||||
Matrix4 m;
|
||||
|
||||
ItemTransform(MItem *_i, const Matrix4 &_m) : i(_i), m(_m) {}
|
||||
};
|
||||
struct GeometryTransform
|
||||
{
|
||||
sMItem i;
|
||||
Matrix4 m;
|
||||
sGeometry g;
|
||||
|
||||
GeometryTransform(MItem *_i, Geometry *_g, const Matrix4 &_m) : i(_i), m(_m), g(_g) {}
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static void MergePhysicShape(MItem *to, MItem *from)
|
||||
{
|
||||
if (!from->physic_item)
|
||||
return;
|
||||
|
||||
ListForeachPtr(PhysicShape *, shape, from->physic_item_desc.shape_list)
|
||||
if (PhysicShape *new_shape = new PhysicShape)
|
||||
{
|
||||
new_shape->SetMatrix(from->GetBaseItem()->GetMatrix() * shape->GetMatrix());
|
||||
new_shape->mass = shape->mass;
|
||||
|
||||
switch (shape->GetType())
|
||||
{
|
||||
case PhysicShape::TypeNone:
|
||||
break;
|
||||
|
||||
case PhysicShape::TypeHeightmap:
|
||||
__LOG_W__ << "Merge heightmap physic shape STUB.\n";
|
||||
break;
|
||||
|
||||
case PhysicShape::TypeBox:
|
||||
case PhysicShape::TypeCapsule:
|
||||
case PhysicShape::TypeCylinder:
|
||||
case PhysicShape::TypeCone:
|
||||
case PhysicShape::TypeSphere:
|
||||
new_shape->Set(shape->GetType(), shape->dimensions);
|
||||
break;
|
||||
|
||||
case PhysicShape::TypeConvex:
|
||||
case PhysicShape::TypeMesh:
|
||||
new_shape->Set(shape->GetType(), shape->path);
|
||||
break;
|
||||
}
|
||||
|
||||
to->physic_item_desc.shape_list.Add(new_shape);
|
||||
}
|
||||
|
||||
to->physic_item_desc.physic_mode = from->physic_item_desc.physic_mode;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool SceneMergeObjectList::Merge(Scene *scene, const SharedList <MItem *> &item_list, Core::ResourceFactory &gf, sMItem &out_item, sGeometry &out_geo)
|
||||
{
|
||||
// Unlink all children, store matrix beforehand.
|
||||
AutoList <ItemTransform *> child_list;
|
||||
|
||||
ListForeachPtr(MItem *, item, item_list)
|
||||
ListForeachPtr(Item *, child, item->GetBaseItem()->GetChildren())
|
||||
{
|
||||
MItem *cchild = Scene::LocateManagedItem(child);
|
||||
|
||||
// Do not store item if it is going to be merged.
|
||||
if (!item_list.Find(cchild))
|
||||
child_list.Add(new ItemTransform(cchild, cchild->GetBaseItem()->GetMatrix()));
|
||||
}
|
||||
|
||||
Vector4 average_vec(0.0,0.0,0.0);
|
||||
int counter_average = 0;
|
||||
ListForeachPtr(MItem *, item, item_list)
|
||||
if (item->GetItemType() == Type_Object)
|
||||
{
|
||||
++counter_average;
|
||||
average_vec += item->GetBaseItem()->GetPosition();
|
||||
}
|
||||
|
||||
average_vec /= counter_average;
|
||||
|
||||
ListForeachPtr(MItem *, item, item_list)
|
||||
if (item->GetItemType() == Type_Object)
|
||||
item->GetBaseItem()->SetPosition(item->GetBaseItem()->GetPosition() - average_vec);
|
||||
|
||||
// Build merge list.
|
||||
AutoList <GeometryTransform *> geo_list;
|
||||
ListForeachPtr(MItem *, item, item_list)
|
||||
if (item->GetItemType() == Type_Object)
|
||||
geo_list.Add(new GeometryTransform(item, gf.LoadGeometry(((MObject *)item)->geometry), item->GetBaseItem()->GetMatrix()));
|
||||
|
||||
if (geo_list.GetCount() < 2)
|
||||
return true; // nothing to be done
|
||||
|
||||
// Count total steps.
|
||||
int total_step_count = 0;
|
||||
for (int count = item_list.GetCount(); count > 1; count /= 2)
|
||||
total_step_count += count;
|
||||
|
||||
// Merge list.
|
||||
AutoList <GeometryTransform *> tgt_list, *in_list = &geo_list, *out_list = &tgt_list;
|
||||
|
||||
int step_count = 0;
|
||||
for (bool running = true; (in_list->GetCount() > 1) && running; )
|
||||
{
|
||||
for (uint n = 0; (n < in_list->GetCount()) && running; n += 2)
|
||||
{
|
||||
if (n == (in_list->GetCount() - 1))
|
||||
{
|
||||
GeometryTransform *t = (*in_list)[n];
|
||||
out_list->Add(new GeometryTransform(t->i, t->g, t->m));
|
||||
continue; // nothing to merge
|
||||
}
|
||||
|
||||
// Merge geometries.
|
||||
GeometryTransform *left = (*in_list)[n], *right = (*in_list)[n + 1];
|
||||
sGeometry merged_geometry(MergeGeometry(left->g, right->g, &left->m, &right->m));
|
||||
if (merged_geometry.IsNull())
|
||||
continue;
|
||||
|
||||
// Merge physic shapes.
|
||||
MObject *merged_object = new MObject;
|
||||
scene->SetupItemComponents(merged_object);
|
||||
MergePhysicShape(merged_object, left->i);
|
||||
MergePhysicShape(merged_object, right->i);
|
||||
|
||||
out_list->Add(new GeometryTransform(merged_object, merged_geometry, Matrix4::IdentityMatrix()));
|
||||
|
||||
running = OnProgress(++step_count, total_step_count);
|
||||
}
|
||||
in_list->Clear();
|
||||
|
||||
AutoList <GeometryTransform *> *tmp_list = in_list; in_list = out_list; out_list = tmp_list;
|
||||
}
|
||||
if (in_list->GetCount() != 1)
|
||||
return false;
|
||||
|
||||
// Remove merged items from scene.
|
||||
ListForeachPtr(MItem *, item, item_list)
|
||||
if (item->GetItemType() == Type_Object)
|
||||
scene->RemoveItem(item);
|
||||
|
||||
// Store output...
|
||||
out_item = (*in_list)[0]->i;
|
||||
out_geo = (*in_list)[0]->g;
|
||||
|
||||
out_item->GetBaseItem()->SetPosition(average_vec);
|
||||
|
||||
out_item->name = "Merged Object";
|
||||
scene->AddItem(out_item, false);
|
||||
|
||||
// ...restore parenting.
|
||||
ListForeachPtr(ItemTransform *, t, child_list)
|
||||
{
|
||||
t->i->GetBaseItem()->SetParent(out_item->GetBaseItem());
|
||||
t->i->GetBaseItem()->SnapshotTransformation(t->m);
|
||||
}
|
||||
child_list.Clear();
|
||||
|
||||
return true;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
Reference in New Issue
Block a user