Cycles: Cleanup, whitespace around keywords
[blender.git] / intern / cycles / blender / blender_texture.cpp
1 /*
2  * Copyright 2011-2015 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "blender_texture.h"
18
19 CCL_NAMESPACE_BEGIN
20
21 namespace {
22
23 /* Point density helpers. */
24
25 static void density_texture_space_invert(float3& loc,
26                                          float3& size)
27 {
28         if(size.x != 0.0f) size.x = 0.5f/size.x;
29         if(size.y != 0.0f) size.y = 0.5f/size.y;
30         if(size.z != 0.0f) size.z = 0.5f/size.z;
31
32         loc = loc*size - make_float3(0.5f, 0.5f, 0.5f);
33 }
34
35 static void density_object_texture_space(BL::Object b_ob,
36                                          float radius,
37                                          float3& loc,
38                                          float3& size)
39 {
40         if(b_ob.type() == BL::Object::type_MESH) {
41                 BL::Mesh b_mesh(b_ob.data());
42                 loc = get_float3(b_mesh.texspace_location());
43                 size = get_float3(b_mesh.texspace_size());
44         }
45         else {
46                 /* TODO(sergey): Not supported currently. */
47         }
48         /* Adjust texture space to include density points on the boundaries. */
49         size = size + make_float3(radius, radius, radius);
50         density_texture_space_invert(loc, size);
51 }
52
53 static void density_particle_system_texture_space(
54         BL::Object b_ob,
55         BL::ParticleSystem b_particle_system,
56         float radius,
57         float3& loc,
58         float3& size)
59 {
60         if(b_particle_system.settings().type() == BL::ParticleSettings::type_HAIR) {
61                 /* TODO(sergey): Not supported currently. */
62                 return;
63         }
64         Transform tfm = get_transform(b_ob.matrix_world());
65         Transform itfm = transform_inverse(tfm);
66         float3 min = make_float3(FLT_MAX, FLT_MAX, FLT_MAX),
67                max = make_float3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
68         float3 particle_size = make_float3(radius, radius, radius);
69         for(int i = 0; i < b_particle_system.particles.length(); ++i) {
70                 BL::Particle particle = b_particle_system.particles[i];
71                 if(particle.alive_state() == BL::Particle::alive_state_ALIVE) {
72                         float3 location = get_float3(particle.location());
73                         location = transform_point(&itfm, location);
74                         min = ccl::min(min, location - particle_size);
75                         max = ccl::max(max, location + particle_size);
76                 }
77         }
78         /* Calculate texture space from the particle bounds.  */
79         loc = (min + max) * 0.5f;
80         size = (max - min) * 0.5f;
81         density_texture_space_invert(loc, size);
82 }
83
84 }  /* namespace */
85
86 void point_density_texture_space(BL::ShaderNodeTexPointDensity b_point_density_node,
87                                  float3& loc,
88                                  float3& size)
89 {
90         /* Fallback values. */
91         loc = make_float3(0.0f, 0.0f, 0.0f);
92         size = make_float3(0.0f, 0.0f, 0.0f);
93         BL::Object b_ob(b_point_density_node.object());
94         if(!b_ob) {
95                 return;
96         }
97         if(b_point_density_node.point_source() ==
98            BL::ShaderNodeTexPointDensity::point_source_PARTICLE_SYSTEM)
99         {
100                 BL::ParticleSystem b_particle_system(
101                         b_point_density_node.particle_system());
102                 if(b_particle_system) {
103                         density_particle_system_texture_space(b_ob,
104                                                               b_particle_system,
105                                                               b_point_density_node.radius(),
106                                                               loc,
107                                                               size);
108                 }
109         }
110         else {
111                 density_object_texture_space(b_ob,
112                                              b_point_density_node.radius(),
113                                              loc,
114                                              size);
115         }
116 }
117
118 CCL_NAMESPACE_END