1 | /** |
---|
2 | * This source file is part of OgreColladaPlugin |
---|
3 | * an addon for OGRE (Object-oriented Graphics Rendering Engine) |
---|
4 | * For the latest info, see http://www.ogre3d.org/ |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify it under |
---|
7 | * the terms of the GNU Lesser General Public License as published by the Free Software |
---|
8 | * Foundation; either version 2 of the License, or (at your option) any later |
---|
9 | * version. |
---|
10 | |
---|
11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
---|
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
13 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
14 | |
---|
15 | * You should have received a copy of the GNU Lesser General Public License along with |
---|
16 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
17 | * Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
18 | * http://www.gnu.org/copyleft/lesser.txt. |
---|
19 | * |
---|
20 | * @author Philipp Hartl |
---|
21 | * @see README |
---|
22 | */ |
---|
23 | |
---|
24 | #include "OgreColladaCamera.h" |
---|
25 | #include "OgreColladaDocument.h" |
---|
26 | #include "OgreColladaSyntax.h" |
---|
27 | #include "OgreColladaUtils.h" |
---|
28 | |
---|
29 | #include "OgreSceneManager.h" |
---|
30 | #include "OgreCamera.h" |
---|
31 | #include "OgreStringConverter.h" |
---|
32 | |
---|
33 | namespace Ogre |
---|
34 | { |
---|
35 | //------------------------------------------------------------------------- |
---|
36 | ColladaCamera::ColladaCamera(ColladaDocument *d, xmlNode *n) : ColladaEntity(d, n) |
---|
37 | { |
---|
38 | mType = UNKNOWN; |
---|
39 | |
---|
40 | mXFov = 0; |
---|
41 | mYFov = 0; |
---|
42 | mZNear = 0; |
---|
43 | mZFar = 0; |
---|
44 | |
---|
45 | mBottom = 0; |
---|
46 | mLeft = 0; |
---|
47 | mRight = 0; |
---|
48 | mTop = 0; |
---|
49 | } |
---|
50 | |
---|
51 | //------------------------------------------------------------------------- |
---|
52 | ColladaCamera::~ColladaCamera(void) { } |
---|
53 | |
---|
54 | //------------------------------------------------------------------------- |
---|
55 | MovableObject *ColladaCamera::getOgreInstance(void) const |
---|
56 | { |
---|
57 | // Camera *camera = new Camera(mId, mDoc->getSceneManager()); |
---|
58 | Camera *camera = mDoc->getSceneManager()->createCamera(mId); |
---|
59 | |
---|
60 | // standard projection type should be perspective |
---|
61 | |
---|
62 | if (mType == ORTHOGRAPHIC) |
---|
63 | { |
---|
64 | camera->setProjectionType(PT_ORTHOGRAPHIC); |
---|
65 | // map from Collada [-1,1] to Ogre [0,1] |
---|
66 | // camera->setWindow(mLeft, mTop, mRight, mBottom); |
---|
67 | } |
---|
68 | else if (mType == PERSPECTIVE) |
---|
69 | { |
---|
70 | camera->setProjectionType(PT_PERSPECTIVE); |
---|
71 | |
---|
72 | float fov = mYFov; |
---|
73 | if (fov == 0 && mXFov > 0) fov = mXFov / camera->getAspectRatio(); |
---|
74 | |
---|
75 | camera->setFOVy(Radian(Math::DegreesToRadians(fov))); |
---|
76 | camera->setNearClipDistance(mZNear); |
---|
77 | camera->setFarClipDistance(mZFar); |
---|
78 | } |
---|
79 | Quaternion rot = mDoc->getUpRotation(); |
---|
80 | camera->rotate(rot); |
---|
81 | return camera; |
---|
82 | } |
---|
83 | |
---|
84 | //------------------------------------------------------------------------- |
---|
85 | bool ColladaCamera::doImport(void) |
---|
86 | { |
---|
87 | if (mLoaded) return mStatus; |
---|
88 | else mLoaded = true; |
---|
89 | |
---|
90 | // <technique> nodes |
---|
91 | xmlNodePtrVector techniques = ColladaUtils::getChildsByTagName(mNode, CS_ELM_TECHNIQUE); |
---|
92 | // currently we are only interested in "COMMON" profile |
---|
93 | xmlNode *technique = NULL; |
---|
94 | xmlNodePtrVector::const_iterator it; |
---|
95 | for (it = techniques.begin(); it != techniques.end(); ++it) |
---|
96 | { |
---|
97 | String profile = ColladaUtils::getProperty(*it, CS_ATR_PROFILE); |
---|
98 | if (profile == CS_VAL_TECHNIQUE_PROFILE_COMMON) |
---|
99 | { |
---|
100 | technique = *it; |
---|
101 | break; |
---|
102 | } |
---|
103 | } |
---|
104 | if (technique == NULL) return false; |
---|
105 | |
---|
106 | // one <optics> child |
---|
107 | xmlNode *optics = ColladaUtils::getChildByTagName(technique, CS_ELM_OPTICS); |
---|
108 | mStatus = importOptics(optics); |
---|
109 | |
---|
110 | // <imager> [0,*] |
---|
111 | xmlNodePtrVector imagers = ColladaUtils::getChildsByTagName(technique, CS_ELM_IMAGER); |
---|
112 | for (it = imagers.begin(); it != imagers.end() && mStatus; ++it) mStatus = importImager(*it); |
---|
113 | |
---|
114 | return mStatus; |
---|
115 | } |
---|
116 | |
---|
117 | //------------------------------------------------------------------------- |
---|
118 | bool ColladaCamera::importImager(xmlNode *node) |
---|
119 | { |
---|
120 | if (node == NULL) return false; |
---|
121 | // currently not implemented |
---|
122 | return true; |
---|
123 | } |
---|
124 | |
---|
125 | //------------------------------------------------------------------------- |
---|
126 | bool ColladaCamera::importOptics(xmlNode *node) |
---|
127 | { |
---|
128 | if (node == NULL) return false; |
---|
129 | |
---|
130 | // <program> |
---|
131 | xmlNode *program = ColladaUtils::getChildByTagName(node, CS_ELM_PROGRAM); |
---|
132 | if (program == NULL) return false; |
---|
133 | |
---|
134 | // camera type |
---|
135 | String url = ColladaUtils::getProperty(program, CS_ATR_URL); |
---|
136 | mType = UNKNOWN; |
---|
137 | if (url == CS_VAL_CAMERA_TYPE_ORTHOGRAPHIC) mType = ORTHOGRAPHIC; |
---|
138 | else if (url == CS_VAL_CAMERA_TYPE_PERSPECTIVE) mType = PERSPECTIVE; |
---|
139 | if (mType == UNKNOWN) |
---|
140 | { |
---|
141 | LogManager::getSingleton().logMessage("ColladaCamera::importOptics - <program name=" + url + "> unknown! " + mId); |
---|
142 | return false; |
---|
143 | } |
---|
144 | |
---|
145 | // <param> childs |
---|
146 | xmlNodePtrVector params = ColladaUtils::getChildsByTagName(program, CS_ELM_PARAM); |
---|
147 | for (xmlNodePtrVector::const_iterator it = params.begin(); it != params.end(); ++it) |
---|
148 | { |
---|
149 | String name = ColladaUtils::getProperty(*it, CS_ATR_NAME); |
---|
150 | String content = ColladaUtils::getContentDirect(*it); |
---|
151 | float tmp = StringConverter::parseReal(content); |
---|
152 | |
---|
153 | /** |
---|
154 | * The field of view may be specified separately for the X and Y directions. |
---|
155 | * If only one is specified, the other should be derived using the aspect ratio |
---|
156 | * of the rendering view-port (to avoid image distortion). |
---|
157 | */ |
---|
158 | |
---|
159 | if (name == CS_VAL_CAMERA_PARAM_BOTTOM) mBottom = tmp; |
---|
160 | else if (name == CS_VAL_CAMERA_PARAM_LEFT) mLeft = tmp; |
---|
161 | else if (name == CS_VAL_CAMERA_PARAM_RIGHT) mRight = tmp; |
---|
162 | else if (name == CS_VAL_CAMERA_PARAM_TOP) mTop = tmp; |
---|
163 | else if (name == CS_VAL_CAMERA_PARAM_XFOV) mXFov = tmp; |
---|
164 | else if (name == CS_VAL_CAMERA_PARAM_YFOV) mYFov = tmp; |
---|
165 | else if (name == CS_VAL_CAMERA_PARAM_ZFAR) mZFar = tmp; |
---|
166 | else if (name == CS_VAL_CAMERA_PARAM_ZNEAR) mZNear = tmp; |
---|
167 | else |
---|
168 | { |
---|
169 | LogManager::getSingleton().logMessage("ColladaCamera::importOptics - <param name=" + name + " unknown! " + mId); |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | return true; |
---|
174 | } |
---|
175 | } |
---|