abc_camera.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * Contributor(s): Esteban Tovagliari, Cedric Paille, Kevin Dietrich
  19. *
  20. * ***** END GPL LICENSE BLOCK *****
  21. */
  22. #include "abc_camera.h"
  23. #include "abc_transform.h"
  24. #include "abc_util.h"
  25. extern "C" {
  26. #include "DNA_camera_types.h"
  27. #include "DNA_object_types.h"
  28. #include "BKE_camera.h"
  29. #include "BKE_object.h"
  30. #include "BLI_math.h"
  31. #include "BLI_string.h"
  32. }
  33. using Alembic::AbcGeom::ICamera;
  34. using Alembic::AbcGeom::ICompoundProperty;
  35. using Alembic::AbcGeom::IFloatProperty;
  36. using Alembic::AbcGeom::ISampleSelector;
  37. using Alembic::AbcGeom::OCamera;
  38. using Alembic::AbcGeom::OFloatProperty;
  39. using Alembic::AbcGeom::CameraSample;
  40. using Alembic::AbcGeom::kWrapExisting;
  41. /* ************************************************************************** */
  42. AbcCameraWriter::AbcCameraWriter(Scene *scene,
  43. Object *ob,
  44. AbcTransformWriter *parent,
  45. uint32_t time_sampling,
  46. ExportSettings &settings)
  47. : AbcObjectWriter(scene, ob, time_sampling, settings, parent)
  48. {
  49. OCamera camera(parent->alembicXform(), m_name, m_time_sampling);
  50. m_camera_schema = camera.getSchema();
  51. m_custom_data_container = m_camera_schema.getUserProperties();
  52. m_stereo_distance = OFloatProperty(m_custom_data_container, "stereoDistance", m_time_sampling);
  53. m_eye_separation = OFloatProperty(m_custom_data_container, "eyeSeparation", m_time_sampling);
  54. }
  55. void AbcCameraWriter::do_write()
  56. {
  57. Camera *cam = static_cast<Camera *>(m_object->data);
  58. m_stereo_distance.set(cam->stereo.convergence_distance);
  59. m_eye_separation.set(cam->stereo.interocular_distance);
  60. const double apperture_x = cam->sensor_x / 10.0;
  61. const double apperture_y = cam->sensor_y / 10.0;
  62. const double film_aspect = apperture_x / apperture_y;
  63. m_camera_sample.setFocalLength(cam->lens);
  64. m_camera_sample.setHorizontalAperture(apperture_x);
  65. m_camera_sample.setVerticalAperture(apperture_y);
  66. m_camera_sample.setHorizontalFilmOffset(apperture_x * cam->shiftx);
  67. m_camera_sample.setVerticalFilmOffset(apperture_y * cam->shifty * film_aspect);
  68. m_camera_sample.setNearClippingPlane(cam->clipsta);
  69. m_camera_sample.setFarClippingPlane(cam->clipend);
  70. if (cam->dof_ob) {
  71. Imath::V3f v(m_object->loc[0] - cam->dof_ob->loc[0],
  72. m_object->loc[1] - cam->dof_ob->loc[1],
  73. m_object->loc[2] - cam->dof_ob->loc[2]);
  74. m_camera_sample.setFocusDistance(v.length());
  75. }
  76. else {
  77. m_camera_sample.setFocusDistance(cam->gpu_dof.focus_distance);
  78. }
  79. /* Blender camera does not have an fstop param, so try to find a custom prop
  80. * instead. */
  81. m_camera_sample.setFStop(cam->gpu_dof.fstop);
  82. m_camera_sample.setLensSqueezeRatio(1.0);
  83. m_camera_schema.set(m_camera_sample);
  84. }
  85. /* ************************************************************************** */
  86. AbcCameraReader::AbcCameraReader(const Alembic::Abc::IObject &object, ImportSettings &settings)
  87. : AbcObjectReader(object, settings)
  88. {
  89. ICamera abc_cam(m_iobject, kWrapExisting);
  90. m_schema = abc_cam.getSchema();
  91. get_min_max_time(m_iobject, m_schema, m_min_time, m_max_time);
  92. }
  93. bool AbcCameraReader::valid() const
  94. {
  95. return m_schema.valid();
  96. }
  97. bool AbcCameraReader::accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
  98. const Object *const ob,
  99. const char **err_str) const
  100. {
  101. if (!Alembic::AbcGeom::ICamera::matches(alembic_header)) {
  102. *err_str = "Object type mismatch, Alembic object path pointed to Camera when importing, but not any more.";
  103. return false;
  104. }
  105. if (ob->type != OB_CAMERA) {
  106. *err_str = "Object type mismatch, Alembic object path points to Camera.";
  107. return false;
  108. }
  109. return true;
  110. }
  111. void AbcCameraReader::readObjectData(Main *bmain, const ISampleSelector &sample_sel)
  112. {
  113. Camera *bcam = static_cast<Camera *>(BKE_camera_add(bmain, m_data_name.c_str()));
  114. CameraSample cam_sample;
  115. m_schema.get(cam_sample, sample_sel);
  116. ICompoundProperty customDataContainer = m_schema.getUserProperties();
  117. if (customDataContainer.valid() &&
  118. customDataContainer.getPropertyHeader("stereoDistance") &&
  119. customDataContainer.getPropertyHeader("eyeSeparation"))
  120. {
  121. IFloatProperty convergence_plane(customDataContainer, "stereoDistance");
  122. IFloatProperty eye_separation(customDataContainer, "eyeSeparation");
  123. bcam->stereo.interocular_distance = eye_separation.getValue(sample_sel);
  124. bcam->stereo.convergence_distance = convergence_plane.getValue(sample_sel);
  125. }
  126. const float lens = static_cast<float>(cam_sample.getFocalLength());
  127. const float apperture_x = static_cast<float>(cam_sample.getHorizontalAperture());
  128. const float apperture_y = static_cast<float>(cam_sample.getVerticalAperture());
  129. const float h_film_offset = static_cast<float>(cam_sample.getHorizontalFilmOffset());
  130. const float v_film_offset = static_cast<float>(cam_sample.getVerticalFilmOffset());
  131. const float film_aspect = apperture_x / apperture_y;
  132. bcam->lens = lens;
  133. bcam->sensor_x = apperture_x * 10;
  134. bcam->sensor_y = apperture_y * 10;
  135. bcam->shiftx = h_film_offset / apperture_x;
  136. bcam->shifty = v_film_offset / apperture_y / film_aspect;
  137. bcam->clipsta = max_ff(0.1f, static_cast<float>(cam_sample.getNearClippingPlane()));
  138. bcam->clipend = static_cast<float>(cam_sample.getFarClippingPlane());
  139. bcam->gpu_dof.focus_distance = static_cast<float>(cam_sample.getFocusDistance());
  140. bcam->gpu_dof.fstop = static_cast<float>(cam_sample.getFStop());
  141. m_object = BKE_object_add_only_object(bmain, OB_CAMERA, m_object_name.c_str());
  142. m_object->data = bcam;
  143. }