curves.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include "device/device.h"
  17. #include "render/curves.h"
  18. #include "render/mesh.h"
  19. #include "render/object.h"
  20. #include "render/scene.h"
  21. #include "util/util_foreach.h"
  22. #include "util/util_map.h"
  23. #include "util/util_progress.h"
  24. #include "util/util_vector.h"
  25. CCL_NAMESPACE_BEGIN
  26. /* Curve functions */
  27. void curvebounds(float *lower, float *upper, float3 *p, int dim)
  28. {
  29. float *p0 = &p[0].x;
  30. float *p1 = &p[1].x;
  31. float *p2 = &p[2].x;
  32. float *p3 = &p[3].x;
  33. float fc = 0.71f;
  34. float curve_coef[4];
  35. curve_coef[0] = p1[dim];
  36. curve_coef[1] = -fc * p0[dim] + fc * p2[dim];
  37. curve_coef[2] = 2.0f * fc * p0[dim] + (fc - 3.0f) * p1[dim] + (3.0f - 2.0f * fc) * p2[dim] -
  38. fc * p3[dim];
  39. curve_coef[3] = -fc * p0[dim] + (2.0f - fc) * p1[dim] + (fc - 2.0f) * p2[dim] + fc * p3[dim];
  40. float discroot = curve_coef[2] * curve_coef[2] - 3 * curve_coef[3] * curve_coef[1];
  41. float ta = -1.0f;
  42. float tb = -1.0f;
  43. if (discroot >= 0) {
  44. discroot = sqrtf(discroot);
  45. ta = (-curve_coef[2] - discroot) / (3 * curve_coef[3]);
  46. tb = (-curve_coef[2] + discroot) / (3 * curve_coef[3]);
  47. ta = (ta > 1.0f || ta < 0.0f) ? -1.0f : ta;
  48. tb = (tb > 1.0f || tb < 0.0f) ? -1.0f : tb;
  49. }
  50. *upper = max(p1[dim], p2[dim]);
  51. *lower = min(p1[dim], p2[dim]);
  52. float exa = p1[dim];
  53. float exb = p2[dim];
  54. if (ta >= 0.0f) {
  55. float t2 = ta * ta;
  56. float t3 = t2 * ta;
  57. exa = curve_coef[3] * t3 + curve_coef[2] * t2 + curve_coef[1] * ta + curve_coef[0];
  58. }
  59. if (tb >= 0.0f) {
  60. float t2 = tb * tb;
  61. float t3 = t2 * tb;
  62. exb = curve_coef[3] * t3 + curve_coef[2] * t2 + curve_coef[1] * tb + curve_coef[0];
  63. }
  64. *upper = max(*upper, max(exa, exb));
  65. *lower = min(*lower, min(exa, exb));
  66. }
  67. /* Hair System Manager */
  68. CurveSystemManager::CurveSystemManager()
  69. {
  70. primitive = CURVE_LINE_SEGMENTS;
  71. curve_shape = CURVE_THICK;
  72. line_method = CURVE_CORRECTED;
  73. triangle_method = CURVE_CAMERA_TRIANGLES;
  74. resolution = 3;
  75. subdivisions = 3;
  76. use_curves = true;
  77. use_encasing = true;
  78. use_backfacing = false;
  79. use_tangent_normal_geometry = false;
  80. need_update = true;
  81. need_mesh_update = false;
  82. }
  83. CurveSystemManager::~CurveSystemManager()
  84. {
  85. }
  86. void CurveSystemManager::device_update(Device *device,
  87. DeviceScene *dscene,
  88. Scene * /*scene*/,
  89. Progress &progress)
  90. {
  91. if (!need_update)
  92. return;
  93. device_free(device, dscene);
  94. progress.set_status("Updating Hair settings", "Copying Hair settings to device");
  95. KernelCurves *kcurve = &dscene->data.curve;
  96. kcurve->curveflags = 0;
  97. if (use_curves) {
  98. if (primitive == CURVE_SEGMENTS || primitive == CURVE_RIBBONS)
  99. kcurve->curveflags |= CURVE_KN_INTERPOLATE;
  100. if (primitive == CURVE_RIBBONS)
  101. kcurve->curveflags |= CURVE_KN_RIBBONS;
  102. if (line_method == CURVE_ACCURATE)
  103. kcurve->curveflags |= CURVE_KN_ACCURATE;
  104. else if (line_method == CURVE_CORRECTED)
  105. kcurve->curveflags |= CURVE_KN_INTERSECTCORRECTION;
  106. if (use_tangent_normal_geometry)
  107. kcurve->curveflags |= CURVE_KN_TRUETANGENTGNORMAL;
  108. if (use_backfacing)
  109. kcurve->curveflags |= CURVE_KN_BACKFACING;
  110. if (use_encasing)
  111. kcurve->curveflags |= CURVE_KN_ENCLOSEFILTER;
  112. kcurve->subdivisions = subdivisions;
  113. }
  114. if (progress.get_cancel())
  115. return;
  116. need_update = false;
  117. }
  118. void CurveSystemManager::device_free(Device * /*device*/, DeviceScene * /*dscene*/)
  119. {
  120. }
  121. bool CurveSystemManager::modified(const CurveSystemManager &CurveSystemManager)
  122. {
  123. return !(
  124. curve_shape == CurveSystemManager.curve_shape &&
  125. line_method == CurveSystemManager.line_method && primitive == CurveSystemManager.primitive &&
  126. use_encasing == CurveSystemManager.use_encasing &&
  127. use_tangent_normal_geometry == CurveSystemManager.use_tangent_normal_geometry &&
  128. use_backfacing == CurveSystemManager.use_backfacing &&
  129. triangle_method == CurveSystemManager.triangle_method &&
  130. resolution == CurveSystemManager.resolution && use_curves == CurveSystemManager.use_curves &&
  131. subdivisions == CurveSystemManager.subdivisions);
  132. }
  133. bool CurveSystemManager::modified_mesh(const CurveSystemManager &CurveSystemManager)
  134. {
  135. return !(
  136. primitive == CurveSystemManager.primitive && curve_shape == CurveSystemManager.curve_shape &&
  137. triangle_method == CurveSystemManager.triangle_method &&
  138. resolution == CurveSystemManager.resolution && use_curves == CurveSystemManager.use_curves);
  139. }
  140. void CurveSystemManager::tag_update(Scene * /*scene*/)
  141. {
  142. need_update = true;
  143. }
  144. void CurveSystemManager::tag_update_mesh()
  145. {
  146. need_mesh_update = true;
  147. }
  148. CCL_NAMESPACE_END