geom_volume.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. /* Volume Primitive
  17. *
  18. * Volumes are just regions inside meshes with the mesh surface as boundaries.
  19. * There isn't as much data to access as for surfaces, there is only a position
  20. * to do lookups in 3D voxel or procedural textures.
  21. *
  22. * 3D voxel textures can be assigned as attributes per mesh, which means the
  23. * same shader can be used for volume objects with different densities, etc. */
  24. CCL_NAMESPACE_BEGIN
  25. #ifdef __VOLUME__
  26. /* Return position normalized to 0..1 in mesh bounds */
  27. ccl_device_inline float3 volume_normalized_position(KernelGlobals *kg,
  28. const ShaderData *sd,
  29. float3 P)
  30. {
  31. /* todo: optimize this so it's just a single matrix multiplication when
  32. * possible (not motion blur), or perhaps even just translation + scale */
  33. const AttributeDescriptor desc = find_attribute(kg, sd, ATTR_STD_GENERATED_TRANSFORM);
  34. object_inverse_position_transform(kg, sd, &P);
  35. if (desc.offset != ATTR_STD_NOT_FOUND) {
  36. Transform tfm = primitive_attribute_matrix(kg, sd, desc);
  37. P = transform_point(&tfm, P);
  38. }
  39. return P;
  40. }
  41. ccl_device float volume_attribute_float(KernelGlobals *kg,
  42. const ShaderData *sd,
  43. const AttributeDescriptor desc)
  44. {
  45. float3 P = volume_normalized_position(kg, sd, sd->P);
  46. InterpolationType interp = (sd->flag & SD_VOLUME_CUBIC) ? INTERPOLATION_CUBIC :
  47. INTERPOLATION_NONE;
  48. float4 r = kernel_tex_image_interp_3d(kg, desc.offset, P.x, P.y, P.z, interp);
  49. return average(float4_to_float3(r));
  50. }
  51. ccl_device float3 volume_attribute_float3(KernelGlobals *kg,
  52. const ShaderData *sd,
  53. const AttributeDescriptor desc)
  54. {
  55. float3 P = volume_normalized_position(kg, sd, sd->P);
  56. InterpolationType interp = (sd->flag & SD_VOLUME_CUBIC) ? INTERPOLATION_CUBIC :
  57. INTERPOLATION_NONE;
  58. float4 r = kernel_tex_image_interp_3d(kg, desc.offset, P.x, P.y, P.z, interp);
  59. if (r.w > 1e-6f && r.w != 1.0f) {
  60. /* For RGBA colors, unpremultiply after interpolation. */
  61. return float4_to_float3(r) / r.w;
  62. }
  63. else {
  64. return float4_to_float3(r);
  65. }
  66. }
  67. #endif
  68. CCL_NAMESPACE_END