Blender  V2.93
geom_volume.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2013 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 /* Volume Primitive
18  *
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.
22  *
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. */
25 
27 
28 #ifdef __VOLUME__
29 
30 /* Return position normalized to 0..1 in mesh bounds */
31 
32 ccl_device_inline float3 volume_normalized_position(KernelGlobals *kg,
33  const ShaderData *sd,
34  float3 P)
35 {
36  /* todo: optimize this so it's just a single matrix multiplication when
37  * possible (not motion blur), or perhaps even just translation + scale */
39 
41 
42  if (desc.offset != ATTR_STD_NOT_FOUND) {
43  Transform tfm = primitive_attribute_matrix(kg, sd, desc);
44  P = transform_point(&tfm, P);
45  }
46 
47  return P;
48 }
49 
50 ccl_device float volume_attribute_value_to_float(const float4 value)
51 {
52  return average(float4_to_float3(value));
53 }
54 
55 ccl_device float volume_attribute_value_to_alpha(const float4 value)
56 {
57  return value.w;
58 }
59 
60 ccl_device float3 volume_attribute_value_to_float3(const float4 value)
61 {
62  if (value.w > 1e-6f && value.w != 1.0f) {
63  /* For RGBA colors, unpremultiply after interpolation. */
64  return float4_to_float3(value) / value.w;
65  }
66  else {
67  return float4_to_float3(value);
68  }
69 }
70 
71 ccl_device float4 volume_attribute_float4(KernelGlobals *kg,
72  const ShaderData *sd,
73  const AttributeDescriptor desc)
74 {
75  /* todo: optimize this so we don't have to transform both here and in
76  * kernel_tex_image_interp_3d when possible. Also could optimize for the
77  * common case where transform is translation/scale only. */
78  float3 P = sd->P;
83 }
84 
85 #endif
86 
ccl_device Transform primitive_attribute_matrix(KernelGlobals *kg, const ShaderData *sd, const AttributeDescriptor desc)
ccl_device_inline AttributeDescriptor find_attribute(KernelGlobals *kg, const ShaderData *sd, uint id)
ccl_device_inline void object_inverse_position_transform(KernelGlobals *kg, const ShaderData *sd, float3 *P)
Definition: geom_object.h:130
#define ccl_device
#define ccl_device_inline
#define CCL_NAMESPACE_END
@ SD_VOLUME_CUBIC
Definition: kernel_types.h:885
@ ATTR_STD_GENERATED_TRANSFORM
Definition: kernel_types.h:753
@ ATTR_STD_NOT_FOUND
Definition: kernel_types.h:773
ShaderData
static float P(float k)
Definition: math_interp.c:41
ccl_device float4 kernel_tex_image_interp_3d(KernelGlobals *kg, int id, float3 P, InterpolationType interp)
ccl_device_inline float3 float4_to_float3(const float4 a)
Definition: util_math.h:415
ccl_device_inline float2 interp(const float2 &a, const float2 &b, float t)
ccl_device_inline float average(const float2 &a)
InterpolationType
Definition: util_texture.h:38
@ INTERPOLATION_NONE
Definition: util_texture.h:39
@ INTERPOLATION_CUBIC
Definition: util_texture.h:42
ccl_device_inline float3 transform_point(const Transform *t, const float3 a)