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