Blender  V2.93
blender_light.cpp
Go to the documentation of this file.
1 
2 
3 /*
4  * Copyright 2011-2013 Blender Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include "render/light.h"
20 
21 #include "blender/blender_sync.h"
22 #include "blender/blender_util.h"
23 
24 #include "util/util_hash.h"
25 
27 
28 void BlenderSync::sync_light(BL::Object &b_parent,
29  int persistent_id[OBJECT_PERSISTENT_ID_SIZE],
30  BL::Object &b_ob,
31  BL::Object &b_ob_instance,
32  int random_id,
33  Transform &tfm,
34  bool *use_portal)
35 {
36  /* test if we need to sync */
37  Light *light;
38  ObjectKey key(b_parent, persistent_id, b_ob_instance, false);
39  BL::Light b_light(b_ob.data());
40 
41  /* Update if either object or light data changed. */
42  if (!light_map.add_or_update(&light, b_ob, b_parent, key)) {
43  Shader *shader;
44  if (!shader_map.add_or_update(&shader, b_light)) {
45  if (light->get_is_portal())
46  *use_portal = true;
47  return;
48  }
49  }
50 
51  /* type */
52  switch (b_light.type()) {
53  case BL::Light::type_POINT: {
54  BL::PointLight b_point_light(b_light);
55  light->set_size(b_point_light.shadow_soft_size());
56  light->set_light_type(LIGHT_POINT);
57  break;
58  }
59  case BL::Light::type_SPOT: {
60  BL::SpotLight b_spot_light(b_light);
61  light->set_size(b_spot_light.shadow_soft_size());
62  light->set_light_type(LIGHT_SPOT);
63  light->set_spot_angle(b_spot_light.spot_size());
64  light->set_spot_smooth(b_spot_light.spot_blend());
65  break;
66  }
67  /* Hemi were removed from 2.8 */
68  // case BL::Light::type_HEMI: {
69  // light->type = LIGHT_DISTANT;
70  // light->size = 0.0f;
71  // break;
72  // }
73  case BL::Light::type_SUN: {
74  BL::SunLight b_sun_light(b_light);
75  light->set_angle(b_sun_light.angle());
76  light->set_light_type(LIGHT_DISTANT);
77  break;
78  }
79  case BL::Light::type_AREA: {
80  BL::AreaLight b_area_light(b_light);
81  light->set_size(1.0f);
82  light->set_axisu(transform_get_column(&tfm, 0));
83  light->set_axisv(transform_get_column(&tfm, 1));
84  light->set_sizeu(b_area_light.size());
85  light->set_spread(b_area_light.spread());
86  switch (b_area_light.shape()) {
87  case BL::AreaLight::shape_SQUARE:
88  light->set_sizev(light->get_sizeu());
89  light->set_round(false);
90  break;
91  case BL::AreaLight::shape_RECTANGLE:
92  light->set_sizev(b_area_light.size_y());
93  light->set_round(false);
94  break;
95  case BL::AreaLight::shape_DISK:
96  light->set_sizev(light->get_sizeu());
97  light->set_round(true);
98  break;
99  case BL::AreaLight::shape_ELLIPSE:
100  light->set_sizev(b_area_light.size_y());
101  light->set_round(true);
102  break;
103  }
104  light->set_light_type(LIGHT_AREA);
105  break;
106  }
107  }
108 
109  /* strength */
110  float3 strength = get_float3(b_light.color()) * BL::PointLight(b_light).energy();
111  light->set_strength(strength);
112 
113  /* location and (inverted!) direction */
114  light->set_co(transform_get_column(&tfm, 3));
115  light->set_dir(-transform_get_column(&tfm, 2));
116  light->set_tfm(tfm);
117 
118  /* shader */
119  array<Node *> used_shaders;
120  find_shader(b_light, used_shaders, scene->default_light);
121  light->set_shader(static_cast<Shader *>(used_shaders[0]));
122 
123  /* shadow */
124  PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
125  PointerRNA clight = RNA_pointer_get(&b_light.ptr, "cycles");
126  light->set_cast_shadow(get_boolean(clight, "cast_shadow"));
127  light->set_use_mis(get_boolean(clight, "use_multiple_importance_sampling"));
128 
129  int samples = get_int(clight, "samples");
130  if (get_boolean(cscene, "use_square_samples"))
131  light->set_samples(samples * samples);
132  else
133  light->set_samples(samples);
134 
135  light->set_max_bounces(get_int(clight, "max_bounces"));
136 
137  if (b_ob != b_ob_instance) {
138  light->set_random_id(random_id);
139  }
140  else {
141  light->set_random_id(hash_uint2(hash_string(b_ob.name().c_str()), 0));
142  }
143 
144  if (light->get_light_type() == LIGHT_AREA)
145  light->set_is_portal(get_boolean(clight, "is_portal"));
146  else
147  light->set_is_portal(false);
148 
149  if (light->get_is_portal())
150  *use_portal = true;
151 
152  /* visibility */
153  uint visibility = object_ray_visibility(b_ob);
154  light->set_use_diffuse((visibility & PATH_RAY_DIFFUSE) != 0);
155  light->set_use_glossy((visibility & PATH_RAY_GLOSSY) != 0);
156  light->set_use_transmission((visibility & PATH_RAY_TRANSMIT) != 0);
157  light->set_use_scatter((visibility & PATH_RAY_VOLUME_SCATTER) != 0);
158 
159  /* tag */
160  light->tag_update(scene);
161 }
162 
163 void BlenderSync::sync_background_light(BL::SpaceView3D &b_v3d, bool use_portal)
164 {
165  BL::World b_world = b_scene.world();
166 
167  if (b_world) {
168  PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
169  PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
170 
171  enum SamplingMethod { SAMPLING_NONE = 0, SAMPLING_AUTOMATIC, SAMPLING_MANUAL, SAMPLING_NUM };
172  int sampling_method = get_enum(cworld, "sampling_method", SAMPLING_NUM, SAMPLING_AUTOMATIC);
173  bool sample_as_light = (sampling_method != SAMPLING_NONE);
174 
175  if (sample_as_light || use_portal) {
176  /* test if we need to sync */
177  Light *light;
178  ObjectKey key(b_world, 0, b_world, false);
179 
180  if (light_map.add_or_update(&light, b_world, b_world, key) || world_recalc ||
181  b_world.ptr.data != world_map) {
182  light->set_light_type(LIGHT_BACKGROUND);
183  if (sampling_method == SAMPLING_MANUAL) {
184  light->set_map_resolution(get_int(cworld, "sample_map_resolution"));
185  }
186  else {
187  light->set_map_resolution(0);
188  }
189  light->set_shader(scene->default_background);
190  light->set_use_mis(sample_as_light);
191  light->set_max_bounces(get_int(cworld, "max_bounces"));
192 
193  /* force enable light again when world is resynced */
194  light->set_is_enabled(true);
195 
196  int samples = get_int(cworld, "samples");
197  if (get_boolean(cscene, "use_square_samples"))
198  light->set_samples(samples * samples);
199  else
200  light->set_samples(samples);
201 
202  light->tag_update(scene);
203  light_map.set_recalc(b_world);
204  }
205  }
206  }
207 
208  world_map = b_world.ptr.data;
209  world_recalc = false;
210  viewport_parameters = BlenderViewportParameters(b_v3d);
211 }
212 
unsigned int uint
Definition: BLI_sys_types.h:83
struct Light Light
struct Object Object
struct World World
@ OBJECT_PERSISTENT_ID_SIZE
static uint object_ray_visibility(BL::Object &b_ob)
Definition: blender_util.h:597
static bool get_boolean(PointerRNA &ptr, const char *name)
Definition: blender_util.h:349
static int get_int(PointerRNA &ptr, const char *name)
Definition: blender_util.h:369
static float3 get_float3(const BL::Array< float, 2 > &array)
Definition: blender_util.h:295
static int get_enum(PointerRNA &ptr, const char *name, int num_values=-1, int default_value=-1)
Definition: blender_util.h:386
Definition: shader.h:80
bool add_or_update(T **r_data, const BL::ID &id)
void set_recalc(const BL::ID &id)
#define CCL_NAMESPACE_END
void KERNEL_FUNCTION_FULL_NAME() shader(KernelGlobals *kg, uint4 *input, float4 *output, int type, int filter, int i, int offset, int sample)
@ PATH_RAY_TRANSMIT
Definition: kernel_types.h:268
@ PATH_RAY_VOLUME_SCATTER
Definition: kernel_types.h:290
@ PATH_RAY_GLOSSY
Definition: kernel_types.h:270
@ PATH_RAY_DIFFUSE
Definition: kernel_types.h:269
@ LIGHT_AREA
Definition: kernel_types.h:603
@ LIGHT_DISTANT
Definition: kernel_types.h:601
@ LIGHT_SPOT
Definition: kernel_types.h:604
@ LIGHT_BACKGROUND
Definition: kernel_types.h:602
@ LIGHT_POINT
Definition: kernel_types.h:600
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6562
void tag_update(Scene *scene)
Definition: light.cpp:164
Shader * default_background
Definition: scene.h:256
Shader * default_light
Definition: scene.h:255
static uint hash_string(const char *str)
Definition: util_hash.h:376
ccl_device_inline uint hash_uint2(uint kx, uint ky)
Definition: util_hash.h:83
ccl_device_inline float3 transform_get_column(const Transform *t, int column)