2 * Copyright 2011-2013 Blender Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * Volumes are just regions inside meshes with the mesh surface as boundaries.
20 * There isn't as much data to access as for surfaces, there is only a position
21 * to do lookups in 3D voxel or procedural textures.
23 * 3D voxel textures can be assigned as attributes per mesh, which means the
24 * same shader can be used for volume objects with different densities, etc. */
30 /* Return position normalized to 0..1 in mesh bounds */
32 ccl_device_inline float3 volume_normalized_position(KernelGlobals *kg,
36 /* todo: optimize this so it's just a single matrix multiplication when
37 * possible (not motion blur), or perhaps even just translation + scale */
38 const AttributeDescriptor desc = find_attribute(kg, sd, ATTR_STD_GENERATED_TRANSFORM);
40 object_inverse_position_transform(kg, sd, &P);
42 if(desc.offset != ATTR_STD_NOT_FOUND) {
43 Transform tfm = primitive_attribute_matrix(kg, sd, desc);
44 P = transform_point(&tfm, P);
50 ccl_device float volume_attribute_float(KernelGlobals *kg, const ShaderData *sd, const AttributeDescriptor desc, float *dx, float *dy)
52 float3 P = volume_normalized_position(kg, sd, sd->P);
53 InterpolationType interp = (sd->flag & SD_VOLUME_CUBIC)? INTERPOLATION_CUBIC: INTERPOLATION_NONE;
54 float4 r = kernel_tex_image_interp_3d(kg, desc.offset, P.x, P.y, P.z, interp);
59 return average(float4_to_float3(r));
62 ccl_device float3 volume_attribute_float3(KernelGlobals *kg, const ShaderData *sd, const AttributeDescriptor desc, float3 *dx, float3 *dy)
64 float3 P = volume_normalized_position(kg, sd, sd->P);
65 InterpolationType interp = (sd->flag & SD_VOLUME_CUBIC)? INTERPOLATION_CUBIC: INTERPOLATION_NONE;
66 float4 r = kernel_tex_image_interp_3d(kg, desc.offset, P.x, P.y, P.z, interp);
68 if(dx) *dx = make_float3(0.0f, 0.0f, 0.0f);
69 if(dy) *dy = make_float3(0.0f, 0.0f, 0.0f);
71 if(r.w != 0.0f && r.w != 1.0f) {
72 /* For RGBA colors, unpremultiply after interpolation. */
73 return float4_to_float3(r) / r.w;
76 return float4_to_float3(r);