Alembic: Construct ISampleSelector once and pass along
[blender.git] / source / blender / alembic / intern / abc_camera.cc
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
23 #include "abc_camera.h"
24
25 #include "abc_transform.h"
26 #include "abc_util.h"
27
28 extern "C" {
29 #include "DNA_camera_types.h"
30 #include "DNA_object_types.h"
31
32 #include "BKE_camera.h"
33 #include "BKE_object.h"
34
35 #include "BLI_math.h"
36 #include "BLI_string.h"
37 }
38
39 using Alembic::AbcGeom::ICamera;
40 using Alembic::AbcGeom::ICompoundProperty;
41 using Alembic::AbcGeom::IFloatProperty;
42 using Alembic::AbcGeom::ISampleSelector;
43
44 using Alembic::AbcGeom::OCamera;
45 using Alembic::AbcGeom::OFloatProperty;
46
47 using Alembic::AbcGeom::CameraSample;
48 using Alembic::AbcGeom::kWrapExisting;
49
50 /* ************************************************************************** */
51
52 AbcCameraWriter::AbcCameraWriter(Scene *scene,
53                                  Object *ob,
54                                  AbcTransformWriter *parent,
55                                  uint32_t time_sampling,
56                                  ExportSettings &settings)
57     : AbcObjectWriter(scene, ob, time_sampling, settings, parent)
58 {
59         OCamera camera(parent->alembicXform(), m_name, m_time_sampling);
60         m_camera_schema = camera.getSchema();
61
62         m_custom_data_container = m_camera_schema.getUserProperties();
63         m_stereo_distance = OFloatProperty(m_custom_data_container, "stereoDistance", m_time_sampling);
64         m_eye_separation = OFloatProperty(m_custom_data_container, "eyeSeparation", m_time_sampling);
65 }
66
67 void AbcCameraWriter::do_write()
68 {
69         Camera *cam = static_cast<Camera *>(m_object->data);
70
71         m_stereo_distance.set(cam->stereo.convergence_distance);
72         m_eye_separation.set(cam->stereo.interocular_distance);
73
74         const double apperture_x = cam->sensor_x / 10.0;
75         const double apperture_y = cam->sensor_y / 10.0;
76         const double film_aspect = apperture_x / apperture_y;
77
78         m_camera_sample.setFocalLength(cam->lens);
79         m_camera_sample.setHorizontalAperture(apperture_x);
80         m_camera_sample.setVerticalAperture(apperture_y);
81         m_camera_sample.setHorizontalFilmOffset(apperture_x * cam->shiftx);
82         m_camera_sample.setVerticalFilmOffset(apperture_y * cam->shifty * film_aspect);
83         m_camera_sample.setNearClippingPlane(cam->clipsta);
84         m_camera_sample.setFarClippingPlane(cam->clipend);
85
86         if (cam->dof_ob) {
87                 Imath::V3f v(m_object->loc[0] - cam->dof_ob->loc[0],
88                         m_object->loc[1] - cam->dof_ob->loc[1],
89                         m_object->loc[2] - cam->dof_ob->loc[2]);
90                 m_camera_sample.setFocusDistance(v.length());
91         }
92         else {
93                 m_camera_sample.setFocusDistance(cam->gpu_dof.focus_distance);
94         }
95
96         /* Blender camera does not have an fstop param, so try to find a custom prop
97          * instead. */
98         m_camera_sample.setFStop(cam->gpu_dof.fstop);
99
100         m_camera_sample.setLensSqueezeRatio(1.0);
101         m_camera_schema.set(m_camera_sample);
102 }
103
104 /* ************************************************************************** */
105
106 AbcCameraReader::AbcCameraReader(const Alembic::Abc::IObject &object, ImportSettings &settings)
107     : AbcObjectReader(object, settings)
108 {
109         ICamera abc_cam(m_iobject, kWrapExisting);
110         m_schema = abc_cam.getSchema();
111
112         get_min_max_time(m_iobject, m_schema, m_min_time, m_max_time);
113 }
114
115 bool AbcCameraReader::valid() const
116 {
117         return m_schema.valid();
118 }
119
120 void AbcCameraReader::readObjectData(Main *bmain, const ISampleSelector &sample_sel)
121 {
122         Camera *bcam = static_cast<Camera *>(BKE_camera_add(bmain, m_data_name.c_str()));
123
124         CameraSample cam_sample;
125         m_schema.get(cam_sample, sample_sel);
126
127         ICompoundProperty customDataContainer = m_schema.getUserProperties();
128
129         if (customDataContainer.valid() &&
130             customDataContainer.getPropertyHeader("stereoDistance") &&
131             customDataContainer.getPropertyHeader("eyeSeparation"))
132         {
133                 IFloatProperty convergence_plane(customDataContainer, "stereoDistance");
134                 IFloatProperty eye_separation(customDataContainer, "eyeSeparation");
135
136                 bcam->stereo.interocular_distance = eye_separation.getValue(sample_sel);
137                 bcam->stereo.convergence_distance = convergence_plane.getValue(sample_sel);
138         }
139
140         const float lens = static_cast<float>(cam_sample.getFocalLength());
141         const float apperture_x = static_cast<float>(cam_sample.getHorizontalAperture());
142         const float apperture_y = static_cast<float>(cam_sample.getVerticalAperture());
143         const float h_film_offset = static_cast<float>(cam_sample.getHorizontalFilmOffset());
144         const float v_film_offset = static_cast<float>(cam_sample.getVerticalFilmOffset());
145         const float film_aspect = apperture_x / apperture_y;
146
147         bcam->lens = lens;
148         bcam->sensor_x = apperture_x * 10;
149         bcam->sensor_y = apperture_y * 10;
150         bcam->shiftx = h_film_offset / apperture_x;
151         bcam->shifty = v_film_offset / apperture_y / film_aspect;
152         bcam->clipsta = max_ff(0.1f, static_cast<float>(cam_sample.getNearClippingPlane()));
153         bcam->clipend = static_cast<float>(cam_sample.getFarClippingPlane());
154         bcam->gpu_dof.focus_distance = static_cast<float>(cam_sample.getFocusDistance());
155         bcam->gpu_dof.fstop = static_cast<float>(cam_sample.getFStop());
156
157         m_object = BKE_object_add_only_object(bmain, OB_CAMERA, m_object_name.c_str());
158         m_object->data = bcam;
159 }