Merge from trunk 16122-16307
[blender.git] / source / gameengine / Ketsji / KX_MaterialIpoController.cpp
1
2 #include "KX_MaterialIpoController.h"
3 #include "KX_ScalarInterpolator.h"
4 #include "KX_GameObject.h"
5
6 #include "BLO_sys_types.h" // for intptr_t support
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 bool KX_MaterialIpoController::Update(double currentTime)
13 {
14         if (m_modified)
15         {
16                 m_rgba[0]=0;
17                 m_rgba[1]=0;
18                 m_rgba[2]=0;
19                 m_rgba[3]=0;
20         
21                 m_specrgb[0] =0;
22                 m_specrgb[1] =0;
23                 m_specrgb[2] =0;
24                 m_hard =0;
25                 m_spec=0;
26                 m_ref=0;
27                 m_emit=0;
28                 m_alpha = 0;
29
30
31                 T_InterpolatorList::iterator i;
32                 for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
33                         (*i)->Execute(m_ipotime);
34                 }
35                 
36
37                 SG_Spatial* ob = (SG_Spatial*)m_pObject;
38                 KX_GameObject* kxgameobj= (KX_GameObject*) ob->GetSGClientObject();
39
40                 //kxgameobj->SetObjectColor(m_rgba);
41                 kxgameobj->UpdateMaterialData( 
42                         m_matname_hash,
43                         m_rgba, 
44                         m_specrgb, 
45                         m_hard, 
46                         m_spec, 
47                         m_ref, 
48                         m_emit,
49                         m_alpha
50                 );
51
52                 m_modified=false;
53         }
54         return false;
55 }
56
57
58 void KX_MaterialIpoController::AddInterpolator(KX_IInterpolator* interp)
59 {
60         this->m_interpolators.push_back(interp);
61 }
62
63 SG_Controller*  KX_MaterialIpoController::GetReplica(class SG_Node* destnode)
64 {
65         KX_MaterialIpoController* iporeplica = new KX_MaterialIpoController(*this);
66         // clear object that ipo acts on
67         iporeplica->ClearObject();
68
69         // dirty hack, ask Gino for a better solution in the ipo implementation
70         // hacken en zagen, in what we call datahiding, not written for replication :(
71
72         T_InterpolatorList oldlist = m_interpolators;
73         iporeplica->m_interpolators.clear();
74
75         T_InterpolatorList::iterator i;
76         for (i = oldlist.begin(); !(i == oldlist.end()); ++i) {
77                 KX_ScalarInterpolator* copyipo = new KX_ScalarInterpolator(*((KX_ScalarInterpolator*)*i));
78                 iporeplica->AddInterpolator(copyipo);
79
80                 MT_Scalar* scaal = ((KX_ScalarInterpolator*)*i)->GetTarget();
81                 intptr_t orgbase = (intptr_t)this;
82                 intptr_t orgloc = (intptr_t)scaal;
83                 intptr_t offset = orgloc-orgbase;
84                 intptr_t newaddrbase = (intptr_t)iporeplica + offset;
85                 MT_Scalar* blaptr = (MT_Scalar*) newaddrbase;
86                 copyipo->SetNewTarget((MT_Scalar*)blaptr);
87         }
88         
89         return iporeplica;
90 }
91
92 KX_MaterialIpoController::~KX_MaterialIpoController()
93 {
94
95         T_InterpolatorList::iterator i;
96         for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
97                 delete (*i);
98         }
99         
100 }
101