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