Merging r40265 through r40492 from trunk into soc-2011-tomato
[blender.git] / source / blender / collada / CameraExporter.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
21  *                 Nathan Letwory
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/collada/CameraExporter.cpp
27  *  \ingroup collada
28  */
29
30
31 #include <string>
32
33 #include "COLLADASWCamera.h"
34 #include "COLLADASWCameraOptic.h"
35
36 #include "DNA_camera_types.h"
37
38 #include "CameraExporter.h"
39
40 #include "collada_internal.h"
41
42 CamerasExporter::CamerasExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings): COLLADASW::LibraryCameras(sw), export_settings(export_settings) {}
43
44 template<class Functor>
45 void forEachCameraObjectInScene(Scene *sce, Functor &f, bool export_selected)
46 {
47         Base *base= (Base*) sce->base.first;
48         while(base) {
49                 Object *ob = base->object;
50                         
51                 if (ob->type == OB_CAMERA && ob->data
52                         && !(export_selected && !(ob->flag & SELECT))) {
53                         f(ob, sce);
54                 }
55                 base= base->next;
56         }
57 }
58
59 void CamerasExporter::exportCameras(Scene *sce)
60 {
61         openLibrary();
62         
63         forEachCameraObjectInScene(sce, *this, this->export_settings->selected);
64         
65         closeLibrary();
66 }
67 void CamerasExporter::operator()(Object *ob, Scene *sce)
68 {
69         // TODO: shiftx, shifty, YF_dofdist
70         Camera *cam = (Camera*)ob->data;
71         std::string cam_id(get_camera_id(ob));
72         std::string cam_name(id_name(cam));
73         
74         if (cam->type == CAM_PERSP) {
75                 COLLADASW::PerspectiveOptic persp(mSW);
76                 persp.setXFov(RAD2DEGF(focallength_to_hfov(cam->lens), cam->sensor_x), "xfov");
77                 persp.setAspectRatio((float)(sce->r.xsch)/(float)(sce->r.ysch),false,"aspect_ratio");
78                 persp.setZFar(cam->clipend, false , "zfar");
79                 persp.setZNear(cam->clipsta,false , "znear");
80                 COLLADASW::Camera ccam(mSW, &persp, cam_id, cam_name);
81                 addCamera(ccam);
82         }
83         else {
84                 COLLADASW::OrthographicOptic ortho(mSW);
85                 ortho.setXMag(cam->ortho_scale,"xmag");
86                 ortho.setAspectRatio((float)(sce->r.xsch)/(float)(sce->r.ysch),false,"aspect_ratio");
87                 ortho.setZFar(cam->clipend , false , "zfar");
88                 ortho.setZNear(cam->clipsta, false , "znear");
89                 COLLADASW::Camera ccam(mSW, &ortho, cam_id, cam_name);
90                 addCamera(ccam);
91         }
92 }