From 8da10f59eb10d2ee135f270c0e1a9ccd20f4f300 Mon Sep 17 00:00:00 2001 From: Arystanbek Dyussenov Date: Thu, 11 Mar 2010 15:37:19 +0000 Subject: [PATCH] Merge -c 27223 from COLLADA branch into trunk. Vertex color export was requested and reported to work by Blake Maltby. --- source/blender/collada/DocumentExporter.cpp | 66 +++++++++++++++++---- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/source/blender/collada/DocumentExporter.cpp b/source/blender/collada/DocumentExporter.cpp index dfc41074ae6..f90a494fd8d 100644 --- a/source/blender/collada/DocumentExporter.cpp +++ b/source/blender/collada/DocumentExporter.cpp @@ -359,6 +359,8 @@ public: std::vector nor; std::vector norind; + bool has_color = (bool)CustomData_has_layer(&me->fdata, CD_MCOL); + create_normals(nor, norind, me); // openMesh(geoId, geoName, meshId) @@ -370,12 +372,15 @@ public: // writes for normal coords createNormalsSource(geom_id, me, nor); - int has_uvs = CustomData_has_layer(&me->fdata, CD_MTFACE); + bool has_uvs = (bool)CustomData_has_layer(&me->fdata, CD_MTFACE); // writes for uv coords if mesh has uv coords - if (has_uvs) { - createTexcoordsSource(geom_id, (Mesh*)ob->data); - } + if (has_uvs) + createTexcoordsSource(geom_id, me); + + if (has_color) + createVertexColorSource(geom_id, me); + // COLLADASW::Vertices verts(mSW); verts.setId(getIdBySemantics(geom_id, COLLADASW::VERTEX)); @@ -389,11 +394,11 @@ public: for(int a = 0; a < ob->totcol; a++) { // account for NULL materials, this should not normally happen? Material *ma = give_current_material(ob, a + 1); - createPolylist(ma != NULL, a, has_uvs, ob, geom_id, norind); + createPolylist(ma != NULL, a, has_uvs, has_color, ob, geom_id, norind); } } else { - createPolylist(false, 0, has_uvs, ob, geom_id, norind); + createPolylist(false, 0, has_uvs, has_color, ob, geom_id, norind); } closeMesh(); @@ -408,14 +413,11 @@ public: void createPolylist(bool has_material, int material_index, bool has_uvs, + bool has_color, Object *ob, std::string& geom_id, std::vector& norind) { -#if 0 - MFace *mfaces = dm->getFaceArray(dm); - int totfaces = dm->getNumFaces(dm); -#endif Mesh *me = (Mesh*)ob->data; MFace *mfaces = me->mface; int totfaces = me->totface; @@ -479,6 +481,11 @@ public: ); til.push_back(input3); } + + if (has_color) { + COLLADASW::Input input4(COLLADASW::COLOR, getUrlBySemantics(geom_id, COLLADASW::COLOR), has_uvs ? 3 : 2); + til.push_back(input4); + } // sets polylist.setVCountList(vcount_list); @@ -501,6 +508,9 @@ public: if (has_uvs) polylist.appendValues(texindex + j); + + if (has_color) + polylist.appendValues(texindex + j); } } @@ -545,6 +555,42 @@ public: } + void createVertexColorSource(std::string geom_id, Mesh *me) + { + if (!CustomData_has_layer(&me->fdata, CD_MCOL)) + return; + + MFace *f; + int totcolor = 0, i, j; + + for (i = 0, f = me->mface; i < me->totface; i++, f++) + totcolor += f->v4 ? 4 : 3; + + COLLADASW::FloatSourceF source(mSW); + source.setId(getIdBySemantics(geom_id, COLLADASW::COLOR)); + source.setArrayId(getIdBySemantics(geom_id, COLLADASW::COLOR) + ARRAY_ID_SUFFIX); + source.setAccessorCount(totcolor); + source.setAccessorStride(3); + + COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList(); + param.push_back("R"); + param.push_back("G"); + param.push_back("B"); + + source.prepareToAppendValues(); + + int index = CustomData_get_active_layer_index(&me->fdata, CD_MCOL); + + MCol *mcol = (MCol*)me->fdata.layers[index].data; + MCol *c = mcol; + + for (i = 0, f = me->mface; i < me->totface; i++, c += 4, f++) + for (j = 0; j < (f->v4 ? 4 : 3); j++) + source.appendValues(c[j].b / 255.0f, c[j].g / 255.0f, c[j].r / 255.0f); + + source.finish(); + } + std::string makeTexcoordSourceId(std::string& geom_id, int layer_index) { char suffix[20];