Blender  V2.93
LightExporter.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <string>
22 
23 #include "COLLADASWColor.h"
24 #include "COLLADASWLight.h"
25 
26 #include "BLI_math.h"
27 
28 #include "LightExporter.h"
29 #include "collada_internal.h"
30 
31 template<class Functor>
32 void forEachLightObjectInExportSet(Scene *sce, Functor &f, LinkNode *export_set)
33 {
34  LinkNode *node;
35  for (node = export_set; node; node = node->next) {
36  Object *ob = (Object *)node->link;
37 
38  if (ob->type == OB_LAMP && ob->data) {
39  f(ob);
40  }
41  }
42 }
43 
44 LightsExporter::LightsExporter(COLLADASW::StreamWriter *sw, BCExportSettings &export_settings)
45  : COLLADASW::LibraryLights(sw), export_settings(export_settings)
46 {
47 }
48 
50 {
51  openLibrary();
52 
53  forEachLightObjectInExportSet(sce, *this, this->export_settings.get_export_set());
54 
55  closeLibrary();
56 }
57 
59 {
60  Light *la = (Light *)ob->data;
61  std::string la_id(get_light_id(ob));
62  std::string la_name(id_name(la));
63  COLLADASW::Color col(la->r * la->energy, la->g * la->energy, la->b * la->energy);
64  float d, constatt, linatt, quadatt;
65 
66  d = la->dist;
67 
68  constatt = 1.0f;
69 
71  linatt = 1.0f / d;
72  quadatt = 0.0f;
73  }
74  else {
75  linatt = 0.0f;
76  quadatt = 1.0f / (d * d);
77  }
78 
79  /* sun */
80  if (la->type == LA_SUN) {
81  COLLADASW::DirectionalLight cla(mSW, la_id, la_name);
82  cla.setColor(col, false, "color");
83  cla.setConstantAttenuation(constatt);
84  exportBlenderProfile(cla, la);
85  addLight(cla);
86  }
87 
88  /* spot */
89  else if (la->type == LA_SPOT) {
90  COLLADASW::SpotLight cla(mSW, la_id, la_name);
91  cla.setColor(col, false, "color");
92  cla.setFallOffAngle(RAD2DEGF(la->spotsize), false, "fall_off_angle");
93  cla.setFallOffExponent(la->spotblend, false, "fall_off_exponent");
94  cla.setConstantAttenuation(constatt);
95  cla.setLinearAttenuation(linatt);
96  cla.setQuadraticAttenuation(quadatt);
97  exportBlenderProfile(cla, la);
98  addLight(cla);
99  }
100  /* lamp */
101  else if (la->type == LA_LOCAL) {
102  COLLADASW::PointLight cla(mSW, la_id, la_name);
103  cla.setColor(col, false, "color");
104  cla.setConstantAttenuation(constatt);
105  cla.setLinearAttenuation(linatt);
106  cla.setQuadraticAttenuation(quadatt);
107  exportBlenderProfile(cla, la);
108  addLight(cla);
109  }
110  /* area light is not supported
111  * it will be exported as a local lamp */
112  else {
113  COLLADASW::PointLight cla(mSW, la_id, la_name);
114  cla.setColor(col, false, "color");
115  cla.setConstantAttenuation(constatt);
116  cla.setLinearAttenuation(linatt);
117  cla.setQuadraticAttenuation(quadatt);
118  exportBlenderProfile(cla, la);
119  addLight(cla);
120  }
121 }
122 
123 bool LightsExporter::exportBlenderProfile(COLLADASW::Light &cla, Light *la)
124 {
125  cla.addExtraTechniqueParameter("blender", "type", la->type);
126  cla.addExtraTechniqueParameter("blender", "flag", la->flag);
127  cla.addExtraTechniqueParameter("blender", "mode", la->mode);
128  cla.addExtraTechniqueParameter("blender", "gamma", la->k, "blender_gamma");
129  cla.addExtraTechniqueParameter("blender", "red", la->r);
130  cla.addExtraTechniqueParameter("blender", "green", la->g);
131  cla.addExtraTechniqueParameter("blender", "blue", la->b);
132  cla.addExtraTechniqueParameter("blender", "shadow_r", la->shdwr, "blender_shadow_r");
133  cla.addExtraTechniqueParameter("blender", "shadow_g", la->shdwg, "blender_shadow_g");
134  cla.addExtraTechniqueParameter("blender", "shadow_b", la->shdwb, "blender_shadow_b");
135  cla.addExtraTechniqueParameter("blender", "energy", la->energy, "blender_energy");
136  cla.addExtraTechniqueParameter("blender", "dist", la->dist, "blender_dist");
137  cla.addExtraTechniqueParameter("blender", "spotsize", RAD2DEGF(la->spotsize));
138  cla.addExtraTechniqueParameter("blender", "spotblend", la->spotblend);
139  cla.addExtraTechniqueParameter("blender", "att1", la->att1);
140  cla.addExtraTechniqueParameter("blender", "att2", la->att2);
141  /* \todo figure out how we can have falloff curve supported here */
142  cla.addExtraTechniqueParameter("blender", "falloff_type", la->falloff_type);
143  cla.addExtraTechniqueParameter("blender", "clipsta", la->clipsta);
144  cla.addExtraTechniqueParameter("blender", "clipend", la->clipend);
145  cla.addExtraTechniqueParameter("blender", "bias", la->bias);
146  cla.addExtraTechniqueParameter("blender", "soft", la->soft);
147  cla.addExtraTechniqueParameter("blender", "bufsize", la->bufsize);
148  cla.addExtraTechniqueParameter("blender", "samp", la->samp);
149  cla.addExtraTechniqueParameter("blender", "buffers", la->buffers);
150  cla.addExtraTechniqueParameter("blender", "area_shape", la->area_shape);
151  cla.addExtraTechniqueParameter("blender", "area_size", la->area_size);
152  cla.addExtraTechniqueParameter("blender", "area_sizey", la->area_sizey);
153  cla.addExtraTechniqueParameter("blender", "area_sizez", la->area_sizez);
154 
155  return true;
156 }
#define RAD2DEGF(_rad)
struct Light Light
#define LA_SPOT
#define LA_SUN
#define LA_LOCAL
#define LA_FALLOFF_INVLINEAR
@ OB_LAMP
void forEachLightObjectInExportSet(Scene *sce, Functor &f, LinkNode *export_set)
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume Image Sky Noise Wave Voronoi Brick Texture Vector Combine Vertex Color
void exportLights(Scene *sce)
LightsExporter(COLLADASW::StreamWriter *sw, BCExportSettings &export_settings)
void operator()(Object *ob)
std::string get_light_id(Object *ob)
std::string id_name(void *id)
OperationNode * node
uint col
float att2
float r
float energy
short bufsize
float dist
float clipend
float att1
float area_sizez
float soft
float area_sizey
short samp
float shdwg
short area_shape
float clipsta
float spotblend
float g
short falloff_type
float spotsize
float area_size
float k
float shdwr
float b
float shdwb
short type
float bias
short buffers
short flag
void * data