Merge branch 'master' into blender2.8
[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(Object *ob,
53                                  AbcTransformWriter *parent,
54                                  uint32_t time_sampling,
55                                  ExportSettings &settings)
56     : AbcObjectWriter(ob, time_sampling, settings, parent)
57 {
58         OCamera camera(parent->alembicXform(), m_name, m_time_sampling);
59         m_camera_schema = camera.getSchema();
60
61         m_custom_data_container = m_camera_schema.getUserProperties();
62         m_stereo_distance = OFloatProperty(m_custom_data_container, "stereoDistance", m_time_sampling);
63         m_eye_separation = OFloatProperty(m_custom_data_container, "eyeSeparation", m_time_sampling);
64 }
65
66 void AbcCameraWriter::do_write()
67 {
68         Camera *cam = static_cast<Camera *>(m_object->data);
69
70         m_stereo_distance.set(cam->stereo.convergence_distance);
71         m_eye_separation.set(cam->stereo.interocular_distance);
72
73         const double apperture_x = cam->sensor_x / 10.0;
74         const double apperture_y = cam->sensor_y / 10.0;
75         const double film_aspect = apperture_x / apperture_y;
76
77         m_camera_sample.setFocalLength(cam->lens);
78         m_camera_sample.setHorizontalAperture(apperture_x);
79         m_camera_sample.setVerticalAperture(apperture_y);
80         m_camera_sample.setHorizontalFilmOffset(apperture_x * cam->shiftx);
81         m_camera_sample.setVerticalFilmOffset(apperture_y * cam->shifty * film_aspect);
82         m_camera_sample.setNearClippingPlane(cam->clipsta);
83         m_camera_sample.setFarClippingPlane(cam->clipend);
84
85         if (cam->dof_ob) {
86                 Imath::V3f v(m_object->loc[0] - cam->dof_ob->loc[0],
87                         m_object->loc[1] - cam->dof_ob->loc[1],
88                         m_object->loc[2] - cam->dof_ob->loc[2]);
89                 m_camera_sample.setFocusDistance(v.length());
90         }
91         else {
92                 m_camera_sample.setFocusDistance(cam->gpu_dof.focus_distance);
93         }
94
95         /* Blender camera does not have an fstop param, so try to find a custom prop
96          * instead. */
97         m_camera_sample.setFStop(cam->gpu_dof.fstop);
98
99         m_camera_sample.setLensSqueezeRatio(1.0);
100         m_camera_schema.set(m_camera_sample);
101 }
102
103 /* ************************************************************************** */
104
105 AbcCameraReader::AbcCameraReader(const Alembic::Abc::IObject &object, ImportSettings &settings)
106     : AbcObjectReader(object, settings)
107 {
108         ICamera abc_cam(m_iobject, kWrapExisting);
109         m_schema = abc_cam.getSchema();
110
111         get_min_max_time(m_iobject, m_schema, m_min_time, m_max_time);
112 }
113
114 bool AbcCameraReader::valid() const
115 {
116         return m_schema.valid();
117 }
118
119 bool AbcCameraReader::accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
120                                           const Object *const ob,
121                                           const char **err_str) const
122 {
123         if (!Alembic::AbcGeom::ICamera::matches(alembic_header)) {
124                 *err_str = "Object type mismatch, Alembic object path pointed to Camera when importing, but not any more.";
125                 return false;
126         }
127
128         if (ob->type != OB_CAMERA) {
129                 *err_str = "Object type mismatch, Alembic object path points to Camera.";
130                 return false;
131         }
132
133         return true;
134 }
135
136 void AbcCameraReader::readObjectData(Main *bmain, const ISampleSelector &sample_sel)
137 {
138         Camera *bcam = static_cast<Camera *>(BKE_camera_add(bmain, m_data_name.c_str()));
139
140         CameraSample cam_sample;
141         m_schema.get(cam_sample, sample_sel);
142
143         ICompoundProperty customDataContainer = m_schema.getUserProperties();
144
145         if (customDataContainer.valid() &&
146             customDataContainer.getPropertyHeader("stereoDistance") &&
147             customDataContainer.getPropertyHeader("eyeSeparation"))
148         {
149                 IFloatProperty convergence_plane(customDataContainer, "stereoDistance");
150                 IFloatProperty eye_separation(customDataContainer, "eyeSeparation");
151
152                 bcam->stereo.interocular_distance = eye_separation.getValue(sample_sel);
153                 bcam->stereo.convergence_distance = convergence_plane.getValue(sample_sel);
154         }
155
156         const float lens = static_cast<float>(cam_sample.getFocalLength());
157         const float apperture_x = static_cast<float>(cam_sample.getHorizontalAperture());
158         const float apperture_y = static_cast<float>(cam_sample.getVerticalAperture());
159         const float h_film_offset = static_cast<float>(cam_sample.getHorizontalFilmOffset());
160         const float v_film_offset = static_cast<float>(cam_sample.getVerticalFilmOffset());
161         const float film_aspect = apperture_x / apperture_y;
162
163         bcam->lens = lens;
164         bcam->sensor_x = apperture_x * 10;
165         bcam->sensor_y = apperture_y * 10;
166         bcam->shiftx = h_film_offset / apperture_x;
167         bcam->shifty = v_film_offset / apperture_y / film_aspect;
168         bcam->clipsta = max_ff(0.1f, static_cast<float>(cam_sample.getNearClippingPlane()));
169         bcam->clipend = static_cast<float>(cam_sample.getFarClippingPlane());
170         bcam->gpu_dof.focus_distance = static_cast<float>(cam_sample.getFocusDistance());
171         bcam->gpu_dof.fstop = static_cast<float>(cam_sample.getFStop());
172
173         m_object = BKE_object_add_only_object(bmain, OB_CAMERA, m_object_name.c_str());
174         m_object->data = bcam;
175 }