doxygen: blender/collada tagged.
[blender-staging.git] / source / blender / collada / EffectExporter.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/EffectExporter.cpp
27  *  \ingroup collada
28  */
29
30
31 #include <map>
32
33 #include "COLLADASWEffectProfile.h"
34
35 #include "EffectExporter.h"
36 #include "MaterialExporter.h"
37
38 #include "DNA_mesh_types.h"
39 #include "DNA_texture_types.h"
40
41 #include "BKE_customdata.h"
42
43 #include "collada_internal.h"
44 #include "collada_utils.h"
45
46 // OB_MESH is assumed
47 static std::string getActiveUVLayerName(Object *ob)
48 {
49         Mesh *me = (Mesh*)ob->data;
50
51         int num_layers = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
52         if (num_layers)
53                 return std::string(bc_CustomData_get_active_layer_name(&me->fdata, CD_MTFACE));
54                 
55         return "";
56 }
57
58
59 EffectsExporter::EffectsExporter(COLLADASW::StreamWriter *sw) : COLLADASW::LibraryEffects(sw){}
60 void EffectsExporter::exportEffects(Scene *sce)
61 {
62         openLibrary();
63         MaterialFunctor mf;
64         mf.forEachMaterialInScene<EffectsExporter>(sce, *this);
65
66         closeLibrary();
67 }
68
69 void EffectsExporter::operator()(Material *ma, Object *ob)
70 {
71         // create a list of indices to textures of type TEX_IMAGE
72         std::vector<int> tex_indices;
73         createTextureIndices(ma, tex_indices);
74
75         openEffect(translate_id(id_name(ma)) + "-effect");
76         
77         COLLADASW::EffectProfile ep(mSW);
78         ep.setProfileType(COLLADASW::EffectProfile::COMMON);
79         ep.openProfile();
80         // set shader type - one of three blinn, phong or lambert
81         if (ma->spec_shader == MA_SPEC_BLINN) {
82                 ep.setShaderType(COLLADASW::EffectProfile::BLINN);
83                 // shininess
84                 ep.setShininess(ma->har);
85         }
86         else if (ma->spec_shader == MA_SPEC_PHONG) {
87                 ep.setShaderType(COLLADASW::EffectProfile::PHONG);
88                 // shininess
89                 ep.setShininess(ma->har);
90         }
91         else {
92                 // XXX write warning "Current shader type is not supported" 
93                 ep.setShaderType(COLLADASW::EffectProfile::LAMBERT);
94         }
95         // index of refraction
96         if (ma->mode & MA_RAYTRANSP) {
97                 ep.setIndexOfRefraction(ma->ang);
98         }
99         else {
100                 ep.setIndexOfRefraction(1.0f);
101         }
102
103         COLLADASW::ColorOrTexture cot;
104
105         // transparency
106         if (ma->mode & MA_TRANSP) {
107                 // Tod: because we are in A_ONE mode transparency is calculated like this:
108                 ep.setTransparency(ma->alpha);
109                 // cot = getcol(1.0f, 1.0f, 1.0f, 1.0f);
110                 // ep.setTransparent(cot);
111         }
112
113         // emission
114         cot=getcol(ma->emit, ma->emit, ma->emit, 1.0f);
115         ep.setEmission(cot);
116
117         // diffuse multiplied by diffuse intensity
118         cot = getcol(ma->r * ma->ref, ma->g * ma->ref, ma->b * ma->ref, 1.0f);
119         ep.setDiffuse(cot);
120
121         // ambient
122         cot = getcol(ma->ambr, ma->ambg, ma->ambb, 1.0f);
123         ep.setAmbient(cot);
124
125         // reflective, reflectivity
126         if (ma->mode & MA_RAYMIRROR) {
127                 cot = getcol(ma->mirr, ma->mirg, ma->mirb, 1.0f);
128                 ep.setReflective(cot);
129                 ep.setReflectivity(ma->ray_mirror);
130         }
131         // else {
132         //      cot = getcol(ma->specr, ma->specg, ma->specb, 1.0f);
133         //      ep.setReflective(cot);
134         //      ep.setReflectivity(ma->spec);
135         // }
136
137         // specular
138         if (ep.getShaderType() != COLLADASW::EffectProfile::LAMBERT) {
139                 cot = getcol(ma->specr * ma->spec, ma->specg * ma->spec, ma->specb * ma->spec, 1.0f);
140                 ep.setSpecular(cot);
141         }       
142
143         // XXX make this more readable if possible
144
145         // create <sampler> and <surface> for each image
146         COLLADASW::Sampler samplers[MAX_MTEX];
147         //COLLADASW::Surface surfaces[MAX_MTEX];
148         //void *samp_surf[MAX_MTEX][2];
149         void *samp_surf[MAX_MTEX][1];
150         
151         // image to index to samp_surf map
152         // samp_surf[index] stores 2 pointers, sampler and surface
153         std::map<std::string, int> im_samp_map;
154
155         unsigned int a, b;
156         for (a = 0, b = 0; a < tex_indices.size(); a++) {
157                 MTex *t = ma->mtex[tex_indices[a]];
158                 Image *ima = t->tex->ima;
159                 
160                 // Image not set for texture
161                 if(!ima) continue;
162                 
163                 std::string key(id_name(ima));
164                 key = translate_id(key);
165
166                 // create only one <sampler>/<surface> pair for each unique image
167                 if (im_samp_map.find(key) == im_samp_map.end()) {
168                         // //<newparam> <surface> <init_from>
169                         // COLLADASW::Surface surface(COLLADASW::Surface::SURFACE_TYPE_2D,
170                         //                                                 key + COLLADASW::Surface::SURFACE_SID_SUFFIX);
171                         // COLLADASW::SurfaceInitOption sio(COLLADASW::SurfaceInitOption::INIT_FROM);
172                         // sio.setImageReference(key);
173                         // surface.setInitOption(sio);
174
175                         // COLLADASW::NewParamSurface surface(mSW);
176                         // surface->setParamType(COLLADASW::CSW_SURFACE_TYPE_2D);
177                         
178                         //<newparam> <sampler> <source>
179                         COLLADASW::Sampler sampler(COLLADASW::Sampler::SAMPLER_TYPE_2D,
180                                                                            key + COLLADASW::Sampler::SAMPLER_SID_SUFFIX,
181                                                                            key + COLLADASW::Sampler::SURFACE_SID_SUFFIX);
182                         sampler.setImageId(key);
183                         // copy values to arrays since they will live longer
184                         samplers[a] = sampler;
185                         //surfaces[a] = surface;
186                         
187                         // store pointers so they can be used later when we create <texture>s
188                         samp_surf[b][0] = &samplers[a];
189                         //samp_surf[b][1] = &surfaces[a];
190                         
191                         im_samp_map[key] = b;
192                         b++;
193                 }
194         }
195
196         // used as fallback when MTex->uvname is "" (this is pretty common)
197         // it is indeed the correct value to use in that case
198         std::string active_uv(getActiveUVLayerName(ob));
199
200         // write textures
201         // XXX very slow
202         for (a = 0; a < tex_indices.size(); a++) {
203                 MTex *t = ma->mtex[tex_indices[a]];
204                 Image *ima = t->tex->ima;
205                 
206                 // Image not set for texture
207                 if(!ima) continue;
208
209                 // we assume map input is always TEXCO_UV
210
211                 std::string key(id_name(ima));
212                 key = translate_id(key);
213                 int i = im_samp_map[key];
214                 COLLADASW::Sampler *sampler = (COLLADASW::Sampler*)samp_surf[i][0];
215                 //COLLADASW::Surface *surface = (COLLADASW::Surface*)samp_surf[i][1];
216
217                 std::string uvname = strlen(t->uvname) ? t->uvname : active_uv;
218
219                 // color
220                 if (t->mapto & MAP_COL) {
221                         ep.setDiffuse(createTexture(ima, uvname, sampler));
222                 }
223                 // ambient
224                 if (t->mapto & MAP_AMB) {
225                         ep.setAmbient(createTexture(ima, uvname, sampler));
226                 }
227                 // specular
228                 if (t->mapto & MAP_SPEC) {
229                         ep.setSpecular(createTexture(ima, uvname, sampler));
230                 }
231                 // emission
232                 if (t->mapto & MAP_EMIT) {
233                         ep.setEmission(createTexture(ima, uvname, sampler));
234                 }
235                 // reflective
236                 if (t->mapto & MAP_REF) {
237                         ep.setReflective(createTexture(ima, uvname, sampler));
238                 }
239                 // alpha
240                 if (t->mapto & MAP_ALPHA) {
241                         ep.setTransparent(createTexture(ima, uvname, sampler));
242                 }
243                 // extension:
244                 // Normal map --> Must be stored with <extra> tag as different technique, 
245                 // since COLLADA doesn't support normal maps, even in current COLLADA 1.5.
246                 if (t->mapto & MAP_NORM) {
247                         COLLADASW::Texture texture(key);
248                         texture.setTexcoord(uvname);
249                         texture.setSampler(*sampler);
250                         // technique FCOLLADA, with the <bump> tag, is most likely the best understood,
251                         // most widespread de-facto standard.
252                         texture.setProfileName("FCOLLADA");
253                         texture.setChildElementName("bump");
254                         ep.addExtraTechniqueColorOrTexture(COLLADASW::ColorOrTexture(texture));
255                 }
256         }
257         // performs the actual writing
258         ep.addProfileElements();
259         bool twoSided = false;
260         if (ob->type == OB_MESH && ob->data) {
261                 Mesh *me = (Mesh*)ob->data;
262                 if (me->flag & ME_TWOSIDED)
263                         twoSided = true;
264         }
265         if (twoSided)
266                 ep.addExtraTechniqueParameter("GOOGLEEARTH", "show_double_sided", 1);
267         ep.addExtraTechniques(mSW);
268
269         ep.closeProfile();
270         if (twoSided)
271                 mSW->appendTextBlock("<extra><technique profile=\"MAX3D\"><double_sided>1</double_sided></technique></extra>");
272         closeEffect();  
273 }
274
275 COLLADASW::ColorOrTexture EffectsExporter::createTexture(Image *ima,
276                                                                                 std::string& uv_layer_name,
277                                                                                 COLLADASW::Sampler *sampler
278                                                                                 /*COLLADASW::Surface *surface*/)
279 {
280         
281         COLLADASW::Texture texture(translate_id(id_name(ima)));
282         texture.setTexcoord(uv_layer_name);
283         //texture.setSurface(*surface);
284         texture.setSampler(*sampler);
285         
286         COLLADASW::ColorOrTexture cot(texture);
287         return cot;
288 }
289
290 COLLADASW::ColorOrTexture EffectsExporter::getcol(float r, float g, float b, float a)
291 {
292         COLLADASW::Color color(r,g,b,a);
293         COLLADASW::ColorOrTexture cot(color);
294         return cot;
295 }
296
297 //returns the array of mtex indices which have image 
298 //need this for exporting textures
299 void EffectsExporter::createTextureIndices(Material *ma, std::vector<int> &indices)
300 {
301         indices.clear();
302
303         for (int a = 0; a < MAX_MTEX; a++) {
304                 if (ma->mtex[a] &&
305                         ma->mtex[a]->tex &&
306                         ma->mtex[a]->tex->type == TEX_IMAGE &&
307                         ma->mtex[a]->texco == TEXCO_UV){
308                         indices.push_back(a);
309                 }
310         }
311 }