SO3Engine
SCOLBones.cpp
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
25
33#include "SCOLPack/SO3SCOL.h"
34
35// Scene Graph includes
40
49int SO3BoneListFromMesh(mmachine m)
50{
51#ifdef SO3_DEBUG
52 MMechostr(MSKDEBUG,"SO3BoneListFromMesh\n");
53#endif
54
55 int obj = MMget(m, 0);
56 if(obj==NIL)
57 {
58 MMset(m, 0, NIL);
59 return 0;
60 }
61
62 SNode* node = MMgetPointer<SNode*>(m, MTOP(obj));
63 if(node == 0)
64 {
65 MMset(m, 0, NIL);
66 return 0;
67 }
68
69 if(node->GetNodeType()!=SNode::ENTITY_TYPE_ID)
70 {
71 MMset(m, 0, NIL);
72 return 0;
73 }
74
75 SEntity* curEntity = static_cast<SEntity*> (node);
76 SSkeleton* skeleton = curEntity->GetSkeleton();
77 if(skeleton == 0)
78 {
79 MMset(m, 0, NIL);
80 return 0;
81 }
82 MMpull(m);
83
84 int p=0;
85 SBoneMap boneListCopy = skeleton->GetBones();
86 SBoneMap::const_iterator iBones = boneListCopy.begin();
87 while (iBones != boneListCopy.end());
88 {
89 int b = OBJfindTH(m, SO3OBJTYPE, SCOL_PTR (iBones->second));
90 if(b!=NIL)
91 b = MMfetch(m, b, OFFOBJMAG);
92
93 if(MMpush(m, b))
94 return MERRMEM;
95
96 p++;
97 iBones++;
98 }
99
100 if(MMpush(m, NIL))
101 return MERRMEM;
102
103 for(int j=0; j<p; j++)
104 {
105 if(MMpush(m, 2*2))
106 return MERRMEM;
107
108 if(int k=MBdeftab(m))
109 return k;
110 }
111 return 0;
112}
113
114
125{
126#ifdef SO3_DEBUG
127 MMechostr(MSKDEBUG, "SO3GetRootBoneFromMesh: DEPRECATED!\n");
128#endif
129
130 int obj = MMget(m, 0);
131 if(obj==NIL)
132 {
133 MMset(m, 0, NIL);
134 return 0;
135 }
136
137 SNode* node = MMgetPointer<SNode*>(m, MTOP(obj));
138 if(node == 0)
139 {
140 MMset(m, 0, NIL);
141 return 0;
142 }
143
144 if(node->GetNodeType()!=SNode::ENTITY_TYPE_ID)
145 {
146 MMset(m, 0, NIL);
147 return 0;
148 }
149
150 SEntity* curEntity = static_cast<SEntity*> (node);
151 SSkeleton* skeleton = curEntity->GetSkeleton();
152 if(!skeleton)
153 {
154 MMset(m, 0, NIL);
155 return 0;
156 }
157
158 SBoneMap rootBonesList = skeleton->GetRootBones();
159 if(rootBonesList.empty())
160 {
161 MMset(m, 0, NIL);
162 return 0;
163 }
164
165 int so3obj = OBJfindTH(m, SO3OBJTYPE, SCOL_PTR(rootBonesList.begin()->second));
166 if(so3obj!=NIL)
167 so3obj = MMfetch(m, so3obj, OFFOBJMAG);
168
169 MMset(m, 0, so3obj);
170 return 0;
171}
172
173
183{
184#ifdef SO3_DEBUG
185 MMechostr(MSKDEBUG, "SO3GetRootBonesFromMesh\n");
186#endif
187
188 int obj = MMget(m, 0);
189 if(obj==NIL)
190 {
191 MMset(m, 0, NIL);
192 return 0;
193 }
194
195 SNode* node = MMgetPointer<SNode*>(m, MTOP(obj));
196 if(node == 0)
197 {
198 MMset(m, 0, NIL);
199 return 0;
200 }
201
202 if(node->GetNodeType()!=SNode::ENTITY_TYPE_ID)
203 {
204 MMset(m, 0, NIL);
205 return 0;
206 }
207
208 SEntity* curEntity = static_cast<SEntity*> (node);
209 SSkeleton* skeleton = curEntity->GetSkeleton();
210 if(!skeleton)
211 {
212 MMset(m, 0, NIL);
213 return 0;
214 }
215 MMpull(m);
216
217 int p=0;
218 SBoneMap rootBoneListCopy = skeleton->GetRootBones();
219 SBoneMap::iterator iRootBones = rootBoneListCopy.begin();
220 while (iRootBones != rootBoneListCopy.end())
221 {
222 int b = OBJfindTH(m, SO3OBJTYPE, SCOL_PTR(iRootBones->second));
223 if(b!=NIL)
224 b = MMfetch(m, b, OFFOBJMAG);
225
226 if(MMpush(m, b))
227 return MERRMEM;
228
229 p++;
230 iRootBones++;
231 }
232
233 if(MMpush(m, NIL))
234 return MERRMEM;
235
236 for(int j=0; j<p; j++)
237 {
238 if(MMpush(m, 2*2))
239 return MERRMEM;
240
241 if(int k=MBdeftab(m))
242 return k;
243 }
244 return 0;
245}
246
247
258{
259#ifdef SO3_DEBUG
260 MMechostr(MSKDEBUG, "SO3GetBoneLocalOrientation\n");
261#endif
262
263 int q = MMpull(m);
264 int obj = MMget(m, 0);
265 if((obj==NIL) || (q==NIL))
266 {
267 MMset(m, 0, NIL);
268 return 0;
269 }
270
271 SNode* node = MMgetPointer<SNode*>(m, MTOP(obj));
272 if(node == 0)
273 {
274 MMset(m, 0, NIL);
275 return 0;
276 }
277
278 if(node->GetNodeType()!=SNode::BONE_TYPE_ID)
279 {
280 MMset(m, 0, NIL);
281 return 0;
282 }
283
284 SBone* curBone = static_cast<SBone*> (node);
285 if(!curBone)
286 {
287 MMset(m, 0, NIL);
288 return 0;
289 }
290
291 int x = MMfetch(m, MTOP(q), 0);
292 int y = MMfetch(m, MTOP(q), 1);
293 int z = MMfetch(m, MTOP(q), 2);
294 int w = MMfetch(m, MTOP(q), 3);
295
296 if((x==NIL) || (y==NIL) || (z==NIL) || (w==NIL))
297 {
298 MMset(m, 0, NIL);
299 return 0;
300 }
301
302 Ogre::Quaternion quat = Ogre::Quaternion(MTOF(w), MTOF(x), MTOF(y), MTOF(z));
303 quat.normalise();
304 Ogre::Quaternion lquat = curBone->ConvertWorldToLocalOrientation(quat);
305
306 int tuple = MMmalloc(m, 4, TYPETAB);
307 if(tuple==NIL)
308 {
309 MMset(m, 0, NIL);
310 return MERRMEM;
311 }
312 MMstore(m, tuple, 0, FTOM((lquat.x)));
313 MMstore(m, tuple, 1, FTOM((lquat.y)));
314 MMstore(m, tuple, 2, FTOM((lquat.z)));
315 MMstore(m, tuple, 3, FTOM((lquat.w)));
316 MMset(m, 0, PTOM(tuple));
317
318 return 0;
319}
320
321
322NativeDefinition natSO3Bone[] = {
323 { "SO3BoneListFromMesh", 1, "fun [SO3_OBJECT] [SO3_OBJECT r1]", SO3BoneListFromMesh },
324 { "SO3GetRootBoneFromMesh", 1, "fun [SO3_OBJECT] SO3_OBJECT", SO3GetRootBoneFromMesh },
325 { "SO3GetRootBonesFromMesh", 1, "fun [SO3_OBJECT] [SO3_OBJECT r1]", SO3GetRootBonesFromMesh },
326 { "SO3GetBoneLocalOrientation", 2, "fun [SO3_OBJECT [F F F F]] [F F F F]", SO3GetBoneLocalOrientation }
327};
328
329
335int SCOLloadBone(mmachine m,cbmachine w)
336{
337 return PKhardpak2(m, "SO3Bone.pkg", sizeof(natSO3Bone) / sizeof(natSO3Bone[0]), natSO3Bone);
338}
339
340
346{
347 return 0;
348}
349
NativeDefinition natSO3Bone[]
int SCOLfreeBone()
free the SO3Engine Viewport function
int SCOLloadBone(mmachine m, cbmachine w)
Load the SO3Engine Viewport function.
MMechostr(MSKDEBUG, " > Start loading Plugin SO3Engine dll\n")
SCOL_EXPORT int cbmachine w
Definition SO3SCOL.cpp:5150
int SO3OBJTYPE
Definition SO3SCOL.cpp:90
int SO3GetRootBonesFromMesh(mmachine m)
SO3GetRootBonesFromMesh : Return the root bone(s) for a given node.
int SO3GetBoneLocalOrientation(mmachine m)
SO3GetBoneLocalOrientation : Return the local orienation from a global quaternion.
int SO3BoneListFromMesh(mmachine m)
main include
Definition SCOLBones.cpp:49
int SO3GetRootBoneFromMesh(mmachine m)
SO3GetRootBoneFromMesh : Return the root bone for a given node.