Blender  V2.93
CameraExporter.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <string>
22 
23 #include "COLLADASWCamera.h"
24 
25 #include "DNA_camera_types.h"
26 
27 #include "CameraExporter.h"
28 
29 #include "collada_internal.h"
30 
31 CamerasExporter::CamerasExporter(COLLADASW::StreamWriter *sw, BCExportSettings &export_settings)
32  : COLLADASW::LibraryCameras(sw), export_settings(export_settings)
33 {
34 }
35 
36 template<class Functor>
37 void forEachCameraObjectInExportSet(Scene *sce, Functor &f, LinkNode *export_set)
38 {
39  LinkNode *node;
40  for (node = export_set; node; node = node->next) {
41  Object *ob = (Object *)node->link;
42 
43  if (ob->type == OB_CAMERA && ob->data) {
44  f(ob, sce);
45  }
46  }
47 }
48 
50 {
51  openLibrary();
52 
53  forEachCameraObjectInExportSet(sce, *this, this->export_settings.get_export_set());
54 
55  closeLibrary();
56 }
58 {
59  Camera *cam = (Camera *)ob->data;
60  std::string cam_id(get_camera_id(ob));
61  std::string cam_name(id_name(cam));
62 
63  switch (cam->type) {
64  case CAM_PANO:
65  case CAM_PERSP: {
66  COLLADASW::PerspectiveOptic persp(mSW);
67  persp.setXFov(RAD2DEGF(focallength_to_fov(cam->lens, cam->sensor_x)), "xfov");
68  persp.setAspectRatio((float)(sce->r.xsch) / (float)(sce->r.ysch), false, "aspect_ratio");
69  persp.setZFar(cam->clip_end, false, "zfar");
70  persp.setZNear(cam->clip_start, false, "znear");
71  COLLADASW::Camera ccam(mSW, &persp, cam_id, cam_name);
72  exportBlenderProfile(ccam, cam);
73  addCamera(ccam);
74 
75  break;
76  }
77  case CAM_ORTHO:
78  default: {
79  COLLADASW::OrthographicOptic ortho(mSW);
80  ortho.setXMag(cam->ortho_scale / 2, "xmag");
81  ortho.setAspectRatio((float)(sce->r.xsch) / (float)(sce->r.ysch), false, "aspect_ratio");
82  ortho.setZFar(cam->clip_end, false, "zfar");
83  ortho.setZNear(cam->clip_start, false, "znear");
84  COLLADASW::Camera ccam(mSW, &ortho, cam_id, cam_name);
85  exportBlenderProfile(ccam, cam);
86  addCamera(ccam);
87  break;
88  }
89  }
90 }
91 bool CamerasExporter::exportBlenderProfile(COLLADASW::Camera &cm, Camera *cam)
92 {
93  cm.addExtraTechniqueParameter("blender", "shiftx", cam->shiftx);
94  cm.addExtraTechniqueParameter("blender", "shifty", cam->shifty);
95  cm.addExtraTechniqueParameter("blender", "dof_distance", cam->dof.focus_distance);
96  return true;
97 }
#define RAD2DEGF(_rad)
float focallength_to_fov(float focal_length, float sensor)
void forEachCameraObjectInExportSet(Scene *sce, Functor &f, LinkNode *export_set)
struct Camera Camera
@ CAM_PERSP
@ CAM_PANO
@ CAM_ORTHO
@ OB_CAMERA
void operator()(Object *ob, Scene *sce)
void exportCameras(Scene *sce)
CamerasExporter(COLLADASW::StreamWriter *sw, BCExportSettings &export_settings)
std::string get_camera_id(Object *ob)
std::string id_name(void *id)
OperationNode * node
float clip_end
float shiftx
float sensor_x
float clip_start
float shifty
struct CameraDOFSettings dof
float ortho_scale
void * data
struct RenderData r