33 #include <libFreeWRL.h>
35 #include "../vrml_parser/Structs.h"
36 #include "../vrml_parser/CRoutes.h"
37 #include "../main/headers.h"
39 #include "../world_script/fieldSet.h"
40 #include "../x3d_parser/Bindable.h"
41 #include "Collision.h"
42 #include "quaternion.h"
44 #include "../opengl/Frustum.h"
45 #include "../opengl/Material.h"
46 #include "../opengl/OpenGL_Utils.h"
47 #include "../input/EAIHelpers.h"
50 #include "LinearAlgebra.h"
58 void *Component_Followers_constructor(){
63 void Component_Followers_init(
struct tComponent_Followers *t){
66 t->prv = Component_Followers_constructor();
72 void Component_Followers_clear(
struct tComponent_Followers *t){
80 void do_ColorChaserTick(
void * ptr);
81 void do_ColorDamperTick(
void * ptr);
82 void do_CoordinateChaserTick(
void * ptr);
83 void do_CoordinateDamperTick(
void * ptr);
84 void do_OrientationChaserTick(
void * ptr);
85 void do_OrientationDamperTick(
void * ptr);
86 void do_PositionChaserTick(
void * ptr);
87 void do_ColorDamperTick(
void * ptr);
88 void do_PositionChaserTick(
void * ptr);
89 void do_PositionDamperTick(
void * ptr);
90 void do_PositionChaser2DTick(
void * ptr);
91 void do_PositionDamper2DTick(
void * ptr);
92 void do_ScalarChaserTick(
void * ptr);
93 void do_ScalarDamperTick(
void * ptr);
94 void do_TexCoordChaser2DTick(
void * ptr);
95 void do_TexCoordDamper2DTick(
void * ptr);
107 static int Buffer_length = 10;
167 void *initialDestination;
169 void *set_destination;
173 void *_previousValue;
179 void *initialDestination;
181 void *set_destination;
191 #define VOIDFN (void(*))
192 #define FLOATFN (float (*)(void *))
193 #define INTFN (int (*) (void *, void *))
194 #define VOIDPTR (void *)
197 void* (*copy)(
void *T,
void *A);
198 void* (*add)(
void *T,
void* A,
void* B);
199 void* (*dif)(
void *T,
void* A,
void* B);
200 void* (*scale)(
void *T,
void* A,
float S);
201 void* (*lerp)(
void *T,
void *A,
void *B,
float alpha);
202 float (*dist)(
void* A);
203 int (*same)(
void* A,
void* B);
204 int (*approx)(
void* A,
void* B);
205 void* (*arr)(
void* A,
int i);
212 float *arr3f(
float *A,
int i){
216 float *veclerp3f(
float *T,
float *A,
float *B,
float alpha){
219 T[i] = (1.0f - alpha)*A[i] + alpha*B[i];
224 void *tmp3f [] = {&tmp3f[0],&tmp3f[1],&tmp3f[2],&tmp3f[3],&tmp3f[4],&tmp3f[5]};
227 ftype ftype_vec3f = {
242 veccopy3f(T->c,A->c);
246 vecadd3f(T->c,A->c,B->c);
250 vecdif3f(T->c,A->c,B->c);
254 vecscale3f(T->c,A->c,S);
258 veclerp3f(T->c,A->c,B->c,S);
261 float sfvec3f_dist(
struct SFVec3f* A){
262 return veclength3f(A->c);
265 return vecsame3f(A->c,B->c);
270 struct SFVec3f sfvec3f_tmps[6];
271 void *sfvec3f_tmp [] = {&sfvec3f_tmps[0],&sfvec3f_tmps[1],&sfvec3f_tmps[2],&sfvec3f_tmps[3],&sfvec3f_tmps[4],&sfvec3f_tmps[5]};
272 ftype ftype_sfvec3f = {
277 VOIDFN sfvec3f_scale,
279 FLOATFN sfvec3f_dist,
295 t->copy(p->_destination,p->initialDestination);
297 t->copy(t->arr(p->_buffer,0),p->initialDestination);
298 for(C= 1; C<Buffer_length; C++ )
300 t->copy(t->arr(p->_buffer,C),p->initialValue);
302 t->copy(p->_previousValue,p->initialValue);
303 node->_steptime= node->duration / (double) Buffer_length;
313 Frac = (Now - node->_bufferendtime) / node->_steptime;
321 int NumToShift= (int)floor(Frac);
322 Frac-= (double) NumToShift;
323 if(NumToShift < Buffer_length)
327 t->copy(p->_previousValue,t->arr(p->_buffer,Buffer_length - NumToShift));
328 for( C= Buffer_length - 1; C>=NumToShift; C-- )
330 t->copy(t->arr(p->_buffer,C),t->arr(p->_buffer,C - NumToShift));
331 for( C= 0; C<NumToShift; C++ )
337 float Alpha= (float)C / (
float)NumToShift;
342 t->lerp(t->arr(p->_buffer,C),p->_destination,t->arr(p->_buffer,NumToShift),Alpha);
358 if(NumToShift == Buffer_length)
359 t->copy(p->_previousValue,t->arr(p->_buffer,0));
361 t->copy(p->_previousValue,p->_destination);
362 for( C= 0; C<Buffer_length; C++ )
364 t->copy(t->arr(p->_buffer,C),p->_destination);
366 node->_bufferendtime+= NumToShift * node->_steptime;
379 t->copy(p->_destination,p->set_destination);
383 chaser_UpdateBuffer(node, Now);
393 double chaser_StepResponseCore(
double T)
395 return .5 - .5 * cos(T * PI);
401 if(t > node->duration)
405 return chaser_StepResponseCore(t / node->duration);
415 void *Output, *DeltaIn, *DeltaOut;
422 DeltaOut = t->tmp[5];
426 if(!node->_bufferendtime)
428 node->_bufferendtime= Now;
430 t->copy(p->value_changed,p->initialValue);
433 Frac= chaser_UpdateBuffer(node, Now);
447 if(t->type == FIELDTYPE_SFRotation){
452 t->copy(Output,p->_previousValue);
455 t->dif(DeltaIn,t->arr(p->_buffer,Buffer_length -1),p->_previousValue);
456 Alpha = chaser_StepResponse(node,((
double)(Buffer_length - 1) + Frac) * node->_steptime);
458 t->lerp(Output,Output,t->add(t->tmp[0],Output,DeltaIn),(float)Alpha);
459 for(C= Buffer_length - 2; C>=0; C-- )
462 t->dif(DeltaIn,t->arr(p->_buffer,C),t->arr(p->_buffer,C+1));
464 Alpha = chaser_StepResponse(node,((
double)C + Frac) * node->_steptime);
466 t->lerp(Output,Output,t->add(t->tmp[0],Output,DeltaIn),(float)Alpha);
472 t->copy(Output,p->_previousValue);
475 t->dif(DeltaIn,t->arr(p->_buffer,Buffer_length -1),p->_previousValue);
477 Alpha = chaser_StepResponse(node,((
double)(Buffer_length - 1) + Frac) * node->_steptime);
481 t->scale(DeltaOut,DeltaIn,(
float)Alpha);
485 t->add(Output,Output,DeltaOut);
487 for(C= Buffer_length - 2; C>=0; C-- )
491 t->dif(DeltaIn,t->arr(p->_buffer,C),t->arr(p->_buffer,C+1));
493 Alpha = chaser_StepResponse(node,((
double)C + Frac) * node->_steptime);
497 t->scale(DeltaOut,DeltaIn,(
float)Alpha);
500 t->add(Output,Output,DeltaOut);
504 if(!t->same(Output,p->value_changed)){
505 t->copy(p->value_changed,Output);
515 t->copy(p->value_changed,p->set_value);
517 t->copy(p->initialValue,p->set_value);
519 node->isActive = TRUE;
524 void do_PositionChaserTick(
void * ptr){
531 _node->_buffer = REALLOCN(node,_node->_buffer,Buffer_length *
sizeof(
struct SFVec3f));
532 node->_t = &ftype_sfvec3f;
534 p->initialDestination = &_node->initialDestination;
535 p->initialValue = &_node->initialValue;
536 p->set_destination = &_node->set_destination;
537 p->set_value = &_node->set_value;
538 p->value_changed = &_node->value_changed;
539 p->_buffer = _node->_buffer;
540 p->_destination = &_node->_destination;
541 p->_previousValue = &_node->_previousvalue;
545 if(NODE_NEEDS_COMPILING){
549 node->isActive = TRUE;
553 if(!t->same(p->set_destination,p->_destination))
554 chaser_set_destination(node, Now);
556 else if(!t->same(p->set_value,p->initialValue))
557 chaser_set_value(node);
561 chaser_tick(node,Now);
586 node->_takefirstinput = TRUE;
588 damper_set_value(node,p->initialValue);
589 node->isActive = TRUE;
603 dist = t->dist(t->dif(t->tmp[0],t->arr(p->_values,0),p->_input));
608 float dist2 = t->dist(t->dif(t->tmp[0],t->arr(p->_values,1),t->arr(p->_values,0)));
609 if( dist2 > dist) dist= dist2;
615 float dist3 = t->dist(t->dif(t->tmp[0],t->arr(p->_values,2),t->arr(p->_values,1)));
616 if( dist3 > dist) dist= dist3;
622 float dist4 = t->dist(t->dif(t->tmp[0],t->arr(p->_values,3),t->arr(p->_values,2)));
623 if( dist4 > dist) dist= dist4;
629 float dist5 = t->dist(t->dif(t->tmp[0],t->arr(p->_values,4),t->arr(p->_values,3)));
630 if( dist5 > dist) dist= dist5;
641 node->_takefirstinput = FALSE;
644 t->copy(t->arr(p->_values,i),opos);
646 t->copy(p->value_changed, opos);
648 t->copy(p->initialValue,opos);
650 node->isActive = TRUE;
659 if(node->_takefirstinput)
661 node->_takefirstinput = FALSE;
662 damper_set_value(node,ipos);
665 if(!t->same(ipos,p->_input))
668 t->copy(p->_input,ipos);
669 node->isActive = TRUE;
707 node->_lasttick= now;
710 delta= now - node->_lasttick;
711 node->_lasttick= now;
712 alpha= exp(-delta / node->tau);
713 if(node->_takefirstinput)
721 if(node->order > 0 && node->tau != 0.0)
723 t->lerp(t->arr(p->_values,0),p->_input,t->arr(p->_values,0),(float)alpha);
725 t->copy(t->arr(p->_values,0),p->_input);
731 if(node->order > 1 && node->tau != 0.0)
733 t->lerp(t->arr(p->_values,1),t->arr(p->_values,0),t->arr(p->_values,1),(float)alpha);
735 t->copy(t->arr(p->_values,1),t->arr(p->_values,0));
741 if(node->order > 2 && node->tau != 0.0)
743 t->lerp(t->arr(p->_values,2),t->arr(p->_values,1),t->arr(p->_values,2),(float)alpha);
745 t->copy(t->arr(p->_values,2),t->arr(p->_values,1));
752 if(node->order > 3 && node->tau != 0.0)
754 t->lerp(t->arr(p->_values,3),t->arr(p->_values,2),t->arr(p->_values,3),(float)alpha);
756 t->copy(t->arr(p->_values,3),t->arr(p->_values,2));
762 if(node->order > 4 && node->tau != 0.0)
764 t->lerp(t->arr(p->_values,4),t->arr(p->_values,3),t->arr(p->_values,4),(float)alpha);
766 t->copy(t->arr(p->_values,4),t->arr(p->_values,3));
768 dist= damper_GetDist(node);
770 if(dist < max(node->tolerance,.001f))
775 t->copy(t->arr(p->_values,i),p->_input);
778 t->copy(p->value_changed,p->_input);
780 node->isActive = FALSE;
785 t->copy(p->value_changed,t->arr(p->_values,4));
793 void do_PositionDamperTick(
void * ptr){
799 node->_t = &ftype_sfvec3f;
801 _node->_values = REALLOCN(node,_node->_values,5 *
sizeof(
struct SFVec3f));
802 p->initialDestination = &_node->initialDestination;
803 p->initialValue = &_node->initialValue;
804 p->set_destination = &_node->set_destination;
805 p->set_value = &_node->set_value;
806 p->value_changed = &_node->value_changed;
807 p->_input = &_node->_input;
808 p->_values = _node->_values;
813 if(NODE_NEEDS_COMPILING){
819 if(!t->same(p->set_destination,p->_input))
821 damper_set_destination(node, p->set_destination);
823 if(node->tau != node->_tau)
824 damper_set_tau(node,node->tau);
827 if(!t->same(p->initialValue,p->set_value))
829 damper_set_value(node,p->set_value);
833 tick_damper(node,TickTime());
840 node->_destination = node->initialDestination;
841 buffer[0]= node->initialDestination;
842 for(C= 1; C<Buffer_length; C++ )
843 buffer[C]= node->initialValue;
844 node->_previousvalue= node->initialValue;
845 node->_steptime= node->duration / (double) Buffer_length;
853 Frac = (Now - node->_bufferendtime) / node->_steptime;
861 int NumToShift= (int)floor(Frac);
862 Frac-= (double) NumToShift;
863 if(NumToShift < Buffer_length)
866 node->_previousvalue= buffer[Buffer_length - NumToShift];
867 for( C= Buffer_length - 1; C>=NumToShift; C-- )
868 buffer[C]= buffer[C - NumToShift];
869 for( C= 0; C<NumToShift; C++ )
874 float tmp1[3],tmp2[3];
875 float Alpha= (float)C / (
float)NumToShift;
886 vecscale3f(tmp1,buffer[NumToShift].c,Alpha);
887 vecscale3f(tmp2,node->_destination.c,1.0f - Alpha);
888 vecadd3f(tmp3,tmp1,tmp2);
889 veccopy3f(buffer[C].c,tmp3);
891 vecadd3f(buffer[C].c,vecscale3f(tmp1,buffer[NumToShift].c,Alpha),vecscale3f(tmp2,node->_destination.c,1.0f - Alpha));
905 node->_previousvalue= NumToShift == Buffer_length? buffer[0] : node->_destination;
907 for( C= 0; C<Buffer_length; C++ )
908 buffer[C]= node->_destination;
910 node->_bufferendtime+= NumToShift * node->_steptime;
919 node->_destination= Dest;
923 chaser_UpdateBuffer(node, Now);
933 double chaser_StepResponseCore(
double T)
935 return .5 - .5 * cos(T * PI);
941 if(t > node->duration)
945 return chaser_StepResponseCore(t / node->duration);
958 if(!node->_bufferendtime)
960 node->_bufferendtime= Now;
961 node->value_changed= node->initialValue;
964 Frac= chaser_UpdateBuffer(node, Now);
978 Output= node->_previousvalue;
980 vecdif3f(DeltaIn.c,buffer[Buffer_length - 1].c,node->_previousvalue.c);
983 vecscale3f(DeltaOut.c,DeltaIn.c,(
float)chaser_StepResponse(node,((
double)Buffer_length - 1.0 + Frac) * node->_steptime));
985 vecadd3f(Output.c,Output.c,DeltaOut.c);
986 for(C= Buffer_length - 2; C>=0; C-- )
989 vecdif3f(DeltaIn.c,buffer[C].c,buffer[C+1].c);
991 vecscale3f(DeltaOut.c,DeltaIn.c,(
float)chaser_StepResponse(node,((
double)C + Frac) * node->_steptime));
993 vecadd3f(Output.c,Output.c,DeltaOut.c);
995 if(!vecsame3f(Output.c,node->value_changed.c)){
996 node->value_changed= Output;
1002 node->value_changed= opos;
1003 node->initialValue = opos;
1005 node->isActive = TRUE;
1010 void do_PositionChaserTick(
void * ptr){
1015 node->_buffer = realloc(node->_buffer,Buffer_length *
sizeof(
struct SFVec3f));
1019 if(NODE_NEEDS_COMPILING){
1020 node->isActive = TRUE;
1023 if(!vecsame3f(node->set_destination.c,node->_destination.c))
1024 chaser_set_destination(node, node->set_destination,Now);
1025 else if(!vecsame3f(node->set_value.c,node->initialValue.c))
1026 chaser_set_value(node,node->set_value);
1030 chaser_tick(node,Now);
1052 node->_takefirstinput = TRUE;
1053 damper_set_value(node,node->initialValue);
1054 node->isActive = TRUE;
1064 dist = veclength3f(vecdif3f(tmp,values[0].c,node->_input.c));
1068 float dist2 = veclength3f(vecdif3f(tmp,values[1].c,values[0].c));
1069 if( dist2 > dist) dist= dist2;
1074 float dist3 = veclength3f(vecdif3f(tmp,values[2].c,values[1].c));
1075 if( dist3 > dist) dist= dist3;
1080 float dist4 = veclength3f(vecdif3f(tmp,values[3].c,values[2].c));
1081 if( dist4 > dist) dist= dist4;
1086 float dist5 = veclength3f(vecdif3f(tmp,values[4].c, values[3].c));
1087 if( dist5 > dist) dist= dist5;
1094 node->_takefirstinput = FALSE;
1095 values[0]= values[1]= values[2]= values[3]= values[4]= opos;
1096 node->value_changed= opos;
1097 node->initialValue = opos;
1099 node->isActive = TRUE;
1105 if(node->_takefirstinput)
1107 node->_takefirstinput = FALSE;
1108 damper_set_value(node,ipos);
1110 if(!vecsame3f(ipos.c,node->_input.c))
1112 node->_input = ipos;
1113 node->isActive = TRUE;
1120 float tmp[3], tmp2[3];
1123 vecdif3f(tmp,b.c,a.c);
1124 vecscale3f(tmp2,tmp,(
float)alpha);
1125 vecadd3f(ret.c,a.c,tmp2);
1127 vecadd3f(ret.c,a.c,vecscale3f(tmp2,vecdif3f(tmp,b.c,a.c),(
float)alpha));
1138 if(!node->_lasttick)
1140 node->_lasttick= now;
1143 delta= now - node->_lasttick;
1144 node->_lasttick= now;
1145 alpha= exp(-delta / node->tau);
1146 if(node->_takefirstinput)
1149 values[0]= node->order > 0 && node->tau != 0.0
1151 ? damper_diftimes(node->_input,values[0],alpha)
1154 values[1]= node->order > 1 && node->tau != 0.0
1156 ? damper_diftimes(values[0],values[1],alpha)
1159 values[2]= node->order > 2 && node->tau != 0.0
1161 ? damper_diftimes(values[1],values[2],alpha)
1164 values[3]= node->order > 3 && node->tau != 0.0
1166 ? damper_diftimes(values[2],values[3],alpha)
1169 values[4]= node->order > 4 && node->tau != 0.0
1171 ? damper_diftimes(values[3],values[4],alpha)
1174 dist= damper_GetDist(node);
1176 if(dist < max(node->tolerance,.001f))
1178 values[0]= values[1]= values[2]= values[3]= values[4]= node->_input;
1179 node->value_changed= node->_input;
1181 node->isActive = FALSE;
1185 node->value_changed= values[4];
1192 void do_PositionDamperTick(
void * ptr){
1196 node->_values = realloc(node->_values,5 *
sizeof(
struct SFVec3f));
1201 if(NODE_NEEDS_COMPILING){
1203 if(!vecsame3f(node->set_destination.c,node->_input.c))
1204 damper_set_destination(node, node->set_destination);
1206 if(node->tau != node->_tau)
1207 damper_set_tau(node,node->tau);
1209 if(!vecsame3f(node->initialValue.c,node->set_value.c))
1210 damper_set_value(node,node->set_value);
1214 tick_positiondamper(node,TickTime());
1219 void do_ColorChaserTick_default(
void * ptr){
1222 if(NODE_NEEDS_COMPILING){
1224 veccopy3f(node->value_changed.c, node->set_destination.c);
1229 void do_ColorDamperTick_default(
void * ptr){
1232 if(NODE_NEEDS_COMPILING){
1234 veccopy3f(node->value_changed.c, node->set_destination.c);
1239 void do_ColorChaserTick(
void * ptr){
1244 if(!_node->_buffer){
1246 _node->_buffer = REALLOCN(node,_node->_buffer,Buffer_length *
sizeof(
struct SFColor));
1247 node->_t = &ftype_sfvec3f;
1249 p->initialDestination = &_node->initialDestination;
1250 p->initialValue = &_node->initialValue;
1251 p->set_destination = &_node->set_destination;
1252 p->set_value = &_node->set_value;
1253 p->value_changed = &_node->value_changed;
1254 p->_buffer = _node->_buffer;
1255 p->_destination = &_node->_destination;
1256 p->_previousValue = &_node->_previousvalue;
1260 if(NODE_NEEDS_COMPILING){
1262 ftype *t = node->_t;
1264 node->isActive = TRUE;
1268 if(!t->same(p->set_destination,p->_destination))
1269 chaser_set_destination(node, Now);
1271 else if(!t->same(p->set_value,p->initialValue))
1272 chaser_set_value(node);
1276 chaser_tick(node,Now);
1279 void do_ColorDamperTick(
void * ptr){
1283 if(!_node->_values){
1285 node->_t = &ftype_sfvec3f;
1287 _node->_values = REALLOCN(node,_node->_values,5 *
sizeof(
struct SFColor));
1288 p->initialDestination = &_node->initialDestination;
1289 p->initialValue = &_node->initialValue;
1290 p->set_destination = &_node->set_destination;
1291 p->set_value = &_node->set_value;
1292 p->value_changed = &_node->value_changed;
1293 p->_input = &_node->_input;
1294 p->_values = _node->_values;
1299 if(NODE_NEEDS_COMPILING){
1302 ftype *t = node->_t;
1305 if(!t->same(p->set_destination,p->_input))
1307 damper_set_destination(node, p->set_destination);
1309 if(node->tau != node->_tau)
1310 damper_set_tau(node,node->tau);
1313 if(!t->same(p->initialValue,p->set_value))
1315 damper_set_value(node,p->set_value);
1319 tick_damper(node,TickTime());
1322 void do_CoordinateChaserTick_default(
void * ptr){
1325 if(NODE_NEEDS_COMPILING){
1328 n = node->set_destination.n;
1329 node->value_changed.n = n;
1330 node->value_changed.p = REALLOC(node->value_changed.p,n *
sizeof(
struct SFVec3f));
1331 memcpy(node->value_changed.p,node->set_destination.p,n*
sizeof(
struct SFVec3f));
1336 void do_CoordinateDamperTick_default(
void * ptr){
1339 if(NODE_NEEDS_COMPILING){
1342 n = node->set_destination.n;
1343 node->value_changed.n = n;
1344 node->value_changed.p = REALLOC(node->value_changed.p,n *
sizeof(
struct SFVec3f));
1345 memcpy(node->value_changed.p,node->set_destination.p,n*
sizeof(
struct SFVec3f));
1352 T->p = REALLOC(T->p,A->n *
sizeof(
struct SFVec3f));
1354 memcpy(T->p,A->p,A->n *
sizeof(
struct SFVec3f));
1359 T->n = min(A->n,B->n);
1360 T->p = REALLOC(T->p,T->n *
sizeof(
struct SFVec3f));
1362 sfvec3f_add(&T->p[i],&A->p[i],&B->p[i]);
1367 T->n = min(A->n,B->n);
1368 T->p = REALLOC(T->p,T->n *
sizeof(
struct SFVec3f));
1370 sfvec3f_dif(&T->p[i],&A->p[i],&B->p[i]);
1376 T->p = REALLOC(T->p,T->n *
sizeof(
struct SFVec3f));
1378 sfvec3f_scale(&T->p[i],&A->p[i],S);
1384 T->p = REALLOC(T->p,T->n *
sizeof(
struct SFVec3f));
1386 sfvec3f_lerp(&T->p[i],&A->p[i],&B->p[i],alpha);
1393 dist += sfvec3f_dist(&A->p[i]);
1398 if(A->n != B->n)
return FALSE;
1401 isame = isame && sfvec3f_same(&A->p[i],&B->p[i]);
1408 void *mfvec3f_tmp [] = {&mfvec3f_tmps[0],&mfvec3f_tmps[1],&mfvec3f_tmps[2],&mfvec3f_tmps[3],&mfvec3f_tmps[4],&mfvec3f_tmps[5]};
1409 ftype ftype_mfvec3f = {
1411 VOIDFN mfvec3f_copy,
1414 VOIDFN mfvec3f_scale,
1415 VOIDFN mfvec3f_lerp,
1416 FLOATFN mfvec3f_dist,
1420 VOIDPTR mfvec3f_tmp,
1430 memcpy(T->c, A->c,
sizeof(
struct SFRotation));
1433 vrmlrot_to_quaternion(&qA, (
double) A->c[0],
1434 (
double) A->c[1], (
double) A->c[2], (
double) A->c[3]);
1437 quaternion_inverse(&qT,&qA);
1440 quaternion_to_vrmlrot(&qT, &x, &y, &z, &a);
1442 T->c[0] = (float) x;
1443 T->c[1] = (float) y;
1444 T->c[2] = (float) z;
1445 T->c[3] = (float) a;
1453 vrmlrot_to_quaternion(&qA, (
double) A->c[0],
1454 (
double) A->c[1], (
double) A->c[2], (
double) A->c[3]);
1456 vrmlrot_to_quaternion(&qB, (
double) B->c[0],
1457 (
double) B->c[1], (
double) B->c[2], (
double) B->c[3]);
1460 quaternion_multiply(&qT,&qA,&qB);
1463 quaternion_to_vrmlrot(&qT, &x, &y, &z, &a);
1465 T->c[0] = (float) x;
1466 T->c[1] = (float) y;
1467 T->c[2] = (float) z;
1468 T->c[3] = (float) a;
1473 memcpy(T->c, A->c,
sizeof(
struct SFRotation));
1478 sfrotation_multiply(T,A,B);
1488 vrmlrot_to_quaternion(&qA, (
double) A->c[0],
1489 (
double) A->c[1], (
double) A->c[2], (
double) A->c[3]);
1491 vrmlrot_to_quaternion(&qB, (
double) B->c[0],
1492 (
double) B->c[1], (
double) B->c[2], (
double) B->c[3]);
1495 quaternion_inverse(&qBI,&qB);
1497 quaternion_multiply(&qT,&qBI,&qA);
1500 quaternion_to_vrmlrot(&qT, &x, &y, &z, &a);
1502 T->c[0] = (float) x;
1503 T->c[1] = (float) y;
1504 T->c[2] = (float) z;
1505 T->c[3] = (float) a;
1522 sfrotation_copy(T,A);
1527 if (APPROX(alpha, 0.0f)) {
1528 memcpy(T->c,A->c,4*
sizeof(
float));
1529 }
else if (APPROX(alpha, 1.0f)) {
1530 memcpy(T->c,B->c,4*
sizeof(
float));
1534 vrmlrot_to_quaternion(&quatA,
1540 vrmlrot_to_quaternion(&quatB,
1546 quaternion_slerp(&quatT, &quatA, &quatB, (
double)alpha);
1547 quaternion_to_vrmlrot(&quatT,&x,&y,&z,&a);
1549 T->c[0] = (float) x;
1550 T->c[1] = (float) y;
1551 T->c[2] = (float) z;
1552 T->c[3] = (float) a;
1563 isame = isame && A->c[i] == B->c[i];
1570 void *sfrotation_tmp [] = {&sfrotation_tmps[0],&sfrotation_tmps[1],&sfrotation_tmps[2],&sfrotation_tmps[3],&sfrotation_tmps[4],&sfrotation_tmps[5]};
1571 ftype ftype_sfrotation = {
1572 FIELDTYPE_SFRotation,
1573 VOIDFN sfrotation_copy,
1574 VOIDFN sfrotation_add,
1575 VOIDFN sfrotation_dif,
1576 VOIDFN sfrotation_scale,
1577 VOIDFN sfrotation_slerp,
1578 FLOATFN sfrotation_dist,
1579 INTFN sfrotation_same,
1580 INTFN sfrotation_same,
1581 VOIDFN sfrotation_arr,
1582 VOIDPTR sfrotation_tmp,
1585 void do_OrientationChaserTick(
void * ptr){
1590 if(!_node->_buffer){
1592 _node->_buffer = REALLOCN(node,_node->_buffer,Buffer_length *
sizeof(
struct SFRotation));
1593 node->_t = &ftype_sfrotation;
1595 p->initialDestination = &_node->initialDestination;
1596 p->initialValue = &_node->initialValue;
1597 p->set_destination = &_node->set_destination;
1598 p->set_value = &_node->set_value;
1599 p->value_changed = &_node->value_changed;
1600 p->_buffer = _node->_buffer;
1601 p->_destination = &_node->_destination;
1602 p->_previousValue = &_node->_previousvalue;
1606 if(NODE_NEEDS_COMPILING){
1608 ftype *t = node->_t;
1609 static int count = 0;
1610 node->isActive = TRUE;
1614 if(!t->same(p->set_destination,p->_destination))
1615 chaser_set_destination(node, Now);
1617 else if(!t->same(p->set_value,p->initialValue))
1618 chaser_set_value(node);
1623 chaser_tick(node,Now);
1626 void do_OrientationDamperTick(
void * ptr){
1630 if(!_node->_values){
1632 node->_t = &ftype_sfrotation;
1634 _node->_values = REALLOCN(node,_node->_values,5 *
sizeof(
struct SFRotation));
1635 p->initialDestination = &_node->initialDestination;
1636 p->initialValue = &_node->initialValue;
1637 p->set_destination = &_node->set_destination;
1638 p->set_value = &_node->set_value;
1639 p->value_changed = &_node->value_changed;
1640 p->_input = &_node->_input;
1641 p->_values = _node->_values;
1646 if(NODE_NEEDS_COMPILING){
1649 ftype *t = node->_t;
1652 if(!t->same(p->set_destination,p->_input))
1654 damper_set_destination(node, p->set_destination);
1656 if(node->tau != node->_tau)
1657 damper_set_tau(node,node->tau);
1660 if(!t->same(p->initialValue,p->set_value))
1662 damper_set_value(node,p->set_value);
1666 tick_damper(node,TickTime());
1670 void do_OrientationChaserTick_default(
void * ptr){
1673 if(NODE_NEEDS_COMPILING){
1675 veccopy3f(node->value_changed.c, node->set_destination.c);
1676 node->value_changed.c[3] = node->set_destination.c[3];
1681 void do_OrientationDamperTick_default(
void * ptr){
1684 if(NODE_NEEDS_COMPILING){
1686 veccopy3f(node->value_changed.c, node->set_destination.c);
1687 node->value_changed.c[3] = node->set_destination.c[3];
1698 node->_destination = node->initialDestination;
1699 buffer[0]= node->initialDestination;
1700 for(C= 1; C<Buffer_length; C++ )
1701 buffer[C]= node->initialValue;
1702 node->_previousvalue= node->initialValue;
1703 node->_steptime= node->duration / (double) Buffer_length;
1711 Frac = (Now - node->_bufferendtime) / node->_steptime;
1719 int NumToShift= (int)floor(Frac);
1720 Frac-= (double) NumToShift;
1721 if(NumToShift < Buffer_length)
1724 node->_previousvalue= buffer[Buffer_length - NumToShift];
1725 for( C= Buffer_length - 1; C>=NumToShift; C-- )
1726 buffer[C]= buffer[C - NumToShift];
1727 for( C= 0; C<NumToShift; C++ )
1732 float Alpha= (float)C / (
float)NumToShift;
1735 if(1)sfrotation_slerp(&buffer[C],&node->_destination,&buffer[NumToShift],Alpha);
1736 else sfrotation_slerp(&buffer[C],&buffer[NumToShift],&node->_destination,Alpha);
1749 node->_previousvalue= NumToShift == Buffer_length? buffer[0] : node->_destination;
1751 for( C= 0; C<Buffer_length; C++ )
1752 buffer[C]= node->_destination;
1754 node->_bufferendtime+= NumToShift * node->_steptime;
1763 node->_destination= Dest;
1767 orichaser_UpdateBuffer(node, Now);
1776 double orichaser_StepResponseCore(
double T)
1778 return .5 - .5 * cos(T * PI);
1784 if(t > node->duration)
1788 return orichaser_StepResponseCore(t / node->duration);
1802 if(!node->_bufferendtime)
1804 node->_bufferendtime= Now;
1805 node->value_changed= node->initialValue;
1808 Frac= orichaser_UpdateBuffer(node, Now);
1823 Output= node->_previousvalue;
1826 sfrotation_inverse(&tmp0,&node->_previousvalue);
1827 sfrotation_multiply(&DeltaIn,&tmp0,&buffer[Buffer_length -1]);
1829 sfrotation_dif(&DeltaIn,&buffer[Buffer_length -1],&node->_previousvalue);
1832 Alpha = orichaser_StepResponse(node,((
double)Buffer_length - 1.0 + Frac) * node->_steptime);
1835 sfrotation_multiply(&tmp0,&Output,&DeltaIn);
1837 sfrotation_add(&tmp0,&Output,&DeltaIn);
1839 sfrotation_slerp(&Output,&Output,&tmp0,(
float)Alpha);
1840 for(C= Buffer_length - 2; C>=0; C-- )
1844 sfrotation_inverse(&tmp0,&buffer[C + 1]);
1845 sfrotation_multiply(&DeltaIn,&tmp0,&buffer[C]);
1847 sfrotation_dif(&DeltaIn,&buffer[C],&buffer[C+1]);
1849 Alpha = orichaser_StepResponse(node,((
float)C + Frac) * node->_steptime);
1852 sfrotation_multiply(&tmp0,&Output,&DeltaIn);
1854 sfrotation_add(&tmp0,&Output,&DeltaIn);
1856 sfrotation_slerp(&Output,&Output,&tmp0,(
float)Alpha);
1858 if(!sfrotation_same(&Output,&node->value_changed)){
1859 node->value_changed= Output;
1865 node->value_changed= opos;
1866 node->initialValue = opos;
1868 node->isActive = TRUE;
1873 void do_OrientationChaserTick_oldway_works(
void * ptr){
1878 node->_buffer = REALLOCN(node,node->_buffer,Buffer_length *
sizeof(
struct SFRotation));
1879 orichaser_init(node);
1882 if(NODE_NEEDS_COMPILING){
1884 node->isActive = TRUE;
1887 if(!sfrotation_same(&node->set_destination,&node->_destination))
1888 orichaser_set_destination(node, node->set_destination,Now);
1889 else if(!sfrotation_same(&node->set_value,&node->initialValue))
1890 orichaser_set_value(node,node->set_value);
1894 orichaser_tick(node,Now);
1901 void do_CoordinateChaserTick(
void * ptr){
1906 if(!_node->_buffer){
1908 _node->_buffer = REALLOCN(node,_node->_buffer,Buffer_length *
sizeof(
struct Multi_Vec3f));
1909 node->_t = &ftype_mfvec3f;
1911 p->initialDestination = &_node->initialDestination;
1912 p->initialValue = &_node->initialValue;
1913 p->set_destination = &_node->set_destination;
1914 p->set_value = &_node->set_value;
1915 p->value_changed = &_node->value_changed;
1916 p->_buffer = _node->_buffer;
1917 p->_destination = &_node->_destination;
1918 p->_previousValue = &_node->_previousvalue;
1922 if(NODE_NEEDS_COMPILING){
1924 ftype *t = node->_t;
1926 node->isActive = TRUE;
1930 if(!t->same(p->set_destination,p->_destination))
1931 chaser_set_destination(node, Now);
1933 else if(!t->same(p->set_value,p->initialValue))
1934 chaser_set_value(node);
1938 chaser_tick(node,Now);
1941 void do_CoordinateDamperTick(
void * ptr){
1945 if(!_node->_values){
1947 node->_t = &ftype_sfvec3f;
1949 _node->_values = REALLOCN(node,_node->_values,5 *
sizeof(
struct Multi_Vec3f));
1950 p->initialDestination = &_node->initialDestination;
1951 p->initialValue = &_node->initialValue;
1952 p->set_destination = &_node->set_destination;
1953 p->set_value = &_node->set_value;
1954 p->value_changed = &_node->value_changed;
1955 p->_input = &_node->_input;
1956 p->_values = _node->_values;
1961 if(NODE_NEEDS_COMPILING){
1964 ftype *t = node->_t;
1967 if(!t->same(p->set_destination,p->_input))
1969 damper_set_destination(node, p->set_destination);
1971 if(node->tau != node->_tau)
1972 damper_set_tau(node,node->tau);
1975 if(!t->same(p->initialValue,p->set_value))
1977 damper_set_value(node,p->set_value);
1981 tick_damper(node,TickTime());
1988 void do_PositionChaser2DTick_default(
void * ptr){
1991 if(NODE_NEEDS_COMPILING){
1993 veccopy2f(node->value_changed.c, node->set_destination.c);
1999 void do_PositionDamper2DTick_default(
void * ptr){
2002 if(NODE_NEEDS_COMPILING){
2004 veccopy2f(node->value_changed.c, node->set_destination.c);
2010 float *veclerp2f(
float *T,
float *A,
float *B,
float alpha){
2013 T[i] = (1.0f - alpha)*A[i] + alpha*B[i];
2018 veccopy2f(T->c,A->c);
2022 vecadd2f(T->c,A->c,B->c);
2026 vecdif2f(T->c,A->c,B->c);
2030 vecscale2f(T->c,A->c,S);
2034 veclerp2f(T->c,A->c,B->c,alpha);
2037 float sfvec2f_dist(
struct SFVec2f* A){
2038 return veclength2f(A->c);
2041 return vecsame2f(A->c,B->c);
2046 struct SFVec2f sfvec2f_tmps[6];
2047 void *sfvec2f_tmp [] = {&sfvec2f_tmps[0],&sfvec2f_tmps[1],&sfvec2f_tmps[2],&sfvec2f_tmps[3],&sfvec2f_tmps[4],&sfvec2f_tmps[5]};
2048 ftype ftype_sfvec2f = {
2050 VOIDFN sfvec2f_copy,
2053 VOIDFN sfvec2f_scale,
2054 VOIDFN sfvec2f_lerp,
2055 FLOATFN sfvec2f_dist,
2059 VOIDPTR sfvec2f_tmp,
2062 void do_PositionChaser2DTick(
void * ptr){
2067 if(!_node->_buffer){
2069 _node->_buffer = REALLOCN(node,_node->_buffer,Buffer_length *
sizeof(
struct SFVec2f));
2070 node->_t = &ftype_sfvec2f;
2072 p->initialDestination = &_node->initialDestination;
2073 p->initialValue = &_node->initialValue;
2074 p->set_destination = &_node->set_destination;
2075 p->set_value = &_node->set_value;
2076 p->value_changed = &_node->value_changed;
2077 p->_buffer = _node->_buffer;
2078 p->_destination = &_node->_destination;
2079 p->_previousValue = &_node->_previousvalue;
2083 if(NODE_NEEDS_COMPILING){
2085 ftype *t = node->_t;
2087 node->isActive = TRUE;
2091 if(!t->same(p->set_destination,p->_destination))
2092 chaser_set_destination(node, Now);
2094 else if(!t->same(p->set_value,p->initialValue))
2095 chaser_set_value(node);
2099 chaser_tick(node,Now);
2101 void do_PositionDamper2DTick(
void * ptr){
2105 if(!_node->_values){
2107 node->_t = &ftype_sfvec2f;
2109 _node->_values = REALLOCN(node,_node->_values,5 *
sizeof(
struct SFVec2f));
2110 p->initialDestination = &_node->initialDestination;
2111 p->initialValue = &_node->initialValue;
2112 p->set_destination = &_node->set_destination;
2113 p->set_value = &_node->set_value;
2114 p->value_changed = &_node->value_changed;
2115 p->_input = &_node->_input;
2116 p->_values = _node->_values;
2121 if(NODE_NEEDS_COMPILING){
2124 ftype *t = node->_t;
2127 if(!t->same(p->set_destination,p->_input))
2129 damper_set_destination(node, p->set_destination);
2131 if(node->tau != node->_tau)
2132 damper_set_tau(node,node->tau);
2135 if(!t->same(p->initialValue,p->set_value))
2137 damper_set_value(node,p->set_value);
2141 tick_damper(node,TickTime());
2145 void do_ScalarChaserTick_default(
void * ptr){
2148 if(NODE_NEEDS_COMPILING){
2150 node->value_changed = node->set_destination;
2155 void do_ScalarDamperTick_default(
void * ptr){
2158 if(NODE_NEEDS_COMPILING){
2160 node->value_changed = node->set_destination;
2165 float *scalar_copy(
float* T,
float *A){
2169 float *scalar_add(
float* T,
float *A,
float *B){
2173 float *scalar_dif(
float* T,
float *A,
float *B){
2177 float *scalar_scale(
float* T,
float *A,
float S){
2181 float *scalar_lerp(
float* T,
float *A,
float *B,
float alpha){
2182 *T = (1.0f -alpha)*(*A) + alpha*(*B);
2185 float scalar_dist(
float* A){
2186 return (
float)fabs(*A);
2188 int scalar_same(
float *A,
float *B){
2189 return *A == *B ? TRUE : FALSE;
2191 float *scalar_arr(
float *A,
int i){
2194 float scalar_tmps[6];
2195 void *scalar_tmp [] = {&scalar_tmps[0],&scalar_tmps[1],&scalar_tmps[2],&scalar_tmps[3],&scalar_tmps[4],&scalar_tmps[5]};
2196 ftype ftype_scalar = {
2201 VOIDFN scalar_scale,
2203 FLOATFN scalar_dist,
2210 void do_ScalarChaserTick(
void * ptr){
2215 if(!_node->_buffer){
2217 _node->_buffer = REALLOCN(node,_node->_buffer,Buffer_length *
sizeof(
float));
2218 node->_t = &ftype_scalar;
2220 p->initialDestination = &_node->initialDestination;
2221 p->initialValue = &_node->initialValue;
2222 p->set_destination = &_node->set_destination;
2223 p->set_value = &_node->set_value;
2224 p->value_changed = &_node->value_changed;
2225 p->_buffer = _node->_buffer;
2226 p->_destination = &_node->_destination;
2227 p->_previousValue = &_node->_previousvalue;
2231 if(NODE_NEEDS_COMPILING){
2233 ftype *t = node->_t;
2235 node->isActive = TRUE;
2239 if(!t->same(p->set_destination,p->_destination))
2240 chaser_set_destination(node, Now);
2242 else if(!t->same(p->set_value,p->initialValue))
2243 chaser_set_value(node);
2247 chaser_tick(node,Now);
2249 void do_ScalarDamperTick(
void * ptr){
2253 if(!_node->_values){
2255 node->_t = &ftype_scalar;
2257 _node->_values = REALLOCN(node,_node->_values,5 *
sizeof(
float));
2258 p->initialDestination = &_node->initialDestination;
2259 p->initialValue = &_node->initialValue;
2260 p->set_destination = &_node->set_destination;
2261 p->set_value = &_node->set_value;
2262 p->value_changed = &_node->value_changed;
2263 p->_input = &_node->_input;
2264 p->_values = _node->_values;
2269 if(NODE_NEEDS_COMPILING){
2272 ftype *t = node->_t;
2275 if(!t->same(p->set_destination,p->_input))
2277 damper_set_destination(node, p->set_destination);
2279 if(node->tau != node->_tau)
2280 damper_set_tau(node,node->tau);
2283 if(!t->same(p->initialValue,p->set_value))
2285 damper_set_value(node,p->set_value);
2289 tick_damper(node,TickTime());
2292 void do_TexCoordChaser2DTick_default(
void * ptr){
2295 if(NODE_NEEDS_COMPILING){
2298 n = node->set_destination.n;
2299 node->value_changed.n = n;
2300 node->value_changed.p = REALLOC(node->value_changed.p,n *
sizeof(
struct SFVec2f));
2301 memcpy(node->value_changed.p,node->set_destination.p,n*
sizeof(
struct SFVec2f));
2306 void do_TexCoordDamper2DTick_default(
void * ptr){
2309 if(NODE_NEEDS_COMPILING){
2312 n = node->set_destination.n;
2313 node->value_changed.n = n;
2314 node->value_changed.p = REALLOC(node->value_changed.p,n *
sizeof(
struct SFVec2f));
2315 memcpy(node->value_changed.p,node->set_destination.p,n*
sizeof(
struct SFVec2f));
2322 T->p = REALLOC(T->p,A->n *
sizeof(
struct SFVec2f));
2324 memcpy(T->p,A->p,A->n *
sizeof(
struct SFVec2f));
2329 T->n = min(A->n,B->n);
2330 T->p = REALLOC(T->p,T->n *
sizeof(
struct SFVec2f));
2332 sfvec2f_add(&T->p[i],&A->p[i],&B->p[i]);
2337 T->n = min(A->n,B->n);
2338 T->p = REALLOC(T->p,T->n *
sizeof(
struct SFVec2f));
2340 sfvec2f_dif(&T->p[i],&A->p[i],&B->p[i]);
2346 T->p = REALLOC(T->p,T->n *
sizeof(
struct SFVec2f));
2348 sfvec2f_scale(&T->p[i],&A->p[i],S);
2353 T->n = min(A->n,B->n);
2354 T->p = REALLOC(T->p,T->n *
sizeof(
struct SFVec2f));
2356 sfvec2f_lerp(&T->p[i],&A->p[i],&B->p[i],alpha);
2364 dist += sfvec2f_dist(&A->p[i]);
2369 if(A->n != B->n)
return FALSE;
2372 isame = isame && sfvec2f_same(&A->p[i],&B->p[i]);
2379 void *mfvec2f_tmp [] = {&mfvec2f_tmps[0],&mfvec2f_tmps[1],&mfvec2f_tmps[2],&mfvec2f_tmps[3],&mfvec2f_tmps[4],&mfvec2f_tmps[5]};
2380 ftype ftype_mfvec2f = {
2382 VOIDFN mfvec2f_copy,
2385 VOIDFN mfvec2f_scale,
2386 VOIDFN mfvec2f_lerp,
2387 FLOATFN mfvec2f_dist,
2391 VOIDPTR mfvec2f_tmp,
2393 void do_TexCoordChaser2DTick(
void * ptr){
2398 if(!_node->_buffer){
2400 _node->_buffer = REALLOCN(node,_node->_buffer,Buffer_length *
sizeof(
struct Multi_Vec2f));
2401 node->_t = &ftype_mfvec2f;
2403 p->initialDestination = &_node->initialDestination;
2404 p->initialValue = &_node->initialValue;
2405 p->set_destination = &_node->set_destination;
2406 p->set_value = &_node->set_value;
2407 p->value_changed = &_node->value_changed;
2408 p->_buffer = _node->_buffer;
2409 p->_destination = &_node->_destination;
2410 p->_previousValue = &_node->_previousvalue;
2414 if(NODE_NEEDS_COMPILING){
2416 ftype *t = node->_t;
2418 node->isActive = TRUE;
2422 if(!t->same(p->set_destination,p->_destination))
2423 chaser_set_destination(node, Now);
2425 else if(!t->same(p->set_value,p->initialValue))
2426 chaser_set_value(node);
2430 chaser_tick(node,Now);
2433 void do_TexCoordDamper2DTick(
void * ptr){
2437 if(!_node->_values){
2439 node->_t = &ftype_sfvec2f;
2441 _node->_values = REALLOCN(node,_node->_values,5 *
sizeof(
struct Multi_Vec2f));
2442 p->initialDestination = &_node->initialDestination;
2443 p->initialValue = &_node->initialValue;
2444 p->set_destination = &_node->set_destination;
2445 p->set_value = &_node->set_value;
2446 p->value_changed = &_node->value_changed;
2447 p->_input = &_node->_input;
2448 p->_values = _node->_values;
2453 if(NODE_NEEDS_COMPILING){
2456 ftype *t = node->_t;
2459 if(!t->same(p->set_destination,p->_input))
2461 damper_set_destination(node, p->set_destination);
2463 if(node->tau != node->_tau)
2464 damper_set_tau(node,node->tau);
2467 if(!t->same(p->initialValue,p->set_value))
2469 damper_set_value(node,p->set_value);
2473 tick_damper(node,TickTime());