Blender V2.61 - r43446

LightExporter.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
00019  *                 Nathan Letwory
00020  *
00021  * ***** END GPL LICENSE BLOCK *****
00022  */
00023 
00028 #include <string>
00029 
00030 #include "COLLADASWColor.h"
00031 #include "COLLADASWLight.h"
00032 
00033 #include "BLI_math.h"
00034 
00035 #include "LightExporter.h"
00036 #include "collada_internal.h"
00037 
00038 template<class Functor>
00039 void forEachLampObjectInScene(Scene *sce, Functor &f, bool export_selected)
00040 {
00041     Base *base= (Base*) sce->base.first;
00042     while(base) {
00043         Object *ob = base->object;
00044             
00045         if (ob->type == OB_LAMP && ob->data
00046             && !(export_selected && !(ob->flag & SELECT))) {
00047             f(ob);
00048         }
00049         base= base->next;
00050     }
00051 }
00052 
00053 LightsExporter::LightsExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings): COLLADASW::LibraryLights(sw), export_settings(export_settings) {}
00054 
00055 void LightsExporter::exportLights(Scene *sce)
00056 {
00057     openLibrary();
00058     
00059     forEachLampObjectInScene(sce, *this, this->export_settings->selected);
00060     
00061     closeLibrary();
00062 }
00063 
00064 void LightsExporter::operator()(Object *ob)
00065 {
00066     Lamp *la = (Lamp*)ob->data;
00067     std::string la_id(get_light_id(ob));
00068     std::string la_name(id_name(la));
00069     COLLADASW::Color col(la->r * la->energy, la->g * la->energy, la->b * la->energy);
00070     float d, constatt, linatt, quadatt;
00071     
00072     d = la->dist;
00073     
00074     constatt = 1.0f;
00075     
00076     if(la->falloff_type==LA_FALLOFF_INVLINEAR) {
00077         linatt = 1.0f / d;
00078         quadatt = 0.0f;
00079     }
00080     else {
00081         linatt = 0.0f;
00082         quadatt = 1.0f / (d * d);
00083     }
00084     
00085     // sun
00086     if (la->type == LA_SUN) {
00087         COLLADASW::DirectionalLight cla(mSW, la_id, la_name);
00088         cla.setColor(col,false,"color");
00089         cla.setConstantAttenuation(constatt);
00090         exportBlenderProfile(cla, la);
00091         addLight(cla);
00092     }
00093     // hemi
00094     else if (la->type == LA_HEMI) {
00095         COLLADASW::AmbientLight cla(mSW, la_id, la_name);
00096         cla.setColor(col,false,"color");
00097         cla.setConstantAttenuation(constatt);
00098         exportBlenderProfile(cla, la);
00099         addLight(cla);
00100     }
00101     // spot
00102     else if (la->type == LA_SPOT) {
00103         COLLADASW::SpotLight cla(mSW, la_id, la_name);
00104         cla.setColor(col,false,"color");
00105         cla.setFallOffAngle(la->spotsize,false,"fall_off_angle");
00106         cla.setFallOffExponent(la->spotblend,false,"fall_off_exponent");
00107         cla.setConstantAttenuation(constatt);
00108         cla.setLinearAttenuation(linatt);
00109         cla.setQuadraticAttenuation(quadatt);
00110         exportBlenderProfile(cla, la);
00111         addLight(cla);
00112     }
00113     // lamp
00114     else if (la->type == LA_LOCAL) {
00115         COLLADASW::PointLight cla(mSW, la_id, la_name);
00116         cla.setColor(col,false,"color");
00117         cla.setConstantAttenuation(constatt);
00118         cla.setLinearAttenuation(linatt);
00119         cla.setQuadraticAttenuation(quadatt);
00120         exportBlenderProfile(cla, la);
00121         addLight(cla);
00122     }
00123     // area lamp is not supported
00124     // it will be exported as a local lamp
00125     else {
00126         COLLADASW::PointLight cla(mSW, la_id, la_name);
00127         cla.setColor(col,false,"color");
00128         cla.setConstantAttenuation(constatt);
00129         cla.setLinearAttenuation(linatt);
00130         cla.setQuadraticAttenuation(quadatt);
00131         exportBlenderProfile(cla, la);
00132         addLight(cla);
00133     }
00134     
00135 }
00136 
00137 bool LightsExporter::exportBlenderProfile(COLLADASW::Light &cla, Lamp *la)
00138 {
00139     cla.addExtraTechniqueParameter("blender", "type", la->type);
00140     cla.addExtraTechniqueParameter("blender", "flag", la->flag);
00141     cla.addExtraTechniqueParameter("blender", "mode", la->mode);
00142     cla.addExtraTechniqueParameter("blender", "gamma", la->k, "blender_gamma");
00143     cla.addExtraTechniqueParameter("blender", "red", la->r);
00144     cla.addExtraTechniqueParameter("blender", "green", la->g);
00145     cla.addExtraTechniqueParameter("blender", "blue", la->b);
00146     cla.addExtraTechniqueParameter("blender", "shadow_r", la->shdwr, "blender_shadow_r");
00147     cla.addExtraTechniqueParameter("blender", "shadow_g", la->shdwg, "blender_shadow_g");
00148     cla.addExtraTechniqueParameter("blender", "shadow_b", la->shdwb, "blender_shadow_b");
00149     cla.addExtraTechniqueParameter("blender", "energy", la->energy, "blender_energy");
00150     cla.addExtraTechniqueParameter("blender", "dist", la->dist, "blender_dist");
00151     cla.addExtraTechniqueParameter("blender", "spotsize", la->spotsize);
00152     cla.addExtraTechniqueParameter("blender", "spotblend", la->spotblend);
00153     cla.addExtraTechniqueParameter("blender", "halo_intensity", la->haint, "blnder_halo_intensity");
00154     cla.addExtraTechniqueParameter("blender", "att1", la->att1);
00155     cla.addExtraTechniqueParameter("blender", "att2", la->att2);
00156     // \todo figure out how we can have falloff curve supported here
00157     cla.addExtraTechniqueParameter("blender", "falloff_type", la->falloff_type);
00158     cla.addExtraTechniqueParameter("blender", "clipsta", la->clipsta);
00159     cla.addExtraTechniqueParameter("blender", "clipend", la->clipend);
00160     cla.addExtraTechniqueParameter("blender", "shadspotsize", la->shadspotsize);
00161     cla.addExtraTechniqueParameter("blender", "bias", la->bias);
00162     cla.addExtraTechniqueParameter("blender", "soft", la->soft);
00163     cla.addExtraTechniqueParameter("blender", "compressthresh", la->compressthresh);
00164     cla.addExtraTechniqueParameter("blender", "bufsize", la->bufsize);
00165     cla.addExtraTechniqueParameter("blender", "samp", la->samp);
00166     cla.addExtraTechniqueParameter("blender", "buffers", la->buffers);
00167     cla.addExtraTechniqueParameter("blender", "filtertype", la->filtertype);
00168     cla.addExtraTechniqueParameter("blender", "bufflag", la->bufflag);
00169     cla.addExtraTechniqueParameter("blender", "buftype", la->buftype);
00170     cla.addExtraTechniqueParameter("blender", "ray_samp", la->ray_samp);
00171     cla.addExtraTechniqueParameter("blender", "ray_sampy", la->ray_sampy);
00172     cla.addExtraTechniqueParameter("blender", "ray_sampz", la->ray_sampz);
00173     cla.addExtraTechniqueParameter("blender", "ray_samp_type", la->ray_samp_type);
00174     cla.addExtraTechniqueParameter("blender", "area_shape", la->area_shape);
00175     cla.addExtraTechniqueParameter("blender", "area_size", la->area_size);
00176     cla.addExtraTechniqueParameter("blender", "area_sizey", la->area_sizey);
00177     cla.addExtraTechniqueParameter("blender", "area_sizez", la->area_sizez);
00178     cla.addExtraTechniqueParameter("blender", "adapt_thresh", la->adapt_thresh);
00179     cla.addExtraTechniqueParameter("blender", "ray_samp_method", la->ray_samp_method);
00180     cla.addExtraTechniqueParameter("blender", "shadhalostep", la->shadhalostep);
00181     cla.addExtraTechniqueParameter("blender", "sun_effect_type", la->shadhalostep);
00182     cla.addExtraTechniqueParameter("blender", "skyblendtype", la->skyblendtype);
00183     cla.addExtraTechniqueParameter("blender", "horizon_brightness", la->horizon_brightness);
00184     cla.addExtraTechniqueParameter("blender", "spread", la->spread);
00185     cla.addExtraTechniqueParameter("blender", "sun_brightness", la->sun_brightness);
00186     cla.addExtraTechniqueParameter("blender", "sun_size", la->sun_size);
00187     cla.addExtraTechniqueParameter("blender", "backscattered_light", la->backscattered_light);
00188     cla.addExtraTechniqueParameter("blender", "sun_intensity", la->sun_intensity);
00189     cla.addExtraTechniqueParameter("blender", "atm_turbidity", la->atm_turbidity);
00190     cla.addExtraTechniqueParameter("blender", "atm_extinction_factor", la->atm_extinction_factor);
00191     cla.addExtraTechniqueParameter("blender", "atm_distance_factor", la->atm_distance_factor);
00192     cla.addExtraTechniqueParameter("blender", "skyblendfac", la->skyblendfac);
00193     cla.addExtraTechniqueParameter("blender", "sky_exposure", la->sky_exposure);
00194     cla.addExtraTechniqueParameter("blender", "sky_colorspace", la->sky_colorspace);
00195     
00196     return true;
00197 }