geom_motion_triangle_shader.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 2011-2016 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. /* Motion Triangle Primitive
  17. *
  18. * These are stored as regular triangles, plus extra positions and normals at
  19. * times other than the frame center. Computing the triangle vertex positions
  20. * or normals at a given ray time is a matter of interpolation of the two steps
  21. * between which the ray time lies.
  22. *
  23. * The extra positions and normals are stored as ATTR_STD_MOTION_VERTEX_POSITION
  24. * and ATTR_STD_MOTION_VERTEX_NORMAL mesh attributes.
  25. */
  26. CCL_NAMESPACE_BEGIN
  27. /* Setup of motion triangle specific parts of ShaderData, moved into this one
  28. * function to more easily share computation of interpolated positions and
  29. * normals */
  30. /* return 3 triangle vertex normals */
  31. ccl_device_noinline void motion_triangle_shader_setup(
  32. KernelGlobals *kg, ShaderData *sd, const Intersection *isect, const Ray *ray, bool is_local)
  33. {
  34. /* Get shader. */
  35. sd->shader = kernel_tex_fetch(__tri_shader, sd->prim);
  36. /* Get motion info. */
  37. /* TODO(sergey): This logic is really similar to motion_triangle_vertices(),
  38. * can we de-duplicate something here?
  39. */
  40. int numsteps, numverts;
  41. object_motion_info(kg, sd->object, &numsteps, &numverts, NULL);
  42. /* Figure out which steps we need to fetch and their interpolation factor. */
  43. int maxstep = numsteps * 2;
  44. int step = min((int)(sd->time * maxstep), maxstep - 1);
  45. float t = sd->time * maxstep - step;
  46. /* Find attribute. */
  47. AttributeElement elem;
  48. int offset = find_attribute_motion(kg, sd->object, ATTR_STD_MOTION_VERTEX_POSITION, &elem);
  49. kernel_assert(offset != ATTR_STD_NOT_FOUND);
  50. /* Fetch vertex coordinates. */
  51. float3 verts[3], next_verts[3];
  52. uint4 tri_vindex = kernel_tex_fetch(__tri_vindex, sd->prim);
  53. motion_triangle_verts_for_step(kg, tri_vindex, offset, numverts, numsteps, step, verts);
  54. motion_triangle_verts_for_step(kg, tri_vindex, offset, numverts, numsteps, step + 1, next_verts);
  55. /* Interpolate between steps. */
  56. verts[0] = (1.0f - t) * verts[0] + t * next_verts[0];
  57. verts[1] = (1.0f - t) * verts[1] + t * next_verts[1];
  58. verts[2] = (1.0f - t) * verts[2] + t * next_verts[2];
  59. /* Compute refined position. */
  60. #ifdef __BVH_LOCAL__
  61. if (is_local) {
  62. sd->P = motion_triangle_refine_local(kg, sd, isect, ray, verts);
  63. }
  64. else
  65. #endif /* __BVH_LOCAL__*/
  66. {
  67. sd->P = motion_triangle_refine(kg, sd, isect, ray, verts);
  68. }
  69. /* Compute face normal. */
  70. float3 Ng;
  71. if (sd->object_flag & SD_OBJECT_NEGATIVE_SCALE_APPLIED) {
  72. Ng = normalize(cross(verts[2] - verts[0], verts[1] - verts[0]));
  73. }
  74. else {
  75. Ng = normalize(cross(verts[1] - verts[0], verts[2] - verts[0]));
  76. }
  77. sd->Ng = Ng;
  78. sd->N = Ng;
  79. /* Compute derivatives of P w.r.t. uv. */
  80. #ifdef __DPDU__
  81. sd->dPdu = (verts[0] - verts[2]);
  82. sd->dPdv = (verts[1] - verts[2]);
  83. #endif
  84. /* Compute smooth normal. */
  85. if (sd->shader & SHADER_SMOOTH_NORMAL) {
  86. /* Find attribute. */
  87. AttributeElement elem;
  88. int offset = find_attribute_motion(kg, sd->object, ATTR_STD_MOTION_VERTEX_NORMAL, &elem);
  89. kernel_assert(offset != ATTR_STD_NOT_FOUND);
  90. /* Fetch vertex coordinates. */
  91. float3 normals[3], next_normals[3];
  92. motion_triangle_normals_for_step(kg, tri_vindex, offset, numverts, numsteps, step, normals);
  93. motion_triangle_normals_for_step(
  94. kg, tri_vindex, offset, numverts, numsteps, step + 1, next_normals);
  95. /* Interpolate between steps. */
  96. normals[0] = (1.0f - t) * normals[0] + t * next_normals[0];
  97. normals[1] = (1.0f - t) * normals[1] + t * next_normals[1];
  98. normals[2] = (1.0f - t) * normals[2] + t * next_normals[2];
  99. /* Interpolate between vertices. */
  100. float u = sd->u;
  101. float v = sd->v;
  102. float w = 1.0f - u - v;
  103. sd->N = (u * normals[0] + v * normals[1] + w * normals[2]);
  104. }
  105. }
  106. CCL_NAMESPACE_END