svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22701:22704
[blender.git] / source / gameengine / Converter / BL_DeformableGameObject.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include "BL_DeformableGameObject.h"
31 #include "BL_ShapeDeformer.h"
32 #include "BL_ShapeActionActuator.h"
33 #include "RAS_MaterialBucket.h"
34
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 BL_DeformableGameObject::~BL_DeformableGameObject()
41 {
42         if (m_pDeformer)
43                 delete m_pDeformer;             //      __NLA : Temporary until we decide where to put this
44 }
45
46 void BL_DeformableGameObject::ProcessReplica()
47 {
48         KX_GameObject::ProcessReplica();
49
50         if (m_pDeformer)
51                 m_pDeformer= (BL_MeshDeformer*)m_pDeformer->GetReplica();
52 }
53
54 CValue*         BL_DeformableGameObject::GetReplica()
55 {
56
57         BL_DeformableGameObject* replica = new BL_DeformableGameObject(*this);//m_float,GetName());
58         replica->ProcessReplica();
59         return replica;
60 }
61
62 bool BL_DeformableGameObject::SetActiveAction(BL_ShapeActionActuator *act, short priority, double curtime)
63 {
64         if (curtime != m_lastframe){
65                 m_activePriority = 9999;
66                 m_lastframe= curtime;
67                 m_activeAct = NULL;
68         }
69
70         if (priority<=m_activePriority)
71         {
72                 if (m_activeAct && (m_activeAct!=act))
73                         m_activeAct->SetBlendTime(0.0f);        /* Reset the blend timer */
74                 m_activeAct = act;
75                 m_activePriority = priority;
76                 m_lastframe = curtime;
77         
78                 return true;
79         }
80         else{
81                 act->SetBlendTime(0.0f);
82                 return false;
83         }
84 }
85
86 bool BL_DeformableGameObject::GetShape(vector<float> &shape)
87 {
88         shape.clear();
89         if (m_pDeformer)
90         {
91                 Mesh* mesh = ((BL_MeshDeformer*)m_pDeformer)->GetMesh();
92                 // this check is normally superfluous: a shape deformer can only be created if the mesh
93                 // has relative keys
94                 if (mesh && mesh->key && mesh->key->type==KEY_RELATIVE) 
95                 {
96                         KeyBlock *kb;
97                         for (kb = (KeyBlock*)mesh->key->block.first; kb; kb = (KeyBlock*)kb->next)
98                         {
99                                 shape.push_back(kb->curval);
100                         }
101                 }
102         }
103         return !shape.empty();
104 }
105
106 void BL_DeformableGameObject::SetDeformer(class RAS_Deformer* deformer)
107 {
108         m_pDeformer = deformer;
109
110         SG_QList::iterator<RAS_MeshSlot> mit(m_meshSlots);
111         for(mit.begin(); !mit.end(); ++mit)
112         {
113                 (*mit)->SetDeformer(deformer);
114         }
115 }
116