~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to source/blender/collada/LightExporter.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kevin Roy
  • Date: 2011-02-08 22:20:54 UTC
  • mfrom: (1.4.2 upstream)
  • mto: (14.2.6 sid) (1.5.1)
  • mto: This revision was merged to the branch mainline in revision 27.
  • Revision ID: james.westby@ubuntu.com-20110208222054-kk0gwa4bu8h5lyq4
Tags: upstream-2.56.1-beta-svn34076
ImportĀ upstreamĀ versionĀ 2.56.1-beta-svn34076

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * $Id: LightExporter.cpp 32468 2010-10-14 09:40:56Z jesterking $
 
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
#include <string>
 
27
 
 
28
#include "COLLADASWColor.h"
 
29
#include "COLLADASWLight.h"
 
30
 
 
31
#include "DNA_lamp_types.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)
 
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
                        f(ob);
 
47
                }
 
48
                base= base->next;
 
49
        }
 
50
}
 
51
 
 
52
LightsExporter::LightsExporter(COLLADASW::StreamWriter *sw): COLLADASW::LibraryLights(sw){}
 
53
 
 
54
void LightsExporter::exportLights(Scene *sce)
 
55
{
 
56
        openLibrary();
 
57
        
 
58
        forEachLampObjectInScene(sce, *this);
 
59
        
 
60
        closeLibrary();
 
61
}
 
62
void LightsExporter::operator()(Object *ob)
 
63
{
 
64
        Lamp *la = (Lamp*)ob->data;
 
65
        std::string la_id(get_light_id(ob));
 
66
        std::string la_name(id_name(la));
 
67
        COLLADASW::Color col(la->r, la->g, la->b);
 
68
        float att1, att2;
 
69
        float e, d, constatt, linatt, quadatt;
 
70
        att1 = att2 = 0.0f;
 
71
        
 
72
        if(la->falloff_type==LA_FALLOFF_INVLINEAR) {
 
73
                att1 = 1.0f;
 
74
                att2 = 0.0f;
 
75
        }
 
76
        else if(la->falloff_type==LA_FALLOFF_INVSQUARE) {
 
77
                att1 = 0.0f;
 
78
                att2 = 1.0f;
 
79
        }
 
80
        else if(la->falloff_type==LA_FALLOFF_SLIDERS) {
 
81
                att1 = la->att1;
 
82
                att2 = la->att2;
 
83
        }
 
84
        
 
85
        e = la->energy;
 
86
        d = la->dist;
 
87
        
 
88
        constatt = linatt = quadatt = MAXFLOAT;
 
89
        if(e > 0.0f) {
 
90
                constatt = 1.0f/e;
 
91
                linatt = att1/(d*e);
 
92
                quadatt = att2/(d*d*(e*2));
 
93
        }
 
94
        
 
95
        // sun
 
96
        if (la->type == LA_SUN) {
 
97
                COLLADASW::DirectionalLight cla(mSW, la_id, la_name, e);
 
98
                cla.setColor(col);
 
99
                cla.setConstantAttenuation(constatt);
 
100
                addLight(cla);
 
101
        }
 
102
        // hemi
 
103
        else if (la->type == LA_HEMI) {
 
104
                COLLADASW::AmbientLight cla(mSW, la_id, la_name, e);
 
105
                cla.setColor(col);
 
106
                cla.setConstantAttenuation(constatt);
 
107
                addLight(cla);
 
108
        }
 
109
        // spot
 
110
        else if (la->type == LA_SPOT) {
 
111
                COLLADASW::SpotLight cla(mSW, la_id, la_name, e);
 
112
                cla.setColor(col);
 
113
                cla.setFallOffAngle(la->spotsize);
 
114
                cla.setFallOffExponent(la->spotblend);
 
115
                cla.setConstantAttenuation(constatt);
 
116
                cla.setLinearAttenuation(linatt);
 
117
                cla.setQuadraticAttenuation(quadatt);
 
118
                addLight(cla);
 
119
        }
 
120
        // lamp
 
121
        else if (la->type == LA_LOCAL) {
 
122
                COLLADASW::PointLight cla(mSW, la_id, la_name, e);
 
123
                cla.setColor(col);
 
124
                cla.setConstantAttenuation(constatt);
 
125
                cla.setLinearAttenuation(linatt);
 
126
                cla.setQuadraticAttenuation(quadatt);
 
127
                addLight(cla);
 
128
        }
 
129
        // area lamp is not supported
 
130
        // it will be exported as a local lamp
 
131
        else {
 
132
                COLLADASW::PointLight cla(mSW, la_id, la_name, e);
 
133
                cla.setColor(col);
 
134
                cla.setConstantAttenuation(constatt);
 
135
                cla.setLinearAttenuation(linatt);
 
136
                cla.setQuadraticAttenuation(quadatt);
 
137
                addLight(cla);
 
138
        }
 
139
}