Added Support for Custom Vertex Normals to Collada exporter

This commit is contained in:
Gaia Clary 2015-04-16 19:02:00 +02:00
parent a7cae2987d
commit bbae0664a3
2 changed files with 42 additions and 17 deletions

@ -82,6 +82,9 @@ void GeometryExporter::operator()(Object *ob)
this->export_settings->apply_modifiers,
this->export_settings->triangulate);
Mesh *mesh = (Mesh *) ob->data;
me->flag = mesh->flag;
std::string geom_id = get_geometry_id(ob, use_instantiation);
std::vector<Normal> nor;
std::vector<BCPolygonNormalsIndices> norind;
@ -563,6 +566,11 @@ void GeometryExporter::createTexcoordsSource(std::string geom_id, Mesh *me)
}
}
bool operator<(const Normal &a, const Normal &b)
{
/* only needed to sort normal vectors and find() them later in a map.*/
return a.x < b.x || (a.x == b.x && (a.y < b.y || (a.y == b.y && a.z < b.z)));
}
//creates <source> for normals
void GeometryExporter::createNormalsSource(std::string geom_id, Mesh *me, std::vector<Normal>& nor)
@ -596,11 +604,18 @@ void GeometryExporter::createNormalsSource(std::string geom_id, Mesh *me, std::v
void GeometryExporter::create_normals(std::vector<Normal> &normals, std::vector<BCPolygonNormalsIndices> &polygons_normals, Mesh *me)
{
std::map<unsigned int, unsigned int> shared_normal_indices;
std::map<Normal, unsigned int> shared_normal_indices;
int last_normal_index = -1;
MVert *verts = me->mvert;
MLoop *mloops = me->mloop;
float(*lnors)[3];
BKE_mesh_calc_normals_split(me);
if (CustomData_has_layer(&me->ldata, CD_NORMAL)) {
lnors = (float(*)[3])CustomData_get_layer(&me->ldata, CD_NORMAL);
}
for (int poly_index = 0; poly_index < me->totpoly; poly_index++) {
MPoly *mpoly = &me->mpoly[poly_index];
@ -615,25 +630,24 @@ void GeometryExporter::create_normals(std::vector<Normal> &normals, std::vector<
last_normal_index++;
}
MLoop *mloop = mloops + mpoly->loopstart;
BCPolygonNormalsIndices poly_indices;
for (int loop_index = 0; loop_index < mpoly->totloop; loop_index++) {
unsigned int vertex_index = mloop[loop_index].v;
unsigned int loop_idx = mpoly->loopstart + loop_index;
if (mpoly->flag & ME_SMOOTH) {
if (shared_normal_indices.find(vertex_index) != shared_normal_indices.end())
poly_indices.add_index (shared_normal_indices[vertex_index]);
float normalized[3];
normalize_v3_v3(normalized, lnors[loop_idx]);
Normal n = { normalized[0], normalized[1], normalized[2] };
if (shared_normal_indices.find(n) != shared_normal_indices.end()) {
poly_indices.add_index(shared_normal_indices[n]);
}
else {
float vector[3];
normal_short_to_float_v3(vector, verts[vertex_index].no);
Normal n = { vector[0], vector[1], vector[2] };
normals.push_back(n);
last_normal_index++;
poly_indices.add_index(last_normal_index);
shared_normal_indices[vertex_index] = last_normal_index;
shared_normal_indices[n] = last_normal_index;
normals.push_back(n);
}
}
else {

@ -48,6 +48,20 @@
extern Object *bc_get_highest_selected_ancestor_or_self(Object *ob);
class Normal
{
public:
float x;
float y;
float z;
friend bool operator< (const Normal &, const Normal &);
};
bool operator< (const Normal &, const Normal &);
// TODO: optimize UV sets by making indexed list with duplicates removed
class GeometryExporter : COLLADASW::LibraryGeometries
{
@ -56,10 +70,7 @@ class GeometryExporter : COLLADASW::LibraryGeometries
unsigned int v1, v2, v3, v4;
};
struct Normal
{
float x, y, z;
};
Normal n;
Scene *mScene;