d74a1d1033d1f32fe7879a01b73e70edd7806e86
[blender-staging.git] / source / blender / collada / LightExporter.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
19  *                 Nathan Letwory
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  */
23
24 /** \file blender/collada/LightExporter.cpp
25  *  \ingroup collada
26  */
27
28 #include <string>
29
30 #include "COLLADASWColor.h"
31 #include "COLLADASWLight.h"
32
33 #include "BLI_math.h"
34
35 #include "LightExporter.h"
36 #include "collada_internal.h"
37
38 template<class Functor>
39 void forEachLampObjectInScene(Scene *sce, Functor &f, bool export_selected)
40 {
41         Base *base= (Base*) sce->base.first;
42         while(base) {
43                 Object *ob = base->object;
44                         
45                 if (ob->type == OB_LAMP && ob->data
46                         && !(export_selected && !(ob->flag & SELECT))) {
47                         f(ob);
48                 }
49                 base= base->next;
50         }
51 }
52
53 LightsExporter::LightsExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings): COLLADASW::LibraryLights(sw), export_settings(export_settings) {}
54
55 void LightsExporter::exportLights(Scene *sce)
56 {
57         openLibrary();
58         
59         forEachLampObjectInScene(sce, *this, this->export_settings->selected);
60         
61         closeLibrary();
62 }
63
64 void LightsExporter::operator()(Object *ob)
65 {
66         Lamp *la = (Lamp*)ob->data;
67         std::string la_id(get_light_id(ob));
68         std::string la_name(id_name(la));
69         COLLADASW::Color col(la->r * la->energy, la->g * la->energy, la->b * la->energy);
70         float d, constatt, linatt, quadatt;
71         
72         d = la->dist;
73         
74         constatt = 1.0f;
75         
76         if(la->falloff_type==LA_FALLOFF_INVLINEAR) {
77                 linatt = 1.0f / d;
78                 quadatt = 0.0f;
79         }
80         else {
81                 linatt = 0.0f;
82                 quadatt = 1.0f / (d * d);
83         }
84         
85         // sun
86         if (la->type == LA_SUN) {
87                 COLLADASW::DirectionalLight cla(mSW, la_id, la_name);
88                 cla.setColor(col,false,"color");
89                 cla.setConstantAttenuation(constatt);
90                 exportBlenderProfile(cla, la);
91                 addLight(cla);
92         }
93         // hemi
94         else if (la->type == LA_HEMI) {
95                 COLLADASW::AmbientLight cla(mSW, la_id, la_name);
96                 cla.setColor(col,false,"color");
97                 cla.setConstantAttenuation(constatt);
98                 exportBlenderProfile(cla, la);
99                 addLight(cla);
100         }
101         // spot
102         else if (la->type == LA_SPOT) {
103                 COLLADASW::SpotLight cla(mSW, la_id, la_name);
104                 cla.setColor(col,false,"color");
105                 cla.setFallOffAngle(la->spotsize,false,"fall_off_angle");
106                 cla.setFallOffExponent(la->spotblend,false,"fall_off_exponent");
107                 cla.setConstantAttenuation(constatt);
108                 cla.setLinearAttenuation(linatt);
109                 cla.setQuadraticAttenuation(quadatt);
110                 exportBlenderProfile(cla, la);
111                 addLight(cla);
112         }
113         // lamp
114         else if (la->type == LA_LOCAL) {
115                 COLLADASW::PointLight cla(mSW, la_id, la_name);
116                 cla.setColor(col,false,"color");
117                 cla.setConstantAttenuation(constatt);
118                 cla.setLinearAttenuation(linatt);
119                 cla.setQuadraticAttenuation(quadatt);
120                 exportBlenderProfile(cla, la);
121                 addLight(cla);
122         }
123         // area lamp is not supported
124         // it will be exported as a local lamp
125         else {
126                 COLLADASW::PointLight cla(mSW, la_id, la_name);
127                 cla.setColor(col,false,"color");
128                 cla.setConstantAttenuation(constatt);
129                 cla.setLinearAttenuation(linatt);
130                 cla.setQuadraticAttenuation(quadatt);
131                 exportBlenderProfile(cla, la);
132                 addLight(cla);
133         }
134         
135 }
136
137 bool LightsExporter::exportBlenderProfile(COLLADASW::Light &cla, Lamp *la)
138 {
139         cla.addExtraTechniqueParameter("blender", "type", la->type);
140         cla.addExtraTechniqueParameter("blender", "flag", la->flag);
141         cla.addExtraTechniqueParameter("blender", "mode", la->mode);
142         cla.addExtraTechniqueParameter("blender", "gamma", la->k, "blender_gamma");
143         cla.addExtraTechniqueParameter("blender", "red", la->r);
144         cla.addExtraTechniqueParameter("blender", "green", la->g);
145         cla.addExtraTechniqueParameter("blender", "blue", la->b);
146         cla.addExtraTechniqueParameter("blender", "shadow_r", la->shdwr, "blender_shadow_r");
147         cla.addExtraTechniqueParameter("blender", "shadow_g", la->shdwg, "blender_shadow_g");
148         cla.addExtraTechniqueParameter("blender", "shadow_b", la->shdwb, "blender_shadow_b");
149         cla.addExtraTechniqueParameter("blender", "energy", la->energy, "blender_energy");
150         cla.addExtraTechniqueParameter("blender", "dist", la->dist, "blender_dist");
151         cla.addExtraTechniqueParameter("blender", "spotsize", la->spotsize);
152         cla.addExtraTechniqueParameter("blender", "spotblend", la->spotblend);
153         cla.addExtraTechniqueParameter("blender", "halo_intensity", la->haint, "blnder_halo_intensity");
154         cla.addExtraTechniqueParameter("blender", "att1", la->att1);
155         cla.addExtraTechniqueParameter("blender", "att2", la->att2);
156         // \todo figure out how we can have falloff curve supported here
157         cla.addExtraTechniqueParameter("blender", "falloff_type", la->falloff_type);
158         cla.addExtraTechniqueParameter("blender", "clipsta", la->clipsta);
159         cla.addExtraTechniqueParameter("blender", "clipend", la->clipend);
160         cla.addExtraTechniqueParameter("blender", "shadspotsize", la->shadspotsize);
161         cla.addExtraTechniqueParameter("blender", "bias", la->bias);
162         cla.addExtraTechniqueParameter("blender", "soft", la->soft);
163         cla.addExtraTechniqueParameter("blender", "compressthresh", la->compressthresh);
164         cla.addExtraTechniqueParameter("blender", "bufsize", la->bufsize);
165         cla.addExtraTechniqueParameter("blender", "samp", la->samp);
166         cla.addExtraTechniqueParameter("blender", "buffers", la->buffers);
167         cla.addExtraTechniqueParameter("blender", "filtertype", la->filtertype);
168         cla.addExtraTechniqueParameter("blender", "bufflag", la->bufflag);
169         cla.addExtraTechniqueParameter("blender", "buftype", la->buftype);
170         cla.addExtraTechniqueParameter("blender", "ray_samp", la->ray_samp);
171         cla.addExtraTechniqueParameter("blender", "ray_sampy", la->ray_sampy);
172         cla.addExtraTechniqueParameter("blender", "ray_sampz", la->ray_sampz);
173         cla.addExtraTechniqueParameter("blender", "ray_samp_type", la->ray_samp_type);
174         cla.addExtraTechniqueParameter("blender", "area_shape", la->area_shape);
175         cla.addExtraTechniqueParameter("blender", "area_size", la->area_size);
176         cla.addExtraTechniqueParameter("blender", "area_sizey", la->area_sizey);
177         cla.addExtraTechniqueParameter("blender", "area_sizez", la->area_sizez);
178         cla.addExtraTechniqueParameter("blender", "adapt_thresh", la->adapt_thresh);
179         cla.addExtraTechniqueParameter("blender", "ray_samp_method", la->ray_samp_method);
180         cla.addExtraTechniqueParameter("blender", "shadhalostep", la->shadhalostep);
181         cla.addExtraTechniqueParameter("blender", "sun_effect_type", la->shadhalostep);
182         cla.addExtraTechniqueParameter("blender", "skyblendtype", la->skyblendtype);
183         cla.addExtraTechniqueParameter("blender", "horizon_brightness", la->horizon_brightness);
184         cla.addExtraTechniqueParameter("blender", "spread", la->spread);
185         cla.addExtraTechniqueParameter("blender", "sun_brightness", la->sun_brightness);
186         cla.addExtraTechniqueParameter("blender", "sun_size", la->sun_size);
187         cla.addExtraTechniqueParameter("blender", "backscattered_light", la->backscattered_light);
188         cla.addExtraTechniqueParameter("blender", "sun_intensity", la->sun_intensity);
189         cla.addExtraTechniqueParameter("blender", "atm_turbidity", la->atm_turbidity);
190         cla.addExtraTechniqueParameter("blender", "atm_extinction_factor", la->atm_extinction_factor);
191         cla.addExtraTechniqueParameter("blender", "atm_distance_factor", la->atm_distance_factor);
192         cla.addExtraTechniqueParameter("blender", "skyblendfac", la->skyblendfac);
193         cla.addExtraTechniqueParameter("blender", "sky_exposure", la->sky_exposure);
194         cla.addExtraTechniqueParameter("blender", "sky_colorspace", la->sky_colorspace);
195         
196         return true;
197 }