merge with/from trunk at r35190
[blender.git] / source / blender / collada / DocumentExporter.cpp
index 9d9d105d04d49dcfe628c4fb2d12aa5020595a92..1a648f43097a7b25278d4ed2feb76fdb3b1eff53 100644 (file)
@@ -290,6 +290,446 @@ public:
        }
 };
 
+<<<<<<< .working
+class ImagesExporter: COLLADASW::LibraryImages
+{
+       const char *mfilename;
+       std::vector<std::string> mImages; // contains list of written images, to avoid duplicates
+public:
+       ImagesExporter(COLLADASW::StreamWriter *sw, const char* filename) : COLLADASW::LibraryImages(sw), mfilename(filename)
+       {}
+       
+       void exportImages(Scene *sce)
+       {
+               openLibrary();
+
+               forEachMaterialInScene(sce, *this);
+
+               closeLibrary();
+       }
+
+       void operator()(Material *ma, Object *ob)
+       {
+               int a;
+               for (a = 0; a < MAX_MTEX; a++) {
+                       MTex *mtex = ma->mtex[a];
+                       if (mtex && mtex->tex && mtex->tex->ima) {
+
+                               Image *image = mtex->tex->ima;
+                               std::string name(id_name(image));
+                               name = translate_id(name);
+                               char rel[FILE_MAX];
+                               char abs[FILE_MAX];
+                               char src[FILE_MAX];
+                               char dir[FILE_MAX];
+                               
+                               BLI_split_dirfile(mfilename, dir, NULL);
+
+                               BKE_rebase_path(abs, sizeof(abs), rel, sizeof(rel), G.sce, image->name, dir);
+
+                               if (abs[0] != '\0') {
+
+                                       // make absolute source path
+                                       BLI_strncpy(src, image->name, sizeof(src));
+                                       BLI_path_abs(src, G.sce);
+
+                                       // make dest directory if it doesn't exist
+                                       BLI_make_existing_file(abs);
+                               
+                                       if (BLI_copy_fileops(src, abs) != 0) {
+                                               fprintf(stderr, "Cannot copy image to file's directory. \n");
+                                       }
+                               } 
+                               
+                               if (find(mImages.begin(), mImages.end(), name) == mImages.end()) {
+                                       COLLADASW::Image img(COLLADABU::URI(COLLADABU::URI::nativePathToUri(rel)), name);
+                                       img.add(mSW);
+
+                                       mImages.push_back(name);
+                               }
+                       }
+               }
+       }
+};
+
+class EffectsExporter: COLLADASW::LibraryEffects
+{
+public:
+       EffectsExporter(COLLADASW::StreamWriter *sw) : COLLADASW::LibraryEffects(sw){}
+       void exportEffects(Scene *sce)
+       {
+               openLibrary();
+
+               forEachMaterialInScene(sce, *this);
+
+               closeLibrary();
+       }
+
+       void operator()(Material *ma, Object *ob)
+       {
+               // create a list of indices to textures of type TEX_IMAGE
+               std::vector<int> tex_indices;
+               createTextureIndices(ma, tex_indices);
+
+               openEffect(translate_id(id_name(ma)) + "-effect");
+               
+               COLLADASW::EffectProfile ep(mSW);
+               ep.setProfileType(COLLADASW::EffectProfile::COMMON);
+               ep.openProfile();
+               // set shader type - one of three blinn, phong or lambert
+               if (ma->spec_shader == MA_SPEC_BLINN) {
+                       ep.setShaderType(COLLADASW::EffectProfile::BLINN);
+                       // shininess
+                       ep.setShininess(ma->spec);
+               }
+               else if (ma->spec_shader == MA_SPEC_PHONG) {
+                       ep.setShaderType(COLLADASW::EffectProfile::PHONG);
+                       // shininess
+                       // XXX not sure, stolen this from previous Collada plugin
+                       ep.setShininess(ma->har / 4);
+               }
+               else {
+                       // XXX write warning "Current shader type is not supported" 
+                       ep.setShaderType(COLLADASW::EffectProfile::LAMBERT);
+               }
+               // index of refraction
+               if (ma->mode & MA_RAYTRANSP) {
+                       ep.setIndexOfRefraction(ma->ang);
+               }
+               else {
+                       ep.setIndexOfRefraction(1.0f);
+               }
+       
+               COLLADASW::ColorOrTexture cot;
+
+               // transparency
+               // Tod: because we are in A_ONE mode transparency is calculated like this:
+               ep.setTransparency(1.0f);
+               cot = getcol(0.0f, 0.0f, 0.0f, ma->alpha);
+               ep.setTransparent(cot);
+
+               // emission
+               cot=getcol(ma->emit, ma->emit, ma->emit, 1.0f);
+               ep.setEmission(cot);
+
+               // diffuse 
+               cot = getcol(ma->r, ma->g, ma->b, 1.0f);
+               ep.setDiffuse(cot);
+
+               // ambient
+               cot = getcol(ma->ambr, ma->ambg, ma->ambb, 1.0f);
+               ep.setAmbient(cot);
+
+               // reflective, reflectivity
+               if (ma->mode & MA_RAYMIRROR) {
+                       cot = getcol(ma->mirr, ma->mirg, ma->mirb, 1.0f);
+                       ep.setReflective(cot);
+                       ep.setReflectivity(ma->ray_mirror);
+               }
+               else {
+                       cot = getcol(ma->specr, ma->specg, ma->specb, 1.0f);
+                       ep.setReflective(cot);
+                       ep.setReflectivity(ma->spec);
+               }
+
+               // specular
+               if (ep.getShaderType() != COLLADASW::EffectProfile::LAMBERT) {
+                       cot = getcol(ma->specr, ma->specg, ma->specb, 1.0f);
+                       ep.setSpecular(cot);
+               }       
+
+               // XXX make this more readable if possible
+
+               // create <sampler> and <surface> for each image
+               COLLADASW::Sampler samplers[MAX_MTEX];
+               //COLLADASW::Surface surfaces[MAX_MTEX];
+               //void *samp_surf[MAX_MTEX][2];
+               void *samp_surf[MAX_MTEX][1];
+               
+               // image to index to samp_surf map
+               // samp_surf[index] stores 2 pointers, sampler and surface
+               std::map<std::string, int> im_samp_map;
+
+               unsigned int a, b;
+               for (a = 0, b = 0; a < tex_indices.size(); a++) {
+                       MTex *t = ma->mtex[tex_indices[a]];
+                       Image *ima = t->tex->ima;
+                       
+                       std::string key(id_name(ima));
+                       key = translate_id(key);
+
+                       // create only one <sampler>/<surface> pair for each unique image
+                       if (im_samp_map.find(key) == im_samp_map.end()) {
+                               //<newparam> <surface> <init_from>
+                       //      COLLADASW::Surface surface(COLLADASW::Surface::SURFACE_TYPE_2D,
+//                                                                                key + COLLADASW::Surface::SURFACE_SID_SUFFIX);
+//                             COLLADASW::SurfaceInitOption sio(COLLADASW::SurfaceInitOption::INIT_FROM);
+//                             sio.setImageReference(key);
+//                             surface.setInitOption(sio);
+                               
+                               //<newparam> <sampler> <source>
+                               COLLADASW::Sampler sampler(COLLADASW::Sampler::SAMPLER_TYPE_2D,
+                                                                                  key + COLLADASW::Sampler::SAMPLER_SID_SUFFIX,
+                                                                                  key + COLLADASW::Sampler::SURFACE_SID_SUFFIX);
+                               sampler.setImageId(key);
+                               // copy values to arrays since they will live longer
+                               samplers[a] = sampler;
+                               //surfaces[a] = surface;
+                               
+                               // store pointers so they can be used later when we create <texture>s
+                               samp_surf[b][0] = &samplers[a];
+                               //samp_surf[b][1] = &surfaces[a];
+                               
+                               im_samp_map[key] = b;
+                               b++;
+                       }
+               }
+
+               // used as fallback when MTex->uvname is "" (this is pretty common)
+               // it is indeed the correct value to use in that case
+               std::string active_uv(getActiveUVLayerName(ob));
+
+               // write textures
+               // XXX very slow
+               for (a = 0; a < tex_indices.size(); a++) {
+                       MTex *t = ma->mtex[tex_indices[a]];
+                       Image *ima = t->tex->ima;
+
+                       // we assume map input is always TEXCO_UV
+
+                       std::string key(id_name(ima));
+                       key = translate_id(key);
+                       int i = im_samp_map[key];
+                       COLLADASW::Sampler *sampler = (COLLADASW::Sampler*)samp_surf[i][0];
+                       //COLLADASW::Surface *surface = (COLLADASW::Surface*)samp_surf[i][1];
+
+                       std::string uvname = strlen(t->uvname) ? t->uvname : active_uv;
+
+                       // color
+                       if (t->mapto & MAP_COL) {
+                               ep.setDiffuse(createTexture(ima, uvname, sampler));
+                       }
+                       // ambient
+                       if (t->mapto & MAP_AMB) {
+                               ep.setAmbient(createTexture(ima, uvname, sampler));
+                       }
+                       // specular
+                       if (t->mapto & MAP_SPEC) {
+                               ep.setSpecular(createTexture(ima, uvname, sampler));
+                       }
+                       // emission
+                       if (t->mapto & MAP_EMIT) {
+                               ep.setEmission(createTexture(ima, uvname, sampler));
+                       }
+                       // reflective
+                       if (t->mapto & MAP_REF) {
+                               ep.setReflective(createTexture(ima, uvname, sampler));
+                       }
+                       // alpha
+                       if (t->mapto & MAP_ALPHA) {
+                               ep.setTransparent(createTexture(ima, uvname, sampler));
+                       }
+                       // extension:
+                       // Normal map --> Must be stored with <extra> tag as different technique, 
+                       // since COLLADA doesn't support normal maps, even in current COLLADA 1.5.
+                       if (t->mapto & MAP_NORM) {
+                               COLLADASW::Texture texture(key);
+                               texture.setTexcoord(uvname);
+                               texture.setSampler(*sampler);
+                               // technique FCOLLADA, with the <bump> tag, is most likely the best understood,
+                               // most widespread de-facto standard.
+                               texture.setProfileName("FCOLLADA");
+                               texture.setChildElementName("bump");                            
+                               ep.addExtraTechniqueColorOrTexture(COLLADASW::ColorOrTexture(texture));
+                       }
+               }
+               // performs the actual writing
+               ep.addProfileElements();
+               bool twoSided = false;
+               if (ob->type == OB_MESH && ob->data) {
+                       Mesh *me = (Mesh*)ob->data;
+                       if (me->flag & ME_TWOSIDED)
+                               twoSided = true;
+               }
+               if (twoSided)
+                       ep.addExtraTechniqueParameter("GOOGLEEARTH", "show_double_sided", 1);
+               ep.addExtraTechniques(mSW);
+
+               ep.closeProfile();
+               if (twoSided)
+                       mSW->appendTextBlock("<extra><technique profile=\"MAX3D\"><double_sided>1</double_sided></technique></extra>");
+               closeEffect();  
+       }
+       
+       COLLADASW::ColorOrTexture createTexture(Image *ima,
+                                                                                       std::string& uv_layer_name,
+                                                                                       COLLADASW::Sampler *sampler
+                                                                                       /*COLLADASW::Surface *surface*/)
+       {
+               
+               COLLADASW::Texture texture(translate_id(id_name(ima)));
+               texture.setTexcoord(uv_layer_name);
+               //texture.setSurface(*surface);
+               texture.setSampler(*sampler);
+               
+               COLLADASW::ColorOrTexture cot(texture);
+               return cot;
+       }
+       
+       COLLADASW::ColorOrTexture getcol(float r, float g, float b, float a)
+       {
+               COLLADASW::Color color(r,g,b,a);
+               COLLADASW::ColorOrTexture cot(color);
+               return cot;
+       }
+       
+       //returns the array of mtex indices which have image 
+       //need this for exporting textures
+       void createTextureIndices(Material *ma, std::vector<int> &indices)
+       {
+               indices.clear();
+
+               for (int a = 0; a < MAX_MTEX; a++) {
+                       if (ma->mtex[a] &&
+                               ma->mtex[a]->tex &&
+                               ma->mtex[a]->tex->type == TEX_IMAGE &&
+                               ma->mtex[a]->texco == TEXCO_UV){
+                               indices.push_back(a);
+                       }
+               }
+       }
+};
+
+class MaterialsExporter: COLLADASW::LibraryMaterials
+{
+public:
+       MaterialsExporter(COLLADASW::StreamWriter *sw): COLLADASW::LibraryMaterials(sw){}
+       void exportMaterials(Scene *sce)
+       {
+               openLibrary();
+
+               forEachMaterialInScene(sce, *this);
+
+               closeLibrary();
+       }
+
+       void operator()(Material *ma, Object *ob)
+       {
+               std::string name(id_name(ma));
+
+               openMaterial(translate_id(name), name);
+
+               std::string efid = translate_id(name) + "-effect";
+               addInstanceEffect(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, efid));
+
+               closeMaterial();
+       }
+};
+
+class CamerasExporter: COLLADASW::LibraryCameras
+{
+public:
+       CamerasExporter(COLLADASW::StreamWriter *sw): COLLADASW::LibraryCameras(sw){}
+       void exportCameras(Scene *sce)
+       {
+               openLibrary();
+               
+               forEachCameraObjectInScene(sce, *this);
+               
+               closeLibrary();
+       }
+       void operator()(Object *ob, Scene *sce)
+       {
+               // XXX add other params later
+               Camera *cam = (Camera*)ob->data;
+               std::string cam_id(get_camera_id(ob));
+               std::string cam_name(id_name(cam));
+               
+               if (cam->type == CAM_PERSP) {
+                       COLLADASW::PerspectiveOptic persp(mSW);
+                       persp.setXFov(1.0);
+                       persp.setAspectRatio(0.1);
+                       persp.setZFar(cam->clipend);
+                       persp.setZNear(cam->clipsta);
+                       COLLADASW::Camera ccam(mSW, &persp, cam_id, cam_name);
+                       addCamera(ccam);
+               }
+               else {
+                       COLLADASW::OrthographicOptic ortho(mSW);
+                       ortho.setXMag(1.0);
+                       ortho.setAspectRatio(0.1);
+                       ortho.setZFar(cam->clipend);
+                       ortho.setZNear(cam->clipsta);
+                       COLLADASW::Camera ccam(mSW, &ortho, cam_id, cam_name);
+                       addCamera(ccam);
+               }
+       }       
+};
+
+class LightsExporter: COLLADASW::LibraryLights
+{
+public:
+       LightsExporter(COLLADASW::StreamWriter *sw): COLLADASW::LibraryLights(sw){}
+       void exportLights(Scene *sce)
+       {
+               openLibrary();
+               
+               forEachLampObjectInScene(sce, *this);
+               
+               closeLibrary();
+       }
+       void operator()(Object *ob)
+       {
+               Lamp *la = (Lamp*)ob->data;
+               std::string la_id(get_light_id(ob));
+               std::string la_name(id_name(la));
+               COLLADASW::Color col(la->r, la->g, la->b);
+               float e = la->energy;
+               
+               // sun
+               if (la->type == LA_SUN) {
+                       COLLADASW::DirectionalLight cla(mSW, la_id, la_name, e);
+                       cla.setColor(col);
+                       addLight(cla);
+               }
+               // hemi
+               else if (la->type == LA_HEMI) {
+                       COLLADASW::AmbientLight cla(mSW, la_id, la_name, e);
+                       cla.setColor(col);
+                       addLight(cla);
+               }
+               // spot
+               else if (la->type == LA_SPOT) {
+                       COLLADASW::SpotLight cla(mSW, la_id, la_name, e);
+                       cla.setColor(col);
+                       cla.setFallOffAngle(la->spotsize);
+                       cla.setFallOffExponent(la->spotblend);
+                       cla.setLinearAttenuation(la->att1);
+                       cla.setQuadraticAttenuation(la->att2);
+                       addLight(cla);
+               }
+               // lamp
+               else if (la->type == LA_LOCAL) {
+                       COLLADASW::PointLight cla(mSW, la_id, la_name, e);
+                       cla.setColor(col);
+                       cla.setLinearAttenuation(la->att1);
+                       cla.setQuadraticAttenuation(la->att2);
+                       addLight(cla);
+               }
+               // area lamp is not supported
+               // it will be exported as a local lamp
+               else {
+                       COLLADASW::PointLight cla(mSW, la_id, la_name, e);
+                       cla.setColor(col);
+                       cla.setLinearAttenuation(la->att1);
+                       cla.setQuadraticAttenuation(la->att2);
+                       addLight(cla);
+               }
+       }
+};
+
+=======
+>>>>>>> .merge-right.r35190
 // TODO: it would be better to instantiate animations rather than create a new one per object
 // COLLADA allows this through multiple <channel>s in <animation>.
 // For this to work, we need to know objects that use a certain action.
@@ -965,6 +1405,17 @@ void DocumentExporter::exportCurrentScene(Scene *sce, const char* filename)
 
        asset.setUnit(unitname, linearmeasure);
        asset.setUpAxisType(COLLADASW::Asset::Z_UP);
+<<<<<<< .working
+       // TODO: need an Author field in userpref
+       asset.getContributor().mAuthor = "Blender User";
+#ifdef NAN_BUILDINFO
+       char version_buf[128];
+       sprintf(version_buf, "Blender %d.%02d.%d r%s", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION, build_rev);
+       asset.getContributor().mAuthoringTool = version_buf;
+#else
+       asset.getContributor().mAuthoringTool = "Blender 2.5x";
+#endif
+=======
        // TODO: need an Author field in userpref
        if(strlen(U.author) > 0) {
                asset.getContributor().mAuthor = U.author;
@@ -979,6 +1430,7 @@ void DocumentExporter::exportCurrentScene(Scene *sce, const char* filename)
 #else
        asset.getContributor().mAuthoringTool = "Blender 2.5x";
 #endif
+>>>>>>> .merge-right.r35190
        asset.add();
        
        // <library_cameras>